Files
writer-work-flow/apps/web/lib/generation/cards.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

227 lines
6.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type {
CharacterCardView,
WorldEntityCardView,
} from "@/lib/api/types";
import {
buildCharacterGenerateRequest,
buildCharacterIngestRequest,
buildWorldGenerateRequest,
clampCharacterCount,
errorCode,
extractIngestConflicts,
generationErrorMessage,
MAX_CHARACTER_COUNT,
mergeCharacterCards,
mergeWorldEntities,
MIN_CHARACTER_COUNT,
updateCard,
worldEntityRules,
} from "./cards";
const card = (over: Partial<CharacterCardView> = {}): CharacterCardView => ({
name: "甲",
role: "主角",
backstory: "出身寒门",
arc: "由怯转勇",
...over,
});
describe("buildWorldGenerateRequest", () => {
it("trims brief", () => {
expect(buildWorldGenerateRequest(" 修真世界 ")).toEqual({
brief: "修真世界",
});
});
});
describe("clampCharacterCount", () => {
it("clamps to [MIN, MAX] and rounds", () => {
expect(clampCharacterCount(0)).toBe(MIN_CHARACTER_COUNT);
expect(clampCharacterCount(99)).toBe(MAX_CHARACTER_COUNT);
expect(clampCharacterCount(3.6)).toBe(4);
expect(clampCharacterCount(Number.NaN)).toBe(MIN_CHARACTER_COUNT);
});
});
describe("buildCharacterGenerateRequest", () => {
it("trims brief, clamps count, omits empty role", () => {
expect(buildCharacterGenerateRequest(" 剑修 ", 99)).toEqual({
brief: "剑修",
count: MAX_CHARACTER_COUNT,
});
});
it("includes trimmed role when present", () => {
expect(buildCharacterGenerateRequest("剑修", 2, " 对手 ")).toEqual({
brief: "剑修",
count: 2,
role: "对手",
});
});
});
describe("buildCharacterIngestRequest", () => {
it("copies cards and carries acknowledge flag", () => {
const cards = [card()];
const req = buildCharacterIngestRequest(cards, true);
expect(req.acknowledge_conflicts).toBe(true);
expect(req.cards).toEqual(cards);
expect(req.cards[0]).not.toBe(cards[0]); // immutable copy
});
it("defaults acknowledge to false", () => {
expect(buildCharacterIngestRequest([card()]).acknowledge_conflicts).toBe(
false,
);
});
});
describe("updateCard", () => {
it("returns a new card with patch applied (no mutation)", () => {
const original = card();
const next = updateCard(original, { name: "乙" });
expect(next.name).toBe("乙");
expect(original.name).toBe("甲");
expect(next).not.toBe(original);
});
});
describe("extractIngestConflicts", () => {
it("narrows CONFLICT_UNRESOLVED envelope details", () => {
const out = extractIngestConflicts({
error: {
code: "CONFLICT_UNRESOLVED",
details: {
conflicts: [
{ type: "性格漂移", where: "第3章", refs: ["甲"], suggestion: "改" },
"junk",
],
conflict_count: 2,
},
},
});
expect(out).toEqual({
conflictCount: 2,
conflicts: [
{
type: "性格漂移",
where: "第3章",
refs: ["甲"],
suggestion: "改",
original: null,
replacement: null,
},
{
type: "未分类",
where: "",
refs: [],
suggestion: "",
original: null,
replacement: null,
},
],
});
});
it("returns null for non-conflict errors", () => {
expect(extractIngestConflicts({ error: { code: "LLM_UNAVAILABLE" } })).toBeNull();
expect(extractIngestConflicts(null)).toBeNull();
});
it("falls back conflict_count to conflicts length", () => {
const out = extractIngestConflicts({
error: {
code: "CONFLICT_UNRESOLVED",
details: { conflicts: [{ type: "设定违例" }] },
},
});
expect(out?.conflictCount).toBe(1);
});
});
describe("errorCode / generationErrorMessage", () => {
it("extracts code and maps 503 to settings prompt", () => {
expect(errorCode({ error: { code: "LLM_UNAVAILABLE" } })).toBe(
"LLM_UNAVAILABLE",
);
expect(generationErrorMessage({ error: { code: "LLM_UNAVAILABLE" } })).toContain(
"设置",
);
expect(generationErrorMessage({ error: { code: "INTERNAL" } })).toContain(
"重试",
);
});
});
describe("worldEntityRules", () => {
it("returns rules or empty array", () => {
expect(worldEntityRules({ type: "势力", name: "宗门", rules: ["a"] })).toEqual([
"a",
]);
expect(worldEntityRules({ type: "势力", name: "宗门" })).toEqual([]);
});
});
describe("mergeCharacterCards", () => {
it("appends session cards not already in the persisted list", () => {
const persisted = [card({ name: "甲" })];
const session = [card({ name: "乙" })];
const out = mergeCharacterCards(persisted, session);
expect(out.map((c) => c.name)).toEqual(["甲", "乙"]);
});
it("dedups by name (persisted wins, no duplicate on reload echo)", () => {
const persisted = [card({ name: "甲", role: "主角" })];
const session = [card({ name: "甲", role: "对手" })];
const out = mergeCharacterCards(persisted, session);
expect(out).toHaveLength(1);
expect(out[0]?.role).toBe("主角"); // persisted version kept
});
it("returns persisted list when no session cards", () => {
const persisted = [card({ name: "甲" })];
expect(mergeCharacterCards(persisted, [])).toEqual(persisted);
});
it("preserves relations on the merged cards (Codex relation display)", () => {
const persisted = [
card({
name: "甲",
relations: [{ name: "乙", kind: "宿敌", note: "灭门之仇" }],
}),
];
const out = mergeCharacterCards(persisted, []);
expect(out[0]?.relations).toEqual([
{ name: "乙", kind: "宿敌", note: "灭门之仇" },
]);
});
});
describe("mergeWorldEntities", () => {
const entity = (
over: Partial<WorldEntityCardView> = {},
): WorldEntityCardView => ({ type: "势力", name: "宗门", ...over });
it("appends entities not already present (keyed by type+name)", () => {
const persisted = [entity({ name: "天剑宗" })];
const session = [entity({ name: "魔渊" })];
expect(mergeWorldEntities(persisted, session).map((e) => e.name)).toEqual([
"天剑宗",
"魔渊",
]);
});
it("dedups by type+name; same name different type kept separate", () => {
const persisted = [entity({ type: "势力", name: "玄" })];
const session = [
entity({ type: "势力", name: "玄" }),
entity({ type: "地点", name: "玄" }),
];
const out = mergeWorldEntities(persisted, session);
expect(out).toHaveLength(2);
expect(out.map((e) => e.type)).toEqual(["势力", "地点"]);
});
});