Files
deerflow-code/frontend-web/src/components/workspace/visualizations/visualization-spec.test.ts
T

387 lines
13 KiB
TypeScript

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 {
parseMapColor,
parseOptionalBoolean,
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("recovers Beijing-Shanghai lines that omit coordinates", () => {
const fromToByName = parseVisualizationSpec(
"map-json",
JSON.stringify({
title: "北京上海位置地图",
markers: [
{ name: "北京", coordinate: [116.4, 39.9] },
{ name: "上海", coordinate: [121.47, 31.23] },
],
lines: [{ name: "北京-上海", from: "北京", to: "上海" }],
}),
);
assert.equal(fromToByName.ok, true);
assert.equal(fromToByName.ok && fromToByName.spec.type, "map");
if (fromToByName.ok && fromToByName.spec.type === "map") {
assert.equal(fromToByName.spec.markers.length, 2);
assert.deepEqual(fromToByName.spec.lines[0]?.coordinates, [
[116.4, 39.9],
[121.47, 31.23],
]);
}
const singularCoordinate = parseVisualizationSpec(
"map-json",
JSON.stringify({
markers: [
{ name: "北京", coordinate: [116.4, 39.9] },
{ name: "上海", coordinate: [121.47, 31.23] },
],
lines: [{ name: "联络", coordinate: [[116.4, 39.9], [121.47, 31.23]] }],
}),
);
assert.equal(singularCoordinate.ok, true);
if (singularCoordinate.ok && singularCoordinate.spec.type === "map") {
assert.equal(singularCoordinate.spec.lines.length, 1);
}
const echartsCoords = parseVisualizationSpec(
"map-json",
JSON.stringify({
markers: [
{ name: "北京", coordinate: [116.4, 39.9] },
{ name: "上海", coordinate: [121.47, 31.23] },
],
lines: [{ name: "联络", coords: [[116.4, 39.9], [121.47, 31.23]] }],
}),
);
assert.equal(echartsCoords.ok, true);
if (echartsCoords.ok && echartsCoords.spec.type === "map") {
assert.equal(echartsCoords.spec.lines.length, 1);
}
const fromToPoints = parseVisualizationSpec(
"map-json",
JSON.stringify({
markers: [
{ name: "北京", lng: 116.4, lat: 39.9 },
{ name: "上海", longitude: "121.47", latitude: "31.23" },
],
lines: [{ from: [116.4, 39.9], to: [121.47, 31.23] }],
}),
);
assert.equal(fromToPoints.ok, true);
if (fromToPoints.ok && fromToPoints.spec.type === "map") {
assert.equal(fromToPoints.spec.markers.length, 2);
assert.equal(fromToPoints.spec.lines.length, 1);
}
});
test("keeps markers when a line cannot be recovered", () => {
const result = parseVisualizationSpec(
"map-json",
JSON.stringify({
title: "北京上海位置地图",
markers: [
{ name: "北京", coordinate: [116.4, 39.9] },
{ name: "上海", coordinate: [121.47, 31.23] },
],
lines: [{ name: "北京-上海" }],
}),
);
assert.equal(result.ok, true);
if (result.ok && result.spec.type === "map") {
assert.equal(result.spec.markers.length, 2);
assert.equal(result.spec.lines.length, 0);
}
});
test("parses named, Chinese and hex marker colors", () => {
assert.equal(parseMapColor("red"), "#dc2626");
assert.equal(parseMapColor("蓝色"), "#2563eb");
assert.equal(parseMapColor("#0f8"), "#00ff88");
assert.equal(parseMapColor("#2563EB"), "#2563eb");
assert.equal(parseMapColor("javascript:alert(1)"), undefined);
assert.equal(parseOptionalBoolean("是"), true);
assert.equal(parseOptionalBoolean("false"), false);
});
test("keeps marker colors and line arrow/flow flags", () => {
const result = parseVisualizationSpec(
"map-json",
JSON.stringify({
markers: [
{ name: "北京", coordinate: [116.4, 39.9], color: "red" },
{ name: "上海", coordinate: [121.47, 31.23], colour: "#16a34a" },
],
lines: [
{
name: "北京-上海",
from: "北京",
to: "上海",
color: "蓝色",
arrow: "是",
flow: true,
},
],
}),
);
assert.equal(result.ok, true);
if (result.ok && result.spec.type === "map") {
assert.equal(result.spec.markers[0]?.color, "#dc2626");
assert.equal(result.spec.markers[1]?.color, "#16a34a");
assert.equal(result.spec.lines[0]?.color, "#2563eb");
assert.equal(result.spec.lines[0]?.arrow, true);
assert.equal(result.spec.lines[0]?.flow, true);
}
});
test("does not treat a generic json log as a map after dropping invalid lines", () => {
const content = '```json\n{"title":"日志","lines":["error 1","error 2"]}\n```';
assert.deepEqual(splitVisualizationContent(content), [{ type: "markdown", content }]);
});
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 }]);
});