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

140 lines
4.1 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 { beforeEach, afterEach, expect, it, vi } from "vitest";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { MemoryRouter } from "react-router-dom";
import { Ui偏好页 } from "../../src/功能/作者经验/偏好页";
const api = vi.hoisted(() => ({
list: vi.fn(),
save: vi.fn(),
review: vi.fn(),
confirm: vi.fn(),
revoke: vi.fn(),
}));
vi.mock("../../src/功能/作者经验/数据", () => ({
列出偏好: api.list,
保存偏好: api.save,
打开审阅: api.review,
确认偏好: api.confirm,
撤回偏好: api.revoke,
}));
vi.mock("../../src/功能/作品规划/数据", () => ({ 列出作品: async () => [] }));
function row(id = "pending", state = "pending", text = "用动作显现心理。") {
return {
preference_id: id,
revision: 1,
content_hash: id + "-hash",
state,
payload: {
topic: id,
text,
quote: "保留作者原话",
scope: { kind: "author", work_id: "", task_id: "" },
origin: "explicit",
duration: "persistent",
feedback_id: "",
},
conflicts: [],
};
}
function mount() {
return render(
<MemoryRouter>
<QueryClientProvider
client={
new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
}
>
<Ui偏好页 />
</QueryClientProvider>
</MemoryRouter>,
);
}
beforeEach(() => {
vi.clearAllMocks();
api.review.mockResolvedValue({ review_id: "review", review_hash: "hash" });
api.confirm.mockResolvedValue({});
api.revoke.mockResolvedValue({});
api.save.mockResolvedValue({ results: [{ state: "active" }] });
});
afterEach(cleanup);
it("NC-ui-preference-conflict:点名冲突并审阅后才能确认", async () => {
api.list.mockResolvedValue([
row("old", "active", "解释人物感受。"),
{ ...row(), conflicts: [["old", 1]] },
]);
mount();
const card = await screen.findByRole("region", { name: "pending" });
const check = within(card).getByRole("button", {
name: "核对确认内容",
}) as HTMLButtonElement;
expect(check.disabled).toBe(true);
fireEvent.click(within(card).getByRole("checkbox"));
fireEvent.click(check);
const confirm = await within(card).findByRole("button", {
name: "确认这项偏好",
});
expect(api.confirm).not.toHaveBeenCalled();
fireEvent.click(confirm);
await waitFor(() =>
expect(api.confirm).toHaveBeenCalledWith(
expect.objectContaining({
preference_id: "pending",
revision: 1,
content_hash: "pending-hash",
replace_versions: [["old", 1]],
}),
{ review_id: "review", review_hash: "hash" },
),
);
});
it("NC-ui-preference-stale:旧批准失败后清除可提交审阅", async () => {
api.list.mockResolvedValue([row()]);
api.confirm.mockRejectedValue(new Error("偏好版本已改变"));
mount();
const card = await screen.findByRole("region", { name: "pending" });
fireEvent.click(within(card).getByRole("button", { name: "核对确认内容" }));
fireEvent.click(
await within(card).findByRole("button", { name: "确认这项偏好" }),
);
await within(card).findByText("偏好版本已改变");
expect(
within(card).queryByRole("button", { name: "确认这项偏好" }),
).toBeNull();
});
it("NC-ui-preference-scope:任务要求不被编辑成长期范围", async () => {
api.list.mockResolvedValue([
{
...row("one-off", "active"),
payload: {
...row().payload,
duration: "request",
scope: { kind: "task", work_id: "work", task_id: "task" },
},
},
]);
mount();
const card = await screen.findByRole("region", { name: "pending" });
expect(within(card).queryByRole("button", { name: "修正内容" })).toBeNull();
fireEvent.click(within(card).getByRole("button", { name: "撤回这项偏好" }));
await waitFor(() =>
expect(api.revoke).toHaveBeenCalledWith(
expect.objectContaining({ preference_id: "one-off", revision: 1 }),
),
);
});