|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Round-trip tests for --goal launch receipts (#499) and the ps/status |
| 3 | +# goal column (#520): write_launch_receipt -> container_goal. |
| 4 | +# Hermetic: scratch XDG_DATA_HOME, docker stubbed. |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 9 | + |
| 10 | +PASS=0 |
| 11 | +FAIL=0 |
| 12 | +ERRORS="" |
| 13 | + |
| 14 | +pass() { PASS=$((PASS + 1)); echo " ok: $1"; } |
| 15 | +fail() { FAIL=$((FAIL + 1)); ERRORS="${ERRORS}\n FAIL: $1"; echo " FAIL: $1" >&2; } |
| 16 | + |
| 17 | +assert_eq() { |
| 18 | + local label="$1" expected="$2" actual="$3" |
| 19 | + if [ "$expected" = "$actual" ]; then |
| 20 | + pass "$label" |
| 21 | + else |
| 22 | + fail "$label: expected='$expected' actual='$actual'" |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +source_helpers() { |
| 27 | + eval "$(sed -n '/^write_launch_receipt()/,/^}/p' "$REPO_ROOT/deva.sh")" |
| 28 | + eval "$(sed -n '/^container_goal()/,/^}/p' "$REPO_ROOT/deva.sh")" |
| 29 | +} |
| 30 | + |
| 31 | +source_helpers |
| 32 | + |
| 33 | +SCRATCH="$(mktemp -d)" |
| 34 | +trap 'rm -rf "$SCRATCH"' EXIT |
| 35 | +export XDG_DATA_HOME="$SCRATCH/data" |
| 36 | +LAUNCH_DIR="$XDG_DATA_HOME/ccx/launches" |
| 37 | + |
| 38 | +# docker stub: emits env lines only for the one container the fallback |
| 39 | +# test expects; fails for everything else like a missing container would. |
| 40 | +docker() { |
| 41 | + if [ "${DOCKER_STUB_NAME:-}" = "${!#}" ]; then |
| 42 | + printf 'PATH=/usr/bin\nDEVA_GOAL=%s\nHOME=/home/deva\n' "$DOCKER_STUB_GOAL" |
| 43 | + return 0 |
| 44 | + fi |
| 45 | + return 1 |
| 46 | +} |
| 47 | + |
| 48 | +echo "=== write_launch_receipt ===" |
| 49 | + |
| 50 | +GOAL="" |
| 51 | +ACTIVE_AGENT="claude" |
| 52 | +CONTAINER_NAME="deva--claude--auth-default--proj..abcd" |
| 53 | +write_launch_receipt |
| 54 | +if [ -d "$LAUNCH_DIR" ] && [ -n "$(ls -A "$LAUNCH_DIR" 2>/dev/null)" ]; then |
| 55 | + fail "no goal writes no receipt" |
| 56 | +else |
| 57 | + pass "no goal writes no receipt" |
| 58 | +fi |
| 59 | + |
| 60 | +GOAL="fix-auth-race" |
| 61 | +write_launch_receipt |
| 62 | +receipt_file="$LAUNCH_DIR/$(date -u +%Y-%m-%d).jsonl" |
| 63 | +if [ -f "$receipt_file" ]; then |
| 64 | + pass "receipt file created" |
| 65 | +else |
| 66 | + fail "receipt file created" |
| 67 | +fi |
| 68 | +assert_eq "receipt line count" "1" "$(wc -l <"$receipt_file" | tr -d ' ')" |
| 69 | +assert_eq "receipt goal field" "fix-auth-race" "$(jq -r '.goal' "$receipt_file")" |
| 70 | +assert_eq "receipt agent field" "claude" "$(jq -r '.agent' "$receipt_file")" |
| 71 | +assert_eq "receipt container field" "$CONTAINER_NAME" "$(jq -r '.container' "$receipt_file")" |
| 72 | +assert_eq "receipt source field" "deva" "$(jq -r '.source' "$receipt_file")" |
| 73 | + |
| 74 | +echo "=== container_goal ===" |
| 75 | + |
| 76 | +assert_eq "goal from receipt" "fix-auth-race" "$(container_goal "$CONTAINER_NAME")" |
| 77 | + |
| 78 | +GOAL="ship-ps-column" |
| 79 | +write_launch_receipt |
| 80 | +assert_eq "last receipt wins" "ship-ps-column" "$(container_goal "$CONTAINER_NAME")" |
| 81 | + |
| 82 | +GOAL="other-goal" |
| 83 | +CONTAINER_NAME="deva--codex--auth-default--other..ef01" |
| 84 | +write_launch_receipt |
| 85 | +assert_eq "receipts keyed per container" "ship-ps-column" "$(container_goal "deva--claude--auth-default--proj..abcd")" |
| 86 | + |
| 87 | +DOCKER_STUB_NAME="deva--grok--auth-default--env..2345" |
| 88 | +DOCKER_STUB_GOAL="from-create-env" |
| 89 | +assert_eq "env fallback when no receipt" "from-create-env" "$(container_goal "$DOCKER_STUB_NAME")" |
| 90 | + |
| 91 | +assert_eq "no receipt no env is --" "--" "$(container_goal "deva--kimi--auth-default--gone..6789")" |
| 92 | + |
| 93 | +rm -rf "$LAUNCH_DIR" |
| 94 | +assert_eq "missing launches dir falls through" "--" "$(container_goal "deva--claude--auth-default--proj..abcd")" |
| 95 | + |
| 96 | +echo "" |
| 97 | +if [ "$FAIL" -gt 0 ]; then |
| 98 | + printf 'FAILED: %d/%d%b\n' "$FAIL" "$((PASS + FAIL))" "$ERRORS" >&2 |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | +echo "All $PASS tests passed" |
0 commit comments