All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 29s
- Check for a new service worker on every return to the foreground and every 30 minutes; apply it in the background or just after launch, never with unsynced edits or an open sheet. The Update prompt remains. - Register the worker at extension-less /sw (no-cache), so proxies that cache *.js regardless of headers cannot hide updates - Return 404 for missing files instead of the HTML shell - Tests for the static routes; smoke test covers /sw; document the Nginx Proxy Manager "Cache Assets" pitfall Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
216 lines
7.3 KiB
JavaScript
216 lines
7.3 KiB
JavaScript
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,
|
|
);
|
|
});
|
|
test("static app: uncacheable worker route, shell fallback for routes, 404 for missing files", async (t) => {
|
|
const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = await import("node:fs");
|
|
const { tmpdir } = await import("node:os");
|
|
const { join } = await import("node:path");
|
|
const dist = mkdtempSync(join(tmpdir(), "calio-dist-"));
|
|
mkdirSync(join(dist, "assets"));
|
|
writeFileSync(join(dist, "index.html"), "<!doctype html><title>Calio</title>");
|
|
writeFileSync(join(dist, "sw.js"), "const CACHE='calio-test';");
|
|
writeFileSync(join(dist, "assets", "index-abc.js"), "console.log(1)");
|
|
const store = openDatabase(":memory:");
|
|
const app = createApp({
|
|
store,
|
|
username: "test",
|
|
passwordHash: hashPassword(password),
|
|
origin,
|
|
secureCookies: false,
|
|
dist,
|
|
});
|
|
const server = app.listen(0, "127.0.0.1");
|
|
await new Promise((resolve) => server.once("listening", resolve));
|
|
t.after(() => {
|
|
server.close();
|
|
store.close();
|
|
rmSync(dist, { recursive: true, force: true });
|
|
});
|
|
const base = `http://127.0.0.1:${server.address().port}`;
|
|
const worker = await fetch(base + "/sw");
|
|
assert.equal(worker.status, 200);
|
|
assert.match(worker.headers.get("content-type"), /javascript/);
|
|
assert.equal(worker.headers.get("cache-control"), "no-cache");
|
|
assert.equal(await worker.text(), "const CACHE='calio-test';");
|
|
assert.equal((await fetch(base + "/sw.js")).status, 200);
|
|
assert.equal((await fetch(base + "/assets/index-abc.js")).status, 200);
|
|
const missing = await fetch(base + "/assets/index-old.js");
|
|
assert.equal(missing.status, 404);
|
|
assert.doesNotMatch(missing.headers.get("content-type"), /html/);
|
|
const route = await fetch(base + "/weight");
|
|
assert.equal(route.status, 200);
|
|
assert.match(await route.text(), /<title>Calio/);
|
|
});
|