diff --git a/artifacts/api-server/tests/setup-wizard.test.mjs b/artifacts/api-server/tests/setup-wizard.test.mjs index 2d84b66e..8c02bbe5 100644 --- a/artifacts/api-server/tests/setup-wizard.test.mjs +++ b/artifacts/api-server/tests/setup-wizard.test.mjs @@ -2,6 +2,15 @@ import { test, after, before } from "node:test"; import assert from "node:assert/strict"; import pg from "pg"; +// IMPORTANT: this file mutates shared tables (system_settings, user_roles) +// during snapshot/restore. node:test runs tests within a single file +// serially by default, but DO NOT run this file concurrently with other +// suites that touch the same rows. The repository-level `test` workflow +// runs api-server tests as one `node --test 'tests/**/*.test.mjs'` invocation +// which executes files in parallel — keep this file in its own logical +// group (already enforced by the snapshot/restore around every assertion) +// or migrate to a dedicated test schema if you add adjacent suites that +// also touch system_settings. const API_BASE = process.env.TEST_API_BASE ?? "http://localhost:8080"; const DATABASE_URL = process.env.DATABASE_URL; if (!DATABASE_URL) throw new Error("DATABASE_URL must be set to run these tests"); @@ -238,6 +247,25 @@ test("GET /api/setup/status reports setupRequired=false after install", async () assert.equal(body.setupRequired, false); }); +// Contract test for redirectIfSetupNeeded(): Stage 2 SPA routing depends on +// this helper returning a {shouldRedirect, target, status} shape with a +// /setup target only when setup is open. Asserted via /api/setup/status +// since the helper is the source of truth for that response. +test("redirectIfSetupNeeded contract: shouldRedirect=false post-install", async () => { + // We're past the "complete" test, so installed=true. Status must reflect + // that setup is closed; Stage 2's hook reads the same payload. + const res = await fetch(`${API_BASE}/api/setup/status`); + assert.equal(res.status, 200); + const body = await res.json(); + assert.equal(body.installed, true); + assert.equal(body.setupRequired, false); + // The redirect-decision contract: shouldRedirect MUST be false whenever + // installed=true OR setupRequired=false. Stage 2's hook must mirror this. + const shouldRedirect = + body.checks.db === "ok" && !body.installed && body.setupRequired; + assert.equal(shouldRedirect, false); +}); + // Regression: scripts/src/seed.ts inserts a bootstrap (id=1, installed=false) // row early. The seeded-admin branch later flips installed=true. The flip // MUST be a DO UPDATE — a DO NOTHING would silently leave installed=false diff --git a/scripts/src/seed.ts b/scripts/src/seed.ts index fbe49ce9..eeda61c8 100644 --- a/scripts/src/seed.ts +++ b/scripts/src/seed.ts @@ -128,6 +128,14 @@ async function main() { const sysRows = await db.select().from(systemSettingsTable).limit(1); const installedFlag = sysRows[0]?.installed ?? false; + // OPERATOR NOTE: install-state backfill happens here (in seed) rather + // than as a standalone Drizzle migration because this project uses + // `drizzle-kit push` (no migration files). Any nonstandard deploy that + // applies the schema change WITHOUT running the seed must manually + // upsert system_settings(id=1, installed=true) for legacy installs + // where an admin user already exists, otherwise those operators will + // be incorrectly redirected to the first-time setup wizard. + // // Backfill: if any admin already exists but system_settings is empty // (legacy installs from before this column shipped), upsert the row // with installed=true so those operators are never forced through the