muse-agent-example/web/tests/交互/上下文材料.test.tsx

112 lines
3.8 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.

/** 组件传输替身只验证渲染与取数路径;真实后端旅程由 Playwright 承担。 */
import { afterEach, expect, it, vi } from "vitest";
import { cleanup, render, screen } from "@testing-library/react";
import { Ui依赖装配 } from "../../src/应用/依赖装配";
import { Ui任务上下文 } from "../../src/功能/上下文";
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
});
const 有效快照 = {
snapshot_id: "snap-1",
task_id: "task-1",
work_id: "work-1",
input_hash: "a".repeat(64),
state: "有效",
invalidation: null,
changes: [] as string[],
assembly: {
included_sources: ["目标章细纲", "历史:ch-1"],
text_hash: "b".repeat(64),
byte_size: 328,
omissions: [{ source: "参考:超长素材", reason: "BUDGET_EXCEEDED" }],
},
sources: [
{
source_id: "doc-1",
data_version: "1",
schema_hash: "c".repeat(64),
projection_version: "visible-text-codepoints/v1:production:generation",
current_state: "有效",
},
],
};
function 桩快照(快照: unknown) {
return vi.fn(async (request: Request) => {
expect(new URL(request.url).pathname).toBe(
"/api/v1/tasks/task-1/context-snapshots",
);
return new Response(JSON.stringify(快照), {
headers: { "Content-Type": "application/json" },
});
});
}
it("NC-ui-context-snapshot-list:材料清单与省略原因渲染", async () => {
vi.stubGlobal("fetch", 桩快照([有效快照]));
render(
<Ui依赖装配>
<Ui任务上下文 taskId="task-1" scope="work-1" />
</Ui依赖装配>,
);
expect(
await screen.findByText("目标章细纲", { selector: ".step-list strong" }),
).toBeTruthy();
expect(screen.getByText("历史:ch-1")).toBeTruthy();
expect(
screen.getByText("省略原因:超出材料预算,整项省略"),
).toBeTruthy();
expect(screen.getByText(/材料 328 字节/)).toBeTruthy();
expect(screen.getByText(new RegExp("b".repeat(64)))).toBeTruthy();
expect(screen.getByText("doc-1")).toBeTruthy();
expect(screen.getByText("版本 1")).toBeTruthy();
expect(
screen.getByText(/visible-text-codepoints\/v1:production:generation/),
).toBeTruthy();
expect(screen.getByText("仍有效")).toBeTruthy();
expect(screen.queryByRole("status")).toBeNull();
});
it("NC-ui-context-stale-evidence:来源漂移后失效标注与具体变化可见", async () => {
const 漂移快照 = {
...有效快照,
state: "SOURCE_STALE",
invalidation: "文稿 ch-1 已漂移(当前版本 2,冻结版本 1);冻结范围当前失效,需重新授权",
changes: ["文稿 ch-1 已从版本 1 前进到 2;来源锁定失效"],
sources: [
{ ...有效快照.sources[0], data_version: "1", current_state: "SOURCE_STALE" },
],
};
vi.stubGlobal("fetch", 桩快照([漂移快照]));
render(
<Ui依赖装配>
<Ui任务上下文 taskId="task-1" scope="work-1" />
</Ui依赖装配>,
);
expect(await screen.findByText("来源已漂移,需重新授权")).toBeTruthy();
expect(screen.getByText(/文稿 ch-1 已漂移/)).toBeTruthy();
expect(
screen.getByText("文稿 ch-1 已从版本 1 前进到 2;来源锁定失效"),
).toBeTruthy();
expect(screen.getByText("已漂移")).toBeTruthy();
// 失效不改写历史装配:清单与省略原因仍按冻结时原样显示。
expect(screen.getByText("历史:ch-1")).toBeTruthy();
expect(screen.getByText("省略原因:超出材料预算,整项省略")).toBeTruthy();
});
it("NC-ui-context-empty-state:无快照任务如实显示空态", async () => {
vi.stubGlobal("fetch", 桩快照([]));
render(
<Ui依赖装配>
<Ui任务上下文 taskId="task-1" scope={null} />
</Ui依赖装配>,
);
expect(
await screen.findByText("这个任务还没有冻结材料快照"),
).toBeTruthy();
expect(screen.getByText("0 份冻结快照")).toBeTruthy();
});