#!/usr/bin/env bash
# the binding test for the first-token invariant.
#
# Invariant: for every new session, at the moment the first token is
# emitted, the stream-log file for that session exists on disk and contains
# that token's bytes. Tasks 1006/1008/1010 each shipped a partial slice;
# this test fixes the invariant itself to one assertion.
#
# The .test.sh entrypoint is the operator-facing surface — runnable from a
# bash prompt, from boot smoke, and from any CI runner that has `node` on
# PATH. The contract assertions live in the vitest spec at:
#   platform/ui/server/routes/admin/__tests__/first-token-creates-stream-log.test.ts
#
# That spec exercises the live chat-route handler, the live appendFileSync
# writer, and a live filesystem (mkdtempSync tmpdir). The only seams
# substituted are upstream of the writer: the agent SDK invocation, the
# session-store identity lookups, and the Neo4j createConversation
# roundtrip. None of those are the writer the invariant binds.
#
# Exit codes:
#   0  test passed — file present, token bytes present
#   1  test failed — file absent OR token bytes absent
#   2  vitest binary not resolvable from platform/ui

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UI_DIR="$(cd "$SCRIPT_DIR/../../ui" && pwd)"
SPEC_REL="server/routes/admin/__tests__/first-token-creates-stream-log.test.ts"

if [[ ! -f "$UI_DIR/$SPEC_REL" ]]; then
  echo "FATAL: vitest spec missing at $UI_DIR/$SPEC_REL" >&2
  exit 2
fi

cd "$UI_DIR"
exec npx --no-install vitest run "$SPEC_REL" --reporter=verbose
