Files
Jyotisha/frontend/tests/personal-report-longform-appendix.test.ts
T
Jesse_Chen e87c58d6e4 fix(report): persist longform appendix on self-hosted upsert (BUG-576)
Engine calls already returned markdown; the worker failed because local postgres upsert required onConflict and the appendix write omitted it.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:19:21 +08:00

71 lines
2.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
hashLongformMarkdown,
nextLongformAppendixState,
parseLongformAppendixRow,
} from "../src/lib/personal-report-longform-appendix.ts";
import { summarizePersonalReportFailure } from "../src/lib/personal-report-failure-summary.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);
});
test("failed longform appendix codes become readable summaries without storing bodies", () => {
const summary = summarizePersonalReportFailure({
themeCount: 5,
sections: [],
failureCode: "calculation_unavailable",
appendixLastErrorCode: "empty_markdown",
});
assert.equal(summary.appendixLastErrorCode, "empty_markdown");
assert.equal(summary.summary, "计算引擎返回了空文,请稍后重试");
assert.deepEqual(summary.lastErrorCodes, []);
});