Files
writer-work-flow/apps/web/lib/outline/outline.test.ts
Yaojia Wang bf39f50b2f fix(qa): 实施 4 组设计型 QA 项——规则删除/codex 角色/大纲卷过滤/文风回炉锚点
#5 规则 DELETE + id:暴露 RuleView.id(PK);新增 DELETE /projects/{id}/rules/{rule_id}
  (项目/规则不存在→404,成功→204,按 (id,project_id) 限定);rule_repo 加
  list_for_project/delete;RulesPage 每条加删除(乐观删+回滚+toast)。assemble 侧
  RuleView(缓存前缀)不动,列表另立 RuleListItemView。
#7 codex 角色 relations:写侧本已持久化、读端点 _existing_characters 硬编码 []。
  加 _relations_from_jsonb 解析 {name,kind,note},CodexPage 渲染关系 chip。
#8 角色入库幂等:SqlCharacterWriteRepo.create 改 (project_id,name) app 层 upsert——
  重复入库改更新而非插入;不加 UNIQUE/迁移(线上已有重复行会让约束迁移失败)。
#1 大纲卷过滤:GET /outline 支持可选 ?volume(无参=全部,向后兼容);OutlineEditor
  加「查看:全部/卷N」筛选,与生成目标卷解耦。
H3/#9 文风回炉锚点:StyleDriftSegment 加 text(逐字命中段),style.md 指示审稿输出;
  前端按内容锚点定位回炉目标(idx 仅排序),命中失败 → 提示「无法定位该段」而非
  静默 no-op。style golden fixture 已重生成。

契约变更已 pnpm gen:api(RuleView.id / DELETE rules / outline ?volume)。无迁移
(alembic 无漂移)。门禁绿:ruff/mypy(210)/alembic/pytest 760 · 前端 tsc/lint/vitest 329。
2026-06-25 12:53:03 +02:00

108 lines
2.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type {
ForeshadowWindowView,
OutlineChapterView,
} from "@/lib/api/types";
import {
distinctVolumes,
filterByViewVolume,
groupByVolume,
isCloseWindow,
windowBadgeLabel,
} from "./outline";
const ch = (over: Partial<OutlineChapterView>): OutlineChapterView => ({
no: 1,
volume: 1,
beats: [],
foreshadow_windows: [],
...over,
});
const win = (
over: Partial<ForeshadowWindowView>,
): ForeshadowWindowView => ({ code: "F-012", ...over });
describe("groupByVolume", () => {
it("groups by volume and sorts volumes and chapters ascending", () => {
const groups = groupByVolume([
ch({ no: 127, volume: 3 }),
ch({ no: 12, volume: 1 }),
ch({ no: 126, volume: 3 }),
]);
expect(groups.map((g) => g.volume)).toEqual([1, 3]);
expect(groups[1]?.chapters.map((c) => c.no)).toEqual([126, 127]);
});
it("handles undefined", () => {
expect(groupByVolume(undefined)).toEqual([]);
});
});
describe("distinctVolumes", () => {
it("returns sorted unique volume numbers", () => {
expect(
distinctVolumes([
ch({ no: 1, volume: 2 }),
ch({ no: 2, volume: 1 }),
ch({ no: 3, volume: 2 }),
]),
).toEqual([1, 2]);
});
it("handles undefined", () => {
expect(distinctVolumes(undefined)).toEqual([]);
});
});
describe("filterByViewVolume", () => {
const chapters = [
ch({ no: 1, volume: 1 }),
ch({ no: 2, volume: 1 }),
ch({ no: 3, volume: 2 }),
];
it("returns all chapters when view is 'all'", () => {
expect(filterByViewVolume(chapters, "all").map((c) => c.no)).toEqual([
1, 2, 3,
]);
});
it("keeps only the selected volume", () => {
expect(filterByViewVolume(chapters, 2).map((c) => c.no)).toEqual([3]);
});
it("returns empty for a volume with no chapters", () => {
expect(filterByViewVolume(chapters, 9)).toEqual([]);
});
it("handles undefined", () => {
expect(filterByViewVolume(undefined, "all")).toEqual([]);
});
});
describe("windowBadgeLabel", () => {
it("renders a range, half-open, or bare badge", () => {
expect(
windowBadgeLabel(win({ expected_close_from: 40, expected_close_to: 60 })),
).toBe("⚑ F-012 (40-60)");
expect(windowBadgeLabel(win({ expected_close_to: 60 }))).toBe(
"⚑ F-012 (≤60)",
);
expect(windowBadgeLabel(win({ expected_close_from: 40 }))).toBe(
"⚑ F-012 (≥40)",
);
expect(windowBadgeLabel(win({}))).toBe("⚑ F-012");
});
});
describe("isCloseWindow", () => {
it("is true when chapter no is inside the recovery window", () => {
const w = win({ expected_close_from: 40, expected_close_to: 60 });
expect(isCloseWindow(ch({ no: 50 }), w)).toBe(true);
expect(isCloseWindow(ch({ no: 61 }), w)).toBe(false);
expect(isCloseWindow(ch({ no: 50 }), win({}))).toBe(false);
});
});