| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // @vitest-environment jsdom
- import { renderHook, waitFor } from "@testing-library/react";
- import { describe, expect, it, vi } from "vitest";
- import type { AssistantTextRow } from "@zcode/shared/zcode-protocol-v4";
- import { useAssistantPreviewCardsForAssistantTextRow } from "@/v4/useAssistantPreviewCardsForRow.js";
- const { extractAssistantFileReferencesSpy } = vi.hoisted(() => ({
- extractAssistantFileReferencesSpy: vi.fn(),
- }));
- vi.mock("@/lib/assistantFileReferences.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("@/lib/assistantFileReferences.js")>();
- const extractAssistantFileReferences: typeof actual.extractAssistantFileReferences = (
- content,
- workspacePath,
- ) => {
- extractAssistantFileReferencesSpy(content, workspacePath);
- return actual.extractAssistantFileReferences(content, workspacePath);
- };
- return {
- ...actual,
- extractAssistantFileReferences,
- hasAssistantPreviewFileChangeCandidates: (content: string, workspacePath: string) =>
- extractAssistantFileReferences(content, workspacePath).some(
- (reference) => reference.kind === "markdown" || reference.kind === "html",
- ),
- };
- });
- function assistantText(text: string): AssistantTextRow {
- return {
- rowId: 2,
- entityId: "assistant-2",
- turnId: "turn-1",
- createdAt: 2,
- createdAtSeq: 2,
- kind: "assistantText",
- state: "complete",
- text,
- };
- }
- describe("useAssistantPreviewCardsForAssistantTextRow", () => {
- it("异步读取本轮 fileChanges 后只放行 active Markdown/HTML 卡片", async () => {
- const row = assistantText("README.md report.pdf index.html");
- const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
- const fetchFileChanges = vi.fn(async () => ({
- state: "active" as const,
- items: [
- { path: "/workspace/README.md", patches: [] },
- { path: "/workspace/index.html", patches: [] },
- ],
- }));
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget,
- fileChangesState: "active",
- fetchFileChanges,
- }),
- );
- await waitFor(() => {
- expect(result.current.map((card) => card.title)).toEqual([
- "index.html",
- "report.pdf",
- "README.md",
- ]);
- });
- expect(fetchFileChanges).toHaveBeenCalledWith(fileChangesTarget, {
- cachePolicy: "terminal",
- fileChangesState: "active",
- });
- });
- it("fileChanges 从 active 切为 reverted 后立即清除 Markdown/HTML 卡片", async () => {
- const row = assistantText("README.md report.pdf index.html");
- const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
- const fetchFileChanges = vi.fn(async () => ({
- state: "active" as const,
- items: [
- { path: "/workspace/README.md", patches: [] },
- { path: "/workspace/index.html", patches: [] },
- ],
- }));
- const { result, rerender } = renderHook(
- ({ fileChangesState }: { fileChangesState: "active" | "reverted" }) =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget,
- fileChangesState,
- fetchFileChanges,
- }),
- { initialProps: { fileChangesState: "active" as const } },
- );
- await waitFor(() => {
- expect(result.current.map((card) => card.title)).toEqual([
- "index.html",
- "report.pdf",
- "README.md",
- ]);
- });
- rerender({ fileChangesState: "reverted" });
- expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
- expect(fetchFileChanges).toHaveBeenCalledTimes(1);
- });
- it("缺少 turnHeader target 时不使用 assistant row 查询 fileChanges", async () => {
- const row = assistantText("index.html report.pdf");
- const fetchFileChanges = vi.fn();
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget: null,
- fetchFileChanges,
- }),
- );
- await waitFor(() => {
- expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
- });
- expect(fetchFileChanges).not.toHaveBeenCalled();
- });
- it("fileChanges 不可用时只抑制 Markdown/HTML,不影响 Office/PDF", async () => {
- const row = assistantText("README.md report.docx paper.pdf");
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget: null,
- }),
- );
- await waitFor(() => {
- expect(result.current.map((card) => card.title)).toEqual(["paper.pdf", "report.docx"]);
- });
- });
- it("Web 远控隐藏本地 URL 和 HTML,同时保留本轮 Markdown 与 Office/PDF", async () => {
- const row = assistantText("http://localhost:5173 README.md index.html report.docx paper.pdf");
- const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
- const fetchFileChanges = vi.fn(async () => ({
- state: "active" as const,
- items: [
- { path: "/workspace/README.md", patches: [] },
- { path: "/workspace/index.html", patches: [] },
- ],
- }));
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget,
- fileChangesState: "active",
- fetchFileChanges,
- compactForRemoteControl: true,
- }),
- );
- await waitFor(() => {
- expect(result.current.map((card) => card.title)).toEqual([
- "paper.pdf",
- "report.docx",
- "README.md",
- ]);
- });
- });
- it("Web 远控只有本地 URL 和 HTML 候选时不读取 fileChanges", () => {
- const row = assistantText("http://127.0.0.1:4173 index.html report.pdf");
- const fetchFileChanges = vi.fn();
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget: { rowId: 1, entityId: "turn-header-1" },
- fileChangesState: "active",
- fetchFileChanges,
- compactForRemoteControl: true,
- }),
- );
- expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
- expect(fetchFileChanges).not.toHaveBeenCalled();
- });
- it("流式行不提前查询 fileChanges", () => {
- const row = { ...assistantText("README.md"), state: "streaming" as const };
- const fetchFileChanges = vi.fn();
- const { result } = renderHook(() =>
- useAssistantPreviewCardsForAssistantTextRow({
- row,
- assistantTextRows: [row],
- latestAssistantTextRow: row,
- workspacePath: "/workspace",
- fileChangesTarget: null,
- fetchFileChanges,
- }),
- );
- expect(result.current).toEqual([]);
- expect(fetchFileChanges).not.toHaveBeenCalled();
- });
- it("相同文本使用新的 rows 引用重渲染时不重复抽取文件", () => {
- extractAssistantFileReferencesSpy.mockClear();
- const initialRow = assistantText("report.pdf");
- const { result, rerender } = renderHook(
- ({ currentRow }: { currentRow: AssistantTextRow }) =>
- useAssistantPreviewCardsForAssistantTextRow({
- row: currentRow,
- assistantTextRows: [currentRow],
- latestAssistantTextRow: currentRow,
- workspacePath: "/workspace",
- fileChangesTarget: null,
- }),
- { initialProps: { currentRow: initialRow } },
- );
- expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
- rerender({ currentRow: { ...initialRow } });
- expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
- expect(extractAssistantFileReferencesSpy).toHaveBeenCalledTimes(1);
- });
- });
|