Files
heidi/lib/domain/usage/usage.test.ts
CatoandClaude Opus 5.5 3879d58823 Owner door, team QR poster, the visitor's path counted, counts from the pack (#397)
* Owner: one page that always says the next step; counts come from the pack

/owner never answers 404: it says whether you are signed out, signed in
with another address, unconfirmed, or the owner, and what to do. The check
asks whether the address is the owner's before asking if it is confirmed.
The investor room's question and review counts and the warm-up's promised
length are tied to the content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r

* Team QR poster, the visitor's path counted, care page follows the pack

The team invite shows a QR code and opens a printable poster for the staff
room. Anonymous totals now count the visitor's path from home page to
answer, and from organisations page to pilot request; /owner lists them.
The care page lists every care scene the pack has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-28 16:15:01 +01:00

78 lines
3.5 KiB
TypeScript

import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { PGlite } from "@electric-sql/pglite";
import type { Run } from "../../db/run.ts";
import { countUsage, usageTotals, MIN_ANSWERS } from "./count.ts";
import { MAX_KEY, usageKey, type Known } from "./keys.ts";
const KNOWN: Known = { scenes: new Map([["buenzli", 2]]), items: new Set(["verify:buenzli:2", "meaning:nöd"]), sectors: new Set(["care"]) };
test("only a key from the closed vocabulary is counted", () => {
for (const ok of ["go:home:view", "go:home:answer", "go:org:care", "go:org:pilot", "run:start:buenzli:0", "run:end:buenzli:1", "sit:start:all", "sit:end:scene", "step:verify:buenzli:2:wrong", "step:meaning:nöd:right", "own:open", "own:right", "effort:write"]) {
assert.equal(usageKey(ok, KNOWN), ok, ok);
}
for (const bad of [
"run:start:buenzli:2", // no such part
"run:start:elsewhere:0", // no such scene
"run:start:buenzli:00",
"sit:start:buenzli", // a scope word, never a scene
"step:verify:buenzli:9:right", // no such question
"step:verify:buenzli:2:maybe",
"step:someone@example.org:right",
"anything else",
"go:org:bakeries", // no such sector
"go:home:someone@example.org",
"go:",
"",
"x".repeat(MAX_KEY + 1),
42,
null,
{ key: "own:open" },
]) {
assert.equal(usageKey(bad, KNOWN), null, String(bad));
}
});
describe("the totals", () => {
let db: PGlite;
const run: Run = async (text, params) => (await db.query<Record<string, unknown>>(text, [...params])).rows;
const DIR = new URL("../../../drizzle/", import.meta.url);
const NOW = new Date("2026-11-28T12:00:00.000Z");
before(async () => {
db = new PGlite();
for (const file of readdirSync(DIR).filter((f) => /^\d{4}_.*\.sql$/.test(f)).sort()) {
for (const statement of readFileSync(new URL(file, DIR), "utf8").split("--> statement-breakpoint")) {
if (statement.trim()) await db.exec(statement);
}
}
});
after(async () => db.close());
test("a count is a day, a key and a number, and adds up", async () => {
for (let i = 0; i < 3; i++) await countUsage(run, "run:start:buenzli:0", NOW);
await countUsage(run, "run:end:buenzli:0", NOW);
for (let i = 0; i < MIN_ANSWERS; i++) await countUsage(run, "step:verify:buenzli:2:wrong", NOW);
await countUsage(run, "step:verify:buenzli:2:right", NOW);
await countUsage(run, "step:meaning:nöd:right", NOW);
await countUsage(run, "own:open", NOW);
await countUsage(run, "own:right", NOW);
await countUsage(run, "go:home:view", NOW);
await countUsage(run, "go:home:view", NOW);
await countUsage(run, "go:org:care", NOW);
// Older than the window: not in the totals.
await countUsage(run, "run:start:buenzli:0", new Date("2026-09-01T12:00:00Z"));
const rows = await run("select * from usage_counts order by key", []);
assert.deepEqual(Object.keys(rows[0]!).sort(), ["count", "day", "key"], "nothing but a day, a key and a count is stored");
const totals = await usageTotals(run, NOW);
assert.deepEqual(totals.runs, [{ scene: "buenzli", started: 3, finished: 1 }]);
assert.deepEqual(totals.ownSentence, { opened: 1, right: 1, wrong: 0 });
assert.deepEqual(totals.path, { "home:view": 2, "org:care": 1 });
// Only a question answered often enough is ranked: one answer says nothing.
assert.deepEqual(totals.hardest, [{ id: "verify:buenzli:2", right: 1, wrong: MIN_ANSWERS, skipped: 0 }]);
});
});