Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : Sheet8# File : scripts/test.sh9# Module : scripts10# Repository : https://git.spboucher.ai/sheet11# Project Website : https://sheet.spboucher.ai12# Created : 2026-11-1713# Last Modified : 2026-11-1714#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: contact@spboucher.ai23# Technical Contact: dev@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# Integration tests: fresh database + storage, real production build, real worker, real headless browser for42# exports. The Anthropic API is replaced by tests/fake-anthropic.ts (no network, no cost).43# scripts/test.sh (SKIP_BUILD=1 reuses .next)44# REAL_AI=1 scripts/test.sh additionally runs tests/real-ai.test.ts against the real API with the key in .env45set -euo pipefail46cd "$(dirname "$0")/.."47export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"48REAL_KEY=""; [ "${REAL_AI:-}" = 1 ] && REAL_KEY="$(grep -E '^ANTHROPIC_API_KEY=' .env | cut -d= -f2-)"49PORT="${TEST_PORT:-8701}"; FAKE_PORT="${FAKE_ANTHROPIC_PORT:-8799}"50ROOT="$(mktemp -d /tmp/sheet-test.XXXXXX)"51DB="sheet_test"52export NODE_ENV=production NEXT_TELEMETRY_DISABLED=153export APP_URL="http://localhost:$PORT" TEST_BASE_URL="http://localhost:$PORT" INTERNAL_URL="http://127.0.0.1:$PORT"54export APP_SECRET="test-secret-test-secret-test-secret-0123456789"55export ADMIN_EMAIL="admin@example.test" ADMIN_NAME="Simon-Pierre Boucher"56export DATABASE_URL="postgres://127.0.0.1:5432/$DB"57export STORAGE_ROOT="$ROOT/storage" SETUP_URL_FILE="$ROOT/setup-url.txt"58export BACKUP_DIR="$ROOT/backups" BACKUP_PASSPHRASE_FILE="$ROOT/pass"59export UPLOAD_MAX_MB=1 STORAGE_QUOTA_GB=1 LOG_LEVEL=info60export ANTHROPIC_API_KEY="sk-ant-test-fake-key" ANTHROPIC_BASE_URL="http://127.0.0.1:$FAKE_PORT" FAKE_ANTHROPIC_PORT="$FAKE_PORT"61unset WEBAUTHN_RP_ID CLAMD_SOCKET ANTHROPIC_MODEL CHROMIUM_PATH || true62echo "test-passphrase" > "$BACKUP_PASSPHRASE_FILE"6364dropdb --if-exists "$DB"; createdb "$DB"65node_modules/.bin/tsx scripts/migrate.ts66node_modules/.bin/tsx scripts/bootstrap.ts67node_modules/.bin/tsx scripts/seed.ts68[ "${SKIP_BUILD:-}" = 1 ] && [ -d .next ] || node_modules/.bin/next build >/dev/null6970PIDS=()71start() {72 node node_modules/tsx/dist/cli.mjs tests/fake-anthropic.ts > "$ROOT/fake.log" 2>&1 & PIDS+=($!)73 PORT="$PORT" HOST=127.0.0.1 node node_modules/tsx/dist/cli.mjs server.ts > "$ROOT/web.log" 2>&1 & PIDS+=($!)74 node node_modules/tsx/dist/cli.mjs src/worker/index.ts > "$ROOT/worker.log" 2>&1 & PIDS+=($!)75 for _ in $(seq 1 80); do curl -fsS "http://localhost:$PORT/api/health" >/dev/null 2>&1 && return 0; sleep 0.5; done76 echo "server did not start"; cat "$ROOT/web.log"; return 177}78stop() { for p in "${PIDS[@]:-}"; do kill "$p" 2>/dev/null || true; done; wait 2>/dev/null || true; PIDS=(); }79trap 'stop; echo "logs and data kept in $ROOT"' EXIT8081start82node_modules/.bin/vitest run tests/api.test.ts8384echo "── restart: sessions, publications and in-flight jobs must survive ──"85stop86# A job the previous worker was running when it died:87psql -q "$DATABASE_URL" -c "UPDATE generation_jobs SET status = 'running', locked_by = 'dead:1', finished_at = NULL, output = NULL WHERE id = (SELECT id FROM generation_jobs WHERE kind = 'outline' AND status = 'succeeded' ORDER BY created_at LIMIT 1)"88start89node_modules/.bin/vitest run tests/restart.test.ts9091echo "── secrets: nothing sensitive in what browsers download or in the logs ──"92! grep -rIl -e "sk-ant-" -e "$APP_SECRET" .next/static 2>/dev/null | grep . || { echo "SECRET FOUND IN CLIENT BUNDLE"; exit 1; }93! grep -l -e "sk-ant-test-fake-key" -e "$APP_SECRET" "$ROOT"/web.log "$ROOT"/worker.log | grep . || { echo "SECRET FOUND IN LOGS"; exit 1; }94node_modules/.bin/tsx scripts/secrets-scan.ts9596echo "── backup → restore into a fresh database and storage ──"97bash scripts/backup.sh98dropdb --if-exists "${DB}_restore"; createdb "${DB}_restore"99bash scripts/restore.sh db "$(ls "$BACKUP_DIR"/db/*.dump.enc | tail -1)" "postgres://127.0.0.1:5432/${DB}_restore"100bash scripts/restore.sh storage "$(ls "$BACKUP_DIR"/storage/*.tar.enc | tail -1)" "$ROOT/storage-restored"101for t in sheets sheet_revisions sheet_publications assets passkey_credentials audit_events; do102 a=$(psql -Atc "select count(*) from $t" "$DATABASE_URL"); b=$(psql -Atc "select count(*) from $t" "postgres://127.0.0.1:5432/${DB}_restore")103 [ "$a" = "$b" ] || { echo "RESTORE MISMATCH on $t: $a vs $b"; exit 1; }104done105# Every stored file of the restored database exists in the restored storage, with the recorded checksum.106psql -Atc "select storage_key || ' ' || sha256 from assets where deleted_at is null" "postgres://127.0.0.1:5432/${DB}_restore" | while read -r key sum; do107 [ "$(shasum -a 256 "$ROOT/storage-restored/$key" | cut -d' ' -f1)" = "$sum" ] || { echo "RESTORED FILE MISMATCH $key"; exit 1; }108done109echo "restore ok: $(psql -Atc 'select count(*) from sheets' "postgres://127.0.0.1:5432/${DB}_restore") sheets, $(psql -Atc 'select count(*) from assets where deleted_at is null' "postgres://127.0.0.1:5432/${DB}_restore") files verified"110dropdb --if-exists "${DB}_restore"111112if [ -n "$REAL_KEY" ]; then113 echo "── real Anthropic API ──"114 stop; unset ANTHROPIC_BASE_URL; export ANTHROPIC_API_KEY="$REAL_KEY"; start115 node_modules/.bin/vitest run tests/real-ai.test.ts116fi117echo "ALL TESTS PASSED"118