useAssistantPreviewCardsForRow.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // @vitest-environment jsdom
  2. import { renderHook, waitFor } from "@testing-library/react";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { AssistantTextRow } from "@zcode/shared/zcode-protocol-v4";
  5. import { useAssistantPreviewCardsForAssistantTextRow } from "@/v4/useAssistantPreviewCardsForRow.js";
  6. const { extractAssistantFileReferencesSpy } = vi.hoisted(() => ({
  7. extractAssistantFileReferencesSpy: vi.fn(),
  8. }));
  9. vi.mock("@/lib/assistantFileReferences.js", async (importOriginal) => {
  10. const actual = await importOriginal<typeof import("@/lib/assistantFileReferences.js")>();
  11. const extractAssistantFileReferences: typeof actual.extractAssistantFileReferences = (
  12. content,
  13. workspacePath,
  14. ) => {
  15. extractAssistantFileReferencesSpy(content, workspacePath);
  16. return actual.extractAssistantFileReferences(content, workspacePath);
  17. };
  18. return {
  19. ...actual,
  20. extractAssistantFileReferences,
  21. hasAssistantPreviewFileChangeCandidates: (content: string, workspacePath: string) =>
  22. extractAssistantFileReferences(content, workspacePath).some(
  23. (reference) => reference.kind === "markdown" || reference.kind === "html",
  24. ),
  25. };
  26. });
  27. function assistantText(text: string): AssistantTextRow {
  28. return {
  29. rowId: 2,
  30. entityId: "assistant-2",
  31. turnId: "turn-1",
  32. createdAt: 2,
  33. createdAtSeq: 2,
  34. kind: "assistantText",
  35. state: "complete",
  36. text,
  37. };
  38. }
  39. describe("useAssistantPreviewCardsForAssistantTextRow", () => {
  40. it("异步读取本轮 fileChanges 后只放行 active Markdown/HTML 卡片", async () => {
  41. const row = assistantText("README.md report.pdf index.html");
  42. const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
  43. const fetchFileChanges = vi.fn(async () => ({
  44. state: "active" as const,
  45. items: [
  46. { path: "/workspace/README.md", patches: [] },
  47. { path: "/workspace/index.html", patches: [] },
  48. ],
  49. }));
  50. const { result } = renderHook(() =>
  51. useAssistantPreviewCardsForAssistantTextRow({
  52. row,
  53. assistantTextRows: [row],
  54. latestAssistantTextRow: row,
  55. workspacePath: "/workspace",
  56. fileChangesTarget,
  57. fileChangesState: "active",
  58. fetchFileChanges,
  59. }),
  60. );
  61. await waitFor(() => {
  62. expect(result.current.map((card) => card.title)).toEqual([
  63. "index.html",
  64. "report.pdf",
  65. "README.md",
  66. ]);
  67. });
  68. expect(fetchFileChanges).toHaveBeenCalledWith(fileChangesTarget, {
  69. cachePolicy: "terminal",
  70. fileChangesState: "active",
  71. });
  72. });
  73. it("fileChanges 从 active 切为 reverted 后立即清除 Markdown/HTML 卡片", async () => {
  74. const row = assistantText("README.md report.pdf index.html");
  75. const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
  76. const fetchFileChanges = vi.fn(async () => ({
  77. state: "active" as const,
  78. items: [
  79. { path: "/workspace/README.md", patches: [] },
  80. { path: "/workspace/index.html", patches: [] },
  81. ],
  82. }));
  83. const { result, rerender } = renderHook(
  84. ({ fileChangesState }: { fileChangesState: "active" | "reverted" }) =>
  85. useAssistantPreviewCardsForAssistantTextRow({
  86. row,
  87. assistantTextRows: [row],
  88. latestAssistantTextRow: row,
  89. workspacePath: "/workspace",
  90. fileChangesTarget,
  91. fileChangesState,
  92. fetchFileChanges,
  93. }),
  94. { initialProps: { fileChangesState: "active" as const } },
  95. );
  96. await waitFor(() => {
  97. expect(result.current.map((card) => card.title)).toEqual([
  98. "index.html",
  99. "report.pdf",
  100. "README.md",
  101. ]);
  102. });
  103. rerender({ fileChangesState: "reverted" });
  104. expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
  105. expect(fetchFileChanges).toHaveBeenCalledTimes(1);
  106. });
  107. it("缺少 turnHeader target 时不使用 assistant row 查询 fileChanges", async () => {
  108. const row = assistantText("index.html report.pdf");
  109. const fetchFileChanges = vi.fn();
  110. const { result } = renderHook(() =>
  111. useAssistantPreviewCardsForAssistantTextRow({
  112. row,
  113. assistantTextRows: [row],
  114. latestAssistantTextRow: row,
  115. workspacePath: "/workspace",
  116. fileChangesTarget: null,
  117. fetchFileChanges,
  118. }),
  119. );
  120. await waitFor(() => {
  121. expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
  122. });
  123. expect(fetchFileChanges).not.toHaveBeenCalled();
  124. });
  125. it("fileChanges 不可用时只抑制 Markdown/HTML,不影响 Office/PDF", async () => {
  126. const row = assistantText("README.md report.docx paper.pdf");
  127. const { result } = renderHook(() =>
  128. useAssistantPreviewCardsForAssistantTextRow({
  129. row,
  130. assistantTextRows: [row],
  131. latestAssistantTextRow: row,
  132. workspacePath: "/workspace",
  133. fileChangesTarget: null,
  134. }),
  135. );
  136. await waitFor(() => {
  137. expect(result.current.map((card) => card.title)).toEqual(["paper.pdf", "report.docx"]);
  138. });
  139. });
  140. it("Web 远控隐藏本地 URL 和 HTML,同时保留本轮 Markdown 与 Office/PDF", async () => {
  141. const row = assistantText("http://localhost:5173 README.md index.html report.docx paper.pdf");
  142. const fileChangesTarget = { rowId: 1, entityId: "turn-header-1" };
  143. const fetchFileChanges = vi.fn(async () => ({
  144. state: "active" as const,
  145. items: [
  146. { path: "/workspace/README.md", patches: [] },
  147. { path: "/workspace/index.html", patches: [] },
  148. ],
  149. }));
  150. const { result } = renderHook(() =>
  151. useAssistantPreviewCardsForAssistantTextRow({
  152. row,
  153. assistantTextRows: [row],
  154. latestAssistantTextRow: row,
  155. workspacePath: "/workspace",
  156. fileChangesTarget,
  157. fileChangesState: "active",
  158. fetchFileChanges,
  159. compactForRemoteControl: true,
  160. }),
  161. );
  162. await waitFor(() => {
  163. expect(result.current.map((card) => card.title)).toEqual([
  164. "paper.pdf",
  165. "report.docx",
  166. "README.md",
  167. ]);
  168. });
  169. });
  170. it("Web 远控只有本地 URL 和 HTML 候选时不读取 fileChanges", () => {
  171. const row = assistantText("http://127.0.0.1:4173 index.html report.pdf");
  172. const fetchFileChanges = vi.fn();
  173. const { result } = renderHook(() =>
  174. useAssistantPreviewCardsForAssistantTextRow({
  175. row,
  176. assistantTextRows: [row],
  177. latestAssistantTextRow: row,
  178. workspacePath: "/workspace",
  179. fileChangesTarget: { rowId: 1, entityId: "turn-header-1" },
  180. fileChangesState: "active",
  181. fetchFileChanges,
  182. compactForRemoteControl: true,
  183. }),
  184. );
  185. expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
  186. expect(fetchFileChanges).not.toHaveBeenCalled();
  187. });
  188. it("流式行不提前查询 fileChanges", () => {
  189. const row = { ...assistantText("README.md"), state: "streaming" as const };
  190. const fetchFileChanges = vi.fn();
  191. const { result } = renderHook(() =>
  192. useAssistantPreviewCardsForAssistantTextRow({
  193. row,
  194. assistantTextRows: [row],
  195. latestAssistantTextRow: row,
  196. workspacePath: "/workspace",
  197. fileChangesTarget: null,
  198. fetchFileChanges,
  199. }),
  200. );
  201. expect(result.current).toEqual([]);
  202. expect(fetchFileChanges).not.toHaveBeenCalled();
  203. });
  204. it("相同文本使用新的 rows 引用重渲染时不重复抽取文件", () => {
  205. extractAssistantFileReferencesSpy.mockClear();
  206. const initialRow = assistantText("report.pdf");
  207. const { result, rerender } = renderHook(
  208. ({ currentRow }: { currentRow: AssistantTextRow }) =>
  209. useAssistantPreviewCardsForAssistantTextRow({
  210. row: currentRow,
  211. assistantTextRows: [currentRow],
  212. latestAssistantTextRow: currentRow,
  213. workspacePath: "/workspace",
  214. fileChangesTarget: null,
  215. }),
  216. { initialProps: { currentRow: initialRow } },
  217. );
  218. expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
  219. rerender({ currentRow: { ...initialRow } });
  220. expect(result.current.map((card) => card.title)).toEqual(["report.pdf"]);
  221. expect(extractAssistantFileReferencesSpy).toHaveBeenCalledTimes(1);
  222. });
  223. });