diff --git a/CLAUDE.md b/CLAUDE.md index 1770442b..b03b69b5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -129,7 +129,7 @@ pnpm db:reset # replay from zero pnpm db:test # pgTAP on the real stack pnpm db:types # regenerate packages/supabase/src/database.types.ts pnpm db:types:check # CI diff -pnpm db:sandbox up fresh|up upgrade []|test-db|types [--check]|test|e2e-shell|verify-e2e|down # throwaway stack (SUPABASE.md §5.1): migrations tested without touching DEV +pnpm db:sandbox up fresh|up upgrade []|test-db|types [--check]|test|e2e-shell|verify-e2e|avatar-e2e|down # throwaway stack (SUPABASE.md §5.1): migrations tested without touching DEV # Reference sheet in the app: the dev server bundles the curated draft; a build does not pnpm build:preview # SHEET_BUNDLE=draft build (manual validation, preview deployments) @@ -149,7 +149,7 @@ pnpm rust:check:docker # the same rust-check.sh inside the builder image pnpm wasm:build:docker # the same wasm-build.sh inside the builder image pnpm worker:test # Docker only: crates/worker/Dockerfile stage test, sandbox tests + seccomp derivation on the musl binary pnpm worker:image # Docker only: runtime image faunosteo_worker:dev (distroless/static, `run --check`) -pnpm e2e:avatar # scripts/avatar-e2e.sh: worker container against the local stack + packages/supabase/tests/e2e (real-stack avatar flow) +pnpm e2e:avatar # scripts/avatar-e2e.sh: worker container against the local DEV stack + packages/supabase/tests/e2e (owner's call only; `pnpm db:sandbox avatar-e2e` runs the same suite on the throwaway stack) ``` ## Test commands @@ -270,7 +270,7 @@ faunosteo/ ├─ packages/supabase/ ← @faunosteo/supabase │ ├─ src/{index,client,database.types,anatomical-sheets,assets}.ts ← typed client, generated types, publish gate, avatar flow (closed AssetFailure kinds) │ ├─ src/{records,workspaces,clients,species,animals,relations,field-limits}.ts ← V1-1 record access (closed RecordFailure kinds, keyset pages), D-R7 bounds -│ ├─ tests/e2e/avatar-flow.e2e.ts + vitest.e2e.config.ts ← real-stack end-to-end (`pnpm e2e:avatar`), skipped without the stack env +│ ├─ tests/e2e/avatar-flow.e2e.ts + vitest.e2e.config.ts ← real-stack end-to-end (`pnpm db:sandbox avatar-e2e`; `pnpm e2e:avatar` on DEV), skipped without the stack env and its database container │ └─ tests/{client,anatomical-sheets}.test.ts, tests/db/ ← PGlite harness, shim, pgTAP runner, schema↔types test ├─ supabase/ │ ├─ config.toml, seed.sql ← CLI config (public schema only), local dev accounts @@ -489,7 +489,7 @@ release 0.2.0 unaffected. - After PDF roundtrip tests fail: check the attachment bytes (the N-1 scanner reads them in the envelope suites), then the layered reader (report first, envelope metadata, snapshot/branding/labels), then the core's inflation (`pdf/read/inflate.rs`). Goldens under `apps/app/tests/fixtures/goldens/step6` and the R4 digests under `goldens/r4` are never regenerated without a user decision. - Node 24 / pnpm 10.13.1: `corepack enable` or `corepack pnpm`; never a global install. - Rust: native on the Ubuntu development machine (user-level rustup, never a global package, never on the VPS); `pnpm rust:check` and `pnpm wasm:build` run natively, the `:docker` variants reproduce them in the builder image. Before every Rust commit: `pnpm rust:check` green, `pnpm worker:test` green when `crates/worker` changed (Docker only: the sandbox tests and the seccomp derivation never run natively), the ts-rs types under `packages/core-wasm/types` committed, `Cargo.lock` committed. Before a merge: `pnpm check:all`. No CI runs on the forge: a workflow file is never cited as a passed check. The core takes ids, time and bytes as inputs (clippy `disallowed_methods`); the worker child is the only process that decodes untrusted bytes. -- Storage or worker change: `pnpm e2e:avatar` (worker container against the local stack, `packages/supabase/tests/e2e`) in addition to `pnpm worker:test`; `docker compose --profile worker` serves the worker from `.env.worker` (never the app `.env`). +- Storage or worker change: `pnpm db:sandbox avatar-e2e` (the avatar suite `packages/supabase/tests/e2e` and the real worker on the throwaway stack) in addition to `pnpm worker:test`; `pnpm e2e:avatar` starts a worker against the DEV stack and runs only on the owner's go (owner decision 2026-11-01); `docker compose --profile worker` serves the worker from `.env.worker` (never the app `.env`). - SQL change: `pnpm --filter @faunosteo/supabase test:run` (PGlite) before commit; `pnpm db:reset && pnpm db:test` on the real stack when Docker is available; regenerate types (`pnpm db:types`). diff --git a/README.md b/README.md index b82a902b..23d07727 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,9 @@ pinned toolchain from `rust-toolchain.toml` on first use; see `docs/development/RUST.md` §1 for the three `cargo install` / `rustup target add` lines and the Docker alternative. The files worker (profile photos, Step 8) runs as a container beside the local Supabase stack: -`pnpm worker:image`, then `pnpm e2e:avatar` for the real-stack end-to-end -run, or `docker compose --profile worker up worker` with a `.env.worker` +`pnpm worker:image`, then `pnpm db:sandbox avatar-e2e` for the real-stack +end-to-end run on the throwaway stack (`pnpm e2e:avatar` runs it against the +local DEV stack), or `docker compose --profile worker up worker` with a `.env.worker` (`docs/development/STORAGE.md`). Docker Compose (matches production topology): diff --git a/docs/development/BROWSER_TESTS.md b/docs/development/BROWSER_TESTS.md index b2297129..7ad8429d 100644 --- a/docs/development/BROWSER_TESTS.md +++ b/docs/development/BROWSER_TESTS.md @@ -102,7 +102,7 @@ Chromium (Playwright prints the install command), a stale core module `pnpm check:all` runs `pnpm e2e:ui` when the pinned Chromium is installed; otherwise it prints `SKIPPED` with the reason and the install command, and goes on. `pnpm e2e:shell` needs the sandbox and stays a separate run, like -`pnpm e2e:avatar`. The last lines of a green run state each browser suite: +`pnpm db:sandbox avatar-e2e`. The last lines of a green run state each browser suite: `PASSED`, `SKIPPED` (environment missing, with the reason) or `NOT RUN` (`e2e:shell`). A suite that runs and fails stops `check:all` with `== FAILED: ` and a non-zero exit, never a warning. Both suites keep diff --git a/docs/development/SUPABASE.md b/docs/development/SUPABASE.md index e290451d..7a8538a1 100644 --- a/docs/development/SUPABASE.md +++ b/docs/development/SUPABASE.md @@ -167,7 +167,8 @@ User files land in private buckets and are processed by the Rust worker ```bash pnpm worker:image # faunosteo_worker:dev -pnpm e2e:avatar # starts the container on supabase_network_faunosteo, runs tests/e2e, stops it +pnpm e2e:avatar # starts the container on supabase_network_faunosteo, runs tests/e2e, stops it (DEV: the owner's call) +pnpm db:sandbox avatar-e2e # the same suite and worker image on the throwaway stack (§5.1), the check of a storage or worker change docker compose --profile worker up worker # the same container from docker-compose.yml, reading .env.worker ``` @@ -801,6 +802,7 @@ Docker. | `pnpm db:sandbox test` | `packages/supabase/tests/sandbox/*.sandbox.ts`: real GoTrue, PostgREST and Storage sessions, concurrent SQL sessions (two-session lock order, V1-2.1), timing at the caps, the PGlite shim against the image; re-runnable on one stack | | `pnpm db:sandbox e2e-shell` (= `pnpm e2e:shell`) | the authenticated `/app` shell in Chromium (`apps/app/e2e/shell`, `docs/development/BROWSER_TESTS.md`): a loopback dev server whose Supabase calls are replayed on the sandbox, fictitious accounts per test | | `pnpm db:sandbox verify-e2e` | V1-2.3: a gateway (`nginx:1.31-alpine`, sandbox network only, no host port) and the real verification worker `faunosteo_worker:dev run --jobs sheet-verify` against the sandbox, then `packages/supabase/tests/sandbox/sheet-verification.sandbox.ts`; both containers removed at the end, the service key passed through a 600 file deleted at start | +| `pnpm db:sandbox avatar-e2e` | V1-2.3: the same gateway, published on `127.0.0.1:57800` only and routing `/auth/v1/` too, the real worker `faunosteo_worker:dev run` (default mode: avatars and sweep, 5 s sweep, no quarantine grace, 512 MiB as the compose `worker`) against the sandbox, then `packages/supabase/tests/e2e/avatar-flow.e2e.ts` through that port; the suite's psql calls go to the sandbox database container, which it requires by name; its two accounts are made per run with random passwords and deleted after; both containers removed at the end | | `pnpm db:sandbox sql` / `down` | psql as postgres on stdin / remove the stack | Every CLI call passes `--db-url` with `sslmode=disable` (the sandbox has no diff --git a/packages/supabase/tests/e2e/avatar-flow.e2e.ts b/packages/supabase/tests/e2e/avatar-flow.e2e.ts index 3a33effc..fedd357f 100644 --- a/packages/supabase/tests/e2e/avatar-flow.e2e.ts +++ b/packages/supabase/tests/e2e/avatar-flow.e2e.ts @@ -1,14 +1,17 @@ /** - * Avatar flow against the real local stack and the worker container + * Avatar flow against a real stack and the worker container * (docs/development/STORAGE.md §4, §3; STEP8_FILES_ANALYSIS IV.2 "integration" - * and "real-stack e2e"). Run through `pnpm e2e:avatar` (scripts/avatar-e2e.sh), - * which starts the worker with a 5 s sweep and a 0 s quarantine grace. + * and "real-stack e2e"). Run through `pnpm db:sandbox avatar-e2e` (the throwaway + * stack) or `pnpm e2e:avatar` (scripts/avatar-e2e.sh, the local DEV stack), both + * starting the worker with a 5 s sweep and a 0 s quarantine grace. * * Environment: SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY * (the service key is used for inspection only: listing objects and shifting - * the purge clock through psql in the database container). + * the purge clock through psql) and AVATAR_E2E_DB_CONTAINER, the database + * container of that same stack, named by the caller: no default, so a run never + * reaches another stack's database. Both accounts are made per run. */ -import { createHash } from 'node:crypto' +import { createHash, randomBytes } from 'node:crypto' import { execFileSync } from 'node:child_process' import { deflateSync } from 'node:zlib' import { createClient, type SupabaseClient } from '@supabase/supabase-js' @@ -26,9 +29,9 @@ import { const URL_ = process.env.SUPABASE_URL const ANON = process.env.SUPABASE_ANON_KEY const SERVICE = process.env.SUPABASE_SERVICE_ROLE_KEY -const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER ?? 'supabase_db_faunosteo' -const PASSWORD = 'LocalDev-only-password-1' -const BOB = 'bob@faunosteo.local' +const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER +/** Per run, kept in memory only: both accounts are deleted after the suite. */ +const PASSWORD = randomBytes(18).toString('base64url') const WORKER_WAIT_MS = 60_000 type Client = SupabaseClient @@ -166,19 +169,24 @@ async function objectExists(service: Client, bucket: string, key: string): Promi function psql(sql: string): string { return execFileSync('docker', [ - 'exec', DB_CONTAINER, 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql, + 'exec', DB_CONTAINER ?? '', 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql, ]).toString().trim() } -/** Moves every due date of the purge queue to now (the grace is 48 h / 1 h in production). */ -function expeditePurges(): void { - psql('update private.object_purge_queue set not_before = now() where not_before > now()') +function accountPattern(userId: string): string { + if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid') + return `'%/${userId}/%'` +} + +/** Moves an account's due dates of the purge queue to now (the grace is 48 h / 1 h in production). */ +function expeditePurges(userId: string): void { + psql(`update private.object_purge_queue set not_before = now() + where not_before > now() and object_name like ${accountPattern(userId)}`) } /** Purge entries still queued for an account (served entries are acked, hence deleted). */ function queuedPurges(userId: string): number { - if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid') - return Number(psql(`select count(*) from private.object_purge_queue where object_name like '%/${userId}/%'`)) + return Number(psql(`select count(*) from private.object_purge_queue where object_name like ${accountPattern(userId)}`)) } async function downloadFails(client: Client, key: string): Promise { @@ -192,31 +200,41 @@ function sha256(bytes: Uint8Array): string { // --- suite ------------------------------------------------------------------ -const configured = Boolean(URL_ && ANON && SERVICE) +const configured = Boolean(URL_ && ANON && SERVICE && DB_CONTAINER) + +/** A confirmed throwaway account; resolves with its id. */ +async function newAccount(service: Client, tag: string, name: string): Promise<{ id: string; email: string }> { + const email = `e2e-${tag}-${Date.now()}@faunosteo.local` + const created = await service.auth.admin.createUser({ + email, + password: PASSWORD, + email_confirm: true, + user_metadata: { display_name: name, locale: 'fr' }, + }) + if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`) + return { id: created.data.user.id, email } +} describe.skipIf(!configured)('avatar flow on the real stack', () => { const service = serviceClient() let alice: Client let aliceId: string let bob: Client + let bobId: string beforeAll(async () => { - // A fresh account per run keeps the seeded accounts clean and the per-hour rate out of reach. - const email = `e2e-avatar-${Date.now()}@faunosteo.local` - const created = await service.auth.admin.createUser({ - email, - password: PASSWORD, - email_confirm: true, - user_metadata: { display_name: 'E2E Avatar', locale: 'fr' }, - }) - if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`) - aliceId = created.data.user.id - alice = await signedIn(email) - bob = await signedIn(BOB) + // Fresh accounts per run keep the seeded accounts out of the suite and the per-hour rate out of reach. + const a = await newAccount(service, 'avatar', 'E2E Avatar') + aliceId = a.id + alice = await signedIn(a.email) + const b = await newAccount(service, 'other', 'E2E Other') + bobId = b.id + bob = await signedIn(b.email) }) afterAll(async () => { if (aliceId) await service.auth.admin.deleteUser(aliceId) + if (bobId) await service.auth.admin.deleteUser(bobId) }) it('cleans a valid upload, and only the owner can download the result', async () => { @@ -260,7 +278,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => { expect(state.clean?.id).toBe(row.id) expect(await downloadFails(alice, previous!.object_key)).toBe(true) expect(await objectExists(service, 'avatars', previous!.object_key)).toBe(true) - expeditePurges() + expeditePurges(aliceId) await waitFor( async () => ((await objectExists(service, 'avatars', previous!.object_key)) ? null : true), 30_000, @@ -316,7 +334,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => { expect(await downloadFails(alice, row.object_key)).toBe(true) const state = await fetchOwnAvatarState(alice, aliceId) expect(state.clean).toBeNull() - expeditePurges() + expeditePurges(aliceId) await waitFor( async () => { const listed = await service.storage.from('avatars').list(`profiles/${aliceId}`, { limit: 100 }) diff --git a/packages/supabase/tests/sandbox/stack.ts b/packages/supabase/tests/sandbox/stack.ts index bb3db309..ce5948d6 100644 --- a/packages/supabase/tests/sandbox/stack.ts +++ b/packages/supabase/tests/sandbox/stack.ts @@ -6,7 +6,7 @@ * never through a command argument. */ import { execFile } from 'node:child_process' -import { randomUUID } from 'node:crypto' +import { randomBytes, randomUUID } from 'node:crypto' const need = (name: string): string => { const value = process.env[name] @@ -25,7 +25,8 @@ export const stack = { serviceKey: need('SANDBOX_SERVICE_KEY'), } const PG_PASSWORD = need('SANDBOX_PG_PASSWORD') -export const PASSWORD = 'Sandbox-only-pw-1' +/** The password of the accounts a run creates: random per run, kept in memory only. */ +export const PASSWORD = randomBytes(18).toString('base64url') /** One psql session as postgres; resolves with stdout (tuples only) or rejects with stderr. */ export function sql(script: string): Promise { diff --git a/scripts/avatar-e2e.sh b/scripts/avatar-e2e.sh index 0a1a3b9d..47a2e1c5 100755 --- a/scripts/avatar-e2e.sh +++ b/scripts/avatar-e2e.sh @@ -55,4 +55,5 @@ docker run -d --name "$name" --read-only --cap-drop ALL \ echo "== e2e suite" SUPABASE_URL="$api_url" SUPABASE_ANON_KEY="$anon_key" SUPABASE_SERVICE_ROLE_KEY="$service_key" \ + AVATAR_E2E_DB_CONTAINER="${AVATAR_E2E_DB_CONTAINER:-supabase_db_faunosteo}" \ pnpm --filter @faunosteo/supabase test:e2e diff --git a/scripts/db-sandbox.sh b/scripts/db-sandbox.sh index 67389d24..b96997c4 100755 --- a/scripts/db-sandbox.sh +++ b/scripts/db-sandbox.sh @@ -17,6 +17,9 @@ # pnpm db:sandbox verify-e2e [args] the sheet verification worker against the sandbox: an # unpublished gateway, the worker container, then # tests/sandbox/sheet-verification.sandbox.ts (args go to vitest) +# pnpm db:sandbox avatar-e2e [args] the avatar worker (default mode: avatars + sweep) against the +# sandbox: a gateway on 127.0.0.1:$PORT_GATEWAY, the worker +# container, then tests/e2e/avatar-flow.e2e.ts (args go to vitest) # pnpm db:sandbox sql psql as postgres on stdin # pnpm db:sandbox down # @@ -41,9 +44,10 @@ STORAGE=faunosteo_dbsandbox_storage MAIL=faunosteo_dbsandbox_mail GATEWAY=faunosteo_dbsandbox_gateway WORKER=faunosteo_dbsandbox_worker_verify +AVATAR_WORKER=faunosteo_dbsandbox_worker_avatar GATEWAY_IMAGE=nginx:1.31-alpine WORKER_IMAGE=faunosteo_worker:dev -PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025 +PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025 PORT_GATEWAY=57800 STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}" SECRETS="$STATE/secrets.sh" CLI=node_modules/.bin/supabase @@ -173,12 +177,16 @@ seed() { done } +# Nobody signs in to these two accounts: each password is random, used once and +# kept nowhere; the body reaches curl on stdin (printf is a builtin), never argv. +throwaway_password() { od -An -N18 -tx1 /dev/urandom | tr -d ' \n'; } gotrue_accounts() { - curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \ - -H @"$STATE/service-header" -H 'Content-Type: application/json' \ - -d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null - curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \ - -d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null + printf '{"email":"pre-confirmed@sandbox.local","password":"%s","email_confirm":true}' "$(throwaway_password)" \ + | curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \ + -H @"$STATE/service-header" -H 'Content-Type: application/json' --data-binary @- >/dev/null + printf '{"email":"pre-unconfirmed@sandbox.local","password":"%s"}' "$(throwaway_password)" \ + | curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \ + --data-binary @- >/dev/null } # A CLI workdir holding only the migrations before ; kept in the @@ -226,7 +234,7 @@ cmd_up() { } cmd_down() { - docker rm -f "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true + docker rm -f "$AVATAR_WORKER" "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true docker network rm "$NET" >/dev/null 2>&1 || true rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header" echo "== sandbox removed" @@ -241,63 +249,91 @@ with_sandbox_env() ( "$@" ) -# The Kong routes the worker calls (/rest/v1/, /storage/v1/), on the sandbox -# network only: no host port is published. +# The Kong routes of the CLI stack (/auth/v1/, /rest/v1/, /storage/v1/) on the +# sandbox network; host port only for avatar-e2e, on 127.0.0.1 like the services. GATEWAY_CONF='server { listen 8000; client_max_body_size 8m; proxy_http_version 1.1; + location /auth/v1/ { proxy_pass http://'"$AUTH"':9999/; } location /rest/v1/ { proxy_pass http://'"$REST"':3000/; } location /storage/v1/ { proxy_pass http://'"$STORAGE"':5000/; } location / { return 404; } }' +start_gateway() { # [docker run options] + docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true "$@" \ + --entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \ + "$GATEWAY_CONF" >/dev/null + wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/ +} + +# The service key reaches the container through a 600 file removed once it started; +# memory as the compose service of the same mode (docker-compose.yml). +start_worker() { # name worker-id memory extra-env-lines args... + local name="$1" id="$2" memory="$3" extra="$4"; shift 4 + worker_env="$(umask 077; mktemp "$STATE/worker.XXXXXX")" + printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=%s\nWORKER_POLL_SECONDS=1\n%s' \ + "$GATEWAY" "$SB_SERVICE_KEY" "$id" "$extra" > "$worker_env" + docker run -d --name "$name" --network "$NET" --env-file "$worker_env" \ + --read-only --cap-drop ALL --security-opt no-new-privileges:true \ + --memory "$memory" --cpus 1 --pids-limit 64 --user nonroot \ + "$WORKER_IMAGE" "$@" >/dev/null + rm -f "$worker_env"; worker_env= + for _ in $(seq 1 30); do + docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' && break + [ "$(docker inspect -f '{{.State.Running}}' "$name")" = true ] || die "worker exited before worker_start" + sleep 1 + done + docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start" + echo "== worker up ($WORKER_IMAGE, $*, via $GATEWAY)" +} + # The worker's JSON lines hold no secret by design (crates/worker log allowlist); # they are printed only after checking that neither key appears in them. -print_worker_log() { +print_worker_log() { # name local lines - lines="$(docker logs "$WORKER" 2>&1)" || return 0 + lines="$(docker logs "$1" 2>&1)" || return 0 case "$lines" in *"$SB_SERVICE_KEY"* | *"$SB_ANON_KEY"*) echo "== worker log withheld: it contains a key" ;; *) echo "== worker log"; printf '%s\n' "$lines" ;; esac } -verify_cleanup() { - [ -n "${verify_env:-}" ] && rm -f "$verify_env" - print_worker_log - docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true +worker_cleanup() { # name + [ -n "${worker_env:-}" ] && rm -f "$worker_env" + print_worker_log "$1" + docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true +} + +worker_preflight() { # name + load_secrets + docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)" + docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true } cmd_verify_e2e() { - load_secrets - docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)" - docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true - trap verify_cleanup EXIT - docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true \ - --entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \ - "$GATEWAY_CONF" >/dev/null - wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/ - # The service key reaches the container through a 600 file removed once it started. - verify_env="$(umask 077; mktemp "$STATE/worker-verify.XXXXXX")" - printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=sandbox-verify\nWORKER_POLL_SECONDS=1\n' \ - "$GATEWAY" "$SB_SERVICE_KEY" > "$verify_env" - docker run -d --name "$WORKER" --network "$NET" --env-file "$verify_env" \ - --read-only --cap-drop ALL --security-opt no-new-privileges:true \ - --memory 1280m --cpus 1 --pids-limit 64 --user nonroot \ - "$WORKER_IMAGE" run --jobs sheet-verify >/dev/null - rm -f "$verify_env"; verify_env= - for _ in $(seq 1 30); do - docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' && break - [ "$(docker inspect -f '{{.State.Running}}' "$WORKER")" = true ] || die "worker exited before worker_start" - sleep 1 - done - docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start" - echo "== worker up ($WORKER_IMAGE, run --jobs sheet-verify, via $GATEWAY)" + worker_preflight "$WORKER" + trap 'worker_cleanup "$WORKER"' EXIT + start_gateway + start_worker "$WORKER" sandbox-verify 1280m '' run --jobs sheet-verify SANDBOX_VERIFY_WORKER="$WORKER" with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase \ test:sandbox tests/sandbox/sheet-verification.sandbox.ts "$@" } +# The avatar suite drives supabase-js through the gateway's loopback port; its +# psql calls name the sandbox database container, which it requires. +cmd_avatar_e2e() { + worker_preflight "$AVATAR_WORKER" + trap 'worker_cleanup "$AVATAR_WORKER"' EXIT + start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000 + start_worker "$AVATAR_WORKER" sandbox-avatar 512m \ + $'WORKER_SWEEP_SECONDS=5\nWORKER_QUARANTINE_GRACE_SECONDS=0\n' run + SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \ + SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" AVATAR_E2E_DB_CONTAINER="$DB" \ + node_modules/.bin/pnpm --filter @faunosteo/supabase test:e2e "$@" +} + case "${1:-}" in up) shift; cmd_up "$@" ;; down) cmd_down ;; @@ -313,6 +349,7 @@ case "${1:-}" in test) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox "$@" ;; e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;; verify-e2e) shift; cmd_verify_e2e "$@" ;; + avatar-e2e) shift; cmd_avatar_e2e "$@" ;; sql) load_secrets; sb_psql ;; - *) sed -n '2,26p' "$0"; exit 1 ;; + *) sed -n '2,29p' "$0"; exit 1 ;; esac