muse-agent-example/web/tests/单元/中文显示与动作.test.tsx
zizi 98530437f2 W11 编辑器、动态表单与人审工作台:编辑器、动态表单与人审工作台(确认前正文不落正式库)。
按 R2 串行阶段整理提交;包内文件为该阶段交付(含后续小增量),状态以工作包清单为准。
2026-09-10 19:25:41 +08:00

127 lines
5.3 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/功能/作品规划/数据";
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",
});
});
});