sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -0,0 +1,248 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
// @ts-expect-error Node's type-stripping test runner requires the explicit .ts suffix.
|
||||
import { parseVisualizationSpec, splitVisualizationContent } from "./visualization-spec.ts";
|
||||
|
||||
test("ordinary markdown remains one unchanged segment", () => {
|
||||
const content = "普通回答\n\n```ts\nconst value = 1;\n```";
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
|
||||
test("splits a dedicated map block while keeping surrounding markdown", () => {
|
||||
const content = [
|
||||
"下面是地图:\n",
|
||||
"```map-json",
|
||||
'{"title":"地点","markers":[{"name":"北京","coordinate":[116.4,39.9]}]}',
|
||||
"```",
|
||||
"\n地图结束。",
|
||||
].join("\n");
|
||||
const segments = splitVisualizationContent(content);
|
||||
assert.equal(segments.length, 3);
|
||||
assert.equal(segments[0]?.type, "markdown");
|
||||
assert.equal(segments[1]?.type, "visualization");
|
||||
assert.equal(segments[2]?.type, "markdown");
|
||||
});
|
||||
|
||||
test("accepts the map contract", () => {
|
||||
const map = parseVisualizationSpec(
|
||||
"map-json",
|
||||
JSON.stringify({ markers: [{ name: "北京", coordinate: [116.4, 39.9] }] }),
|
||||
);
|
||||
assert.equal(map.ok, true);
|
||||
});
|
||||
|
||||
test("accepts a dedicated mindmap contract", () => {
|
||||
const result = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
title: "形势分析",
|
||||
rootId: "root",
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "形势分析", kind: "topic" },
|
||||
{ id: "risk", parentId: "root", label: "主要风险", kind: "risk" },
|
||||
{ id: "action", parentId: "root", label: "处置建议", kind: "action" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.ok && result.spec.type, "mindmap");
|
||||
});
|
||||
|
||||
test("accepts four meaningful children under a non-root analysis node", () => {
|
||||
const result = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
title: "人物分析",
|
||||
rootId: "root",
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "人物分析", kind: "topic" },
|
||||
{ id: "basic", parentId: "root", label: "基本信息", kind: "dimension" },
|
||||
{ id: "identity", parentId: "basic", label: "身份", kind: "finding" },
|
||||
{ id: "experience", parentId: "basic", label: "经历", kind: "finding" },
|
||||
{ id: "relations", parentId: "basic", label: "关系", kind: "finding" },
|
||||
{ id: "events", parentId: "basic", label: "事件", kind: "finding" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(
|
||||
result.ok && result.spec.type === "mindmap" && result.spec.nodes.length,
|
||||
6,
|
||||
);
|
||||
});
|
||||
|
||||
test("renders a completed legacy mindmap fence as a mindmap", () => {
|
||||
const content = [
|
||||
"```mindmap",
|
||||
'{"rootId":"root","nodes":[{"id":"root","parentId":null,"label":"主题"}]}',
|
||||
"```",
|
||||
].join("\n");
|
||||
const visual = splitVisualizationContent(content).find((segment) => segment.type === "visualization");
|
||||
assert.ok(visual && visual.type === "visualization");
|
||||
assert.equal(visual.language, "mindmap-json");
|
||||
assert.equal(visual.complete, true);
|
||||
});
|
||||
|
||||
test("rejects mindmaps with a cycle or missing parent", () => {
|
||||
const cycle = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
{ id: "a", parentId: "b", label: "A" },
|
||||
{ id: "b", parentId: "a", label: "B" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(cycle.ok, false);
|
||||
|
||||
const missingParent = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
{ id: "a", parentId: "missing", label: "A" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(missingParent.ok, false);
|
||||
});
|
||||
|
||||
test("rejects dense mindmaps before they can overload the canvas", () => {
|
||||
const tooManyChildren = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
{ id: "a", parentId: "root", label: "维度 A" },
|
||||
{ id: "b", parentId: "root", label: "维度 B" },
|
||||
{ id: "c", parentId: "root", label: "维度 C" },
|
||||
{ id: "d", parentId: "root", label: "维度 D" },
|
||||
{ id: "e", parentId: "root", label: "维度 E" },
|
||||
{ id: "f", parentId: "root", label: "维度 F" },
|
||||
{ id: "g", parentId: "root", label: "维度 G" },
|
||||
{ id: "h", parentId: "root", label: "维度 H" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(tooManyChildren.ok, false);
|
||||
|
||||
const tooManyBranchChildren = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
{ id: "basic", parentId: "root", label: "基本信息" },
|
||||
...Array.from({ length: 5 }, (_, index) => ({
|
||||
id: `detail-${index}`,
|
||||
parentId: "basic",
|
||||
label: `要点 ${index + 1}`,
|
||||
})),
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(tooManyBranchChildren.ok, false);
|
||||
assert.equal(
|
||||
tooManyBranchChildren.ok ? "" : tooManyBranchChildren.error,
|
||||
"节点“基本信息”最多保留 4 个子节点,当前有 5 个;请合并同类信息",
|
||||
);
|
||||
|
||||
const tooManyNodes = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
...Array.from({ length: 36 }, (_, index) => ({
|
||||
id: `node-${index}`,
|
||||
parentId: "root",
|
||||
label: `节点 ${index + 1}`,
|
||||
})),
|
||||
],
|
||||
}),
|
||||
);
|
||||
assert.equal(tooManyNodes.ok, false);
|
||||
assert.equal(
|
||||
tooManyNodes.ok ? "" : tooManyNodes.error,
|
||||
"思维导图节点过多(37 个);请将同类信息合并后控制在 36 个以内",
|
||||
);
|
||||
});
|
||||
|
||||
test("keeps the four-layer depth guard after branch tolerance", () => {
|
||||
const tooDeep = parseVisualizationSpec(
|
||||
"mindmap-json",
|
||||
JSON.stringify({
|
||||
nodes: [
|
||||
{ id: "root", parentId: null, label: "主题" },
|
||||
{ id: "level-1", parentId: "root", label: "一级" },
|
||||
{ id: "level-2", parentId: "level-1", label: "二级" },
|
||||
{ id: "level-3", parentId: "level-2", label: "三级" },
|
||||
{ id: "level-4", parentId: "level-3", label: "四级" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(tooDeep.ok, false);
|
||||
assert.equal(
|
||||
tooDeep.ok ? "" : tooDeep.error,
|
||||
"思维导图最多支持 4 层层级",
|
||||
);
|
||||
});
|
||||
|
||||
test("renders a valid map contract from a generic json fence", () => {
|
||||
const content = [
|
||||
"下面是地图:",
|
||||
"```json",
|
||||
'{"title":"城市分布","markers":[{"name":"北京","coordinate":[116.4,39.9]}]}',
|
||||
"```",
|
||||
].join("\n");
|
||||
const segments = splitVisualizationContent(content);
|
||||
const visual = segments.find((segment) => segment.type === "visualization");
|
||||
assert.ok(visual && visual.type === "visualization");
|
||||
assert.equal(visual.language, "map-json");
|
||||
});
|
||||
|
||||
test("keeps non-map json fences on the ordinary Markdown path", () => {
|
||||
const content = '```json\n{"name":"普通数据","items":[1,2,3]}\n```';
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
|
||||
test("keeps generic mindmap-shaped JSON on the ordinary Markdown path", () => {
|
||||
const content = [
|
||||
"```json",
|
||||
'{"type":"mindmap","nodes":[{"id":"root","parentId":null,"label":"主题"}]}',
|
||||
"```",
|
||||
].join("\n");
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
|
||||
test("keeps unfinished generic json fences on the ordinary Markdown path", () => {
|
||||
const content = '```json\n{"markers":[';
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
|
||||
test("rejects invalid map coordinates", () => {
|
||||
const result = parseVisualizationSpec(
|
||||
"map-json",
|
||||
JSON.stringify({ markers: [{ name: "错误地点", coordinate: [200, 100] }] }),
|
||||
);
|
||||
assert.equal(result.ok, false);
|
||||
});
|
||||
|
||||
test("flow and chart fences stay on the ordinary Markdown path", () => {
|
||||
const content = "```flow-json\n{}\n```\n```chart-json\n{}\n```";
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
|
||||
test("keeps an unfinished map fence for streaming fallback", () => {
|
||||
const segments = splitVisualizationContent("说明\n```map-json\n{\"title\":\"地点\"");
|
||||
const visual = segments.find((segment) => segment.type === "visualization");
|
||||
assert.ok(visual && visual.type === "visualization");
|
||||
assert.equal(visual.complete, false);
|
||||
});
|
||||
|
||||
test("shows unfinished dedicated mindmap JSON before mounting the renderer", () => {
|
||||
const content = "说明\n```mindmap-json\n{\"title\":\"分析导图\"";
|
||||
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
|
||||
});
|
||||
Reference in new issue
Block a user