Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
import { test, expect, describe } from "bun:test";
import { computeHeads, lineageDepth, lineageOf, type LineageRow } from "../src/prompt_lineage";
/**
* 版本链算错不会抛异常,只会让选择器里少一个版本、或者多出一堆早就被取代的旧版。
* 这里钉的是三件会静默出错的事:归档能不能把 head 交还给父版本、深度算不算已归档
* 的祖先、以及脏数据(环、断链)会不会把请求挂死。
*/
const row = (id: string, parentId: string | null = null, archived = 0): LineageRow =>
({ id, parentId, archived });
describe("computeHeads", () => {
test("一条直链只有链尖是 head", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2")];
expect(computeHeads(all).map((r) => r.id)).toEqual(["v3"]);
});
test("互不相干的根各自是 head", () => {
const all = [row("a"), row("b"), row("c", "a")];
expect(computeHeads(all).map((r) => r.id).sort()).toEqual(["b", "c"]);
});
test("归档链尖后,父版本重新成为 head", () => {
// 这条是最容易写错的:如果按「全表」算取代关系,归档 v3 会让 v2 依旧被判成
// 已被取代,于是整条血缘从选择器里消失,而 v2 还好端端地在线上跑着。
const all = [row("v1"), row("v2", "v1"), row("v3", "v2", 1)];
expect(computeHeads(all).map((r) => r.id)).toEqual(["v2"]);
});
test("连续归档会一路把 head 交还回去", () => {
const all = [row("v1"), row("v2", "v1", 1), row("v3", "v2", 1)];
expect(computeHeads(all).map((r) => r.id)).toEqual(["v1"]);
});
test("includeArchived 时链尖仍是链尖", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2", 1)];
expect(computeHeads(all, true).map((r) => r.id)).toEqual(["v3"]);
});
test("父行不存在(断链)的行仍然是 head", () => {
// 迁移前的历史行 parentId 为 NULL;但手工改过的行可能指向一个已被删掉的 id。
const all = [row("orphan", "gone-forever")];
expect(computeHeads(all).map((r) => r.id)).toEqual(["orphan"]);
});
test("空表返回空,不报错", () => {
expect(computeHeads([])).toEqual([]);
});
});
describe("lineageDepth", () => {
test("根是 1,每加一代 +1", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2")];
expect(lineageDepth(all[0], all)).toBe(1);
expect(lineageDepth(all[2], all)).toBe(3);
});
test("已归档的祖先照样计入深度", () => {
// 「这份提示词被改过几次」问的是历史,不是当前可见性。
const all = [row("v1", null, 1), row("v2", "v1", 1), row("v3", "v2")];
expect(lineageDepth(all[2], all)).toBe(3);
});
test("断链在断点处停下,不是 0 也不是无穷", () => {
const all = [row("v2", "missing"), row("v3", "v2")];
expect(lineageDepth(all[1], all)).toBe(2);
});
test("成环时返回一个有限数而不是挂死", () => {
// parentId 是没有外键的裸 text 列,回填写错就能造出环。
const all = [row("a", "b"), row("b", "a")];
expect(lineageDepth(all[0], all)).toBe(2);
});
});
describe("lineageOf", () => {
test("从指定版本一路向上,新的在前", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2")];
expect(lineageOf("v3", all).map((r) => r.id)).toEqual(["v3", "v2", "v1"]);
});
test("从中间某版开始只往上走,不含它的子版本", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2")];
expect(lineageOf("v2", all).map((r) => r.id)).toEqual(["v2", "v1"]);
});
test("不存在的 id 返回空", () => {
expect(lineageOf("nope", [row("v1")])).toEqual([]);
});
test("max 截断长链", () => {
const all = [row("v1"), row("v2", "v1"), row("v3", "v2"), row("v4", "v3")];
expect(lineageOf("v4", all, 2).map((r) => r.id)).toEqual(["v4", "v3"]);
});
test("成环时终止", () => {
const all = [row("a", "b"), row("b", "a")];
expect(lineageOf("a", all).map((r) => r.id)).toEqual(["a", "b"]);
});
});