muse-agent-example/web/tests/单元/中文显示与动作.test.tsx
zizi 9e6f1c4481 R2 改造交付:新版模块化单体全量成果
- src/muse 新版全模块(装配/共享/上下文/任务运行/作品规划/故事世界/正文写作/审校修订/知识方法/作者经验/效果评测/交付连载/资料研究/正式变更/元数据/接入/基础设施/编排)+ 测试树(单元/契约/集成/架构/迁移/端到端/夹具)
- 129 项功能全部实现与自动验证(功能覆盖.json/矩阵),含 W31 补齐的规则与代价/节奏安排/伏笔与承诺
- 旧实现按处置清单退出(702 条中 324 删,保护合同与未迁移条目留存有据);web/app.py 旧工作台退役,新工作台为唯一写入口
- 数据库/旧库迁移:真实旧库内容批次迁移链(端点守卫/PG作品正文映射/质量资产缺省投影)
- 运行手册 docs/运行手册.md;W30 本机服务阶段一已运行(infra PG 为正式内容权威)
- R2 执行证据与私有运行材料在 .agents.local/改造/R2-20260909/(不入库)
2026-09-15 12:47:42 +08:00

223 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 规划标题与摘要从载荷提取;作者动作经真实请求界面发出。 */
import { afterEach, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { 规划条目摘要 } from "../../src/功能/作品规划/数据";
import { Ui诊断报告 } from "../../src/功能/审校修订/问题清单";
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
sessionStorage.clear();
});
it("TC-00f820aca9ee:规划标题和摘要从载荷提取", () => {
const 载荷 = { targetChapter: 2, chapterGoal: "林深被隔离审讯", chapterEndHook: "茧准备出击" };
const { 标题, 摘要 } = 规划条目摘要(载荷);
expect(标题).toBe("第2章目标");
expect(摘要).toBe("林深被隔离审讯");
// 场景规划用目标推进收束,大纲用主线一句话,不写死题材字段。
const 场景 = 规划条目摘要({ 目标推进收束: "访客想借一盏灯" });
expect(场景.标题).toBe("场景目标");
expect(场景.摘要).toBe("访客想借一盏灯");
const 大纲 = 规划条目摘要({ 名称: "雪中的信", 主线一句话: "邮差寻找无人认领的信。" });
expect(大纲.标题).toBe("大纲方向");
expect(大纲.摘要).toBe("邮差寻找无人认领的信。");
expect(规划条目摘要(undefined).标题).toBe("规划候选");
});
it("TC-00f820aca9ee:候选列表展示提取结果且决定动作发真实请求", async () => {
const 请求: { 方法: string; 地址: string; 体: unknown }[] = [];
const 候选载荷 = { targetChapter: 2, chapterGoal: "林深被隔离审讯" };
vi.stubGlobal(
"fetch",
vi.fn(async (request: Request) => {
const 地址 = new URL(request.url).pathname + new URL(request.url).search;
请求.push({ 方法: request.method, 地址, 体: await request.clone().json().catch(() => null) });
const 应答 = (体: unknown) =>
new Response(JSON.stringify(体), { headers: { "Content-Type": "application/json" } });
if (地址.startsWith("/api/v1/session") || 地址.startsWith("/api/v1/works/list"))
return 应答([]);
if (地址.includes("/plan-nodes"))
return 应答({ work_id: "work", revision: 1, nodes: [] });
if (地址.includes("/plans/form"))
return 应答({
work_id: "work",
chapter_id: null,
node_id: null,
plan_id: "plan",
revision: 0,
schema_binding: {},
content: {},
visible_schema: { type: "object", properties: {}, required: [] },
editable_schema: { type: "object", properties: {}, required: [] },
});
if (地址.includes("/plans/candidates"))
return 应答([
{
candidate_id: "c1",
plan_id: "plan",
revision: 1,
candidate_hash: "hash",
work_id: "work",
chapter_id: null,
node_id: null,
expected_revision: 0,
schema_binding: {},
content: 候选载荷,
decision: "undecided",
visible_schema: { type: "object", properties: {}, required: [] },
editable_schema: { type: "object", properties: {}, required: [] },
},
]);
if (地址.includes("/explorations/selection")) return new Response("404", { status: 404 });
if (地址.includes("/review"))
return 应答({
review_id: "review-1",
review_hash: "review-hash",
snapshot: { 数据版本: 0 },
before: null,
candidate: {
candidate_id: "c1",
plan_id: "plan",
revision: 1,
candidate_hash: "hash",
work_id: "work",
chapter_id: null,
node_id: null,
expected_revision: 0,
schema_binding: {},
content: 候选载荷,
decision: "undecided",
visible_schema: { type: "object", properties: {}, required: [] },
editable_schema: { type: "object", properties: {}, required: [] },
},
changes: [],
});
if (地址.includes("/decisions"))
return 应答({ receipt_id: "receipt-1", action: "adopt", results: [] });
return 应答({});
}),
);
const { Ui规划工作区 } = await import("../../src/功能/作品规划/规划工作区");
const 客户端 = new QueryClient({ defaultOptions: { queries: { retry: false } } });
render(
<QueryClientProvider client={客户端}>
<MemoryRouter initialEntries={["/works/work/plans?schema=fine_outline"]}>
<Ui规划工作区 />
</MemoryRouter>
</QueryClientProvider>,
);
expect(await screen.findByText("第2章目标")).toBeTruthy();
expect(screen.getByText("林深被隔离审讯")).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: /候选 1 ·/ }));
const 确认 = await screen.findByRole("button", { name: "确认这份规划" });
fireEvent.click(确认);
await waitFor(() => {
const 决定 = 请求.find((r) => r.地址.includes("/decisions"));
expect(决定).toBeTruthy();
expect(决定!.体).toMatchObject({
action: "adopt",
approved_changes: ["plan"],
author_review_id: "review-1",
review_hash: "review-hash",
});
});
});
it("TC-f9bc3a8d21cf:诊断结果展示正式正文汇总与来源引文", () => {
render(
<Ui诊断报告
value={{
diagnosis_id: "d",
report: {
mode: "candidate_preview",
findings: [
{ start: 3, end: 6, quote: "值得注意的是", decision_proposal: "ask", rule_id: "l002" },
],
executed_rules: ["l002", "m001"],
unreviewed_rules: [{ rule_id: "sem001", reason: "未审" }],
text_hash: "f".repeat(64),
calibration: "uncalibrated",
target: {
work_id: "w",
chapter_id: "c",
revision: 2,
document_hash: "f".repeat(64),
branch_id: "main",
},
},
}}
/>
);
expect(screen.getByText(/机器味诊断/)).toBeTruthy();
expect(screen.getByText(/候选规则预览 · 未标定/)).toBeTruthy();
expect(screen.getByText(/正文版本 2/)).toBeTruthy();
expect(screen.getByText(/确定性规则已执行 2 条/)).toBeTruthy();
expect(screen.getByText("值得注意的是")).toBeTruthy();
});
it("TC-ea9587d92a04:正文依据变化时渲染失败关闭而非崩溃", () => {
render(
<Ui诊断报告
value={{
diagnosis_id: "d",
stale: true,
stale_reasons: ["正文版本已推进", "规则库已更新"],
report: {
mode: "candidate_preview",
findings: [],
executed_rules: [],
unreviewed_rules: [],
text_hash: "a",
calibration: "uncalibrated",
target: {
work_id: "w",
chapter_id: "c",
revision: 1,
document_hash: "a",
branch_id: "main",
},
},
}}
/>
);
const 告警 = screen.getByRole("alert");
expect(告警.textContent).toContain("依据已变化");
expect(告警.textContent).toContain("正文版本已推进");
expect(screen.queryByText(/NameError/)).toBeNull();
});
it("TC-8098487cc24d:作品定位与命中详情随诊断结果展示", () => {
render(
<Ui诊断报告
value={{
diagnosis_id: "d",
report: {
mode: "candidate_preview",
findings: [
{ start: 12, end: 18, quote: "嘴角微微上扬", decision_proposal: "retain", rule_id: "l003" },
],
executed_rules: ["l003"],
unreviewed_rules: [],
text_hash: "b".repeat(64),
calibration: "uncalibrated",
target: {
work_id: "机破星河",
chapter_id: "当年离歌",
revision: 5,
document_hash: "b".repeat(64),
branch_id: "main",
},
},
}}
/>
);
expect(screen.getByText(/正文版本 5/)).toBeTruthy();
expect(screen.getByText(/位置 12—18/)).toBeTruthy();
expect(screen.getByText("嘴角微微上扬")).toBeTruthy();
expect(screen.getByText("保留")).toBeTruthy();
});