// The notes route's writer (release 23 F3): no Authorization is the // operator, umtool's WORKER_TOKEN as a bearer is an agent, any other header is // a 401 with nothing written, and `by` in the body is never believed. The // POST's logic (postNoteOp) over a temp notes.json, and the token read. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { agentToken, dotenvValue, noteWriter, postNoteOp } from "./agentAuth.mjs"; const TOKEN = "notes-test-token"; const SUBJECT = { kind: "article", site: "s1", report: "r1" }; async function target() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-agent-auth-")); return { dir, target: { file: path.join(dir, "notes.json"), subject: SUBJECT, source: async () => null } }; } const ADD = { op: "add", text: "fix this", anchor: { kind: "whole" } }; test("no Authorization: the operator's write, as before", async () => { const { dir, target: t } = await target(); const r = await postNoteOp(t, { op: ADD }, { authorization: null, workerToken: TOKEN }); assert.equal(r.status, 200); assert.equal(r.body.note.author, "operator"); await rm(dir, { recursive: true }); }); test("umtool's WORKER_TOKEN as a bearer: the agent's write — a note, a reply, a resolve", async () => { const { dir, target: t } = await target(); const auth = { authorization: `Bearer ${TOKEN}`, workerToken: TOKEN }; const add = await postNoteOp(t, { op: ADD }, { authorization: null, workerToken: TOKEN }); const id = add.body.note.id; const r = await postNoteOp(t, { op: { op: "reply", id, text: "done in drafts/x.json", resolve: true } }, auth); assert.equal(r.status, 200); assert.equal(r.body.note.replies[0].author, "agent"); assert.equal(r.body.note.status, "resolved"); assert.equal(r.body.note.resolvedBy, "agent"); const own = await postNoteOp(t, { op: ADD }, auth); assert.equal(own.body.note.author, "agent"); await rm(dir, { recursive: true }); }); test("a wrong bearer, a malformed header, or any bearer when umtool holds no token: 401, nothing written", async () => { const { dir, target: t } = await target(); for (const [authorization, workerToken] of [ ["Bearer not-the-token", TOKEN], [`Bearer ${TOKEN}x`, TOKEN], [`Basic ${TOKEN}`, TOKEN], [`Bearer ${TOKEN}`, ""], ]) { const r = await postNoteOp(t, { op: ADD }, { authorization, workerToken }); assert.equal(r.status, 401, `${authorization} / ${workerToken || "(none)"}`); assert.match(r.body.error, /^notes: /); } await assert.rejects(stat(t.file), /ENOENT/); await rm(dir, { recursive: true }); }); test("`by` in the body is never believed", async () => { const { dir, target: t } = await target(); const asOperator = await postNoteOp(t, { by: "agent", op: { ...ADD, by: "agent", author: "agent" } }, { authorization: null, workerToken: TOKEN }); assert.equal(asOperator.body.note.author, "operator"); const asAgent = await postNoteOp(t, { by: "operator", op: { ...ADD, by: "operator", author: "operator" } }, { authorization: `Bearer ${TOKEN}`, workerToken: TOKEN }); assert.equal(asAgent.body.note.author, "agent"); const doc = JSON.parse(await readFile(t.file, "utf8")); assert.deepEqual(doc.notes.map((n) => n.author), ["operator", "agent"]); await rm(dir, { recursive: true }); }); test("a body with no op is a 400", async () => { const { dir, target: t } = await target(); assert.equal((await postNoteOp(t, null, { authorization: null, workerToken: TOKEN })).status, 400); assert.equal((await postNoteOp(t, { token: "absent" }, { authorization: `Bearer ${TOKEN}`, workerToken: TOKEN })).status, 400); await rm(dir, { recursive: true }); }); test("noteWriter: the header alone decides", () => { assert.deepEqual(noteWriter(undefined, TOKEN), { ok: true, by: "operator" }); assert.deepEqual(noteWriter("", TOKEN), { ok: true, by: "operator" }); assert.deepEqual(noteWriter(`bearer ${TOKEN}`, TOKEN), { ok: true, by: "agent" }); assert.equal(noteWriter("Bearer x", TOKEN).ok, false); }); test("agentToken: the environment first (even empty), else the editor's env files in order", async () => { const dir = await mkdtemp(path.join(tmpdir(), "umtool-agent-token-")); const local = path.join(dir, ".env.local"); const plain = path.join(dir, ".env"); await writeFile(plain, "# the editor's\nOTHER=1\nexport WORKER_TOKEN=\"from-env-file\"\n"); assert.equal(agentToken({ WORKER_TOKEN: "from-environment" }, [local, plain]), "from-environment"); assert.equal(agentToken({ WORKER_TOKEN: "" }, [local, plain]), ""); assert.equal(agentToken({}, [local, plain]), "from-env-file"); await writeFile(local, "WORKER_TOKEN=from-local # comment\n"); assert.equal(agentToken({}, [local, plain]), "from-local"); assert.equal(agentToken({}, [path.join(dir, "missing")]), ""); assert.equal(dotenvValue("A=1\nWORKER_TOKEN='x y'\n", "WORKER_TOKEN"), "x y"); assert.equal(dotenvValue("A=1\n", "WORKER_TOKEN"), undefined); await rm(dir, { recursive: true }); });