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 { PersonalReportMarkdownView } from "../src/components/personal-report/personal-report-markdown-view.tsx";
import { planetDisplayLabel, type ReportChartBlock } from "../src/lib/report-chart-block.ts";
Object.assign(globalThis, { React });
const SAMPLE_BLOCK: ReportChartBlock = {
version: 1,
id: "D1",
title: "D1 — Rashi Chart(本命盘)",
layout: "north",
ascendant: { sign: "Leo", degree: 12.34 },
planets: [
{ name: "Sun", sign: "Leo", degree: 3.21, retrograde: false },
{ name: "Saturn", sign: "Capricorn", degree: 3.4, retrograde: true },
],
};
const D9_BLOCK: ReportChartBlock = { ...SAMPLE_BLOCK, id: "D9", title: "D9 — Navamsa(婚盘)" };
const MOON_BLOCK: ReportChartBlock = { ...SAMPLE_BLOCK, id: "MOON", title: "Moon Chart(月亮参考盘)" };
const MARKDOWN = [
"#### D1 — Rashi Chart(本命盘)",
"",
'',
"",
"```jyotish-chart",
JSON.stringify(SAMPLE_BLOCK),
"```",
"",
"```jyotish-chart",
"{not-json",
"```",
].join("\n");
const GOLDEN = JSON.parse(
readFileSync(new URL("./fixtures/report-chart-blocks-golden.json", import.meta.url), "utf8"),
) as { blocks: ReportChartBlock[] };
function fence(block: ReportChartBlock): string {
return ["```jyotish-chart", JSON.stringify(block), "```"].join("\n");
}
function countClass(markup: string, className: string): number {
const escaped = className.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
return (markup.match(new RegExp(`(?:class|className)="[^"]*\\b${escaped}\\b`, "g")) ?? []).length;
}
function cardInnerHtml(markup: string): string[] {
const cards: string[] = [];
const re = /
([\s\S]*?)<\/div>/g;
let match: RegExpExecArray | null;
while ((match = re.exec(markup))) cards.push(match[1] ?? "");
return cards;
}
test("markdown view draws the North Indian component and skips engine SVG HTML", () => {
const markup = renderToStaticMarkup(
React.createElement(PersonalReportMarkdownView, { markdown: MARKDOWN }),
);
assert.match(markup, /