import test from "node:test"; import assert from "node:assert/strict"; import crypto from "node:crypto"; import path from "node:path"; import { fileURLToPath } from "node:url"; import request from "supertest"; import { newDb } from "pg-mem"; import { createApp } from "../server/app.js"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const password = "this is a long password"; async function setup() { const mem = newDb(); mem.public.registerFunction({ name: "gen_random_uuid", returns: "uuid", implementation: () => crypto.randomUUID(), impure: true, }); const pg = mem.adapters.createPg(); const db = new pg.Pool(); await db.query( `CREATE TABLE users(id uuid PRIMARY KEY DEFAULT gen_random_uuid(),email text UNIQUE NOT NULL,password_hash text NOT NULL,role text NOT NULL DEFAULT 'user',created_at timestamptz DEFAULT now()); CREATE TABLE sessions(token_hash text PRIMARY KEY,user_id uuid NOT NULL REFERENCES users(id),csrf_hash text NOT NULL,expires_at timestamptz NOT NULL,created_at timestamptz DEFAULT now()); CREATE TABLE roast_plans(id uuid PRIMARY KEY DEFAULT gen_random_uuid(),user_id uuid NOT NULL REFERENCES users(id),plan jsonb NOT NULL,created_at timestamptz DEFAULT now(),updated_at timestamptz DEFAULT now()); CREATE TABLE app_settings(key text PRIMARY KEY,value text NOT NULL); INSERT INTO app_settings VALUES('signup_enabled','true')`, ); const app = createApp({ db, root, env: { NODE_ENV: "test", BOOTSTRAP_SETUP_TOKEN: "a-secure-bootstrap-token", }, }); return { db, app, agent: request.agent(app) }; } async function signup(agent, email) { const response = await agent .post("/api/auth/signup") .send({ email, password }); return { response, csrf: response.body.csrfToken }; } test("strict CSP/static modules, no-store data, auth lifecycle, and ownership share one database", async () => { const { db, app, agent: first } = await setup(); const second = request.agent(app); const anonymous = request.agent(app); const landing = await anonymous.get("/"); assert.equal(landing.status, 200); assert.match( landing.headers["content-security-policy"], /default-src 'self'/, ); assert.doesNotMatch( landing.headers["content-security-policy"], /(?:default-src|script-src)[^;]*unsafe-inline/, ); assert.match( landing.text, /