import test from "node:test"; import assert from "node:assert/strict"; import { createApp } from "../server/app.js"; import { openDatabase } from "../server/db.js"; import { checkPassword, hashPassword } from "../server/auth.js"; import { emptyNutrients } from "../shared/domain.js"; const password = "test-only-password-123"; const origin = "http://localhost:3000"; async function fixture(t, secureCookies = false) { const store = openDatabase(":memory:"); const app = createApp({ store, username: "test", passwordHash: hashPassword(password), origin, secureCookies, dist: ".local/nonexistent", }); const server = app.listen(0, "127.0.0.1"); await new Promise((resolve) => server.once("listening", resolve)); const base = `http://127.0.0.1:${server.address().port}/api`; t.after( () => new Promise((resolve) => server.close(() => { store.close(); resolve(); }), ), ); const request = (path, body, cookie = "", customHeaders = {}) => fetch(base + path, { method: body === undefined ? "GET" : "POST", headers: { "Content-Type": "application/json", Origin: origin, "X-Calio-Request": "1", Cookie: cookie, ...customHeaders, }, body: body === undefined ? undefined : JSON.stringify(body), }); const signIn = async () => { const response = await request("/login", { username: "test", password }); assert.equal(response.status, 200); return response.headers.get("set-cookie").split(";")[0]; }; return { store, request, signIn }; } test("passwords use salted scrypt hashes and reject incorrect values", () => { const hash = hashPassword(password); assert.notEqual(hashPassword(password), hash); assert.ok(checkPassword(password, hash)); assert.equal(checkPassword("wrong", hash), false); assert.equal(checkPassword(password, "garbage"), false); }); test("journal endpoints require authentication and never allow cross-origin writes", async (t) => { const { request, signIn } = await fixture(t); assert.equal((await request("/records")).status, 401); assert.equal((await request("/sync", { records: [] })).status, 401); const cookie = await signIn(); assert.equal( ( await request("/sync", { records: [] }, cookie, { Origin: "https://evil.example", }) ).status, 403, ); assert.equal( (await request("/sync", { records: [] }, cookie, { "X-Calio-Request": "" })) .status, 403, ); assert.equal((await request("/records", undefined, cookie)).status, 200); assert.equal( (await request("/records", undefined, cookie)).headers.get("cache-control"), "no-store", ); }); test("invalid credentials, schema errors, invalid clocks and oversized bodies are rejected", async (t) => { const { request, signIn } = await fixture(t); assert.equal( (await request("/login", { username: "test", password: "wrong" })).status, 401, ); const cookie = await signIn(); assert.equal( (await request("/sync", { records: [{ id: "bad" }] }, cookie)).status, 400, ); const record = { id: "one", type: "entry", data: { date: "2026-10-20", meal: "Lunch", name: "Test", serving: "1 serving", quantity: 1, nutrients: emptyNutrients(), }, updatedAt: new Date(Date.now() + 600000).toISOString(), deleted: false, }; assert.equal( (await request("/sync", { records: [record] }, cookie)).status, 400, ); assert.equal( ( await request( "/sync", { records: [], padding: "a".repeat(2100000) }, cookie, ) ).status, 413, ); }); test("login issues secure HttpOnly SameSite cookies and logout revokes sessions", async (t) => { const { request } = await fixture(t, true); const response = await request("/login", { username: "test", password }); const header = response.headers.get("set-cookie"); assert.match(header, /__Host-calio=/); assert.match(header, /HttpOnly/); assert.match(header, /Secure/); assert.match(header, /SameSite=Strict/); const cookie = header.split(";")[0]; assert.equal((await request("/session", undefined, cookie)).status, 200); assert.equal((await request("/logout", {}, cookie)).status, 200); assert.equal((await request("/session", undefined, cookie)).status, 401); }); test("sync is idempotent and returns winning edits to a stale device", async (t) => { const { request, signIn } = await fixture(t); const cookie = await signIn(); const record = { id: "one", type: "food", data: { name: "Oats", serving: "40 g", nutrients: { ...emptyNutrients(), calories: 150 }, }, updatedAt: "2026-01-01T10:00:00.000Z", deleted: false, }; assert.equal( (await request("/sync", { records: [record] }, cookie)).status, 200, ); const changed = { ...record, updatedAt: "2026-01-01T11:00:00.000Z", data: { ...record.data, name: "Updated oats" }, }; await request("/sync", { records: [changed] }, cookie); const result = await ( await request("/sync", { records: [record, record] }, cookie) ).json(); assert.equal(result.records.length, 1); assert.deepEqual(result.records[0], changed); }); test("login is rate limited", async (t) => { const { request } = await fixture(t); for (let i = 0; i < 10; i++) assert.equal( (await request("/login", { username: "bad", password: "wrong" })).status, 401, ); assert.equal( (await request("/login", { username: "test", password })).status, 429, ); });