Skip to content

Commit

Permalink
test: Initial testing with helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
puria committed May 11, 2024
1 parent 754b0ee commit 0106f7a
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
slangroom-exec
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs
Expand Down
68 changes: 44 additions & 24 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Slangroom } from "@slangroom/core";

import { ethereum } from "@slangroom/ethereum";
import { fs } from "@slangroom/fs";
import { git } from "@slangroom/git";
Expand All @@ -10,32 +9,53 @@ import { oauth } from "@slangroom/oauth";
import { pocketbase } from "@slangroom/pocketbase";
import { qrcode } from "@slangroom/qrcode";
import { redis } from "@slangroom/redis";
import type { ZenParams } from "@slangroom/shared";
import { shell } from "@slangroom/shell";
import { timestamp } from "@slangroom/timestamp";
import { wallet } from "@slangroom/wallet";
import { zencode } from "@slangroom/zencode";

for await (const chunk of Bun.stdin.stream()) {
// chunk is Uint8Array
// this converts it to text (assumes ASCII encoding)
const the_input = Buffer.from(chunk).toString();
const s = new Slangroom([
ethereum,
fs,
git,
helpers,
http,
JSONSchema,
oauth,
pocketbase,
qrcode,
redis,
shell,
timestamp,
wallet,
zencode,
]);
const the_input = await Bun.stdin.text();
const s = new Slangroom([
ethereum,
fs,
git,
helpers,
http,
JSONSchema,
oauth,
pocketbase,
qrcode,
redis,
shell,
timestamp,
wallet,
zencode,
]);

const decode_and_trim = (r: string) =>
Buffer.from(r, "base64").toString().trim();
const decode = (r: string) => Buffer.from(r, "base64").toString();
const decode_and_json = (r: string) => JSON.parse(decode(r));
type Names = "conf" | "data" | "keys" | "extra";

const decode_param = (source: string, name: Names, fn: Function) => {
if (source && source !== "") {
try {
opts[name] = fn(source);
} catch (e) {
console.error(`${name} is malformed`);
process.exit(2);
}
}
};

const { result } = await s.execute(the_input, {});
console.log(JSON.stringify(result));
}
const [c, sl, d, k, e, cx] = the_input.split("\n");
const opts: ZenParams = { data: {}, keys: {} };
const contract = decode(sl);
decode_param(c, "conf", decode_and_trim);
decode_param(d, "data", decode_and_json);
decode_param(k, "keys", decode_and_json);
decode_param(e, "extra", decode_and_json);
const { result } = await s.execute(contract, opts);
await Bun.write(Bun.stdout, JSON.stringify(result));
3 changes: 3 additions & 0 deletions test/fixtures/read_file.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"filename": "test/fixtures/test.txt"
}
5 changes: 5 additions & 0 deletions test/fixtures/read_file.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rule unknown ignore
Given I send path 'filename' and read verbatim file content and output into 'content'
Given I have a 'string' named 'filename'
Given I have a 'string' named 'content'
Then print data
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do you know who greets you? 🥒
17 changes: 13 additions & 4 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ setup() {
}

@test "should execute simple zencode" {
run ./slangroom-exec < test/fixtures/simple_zencode.zen
load_fixture "simple_zencode"
run_slangroom_exec
assert_output '{"output":["Welcome_to_slangroom-exec_🥳"]}'
assert_success
}

@test "should execute simple slangroom" {
run ./slangroom-exec < test/fixtures/simple_slangroom.zen
assert_output --partial timestamp
load_fixture "simple_slangroom"
run_slangroom_exec
assert_output --partial 'timestamp'

# check that ts is a number
ts=$(echo $output | jq '.timestamp')
assert_regex "$ts" '^[0-9]+$'

assert_success
}

@test "should read data correctly" {
export FILES_DIR="."
load_fixture "read_file"
run_slangroom_exec
assert_output --partial "Do you know who greets you? 🥒"
assert_success
}
45 changes: 45 additions & 0 deletions test/test_helper/common-setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,48 @@ _common_setup() {
# make executables in src/ visible to PATH
PATH="$PROJECT_ROOT/src:$PATH"
}

load_fixture() {
name=$1

conf_file="test/fixtures/${name}.conf"
zencode_file="test/fixtures/${name}.slang"
keys_file="test/fixtures/${name}.keys.json"
data_file="test/fixtures/${name}.data.json"
extra_file="test/fixtures/${name}.extra.json"
context_file="test/fixtures/${name}.context.json"

conf=""
zencode=""
keys=""
data=""
extra=""
context=""

if [ -f "$conf_file" ]; then
conf=$(cat "$conf_file")
fi
if [ -f "$zencode_file" ]; then
zencode=$(base64 -w 0 "$zencode_file")
fi
if [ -f "$keys_file" ]; then
keys=$(jq -c . "$keys_file" | base64 -w 0)
fi
if [ -f "$data_file" ]; then
data=$(jq -c . "$data_file" | base64 -w 0)
fi
if [ -f "$extra_file" ]; then
extra=$(jq -c . "$extra_file" | base64 -w 0)
fi
if [ -f "$context_file" ]; then
context=$(jq -c . "$context_file" | base64 -w 0)
fi

printf -v slang_input '%s\n%s\n%s\n%s\n%s\n%s' "$conf" "$zencode" "$keys" "$data" "$extra" "$context"
}


run_slangroom_exec() {
bats_require_minimum_version 1.5.0
run -0 bats_pipe echo "$slang_input" \| ./slangroom-exec
}

0 comments on commit 0106f7a

Please sign in to comment.