Files
streamline/control-web/test/promptGraphLayout.test.ts
2026-11-16 17:26:44 -08:00

119 lines
4.8 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { layoutPromptGraph, edgePath, LAYOUT } from "../src/lib/promptGraphLayout";
import { PROMPT_NODES, PROMPT_SLOTS, PROMPT_TRACKS } from "@bbbug/shared/prompt_registry";
import type { PromptGraphNode } from "../src/lib/api";
/**
* 图的布局出错不会抛异常,只会把两个节点叠在一起、或者把某个节点画到画布外 ——
* 图还是图,只是读不出结构。这里钉住的就是那几件「看不出来但是错了」的事。
*/
const nodes = PROMPT_NODES.map((n) => ({
...n, dependents: [], library: {
versionCount: 0, activeVersionId: null, activeVersionName: null,
lastVersionAt: null, usingBuiltinDefault: true,
},
})) as unknown as PromptGraphNode[];
const slots = PROMPT_SLOTS.map((s) => ({ ...s }));
const tracks = PROMPT_TRACKS.map((t) => ({ ...t }));
describe("提示词图谱布局", () => {
test("每个节点都被摆放,且只摆一次", () => {
const l = layoutPromptGraph(nodes, slots, tracks);
expect(l.nodes.length).toBe(nodes.length);
expect(new Set(l.nodes.map((n) => n.id)).size).toBe(nodes.length);
});
test("没有两个节点重叠", () => {
// 叠在一起的两个节点看起来就是「一个节点」,整张图静默地少了一项。
const l = layoutPromptGraph(nodes, slots, tracks);
for (let i = 0; i < l.nodes.length; i++) {
for (let j = i + 1; j < l.nodes.length; j++) {
const a = l.nodes[i], b = l.nodes[j];
const overlap = a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
expect(overlap, `${a.id} 与 ${b.id} 重叠`).toBe(false);
}
}
});
test("所有节点都落在画布内", () => {
const l = layoutPromptGraph(nodes, slots, tracks);
for (const n of l.nodes) {
expect(n.x).toBeGreaterThanOrEqual(0);
expect(n.y).toBeGreaterThanOrEqual(l.bodyTop);
expect(n.x + n.w).toBeLessThanOrEqual(l.width);
expect(n.y + n.h).toBeLessThanOrEqual(l.height);
}
});
test("节点的横向位置由它的 slot 决定", () => {
const l = layoutPromptGraph(nodes, slots, tracks);
const byId = new Map(l.nodes.map((n) => [n.id, n]));
const colOf = (id: string) => {
const p = byId.get(id)!;
return l.columns.findIndex((c) => p.x >= c.x && p.x < c.x + c.w);
};
// 评估在执行左边,执行在报告左边 —— 这条读序是整张图的意义所在。
expect(colOf("bug.eval")).toBeLessThan(colOf("bug.run"));
expect(colOf("bug.run")).toBeLessThan(colOf("report.readable"));
expect(colOf("review.asking")).toBeLessThan(colOf("report.readable"));
});
test("片段带在所有类型带的下方", () => {
const l = layoutPromptGraph(nodes, slots, tracks);
const frag = l.bands.find((b) => b.kind === "fragment")!;
for (const b of l.bands.filter((x) => x.kind === "track")) {
expect(frag.y).toBeGreaterThanOrEqual(b.y + b.h);
}
});
test("空的类型带不占位", () => {
// 一条标着 Security 却什么都没有的空行,读起来像图坏了。
const onlyBug = nodes.filter((n) => n.track === "bug");
const l = layoutPromptGraph(onlyBug, slots, tracks);
expect(l.bands.map((b) => b.key)).toEqual(["bug", "fragments"]);
});
test("同一格里的多个节点纵向堆叠,间距一致", () => {
const l = layoutPromptGraph(nodes, slots, tracks);
const byId = new Map(l.nodes.map((n) => [n.id, n]));
const a = byId.get("feature.run.first")!;
const b = byId.get("feature.run.second")!;
expect(a.x).toBe(b.x);
expect(Math.abs(b.y - a.y)).toBe(LAYOUT.nodeH + LAYOUT.vGap);
});
test("没有节点时也给出可画的画布,而不是 NaN", () => {
const l = layoutPromptGraph([], slots, tracks);
expect(l.bands).toEqual([]);
expect(Number.isFinite(l.width)).toBe(true);
expect(Number.isFinite(l.height)).toBe(true);
});
});
describe("连线路径", () => {
const box = (x: number, y: number) => ({ id: "x", x, y, w: 170, h: 54 });
test("路径永远是合法的 SVG path,不含 NaN", () => {
const pairs = [[box(0, 0), box(0, 300)], [box(400, 0), box(0, 300)], [box(0, 100), box(400, 100)]];
for (const [a, b] of pairs) {
const d = edgePath(a, b);
expect(d).toMatch(/^M[\d.,-]+ C[\d.,\s-]+$/);
expect(d).not.toContain("NaN");
}
});
test("上下关系的连线从下边缘出发、到上边缘结束", () => {
const a = box(0, 0), b = box(0, 300);
// 从节点中间穿出去的线会盖住标题;必须走边缘。
expect(edgePath(a, b).startsWith(`M${a.x + a.w / 2},${a.y + a.h}`)).toBe(true);
expect(edgePath(a, b).endsWith(`${b.x + b.w / 2},${b.y}`)).toBe(true);
});
test("同一带内的连线走侧边,不穿过节点自身", () => {
const a = box(0, 100), b = box(400, 100);
const d = edgePath(a, b);
expect(d.startsWith(`M${a.x + a.w},`)).toBe(true);
});
});