Skip to content
pi-c Artifact [91537e7da6]
Login

Artifact [91537e7da6]

Artifact 91537e7da66a78d1e379d79768ba09f42780353ca60f104af9e543d9ae2987df:


#!/bin/sh
# Tool contracts: each case runs one scripted tool call and compares the
# journaled result exactly (Pi's messages, limits and notices). --no-faults
# skips the crash cases (the release build has no crash hooks).
set -eu
PIC=${PIC:?set PIC to the pic binary}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tools.XXXXXX")
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
n=0

# call NAME JSON: a fresh journal and workspace (WS), one call to tool NAME
# with input JSON, then an answer. The result lands in $T/result.
setup() { n=$((n + 1)); DB="$T/$n.sqlite"; WS="$T/ws$n"; mkdir "$WS"; }
call() {
    printf 'tool raw %s %s\ntext done\n' "$1" "$2" > "$T/script"
    "$PIC" --db "$DB" --workspace "$WS" submit job 'go' > /dev/null
    "$PIC" --db "$DB" --workspace "$WS" run --script "$T/script" 2> "$T/stderr" || fail "run: $(cat "$T/stderr")"
    "$PIC" --db "$DB" result 1 > "$T/result"
}
expect() { # expect LABEL ERROR_FLAG: the result must equal stdin exactly
    { printf 'error=%s\n' "$2"; cat; } > "$T/want"
    cmp -s "$T/want" "$T/result" || { diff "$T/want" "$T/result" >&2 || true; fail "$1"; }
    printf 'PASS: %s\n' "$1"
}
contains() { # contains LABEL TEXT
    grep -qF -- "$2" "$T/result" || { cat "$T/result" >&2; fail "$1"; }
    printf 'PASS: %s\n' "$1"
}

# ---- read
setup; printf 'a\nb\nc\n' > "$WS/f.txt"
call read '{"path":"f.txt"}'
printf 'a\nb\nc\n\n' | expect 'read: whole small file' 0
setup; printf 'a\nb\nc\n' > "$WS/f.txt"
call read '{"path":"f.txt","offset":2,"limit":1}'
printf 'b\n\n[2 more lines in file. Use offset=3 to continue.]\n' | expect 'read: offset and limit' 0
setup; printf 'a\nb\nc\n' > "$WS/f.txt"
call read '{"path":"f.txt","offset":10}'
printf 'Offset 10 is beyond end of file (4 lines total)\n' | expect 'read: offset past the end' 1
setup; seq 1 2500 > "$WS/f.txt"
call read '{"path":"f.txt"}'
contains 'read: 2000-line limit' '[Showing lines 1-2000 of 2501. Use offset=2001 to continue.]'
setup; awk 'BEGIN { for (i = 0; i < 100; i++) { s = ""; for (j = 0; j < 1000; j++) s = s "x"; print s } }' > "$WS/f.txt"
call read '{"path":"f.txt"}'
contains 'read: 50KB limit' '[Showing lines 1-51 of 101 (50KB limit). Use offset=52 to continue.]'
setup; awk 'BEGIN { s = ""; for (j = 0; j < 60000; j++) s = s "y"; print s }' > "$WS/f.txt"
call read '{"path":"f.txt"}'
printf "[Line 1 is 58.6KB, exceeds 50KB limit. Use bash: sed -n '1p' f.txt | head -c 51200]\n" | expect 'read: first line over the limit' 0
setup
call read '{"path":"missing.txt"}'
contains 'read: missing file' 'ENOENT'

# ---- write
setup
call write '{"path":"a/b/c.txt","content":"x\ny"}'
printf 'Successfully wrote to a/b/c.txt\n' | expect 'write: creates parent directories' 0
[ "$(cat "$WS/a/b/c.txt")" = "$(printf 'x\ny')" ] || fail 'write: content'
setup
call write '{"path":"f.txt"}'
printf 'write needs string inputs path and content\n' | expect 'write: missing content' 1

# ---- edit
setup; printf 'alpha\nbeta\ngamma\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"beta","newText":"BETA"},{"oldText":"gamma","newText":"GAMMA"}]}'
printf 'Successfully replaced 2 block(s) in f.txt.\n' | expect 'edit: two disjoint edits' 0
[ "$(cat "$WS/f.txt")" = "$(printf 'alpha\nBETA\nGAMMA')" ] || fail 'edit: content'
setup; printf 'alpha\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"zeta","newText":"x"}]}'
printf 'Could not find the exact text in f.txt. The old text must match exactly including all whitespace and newlines.\n' |
    expect 'edit: not found' 1
setup; printf 'x x\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"x","newText":"y"}]}'
printf 'Found 2 occurrences of the text in f.txt. The text must be unique. Please provide more context to make it unique.\n' |
    expect 'edit: not unique' 1
setup; printf 'abcdef\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"abcd","newText":"1"},{"oldText":"cdef","newText":"2"}]}'
printf 'edits[0] and edits[1] overlap in f.txt. Merge them into one edit or target disjoint regions.\n' |
    expect 'edit: overlapping edits' 1
setup; printf 'one\r\ntwo\r\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"one\ntwo","newText":"uno\ndos"}]}'
[ "$(od -An -c "$WS/f.txt" | tr -s ' ')" = "$(printf 'uno\r\ndos\r\n' | od -An -c | tr -s ' ')" ] ||
    fail 'edit: CRLF line endings not kept'
printf 'PASS: edit: CRLF matched as LF and kept\n'
setup; printf '\357\273\277hello\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"hello","newText":"bye"}]}'
[ "$(od -An -tx1 "$WS/f.txt" | tr -d ' \n')" = efbbbf6279650a ] || fail 'edit: BOM not kept'
printf 'PASS: edit: byte-order mark kept\n'
setup; printf 'same\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[{"oldText":"same","newText":"same"}]}'
printf 'No changes made to f.txt. The replacement produced identical content.\n' | expect 'edit: no change' 1
setup
call edit '{"path":"nope.txt","edits":[{"oldText":"a","newText":"b"}]}'
printf 'Could not edit file: nope.txt. Error code: ENOENT.\n' | expect 'edit: missing file' 1
setup; printf 'old\n' > "$WS/f.txt"
call edit '{"path":"f.txt","oldText":"old","newText":"new"}'
[ "$(cat "$WS/f.txt")" = new ] || fail 'edit: legacy top-level oldText/newText'
setup; printf 'old\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":"[{\"oldText\":\"old\",\"newText\":\"new\"}]"}'
[ "$(cat "$WS/f.txt")" = new ] || fail 'edit: edits given as a JSON string'
printf 'PASS: edit: accepts the input forms Pi accepts\n'
setup; printf 'a\n' > "$WS/f.txt"
call edit '{"path":"f.txt","edits":[]}'
printf 'Edit tool input is invalid. edits must contain at least one replacement.\n' | expect 'edit: no edits' 1

# ---- bash
setup
call bash '{"command":"echo hi; exit 3"}'
printf 'hi\n\nCommand exited with code 3\n' | expect 'bash: exit status' 1
setup
call bash '{"command":"true"}'
printf '(no output)\n' | expect 'bash: no output' 0
# A cut result keeps the full output in the journal, named pic:output/N; no
# temp file is made, and read opens the name like a file.
setup
mkdir "$T/tmp$n"
TMPDIR="$T/tmp$n" call bash '{"command":"seq 1 3000"}'
contains 'bash: keeps the last 2000 lines' '[Showing lines 1001-3000 of 3000. Full output: pic:output/1 (read it with the read tool'
[ -z "$(ls -A "$T/tmp$n")" ] || fail "bash: temp files made: $(ls "$T/tmp$n")"
printf 'tool raw read {"path":"pic:output/1","offset":2998,"limit":2}\ntool raw read {"path":"pic:output/9"}\ntext done\n' > "$T/script"
"$PIC" --db "$DB" --workspace "$WS" submit job2 'go' > /dev/null
"$PIC" --db "$DB" --workspace "$WS" run --script "$T/script" 2> "$T/stderr" || fail "run: $(cat "$T/stderr")"
"$PIC" --db "$DB" result 2 > "$T/result"
printf 'error=0\n2998\n2999\n\n[2 more lines in file. Use offset=3000 to continue.]\n' | cmp -s - "$T/result" || { cat "$T/result" >&2; fail 'bash: read of the output slice'; }
"$PIC" --db "$DB" result 3 > "$T/result"
contains 'bash: missing output id' 'ENOENT: No such file or directory, read '"'"'pic:output/9'"'"
printf 'PASS: bash: full output journaled when cut\n'
if command -v sqlite3 > /dev/null 2>&1; then
    row=$(sqlite3 "$DB" 'SELECT sz, length(data) FROM pic_output')
    sz=${row%%|*}; stored=${row##*|}
    [ "$sz" = 13893 ] && [ "$stored" -lt "$sz" ] || fail "bash: output not stored compressed ($row)"
    printf 'PASS: bash: output stored compressed\n'
fi
setup
start=$(date +%s)
call bash '{"command":"sleep 30","timeout":1}'
[ $(( $(date +%s) - start )) -lt 10 ] || fail 'bash: timeout not applied'
printf '(no output)\n\nCommand timed out after 1 seconds\n' | expect 'bash: timeout input' 1
setup
call bash '{"command":"true","timeout":-1}'
printf 'Invalid timeout: must be a finite number of seconds\n' | expect 'bash: invalid timeout' 1

# ---- other
setup
call nope '{}'
printf 'Tool nope not found\n' | expect 'unknown tool' 1

[ "$FAULTS" = yes ] || exit 0

# Cancelling a submission whose tool call never ran skips the call with a
# result saying so; the journal stays consistent and nothing runs later.
setup
printf 'tool bash touch ran\ntext done\n' > "$T/script"
"$PIC" --db "$DB" --workspace "$WS" submit job 'go' > /dev/null
if env PIC_CRASH_AT=after-assistant:1 "$PIC" --db "$DB" --workspace "$WS" run --script "$T/script" 2>/dev/null; then
    fail 'cancel: no crash'
fi
[ "$("$PIC" --db "$DB" --workspace "$WS" cancel job)" = cancelled ] || fail 'cancel: refused'
"$PIC" --db "$DB" --workspace "$WS" run --script "$T/script"
"$PIC" --db "$DB" check > /dev/null || fail 'cancel: integrity'
"$PIC" --db "$DB" dump | grep -q '^submission 1 job failed .* error=cancelled$' || fail 'cancel: status'
"$PIC" --db "$DB" dump | grep -q '^effect 1 2\.0 bash skipped' || fail 'cancel: call not skipped'
[ ! -e "$WS/ran" ] || fail 'cancel: the call ran'
if "$PIC" --db "$DB" --workspace "$WS" cancel job 2>/dev/null; then fail 'cancel: cancelled twice'; fi
printf 'PASS: cancel skips calls that have not run\n'

# An edit interrupted after it wrote the file but before its result was
# recorded is replayed from its journaled plan: same bytes, a success.
for point in after-started:1 after-exec:1; do
    setup; printf 'alpha\nbeta\n' > "$WS/f.txt"
    printf 'tool raw edit {"path":"f.txt","edits":[{"oldText":"beta","newText":"BETA"}]}\ntext done\n' > "$T/script"
    "$PIC" --db "$DB" --workspace "$WS" submit job 'go' > /dev/null
    if env PIC_CRASH_AT="$point" "$PIC" --db "$DB" --workspace "$WS" run --script "$T/script" 2>/dev/null; then
        fail "edit $point: no crash"
    fi
    "$PIC" --db "$DB" --workspace "$WS" run --script "$T/script"
    "$PIC" --db "$DB" check > /dev/null || fail "edit $point: integrity"
    "$PIC" --db "$DB" result 1 > "$T/result"
    printf 'Successfully replaced 1 block(s) in f.txt.\n' | expect "edit replayed from its plan after a crash at $point" 0
    [ "$(cat "$WS/f.txt")" = "$(printf 'alpha\nBETA')" ] || fail "edit $point: content"
done