#!/usr/bin/env bash # tests/e2e/dispatch_helpers.test.sh — covers C1, C2, C3, C4, C5, C6 from chg_0003. # # Strategy: each test sets up its own scratch sandbox dir under # /tmp/xstudio-sandbox//space, runs the script, asserts, cleans up. # # Why not use real /tmp/xstudio-sandbox from kernel: tests are self-contained, # don't depend on kernel state, can run anywhere (CI / dev box / agent loop). # # Exit codes: # 0 all cases passed # 1 at least one case failed (with FAIL: prefix printed) # 2 setup error set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" SCRIPTS="$ROOT/scripts" SANDBOX_ROOT="/tmp/xstudio-sandbox" SRC_SKILLS="$HOME/.claude/skills" PASS_COUNT=0 FAIL_COUNT=0 FAILED_CASES=() log_pass() { echo "PASS: $1"; PASS_COUNT=$((PASS_COUNT + 1)); } log_fail() { echo "FAIL: $1"; FAIL_COUNT=$((FAIL_COUNT + 1)); FAILED_CASES+=("$1"); } # ---- helpers ---- setup_fake_sandbox() { local hash="$1" local space="$SANDBOX_ROOT/$hash/space" rm -rf "$SANDBOX_ROOT/$hash" mkdir -p "$space" echo "$hash" } cleanup_fake_sandbox() { local hash="$1" rm -rf "$SANDBOX_ROOT/$hash" } # Fake ssw.ini for setup test (writes key= row in [llm-api] section) setup_fake_ssw_ini() { local ini="$HOME/ssw.ini" if [[ ! -f "$ini" ]]; then cat > "$ini" <<'EOF' [llm-api] key=test-token-fake-12345 [other] key=should-not-pick EOF echo "$ini" return 0 fi echo "" return 1 } # Cleanup a fake ssw.ini we created cleanup_fake_ssw_ini() { local ini="$1" [[ -n "$ini" && -f "$ini" ]] && rm -f "$ini" } # ---- C1 + C2: prep copies skills, excludes template-boot-jnpf ---- test_C1_C2_prep() { local hash hash=$(setup_fake_sandbox "test-prep-$$") # 容器内 ~/.claude/skills/ 是 symlink 到 host 不可解析路径;准备 mock 源目录 local mock_src="/tmp/dispatch-helpers-test-src-$$" rm -rf "$mock_src" mkdir -p "$mock_src/ai-collab-framework" "$mock_src/ssw-jnpf-lite-react/template-boot-jnpf" echo "fake-acf" > "$mock_src/ai-collab-framework/SKILL.md" echo "fake-jnpf" > "$mock_src/ssw-jnpf-lite-react/SKILL.md" echo "fake-tpl" > "$mock_src/ssw-jnpf-lite-react/template-boot-jnpf/index.html" if ! bash "$SCRIPTS/prep-sandbox-skills.sh" "$hash" --src "$mock_src" >/tmp/prep-out.log 2>&1; then log_fail "C1_prep_copies_ai_collab_framework (script exit nonzero; see /tmp/prep-out.log)" rm -rf "$mock_src" cleanup_fake_sandbox "$hash" return 1 fi local dest="$SANDBOX_ROOT/$hash/space/.claude/skills" # C1: ai-collab-framework exists if [[ -d "$dest/ai-collab-framework" ]]; then log_pass "C1_prep_copies_ai_collab_framework" else log_fail "C1_prep_copies_ai_collab_framework (ai-collab-framework missing in $dest)" rm -rf "$mock_src" cleanup_fake_sandbox "$hash" return 1 fi # C2: ssw-jnpf-lite-react/template-boot-jnpf excluded (either top-level skip or inner exclude) if [[ -d "$dest/ssw-jnpf-lite-react" ]]; then if [[ -d "$dest/ssw-jnpf-lite-react/template-boot-jnpf" ]]; then log_fail "C2_prep_skips_template_boot_jnpf (template-boot-jnpf not excluded)" else log_pass "C2_prep_skips_template_boot_jnpf" fi else # ssw-jnpf-lite-react not copied at all (acceptable: only ai-collab-framework is required by C1) log_pass "C2_prep_skips_template_boot_jnpf (ssw-jnpf-lite-react not copied)" fi rm -rf "$mock_src" cleanup_fake_sandbox "$hash" return 0 } # ---- C3: setup writes claude-process provider ---- test_C3_setup_writes() { local wsRoot="/tmp/dispatch-helpers-test-ws-$$" mkdir -p "$wsRoot" local ini="" if [[ -z "${ANTHROPIC_AUTH_TOKEN:-}" && ! -f "$HOME/ssw.ini" ]]; then ini=$(setup_fake_ssw_ini) || true fi if ! bash "$SCRIPTS/setup-xstudio-ai.sh" "$wsRoot" >/tmp/setup-out.log 2>&1; then log_fail "C3_setup_writes_claude_process_provider (script exit nonzero; see /tmp/setup-out.log)" rm -rf "$wsRoot" [[ -n "$ini" ]] && cleanup_fake_ssw_ini "$ini" return 1 fi local cfg="$wsRoot/.xstudio/ai-config.json" if [[ ! -f "$cfg" ]]; then log_fail "C3_setup_writes_claude_process_provider (no config file written)" rm -rf "$wsRoot" [[ -n "$ini" ]] && cleanup_fake_ssw_ini "$ini" return 1 fi # python3 checks (assert baseURL non-empty, model non-empty, authTokenEnv == "ANTHROPIC_AUTH_TOKEN") local base_url model auth_token_env read -r base_url model auth_token_env < <(python3 -c " import json, sys with open('$cfg') as f: cfg = json.load(f) cp = cfg['providers']['claude-process'] print(cp.get('baseURL', ''), cp.get('model', ''), cp.get('authTokenEnv', '')) ") if [[ -z "$base_url" ]]; then log_fail "C3_setup_writes_claude_process_provider (baseURL empty: $base_url)" elif [[ -z "$model" ]]; then log_fail "C3_setup_writes_claude_process_provider (model empty: $model)" elif [[ "$auth_token_env" != "ANTHROPIC_AUTH_TOKEN" ]]; then log_fail "C3_setup_writes_claude_process_provider (authTokenEnv mismatch: $auth_token_env)" else log_pass "C3_setup_writes_claude_process_provider" fi # store cfg path for C4 echo "$cfg" > /tmp/dispatch-test-cfg-path rm -rf "$wsRoot" [[ -n "$ini" ]] && cleanup_fake_ssw_ini "$ini" return 0 } # ---- C4: setup preserves existing providers ---- test_C4_setup_preserves() { local wsRoot="/tmp/dispatch-helpers-test-preserve-$$" mkdir -p "$wsRoot/.xstudio" # Pre-seed ai-config with anthropic-api provider cat > "$wsRoot/.xstudio/ai-config.json" <<'EOF' { "providers": { "anthropic-api": { "baseURL": "https://api.anthropic.com", "authTokenEnv": "ANTHROPIC_API_KEY", "model": "claude-fable-5" } } } EOF local ini="" if [[ -z "${ANTHROPIC_AUTH_TOKEN:-}" && ! -f "$HOME/ssw.ini" ]]; then ini=$(setup_fake_ssw_ini) || true fi if ! bash "$SCRIPTS/setup-xstudio-ai.sh" "$wsRoot" >/tmp/setup-preserve-out.log 2>&1; then log_fail "C4_setup_preserves_existing_providers (script exit nonzero)" rm -rf "$wsRoot" [[ -n "$ini" ]] && cleanup_fake_ssw_ini "$ini" return 1 fi local cfg="$wsRoot/.xstudio/ai-config.json" local preserved claude_present read -r preserved claude_present < <(python3 -c " import json, sys with open('$cfg') as f: cfg = json.load(f) print(cfg['providers'].get('anthropic-api', {}).get('baseURL', ''), cfg['providers'].get('claude-process', {}).get('baseURL', '')) ") if [[ "$preserved" != "https://api.anthropic.com" ]]; then log_fail "C4_setup_preserves_existing_providers (anthropic-api.baseURL lost: '$preserved')" elif [[ -z "$claude_present" ]]; then log_fail "C4_setup_preserves_existing_providers (claude-process not added)" else log_pass "C4_setup_preserves_existing_providers" fi rm -rf "$wsRoot" [[ -n "$ini" ]] && cleanup_fake_ssw_ini "$ini" return 0 } # ---- C5: wait returns when report appears ---- test_C5_wait_returns() { local hash="test-wait-ok-$$" setup_fake_sandbox "$hash" local report="$SANDBOX_ROOT/$hash/space/.xstudio-report.md" # Background: touch report after 2s (sleep 2 && echo "=== fake report ===" > "$report") & local touch_pid=$! # wait with timeout 10s if bash "$SCRIPTS/wait-for-task.sh" "fake-task-$$" 10 --poll-interval 1 >/tmp/wait-ok-out.log 2>&1; then if grep -q "fake report" /tmp/wait-ok-out.log; then log_pass "C5_wait_returns_when_report_appears" else log_fail "C5_wait_returns_when_report_appears (script exit 0 but no report content in stdout)" fi else log_fail "C5_wait_returns_when_report_appears (script exit nonzero)" fi # If touch never happened, kill it kill "$touch_pid" 2>/dev/null || true wait "$touch_pid" 2>/dev/null || true cleanup_fake_sandbox "$hash" return 0 } # ---- C6: wait times out when report missing ---- test_C6_wait_times_out() { local hash="test-wait-tmo-$$" setup_fake_sandbox "$hash" # Note: never create the report local start end elapsed start=$(date +%s) # timeout 4s set +e bash "$SCRIPTS/wait-for-task.sh" "fake-task-$$" 4 --poll-interval 1 >/tmp/wait-tmo-out.log 2>&1 local rc=$? set -e end=$(date +%s) elapsed=$((end - start)) if [[ $rc -ne 1 ]]; then log_fail "C6_wait_times_out_when_report_missing (expected exit 1, got $rc)" elif ! grep -q "TIMEOUT" /tmp/wait-tmo-out.log; then log_fail "C6_wait_times_out_when_report_missing (no TIMEOUT in stderr)" elif [[ $elapsed -lt 3 || $elapsed -gt 8 ]]; then log_fail "C6_wait_times_out_when_report_missing (elapsed=${elapsed}s, expected ~4s)" else log_pass "C6_wait_times_out_when_report_missing" fi cleanup_fake_sandbox "$hash" return 0 } # ---- preflight ---- need_cmd() { if ! command -v "$1" >/dev/null 2>&1; then echo "missing required command: $1" >&2 exit 2 fi } need_cmd bash need_cmd python3 mkdir -p /tmp/xstudio-sandbox # ---- run ---- echo "=== chg_0003 dispatch_helpers e2e ===" echo "ROOT=$ROOT" echo "SCRIPTS=$SCRIPTS" echo "SRC_SKILLS=$SRC_SKILLS" echo test_C1_C2_prep test_C3_setup_writes test_C4_setup_preserves test_C5_wait_returns test_C6_wait_times_out echo echo "================================================" echo "Pass: $PASS_COUNT Fail: $FAIL_COUNT" if [[ $FAIL_COUNT -gt 0 ]]; then echo "Failed cases:" for c in "${FAILED_CASES[@]}"; do echo " - $c" done exit 1 fi exit 0