Files

260 lines
10 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* flow-graph.ts 纯函数单测。
* 运行方式(项目无 vitest,对齐 dag-types.test.ts):
* npx esbuild src/roundtable-planning/lib/flow-graph.test.ts --bundle --format=esm \
* --platform=node --outfile=.tmp-tests/flow-graph.test.mjs
* node --test .tmp-tests/flow-graph.test.mjs
*/
import assert from "node:assert/strict";
import test from "node:test";
import {
applyNodeEdit,
deleteNodeFromGraph,
detectFlowIntent,
extractFlowGraph,
hasFlowFence,
layoutFlowGraph,
serializeFlowGraph,
stripFlowFence,
} from "./flow-graph";
const FULL = [
"报告正文……",
"```flow-json",
'{"title":"方案执行流程","nodes":[',
'{"id":"n1","label":"环境评估","type":"stage","detail":"评估现场环境"},',
'{"id":"n2","label":"编制方案","type":"task","from":["n1"]},',
'{"id":"n3","label":"是否可行","type":"decision","from":[{"id":"n2","label":"评审"}]}',
"]}",
"```",
].join("\n");
test("extractFlowGraph:完整块解析出节点/边/标题", () => {
const g = extractFlowGraph(FULL);
assert.ok(g);
assert.equal(g.title, "方案执行流程");
assert.equal(g.nodes.length, 3);
assert.equal(g.nodes[0].type, "stage");
assert.deepEqual(g.edges, [
{ from: "n1", to: "n2" },
{ from: "n2", to: "n3", label: "评审" },
]);
});
test("extractFlowGraph:流式半截(fence 未闭合 + 尾对象截断)只取完整节点", () => {
const partial = [
"正文",
"```flow-json",
'{"title":"T","nodes":[',
'{"id":"a","label":"A","type":"start"},',
'{"id":"b","label":"B","type":"task","from":["a"]},',
'{"id":"c","label":"C","ty', // 截断
].join("\n");
const g = extractFlowGraph(partial);
assert.ok(g);
assert.equal(g.nodes.length, 2);
assert.deepEqual(g.edges, [{ from: "a", to: "b" }]);
});
test("extractFlowGraph:未知 type 落 task、重复 id 去重、悬空边丢弃", () => {
const text = [
"```flow-json",
'{"nodes":[',
'{"id":"x","label":"X","type":"banana"},',
'{"id":"x","label":"X-dup","type":"task"},',
'{"id":"y","label":"Y","type":"risk","from":["x","ghost"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.equal(g.nodes.length, 2);
assert.equal(g.nodes[0].type, "task");
assert.equal(g.nodes[0].label, "X");
assert.deepEqual(g.edges, [{ from: "x", to: "y" }]);
});
test("extractFlowGraph:无 fence / 无完整节点返回 null", () => {
assert.equal(extractFlowGraph("普通回复,没有结构化数据"), null);
assert.equal(extractFlowGraph('```flow-json\n{"nodes":[{"id":"a"'), null);
});
test("extractFlowGraph:group 层级名解析(兼容 group/level)", () => {
const text = [
"```flow-json",
'{"nodes":[',
'{"id":"n1","label":"暑期文旅","type":"start","group":"任务"},',
'{"id":"n2","label":"情绪管理","type":"task","level":"目的","from":["n1"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.equal(g.nodes[0].group, "任务");
assert.equal(g.nodes[1].group, "目的"); // level 别名也认
});
test("extractFlowGraph:容错——detail 含裸换行的非法 JSON 节点不被整个丢弃", () => {
// 模型常在 JSON 字符串里吐真实换行(非法 JSON):n2 的 detail 跨行,
// 旧逻辑会 JSON.parse 失败把 n2/n3 整个丢掉,只剩 n1(即「只出两个节点」bug)。
const text = [
"```flow-json",
'{"title":"T","nodes":[',
'{"id":"n1","label":"任务X","type":"start","group":"任务"},',
'{"id":"n2","label":"目的A","type":"task","group":"目的","detail":"名称:目的A',
'理由:跨了行的说明文字","from":["n1"]},',
'{"id":"n3","label":"行为体A","type":"role","group":"行为体","detail":"名称:行为体A;理由:单行说明","from":["n2"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
// 三个节点都救回来(含裸换行的 n2 不再丢失)。
assert.equal(g.nodes.length, 3);
assert.equal(g.nodes[1].group, "目的");
assert.ok((g.nodes[1].detail ?? "").includes("目的A"));
// 连线也按 from 串起来。
assert.deepEqual(g.edges, [
{ from: "n1", to: "n2" },
{ from: "n2", to: "n3" },
]);
});
test("extractFlowGraph:容错——尾逗号的非法 JSON 节点仍解析", () => {
const text = [
"```flow-json",
'{"nodes":[',
'{"id":"a","label":"A","type":"task","group":"目的",},', // 尾逗号 → JSON.parse 失败
'{"id":"b","label":"B","type":"task","from":["a"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.equal(g.nodes.length, 2);
assert.equal(g.nodes[0].group, "目的");
});
test("extractFlowGraph:兼容独立 edges 数组(source/target 别名)", () => {
const text = [
"```flow-json",
'{"nodes":[{"id":"a","label":"A"},{"id":"b","label":"B"}],',
'"edges":[{"source":"a","target":"b","label":"走"}]}',
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.deepEqual(g.edges, [{ from: "a", to: "b", label: "走" }]);
});
test("stripFlowFence:闭合块替换为引导语并保留前后文", () => {
const out = stripFlowFence(FULL, false);
assert.ok(out.includes("报告正文"));
assert.ok(!out.includes("```flow-json"));
assert.ok(out.includes("已生成结构化流程数据"));
});
test("stripFlowFence:未闭合(流式中)用「正在输出」措辞", () => {
const out = stripFlowFence('正文\n```flow-json\n{"nodes":[', true);
assert.ok(out.includes("正在输出结构化流程数据"));
assert.ok(!out.includes("```flow-json"));
});
test("layoutFlowGraph:按前驱分层(根 0 层,子层递增),保持原始顺序", () => {
const g = extractFlowGraph(FULL)!;
const positioned = layoutFlowGraph(g);
assert.equal(positioned[0].y, 0);
assert.ok(positioned[1].y > positioned[0].y);
assert.ok(positioned[2].y > positioned[1].y);
assert.deepEqual(positioned.map((n) => n.id), ["n1", "n2", "n3"]);
});
test("detectFlowIntent:明确短语命中(编排提示 / 输入框消息)", () => {
assert.ok(detectFlowIntent("最终总结请用结构化输出"));
assert.ok(detectFlowIntent("总结成格式化输出"));
assert.ok(detectFlowIntent("给我画一张流程图"));
assert.ok(detectFlowIntent("输出可视化流程"));
assert.ok(detectFlowIntent("请按 flow-json 契约输出"));
assert.ok(detectFlowIntent("重点总结方案设计席位,最后给结构化数据"));
});
test("detectFlowIntent:普通措辞不误触发", () => {
assert.equal(detectFlowIntent("用表格把要点结构化呈现"), false);
assert.equal(detectFlowIntent("把执行路线写得更细一些"), false);
assert.equal(detectFlowIntent("面向高管汇报,控制在一页内"), false);
assert.equal(detectFlowIntent(""), false);
assert.equal(detectFlowIntent(null), false);
assert.equal(detectFlowIntent(undefined), false);
});
test("hasFlowFence:判定回复主产物是否为流程图(流式半截也算)", () => {
assert.ok(hasFlowFence(FULL)); // 完整闭合块
assert.ok(hasFlowFence('说明文字\n```flow-json\n{"nodes":[')); // 流式半截、未闭合
assert.equal(hasFlowFence("纯文字报告正文,没有流程图"), false);
assert.equal(hasFlowFence("```json\n{}\n```"), false); // 普通 json 块不算
assert.equal(hasFlowFence(""), false);
assert.equal(hasFlowFence(null), false);
assert.equal(hasFlowFence(undefined), false);
});
test("layoutFlowGraph:环不死循环", () => {
const text = [
"```flow-json",
'{"nodes":[{"id":"a","label":"A","from":["b"]},{"id":"b","label":"B","from":["a"]}]}',
"```",
].join("\n");
const g = extractFlowGraph(text)!;
const positioned = layoutFlowGraph(g);
assert.equal(positioned.length, 2);
positioned.forEach((n) => assert.ok(Number.isFinite(n.x) && Number.isFinite(n.y)));
});
test("serializeFlowGraph:与 extractFlowGraph 往返一致(含 group/detail/边标签)", () => {
const text = [
"```flow-json",
'{"title":"方案","nodes":[',
'{"id":"t","label":"任务X","type":"start","group":"任务","detail":"目标说明"},',
'{"id":"p1","label":"目的1","type":"task","group":"目的","detail":"名称+理由","from":["t"]},',
'{"id":"d","label":"判断","type":"decision","from":[{"id":"p1","label":"可行"}]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text)!;
const round = extractFlowGraph(serializeFlowGraph(g))!;
assert.equal(round.title, "方案");
assert.deepEqual(
round.nodes.map((n) => [n.id, n.label, n.group, n.detail]),
g.nodes.map((n) => [n.id, n.label, n.group, n.detail]),
);
assert.deepEqual(round.edges, g.edges); // 含 {from:"p1",to:"d",label:"可行"}
});
test("applyNodeEdit:改 label/group/detail,其余节点不变", () => {
const g = extractFlowGraph(
'```flow-json\n{"nodes":[{"id":"a","label":"A","type":"task","group":"目的"},{"id":"b","label":"B","type":"role","group":"行为体","from":["a"]}]}\n```',
)!;
const next = applyNodeEdit(g, "a", { label: "新目的", group: "目标", detail: "改后的详情" });
const a = next.nodes.find((n) => n.id === "a")!;
assert.equal(a.label, "新目的");
assert.equal(a.group, "目标");
assert.equal(a.detail, "改后的详情");
// b 不变;边不变。
assert.equal(next.nodes.find((n) => n.id === "b")!.label, "B");
assert.deepEqual(next.edges, g.edges);
// 空 group/detail → 清空(置 undefined)。
const cleared = applyNodeEdit(g, "a", { group: " ", detail: "" });
const a2 = cleared.nodes.find((n) => n.id === "a")!;
assert.equal(a2.group, undefined);
});
test("deleteNodeFromGraph:删节点同时删掉与它相连的所有边", () => {
const g = extractFlowGraph(
'```flow-json\n{"nodes":[{"id":"t","label":"任务"},{"id":"p","label":"目的","from":["t"]},{"id":"b","label":"行为体","from":["p"]}]}\n```',
)!;
const next = deleteNodeFromGraph(g, "p");
assert.deepEqual(next.nodes.map((n) => n.id), ["t", "b"]);
// t→p 和 p→b 两条边都没了(b 变孤点)。
assert.equal(next.edges.length, 0);
});