#!/bin/sh
# The same codemode scripts on both engines (src/jsengine.h): QuickJS, out of process ($PIC), and
# Micro QuickJS, in process ($PIC_MICRO, the no-OS build's engine). Scripts in the subset both
# run (ES5, for...of, **, calls that return values, parallel()) give the same result on each; what
# Micro QuickJS lacks fails there with a SyntaxError and runs on QuickJS.
set -eu
PIC=${PIC:?set PIC to the pic binary}
PIC_MICRO=${PIC_MICRO:?set PIC_MICRO to the micro build of pic}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-engines.XXXXXX")
export HOME="$T/home" PIC_CODEMODE=on
mkdir -p "$HOME"
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
# run BINARY JSON: the script's result on that build, wall time left out (a journal of its own)
run() {
    d=$(mktemp -d "$T/run.XXXXXX")
    mkdir -p "$d/ws"
    printf 'hello\nworld\n' > "$d/ws/notes.txt"
    printf 'tool raw codemode %s\ntext done\n' "$2" > "$d/script"
    "$1" --db "$d/j.sqlite" --workspace "$d/ws" submit job-1 'go' > /dev/null
    "$1" --db "$d/j.sqlite" --workspace "$d/ws" run --script "$d/script" 2> /dev/null || fail "run on $1: $2"
    "$1" --db "$d/j.sqlite" --workspace "$d/ws" check > /dev/null || fail "integrity on $1: $2"
    sqlite3 "$d/j.sqlite" "SELECT is_error || ' ' || result FROM pic_effect WHERE tool = 'codemode'" |
        sed -e '/^Wall time /d' -e "s#$d#RUN#g"
}
same() { # same NAME JSON: both engines give one result
    a=$(run "$PIC" "$2")
    b=$(run "$PIC_MICRO" "$2")
    [ "$a" = "$b" ] || fail "$1: the engines differ:
QuickJS:
$a
Micro QuickJS:
$b"
    printf 'PASS: %s\n' "$1"
}
same 'calls return values' '{"script":"var t = tools.read({path: \"notes.txt\"});\nreturn t.split(\"\\n\").length"}'
same 'parallel, in order' '{"script":"var r = parallel([function () { return tools.bash({command: \"echo a\"}); }, {tool: \"bash\", args: {command: \"echo b\"}}]);\nfor (var x of r) text(x);\nreturn r.length"}'
same 'parallel settled' '{"script":"return parallel([{tool: \"bash\", args: {command: \"exit 3\"}}, {tool: \"read\", args: {path: \"notes.txt\"}}], {settled: true}).map(function (r) { return r.status; })"}'
same 'a failing call throws' '{"script":"text(\"before\");\ntry { tools.bash({command: \"exit 2\"}); } catch (e) { text(\"caught\"); }\ntools.read({path: \"missing.txt\"})"}'
same 'console, text, JSON, **' '{"script":"console.log(\"x\", 1, {a: [1, 2]});\ntext({b: 2 ** 10});\nreturn JSON.parse(\"[1,2]\").concat([3])"}'
same 'options and truncation' '{"script":"// @options: {\"max_output_tokens\": 10}\nfor (var i = 0; i < 20; i++) text(\"line \" + i)"}'
same 'bad options' '{"script":"// @options: {\"bad\": 1}\nreturn 1"}'
same 'store and load' '{"script":"store(\"k\", {v: 1});\nreturn load(\"k\").v + 1"}'
same 'discovery' '{"script":"return [typeof searchTools(\"read file\")[0].name, typeof describeTool(\"read\"), ALL_TOOLS.length > 3]"}'
same 'exit' '{"script":"text(\"a\"); exit(); text(\"b\")"}'
same 'the deadline' '{"script":"// @options: {\"timeout_ms\": 300}\nwhile (true) {}"}'
same 'a script cannot start a script' '{"script":"return \"codemode\" in tools"}'
# Past the subset: Micro QuickJS refuses the syntax; QuickJS runs it.
es6='{"script":"const xs = [1, 2, 3];\nreturn xs.map((x) => `${x * 2}`)"}'
run "$PIC" "$es6" | grep -q '^0 ' || fail "ES2015 on QuickJS: $(run "$PIC" "$es6")"
run "$PIC_MICRO" "$es6" | grep -q 'SyntaxError' || fail "ES2015 on Micro QuickJS: $(run "$PIC_MICRO" "$es6")"
printf 'PASS: past the subset, Micro QuickJS refuses the syntax and QuickJS runs it\n'
