Files
Jyotisha/frontend/tests/report-calculation-charts.test.tsx
T
jesse-uxandGrok (agent loop) a295afa40f fix(report): draw charts in the full-data body and clean Chinese residue
New reports draw one north-Indian chart for each ready divisional in the body, so the trailing duplicate section drops. Chinese full-data prose no longer glues English onto Han or leaves listed, background, or native. The birth table starts with the ascendant. The D9 same-sign column says yes or no.

Self-test: tsc exit 0; npm run lint exit 0 (0 errors, 125 existing warnings); three frontend test files 39 pass, 0 fail, 0 cancelled; pytest test_report_chart_blank_columns.py 16 passed; seven related report files 88 passed, exit 0; privacy markers 63 passed.

Co-Authored-By: Grok (agent loop)
2026-10-13 14:02:26 +08:00

62 lines
3.3 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import test from "node:test";
import { ReportCalculationCharts } from "../src/components/personal-report/report-calculation-charts.tsx";
import { buildReportExportBlocks } from "../src/lib/report-export-blocks.ts";
import { fencedReportChartIds } from "../src/lib/report-chart-block.ts";
import { assembleLongformCharts } from "../src/lib/personal-report-generation.ts";
Object.assign(globalThis, { React });
// Real engine output and its real reader body (fictional chart).
const fixture = JSON.parse(readFileSync(new URL("./fixtures/report-density-fictional-reader.json", import.meta.url), "utf8"));
const engine = JSON.parse(readFileSync(new URL("./fixtures/report-density-fictional-engine.json", import.meta.url), "utf8"));
const charts = assembleLongformCharts(engine, "ev-cover-longform");
function render(markdown: string, language: "zh" | "en" = "zh"): string {
return renderToStaticMarkup(React.createElement(ReportCalculationCharts, { charts, markdown, language }));
}
// BUG-1203: the body already drew these charts; the tail drew them again.
test("a report whose body draws every chart has no trailing chart section", () => {
const drawn = fencedReportChartIds(fixture.markdown);
assert.ok(charts.length > 0);
for (const chart of charts) assert.ok(drawn.has(chart.id), chart.id);
assert.equal(render(fixture.markdown), "");
assert.equal(render(fixture.markdown, "en"), "");
});
test("an older body without chart fences keeps its charts, without the moved caveat", () => {
const markup = render("# 报告\n\n正文");
assert.match(markup, /<h2>分盘核对<\/h2>/);
assert.equal((markup.match(/personal-report-chart-card/g) ?? []).length, charts.filter(chart => chart.houses.some(house => house.occupants.length)).length);
assert.doesNotMatch(markup, /出生时间敏感/);
});
test("a full-data body that fences every calculation chart drops the trailing section", () => {
const fullData = JSON.parse(readFileSync(new URL("./fixtures/report-full-data-chart-fences.json", import.meta.url), "utf8"));
const fullCharts = assembleLongformCharts(fullData.packet, "ev-cover-longform");
const drawn = fencedReportChartIds(fullData.markdown);
assert.ok(fullCharts.length > 0);
for (const chart of fullCharts) assert.ok(drawn.has(chart.id), chart.id);
assert.equal(renderToStaticMarkup(React.createElement(ReportCalculationCharts, {
charts: fullCharts,
markdown: fullData.markdown,
language: "zh",
})), "");
});
test("the page and the export skip the same charts", () => {
const first = /```jyotish-chart\n[\s\S]*?```/.exec(fixture.markdown)![0];
const drawn = [...fencedReportChartIds(first)];
assert.equal(drawn.length, 1);
const markup = render(first);
const pageIds = charts.filter(chart => !drawn.includes(chart.id) && chart.houses.some(house => house.occupants.length)).map(chart => chart.id);
assert.equal((markup.match(/personal-report-chart-card/g) ?? []).length, pageIds.length);
const exported = buildReportExportBlocks({ markdown: first, charts }).filter(block => block.group === "chart" && !/^chart:[^:]+:\d+$/.test(block.id)); // fence charts carry an occurrence suffix
assert.deepEqual(exported.map(block => block.id.replace(/^chart:/, "")), pageIds);
});