{"draft":null,"from":175,"to":318,"files":[{"path":"client/test/caller.test.ts","added":104,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/caller.test.ts\n@@ -0,0 +1,104 @@\n+// Who is calling a seat: one header with the chat's session, the computer and the folder, read back exactly or not at all.\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { CALLER_HEADER, CALLER_MAX, callerHeader, callerPlace, parseCaller } from \"../../src/caller.ts\";\n+import { Api } from \"../src/api.ts\";\n+import { callerFor } from \"../src/command-env.ts\";\n+\n+const session = \"0da5c2b7-ab07-4249-b868-e8f09b6ec7b6\";\n+\n+test(\"the client sends a hash of the session and only the folder's last part\", () => {\n+\tconst header = callerFor(session, \"DESKTOP-1\", \"C:\\\\Users\\\\sam\\\\repos\\\\abso\\\\groups\")!;\n+\tconst caller = parseCaller(header)!;\n+\tassert.equal(caller.folder, \"groups\");\n+\tassert.match(caller.session!, /^[0-9a-f]{16}$/);\n+\tassert.notEqual(caller.session, session);\n+\tassert.doesNotMatch(Buffer.from(header, \"base64url\").toString(), /sam|0da5c2b7/);\n+\t// The same chat hashes the same way, so the server still tells one chat from another.\n+\tassert.equal(parseCaller(callerFor(session, \"DESKTOP-1\", \"/x/groups\"))!.session, caller.session);\n+\tassert.equal(parseCaller(callerFor(undefined, \"box\", \"/work/über\"))!.session, undefined);\n+\tassert.equal(callerPlace(caller), \"groups on DESKTOP-1\");\n+});\n+\n+test(\"a caller goes out and comes back as it was\", () => {\n+\tconst header = callerHeader({ session, machine: \"DESKTOP-1\", folder: \"C:\\\\Users\\\\sam\\\\repos\\\\abso\\\\groups\" })!;\n+\tassert.match(header, /^[A-Za-z0-9_-]+$/);\n+\tassert.deepEqual(parseCaller(header), {\n+\t\tsession,\n+\t\tmachine: \"DESKTOP-1\",\n+\t\tfolder: \"C:\\\\Users\\\\sam\\\\repos\\\\abso\\\\groups\",\n+\t});\n+\tassert.equal(callerPlace(parseCaller(header)!), \"groups on DESKTOP-1\");\n+\t// Outside a chat there's no session, and that's still a caller.\n+\tassert.deepEqual(parseCaller(callerHeader({ machine: \"box\", folder: \"/work/über\" })), {\n+\t\tmachine: \"box\",\n+\t\tfolder: \"/work/über\",\n+\t});\n+});\n+\n+test(\"a session that isn't an id is left out, and a long folder keeps its end\", () => {\n+\tassert.deepEqual(parseCaller(callerHeader({ session: \"not a session!\", machine: \"m\", folder: \"/a\" })), {\n+\t\tmachine: \"m\",\n+\t\tfolder: \"/a\",\n+\t});\n+\tconst deep = `/${\"very-long-folder-name/\".repeat(40)}groups`;\n+\tconst header = callerHeader({ session, machine: \"m\", folder: deep })!;\n+\tassert.ok(header.length <= CALLER_MAX);\n+\tassert.ok(parseCaller(header)!.folder.endsWith(\"/groups\"));\n+});\n+\n+test(\"a header that isn't a caller is refused, never guessed at\", () => {\n+\tconst enc = (v: unknown) => Buffer.from(JSON.stringify(v)).toString(\"base64url\");\n+\tfor (const bad of [\n+\t\tundefined,\n+\t\t\"\",\n+\t\t\"not base64!\",\n+\t\tenc(\"text\"),\n+\t\tenc([1]),\n+\t\tenc({ machine: \"m\" }),\n+\t\tenc({ machine: \"has space\", folder: \"/a\" }),\n+\t\tenc({ machine: \"m\", folder: \"/a\", session: \"bad id!\" }),\n+\t\tenc({ machine: \"m\", folder: \"/a\", token: \"x\" }),\n+\t\tenc({ machine: \"m\", folder: \"/a\\nb\" }),\n+\t\t\"A\".repeat(CALLER_MAX + 1),\n+\t])\n+\t\tassert.equal(parseCaller(bad), null, String(bad));\n+});\n+\n+test(\"the Api sends the caller with its calls, except from a visitor folder\", () => {\n+\tconst config = {\n+\t\tserver: \"https://abso.test\",\n+\t\tproject: \"p\",\n+\t\tagent: \"ann\",\n+\t\ttoken: \"abso_sk_x\",\n+\t} as ConstructorParameters<typeof Api>[0];\n+\tconst api = new Api(config);\n+\tassert.equal(api.headers()[CALLER_HEADER], undefined);\n+\tapi.caller = callerHeader({ session, machine: \"m\", folder: \"/a\" });\n+\tassert.equal(api.headers()[CALLER_HEADER], api.caller);\n+\tconst visitor = new Api({ ...config, visitor: true } as ConstructorParameters<typeof Api>[0]);\n+\tvisitor.caller = api.caller;\n+\tassert.equal(visitor.headers()[CALLER_HEADER], undefined);\n+});\n+\n+test(\"a seat notice on any answer is printed once with the news\", async () => {\n+\tconst { takeNews } = await import(\"../src/command-env.ts\");\n+\ttakeNews();\n+\tconst real = globalThis.fetch;\n+\tconst line = \"ann was used from another chat (groups on DESKTOP-2) in the last 30 minutes\";\n+\tglobalThis.fetch = (async () => Response.json({ ok: true, seatNotice: line })) as typeof fetch;\n+\ttry {\n+\t\tconst api = new Api({\n+\t\t\tserver: \"https://abso.test\",\n+\t\t\tproject: \"p\",\n+\t\t\tagent: \"ann\",\n+\t\t\ttoken: \"abso_sk_x\",\n+\t\t} as ConstructorParameters<typeof Api>[0]);\n+\t\tawait api.json(\"GET\", \"/status\");\n+\t\tawait api.json(\"GET\", \"/status\");\n+\t\tassert.deepEqual(takeNews(), [line]);\n+\t\tassert.deepEqual(takeNews(), []);\n+\t} finally {\n+\t\tglobalThis.fetch = real;\n+\t}\n+});"}],"unmatched":[]}