Files
writer-work-flow/apps/web/lib/generation/cards.test.ts
Yaojia Wang 29265fa0fd feat(web): 审稿改稿闭环——一键采纳/AI改写/正文高亮定位编辑/伏笔确认/草稿自动保存/章节目录
- 冲突「采纳改法」:有补丁瞬时改入终稿并选中;无补丁但可定位则采纳时调 AI(refiner)重写该段套入;定位不到提示手改
- 正文改为行内高亮叠层编辑器(与页面同底色、随内容增高、无滚动条),点冲突卡/锚点整页滚到并选中+闪烁
- 仅对可在正文定位的冲突显示「跳转」/锚点;locate 支持从 where 引号抽原文,杜绝定位失败刷屏
- 伏笔建议卡:新埋一键登记(预填表单)/ 疑似回收一键标记 CLOSED(复用现有端点)
- 审稿页草稿自动保存(防抖 PUT /draft),与「验收本章」解耦,刷新不丢
- 写作路由带章号 ?chapter=N + 验收成功面板/大纲页「写下一章」入口;写作目录从大纲列出全部章节

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 18:28:29 +02:00

214 lines
6.2 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);
});
});
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(["势力", "地点"]);
});
});