import assert from "node:assert/strict"; import test from "node:test"; import { hashLongformMarkdown, nextLongformAppendixState, parseLongformAppendixRow, } from "../src/lib/personal-report-longform-appendix.ts"; test("hashLongformMarkdown is stable sha256", () => { assert.equal(hashLongformMarkdown("# a"), hashLongformMarkdown("# a")); assert.equal(hashLongformMarkdown("# a").length, 64); assert.notEqual(hashLongformMarkdown("# a"), hashLongformMarkdown("# b")); }); test("parseLongformAppendixRow rejects foreign or incomplete rows", () => { assert.equal(parseLongformAppendixRow(null), null); assert.equal(parseLongformAppendixRow({ status: "ready" }), null); assert.equal(parseLongformAppendixRow({ report_id: "123e4567-e89b-12d3-a456-426614174000", user_id: "user-1", request_id: "123e4567-e89b-12d3-a456-426614174001", status: "ready", markdown: "# Full", })?.status, "ready"); }); test("nextLongformAppendixState marks unavailable after two failures and never stores error bodies", () => { const first = nextLongformAppendixState({ current: null, errorCode: "upstream_unavailable" }); assert.equal(first.status, "pending"); assert.equal(first.attemptCount, 1); assert.equal(first.markdown, null); assert.equal(first.lastErrorCode, "upstream_unavailable"); const second = nextLongformAppendixState({ current: { reportId: "r", userId: "u", requestId: "q", status: "pending", markdown: null, contentSha256: null, attemptCount: 1, lastErrorCode: "upstream_unavailable", }, errorCode: "empty_markdown", }); assert.equal(second.status, "unavailable"); assert.equal(second.attemptCount, 2); assert.equal(second.markdown, null); const ready = nextLongformAppendixState({ current: null, successMarkdown: "# Full" }); assert.equal(ready.status, "ready"); assert.equal(ready.markdown, "# Full"); assert.equal(ready.contentSha256, hashLongformMarkdown("# Full")); assert.equal(ready.lastErrorCode, null); });