assistantFilePreviewV2.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import { describe, expect, it, vi } from "vitest";
  2. import { shouldRenderAssistantPreviewCardAsFile } from "@/AssistantPreviewCards.js";
  3. import {
  4. ASSISTANT_PREVIEW_CARD_CANDIDATE_LIMIT,
  5. ASSISTANT_PREVIEW_CARD_VISIBLE_LIMIT,
  6. buildAssistantPreviewCards,
  7. buildAssistantPreviewCardsFromReferences,
  8. extractAssistantFileReferences,
  9. shouldOpenAssistantHtmlInBrowser,
  10. } from "@/lib/assistantPreviewCards.js";
  11. import { resolveValidatedAssistantPreviewCards } from "@/lib/assistantPreviewCardValidation.js";
  12. describe("assistant file extraction", () => {
  13. it("extracts the first-release audio and video formats", () => {
  14. const references = extractAssistantFileReferences(
  15. "Created clip.mp4, clip.mov, clip.webm, clip.m4v, song.mp3, song.wav, song.m4a, song.ogg, song.opus, song.flac and song.weba.",
  16. "/workspace",
  17. );
  18. expect(references.map(({ kind, path }) => ({ kind, path }))).toEqual([
  19. { kind: "video", path: "/workspace/clip.mp4" },
  20. { kind: "video", path: "/workspace/clip.mov" },
  21. { kind: "video", path: "/workspace/clip.webm" },
  22. { kind: "video", path: "/workspace/clip.m4v" },
  23. { kind: "audio", path: "/workspace/song.mp3" },
  24. { kind: "audio", path: "/workspace/song.wav" },
  25. { kind: "audio", path: "/workspace/song.m4a" },
  26. { kind: "audio", path: "/workspace/song.ogg" },
  27. { kind: "audio", path: "/workspace/song.opus" },
  28. { kind: "audio", path: "/workspace/song.flac" },
  29. { kind: "audio", path: "/workspace/song.weba" },
  30. ]);
  31. });
  32. it("正文和卡片共享可扩展的 Markdown、HTML、Office 与 PDF 文件抽取", () => {
  33. const references = extractAssistantFileReferences(
  34. "Created README.md, page.html, report.docx, data.xlsx, deck.pptx and paper.pdf.",
  35. "/workspace",
  36. );
  37. expect(references.map(({ kind, path }) => ({ kind, path }))).toEqual([
  38. { kind: "markdown", path: "/workspace/README.md" },
  39. { kind: "html", path: "/workspace/page.html" },
  40. { kind: "docx", path: "/workspace/report.docx" },
  41. { kind: "xlsx", path: "/workspace/data.xlsx" },
  42. { kind: "pptx", path: "/workspace/deck.pptx" },
  43. { kind: "pdf", path: "/workspace/paper.pdf" },
  44. ]);
  45. });
  46. it("citation 只向卡片抽取 DOCX、XLSX、PPTX 和 PDF,且不会重复抽取内部路径", async () => {
  47. const content = [
  48. '::zcode-file-citation{path="docs/项目 简介.docx" purpose="output" artifact_kind="document" page_number=2}',
  49. ':zcode-file-citation{path="sheets/年度 总结.xlsx" purpose="output"}',
  50. '::zcode-file-citation{path="slides/产品 路线.pptx" artifact_kind="presentation" slide_number=3}',
  51. '::zcode-file-citation{path="reports/审计 报告.pdf"}',
  52. ':::zcode-file-citation{path="reports/论文.pdf"}',
  53. '::zcode-file-citation{path="docs/说明.md"}',
  54. '::zcode-file-citation{path="reports/冲突.pdf" artifact_kind="document"}',
  55. '::zcode-file-citation{path="foo/../../private.pdf"}',
  56. '::zcode-file-citation{file="reports/伪装.pdf"}',
  57. ].join("\n");
  58. const references = extractAssistantFileReferences(content, "/workspace");
  59. expect(references.map(({ kind, path, raw }) => ({ kind, path, raw }))).toEqual([
  60. {
  61. kind: "docx",
  62. path: "/workspace/docs/项目 简介.docx",
  63. raw: "docs/项目 简介.docx",
  64. },
  65. {
  66. kind: "xlsx",
  67. path: "/workspace/sheets/年度 总结.xlsx",
  68. raw: "sheets/年度 总结.xlsx",
  69. },
  70. {
  71. kind: "pptx",
  72. path: "/workspace/slides/产品 路线.pptx",
  73. raw: "slides/产品 路线.pptx",
  74. },
  75. {
  76. kind: "pdf",
  77. path: "/workspace/reports/审计 报告.pdf",
  78. raw: "reports/审计 报告.pdf",
  79. },
  80. {
  81. kind: "pdf",
  82. path: "/workspace/reports/论文.pdf",
  83. raw: "reports/论文.pdf",
  84. },
  85. ]);
  86. const cards = buildAssistantPreviewCards(content, "/workspace");
  87. expect(cards.map(({ title }) => title)).toEqual([
  88. "论文.pdf",
  89. "审计 报告.pdf",
  90. "产品 路线.pptx",
  91. "年度 总结.xlsx",
  92. "项目 简介.docx",
  93. ]);
  94. const checkFilesExist = vi.fn(async ({ paths }: { paths: string[] }) =>
  95. paths.map((path) => ({ path, exists: true })),
  96. );
  97. await resolveValidatedAssistantPreviewCards(cards, { checkFilesExist });
  98. expect(checkFilesExist).toHaveBeenCalledWith({
  99. paths: [
  100. "/workspace/reports/论文.pdf",
  101. "/workspace/reports/审计 报告.pdf",
  102. "/workspace/slides/产品 路线.pptx",
  103. "/workspace/sheets/年度 总结.xlsx",
  104. "/workspace/docs/项目 简介.docx",
  105. ],
  106. });
  107. });
  108. it("citation 支持 video/audio artifact_kind 且要求后缀兼容", () => {
  109. const content = [
  110. '::zcode-file-citation{path="media/片段.mp4" artifact_kind="video"}',
  111. '::zcode-file-citation{path="media/声音.wav" artifact_kind="audio"}',
  112. '::zcode-file-citation{path="media/错误.mp3" artifact_kind="video"}',
  113. ].join("\n");
  114. expect(
  115. extractAssistantFileReferences(content, "/workspace").map(({ kind, path }) => ({
  116. kind,
  117. path,
  118. })),
  119. ).toEqual([
  120. { kind: "video", path: "/workspace/media/片段.mp4" },
  121. { kind: "audio", path: "/workspace/media/声音.wav" },
  122. ]);
  123. });
  124. it("支持智能引号 citation 和普通文件路径预览卡片", () => {
  125. const content = [
  126. "::zcode-file-citation{path=“reports/冰岛 行程.pdf” purpose=‘output’}",
  127. "普通输出 ‘docs/说明.md’ 和 “public/index.html”。",
  128. "Markdown 链接 [文档](<“docs/说明 文档.md”>)。",
  129. ].join("\n");
  130. expect(
  131. extractAssistantFileReferences(content, "/workspace").map(({ kind, path, raw }) => ({
  132. kind,
  133. path,
  134. raw,
  135. })),
  136. ).toEqual([
  137. {
  138. kind: "pdf",
  139. path: "/workspace/reports/冰岛 行程.pdf",
  140. raw: "reports/冰岛 行程.pdf",
  141. },
  142. {
  143. kind: "markdown",
  144. path: "/workspace/docs/说明.md",
  145. raw: "docs/说明.md",
  146. },
  147. {
  148. kind: "html",
  149. path: "/workspace/public/index.html",
  150. raw: "public/index.html",
  151. },
  152. {
  153. kind: "markdown",
  154. path: "/workspace/docs/说明 文档.md",
  155. raw: "docs/说明 文档.md",
  156. },
  157. ]);
  158. });
  159. it("普通路径错配引号时不回退为可预览文件", () => {
  160. for (const content of [
  161. '输出 ‘docs/report.pdf"。',
  162. "输出 ”docs/report.pdf”。",
  163. "输出 ’docs/report.pdf’。",
  164. "输出 “docs/report.pdf“。",
  165. "输出 “docs/report.pdf。",
  166. "输出 docs/report.pdf”。",
  167. "这是“重点.md。",
  168. '输出 ::zcode-file-citation{path=‘docs/report.pdf"}',
  169. ]) {
  170. expect(extractAssistantFileReferences(content, "/workspace")).toEqual([]);
  171. }
  172. });
  173. it("用同一规则抽取 Unicode、空格和 Windows 文件路径", () => {
  174. const references = extractAssistantFileReferences(
  175. [
  176. "生成了 **`dist/排队棋-Apagos.pptx`**。",
  177. "归档为 `资料 归档/年度 报告.pdf` 和 `年度 总结.xlsx`。",
  178. "Windows 路径是 C:\\Users\\张三\\演示 文稿.pptx。",
  179. "同时更新 docs/产品 说明.md 和 public/首页 index.html。",
  180. ].join("\n"),
  181. "/workspace",
  182. );
  183. expect(references.map(({ kind, path, raw }) => ({ kind, path, raw }))).toEqual([
  184. {
  185. kind: "pptx",
  186. path: "/workspace/dist/排队棋-Apagos.pptx",
  187. raw: "dist/排队棋-Apagos.pptx",
  188. },
  189. {
  190. kind: "pdf",
  191. path: "/workspace/资料 归档/年度 报告.pdf",
  192. raw: "资料 归档/年度 报告.pdf",
  193. },
  194. {
  195. kind: "xlsx",
  196. path: "/workspace/年度 总结.xlsx",
  197. raw: "年度 总结.xlsx",
  198. },
  199. {
  200. kind: "pptx",
  201. path: "C:/Users/张三/演示 文稿.pptx",
  202. raw: "C:\\Users\\张三\\演示 文稿.pptx",
  203. },
  204. {
  205. kind: "markdown",
  206. path: "/workspace/docs/产品 说明.md",
  207. raw: "docs/产品 说明.md",
  208. },
  209. {
  210. kind: "html",
  211. path: "/workspace/public/首页 index.html",
  212. raw: "public/首页 index.html",
  213. },
  214. ]);
  215. });
  216. it("使用当前 Host Home 展开 Home-relative Office 路径", () => {
  217. const references = extractAssistantFileReferences(
  218. "生成 `~/Desktop/GLM-5.3-Flash 模型能力介绍.pptx`。",
  219. "/workspace",
  220. { homePath: "/Users/demo" },
  221. );
  222. expect(references).toMatchObject([
  223. {
  224. kind: "pptx",
  225. path: "/Users/demo/Desktop/GLM-5.3-Flash 模型能力介绍.pptx",
  226. raw: "~/Desktop/GLM-5.3-Flash 模型能力介绍.pptx",
  227. },
  228. ]);
  229. });
  230. it("Home-relative PPTX 在 Host 存在性校验通过后展示卡片", async () => {
  231. const cards = buildAssistantPreviewCards(
  232. "生成 `~/Desktop/GLM-5.3-Flash 模型能力介绍.pptx`。",
  233. "/workspace",
  234. { homePath: "/Users/demo" },
  235. );
  236. const visibleCards = await resolveValidatedAssistantPreviewCards(cards, {
  237. checkFilesExist: async ({ paths }) => paths.map((path) => ({ path, exists: true })),
  238. });
  239. expect(visibleCards).toMatchObject([
  240. {
  241. kind: "pptx",
  242. path: "/Users/demo/Desktop/GLM-5.3-Flash 模型能力介绍.pptx",
  243. },
  244. ]);
  245. });
  246. it("Host Home 未就绪时不把 ~/ 路径错误拼入 workspace", () => {
  247. expect(extractAssistantFileReferences("生成 `~/Desktop/deck.pptx`。", "/workspace")).toEqual(
  248. [],
  249. );
  250. });
  251. it("相对路径越界时不生成卡片或存在性探测,安全回退路径先规范化", async () => {
  252. const content = ["foo/../../private.pdf", "./a/../report.pdf", "dir\\..\\..\\private.pdf"].join(
  253. " ",
  254. );
  255. expect(extractAssistantFileReferences(content, "/workspace")).toEqual([
  256. {
  257. kind: "pdf",
  258. path: "/workspace/report.pdf",
  259. raw: "./a/../report.pdf",
  260. start: 22,
  261. end: 39,
  262. },
  263. ]);
  264. const cards = buildAssistantPreviewCards(content, "/workspace");
  265. const checkFilesExist = vi.fn(async ({ paths }: { paths: string[] }) =>
  266. paths.map((path) => ({ path, exists: true })),
  267. );
  268. await resolveValidatedAssistantPreviewCards(cards, { checkFilesExist });
  269. expect(cards.map((card) => card.type === "file" && card.path)).toEqual([
  270. "/workspace/report.pdf",
  271. ]);
  272. expect(checkFilesExist).toHaveBeenCalledTimes(1);
  273. expect(checkFilesExist).toHaveBeenCalledWith({
  274. paths: ["/workspace/report.pdf"],
  275. });
  276. });
  277. it("Unicode 和空格 Markdown/HTML 仍使用本轮 fileChanges 门控", () => {
  278. const cards = buildAssistantPreviewCards(
  279. "生成 docs/产品 说明.md、public/首页 index.html 和 dist/排队棋-Apagos.pptx。",
  280. "/workspace",
  281. {
  282. changedFilePaths: ["/workspace/docs/产品 说明.md", "/workspace/public/首页 index.html"],
  283. },
  284. );
  285. expect(cards.map(({ title }) => title)).toEqual([
  286. "排队棋-Apagos.pptx",
  287. "首页 index.html",
  288. "产品 说明.md",
  289. ]);
  290. });
  291. it("卡片按正文出现位置逆序,Markdown/HTML 只保留本轮 active fileChanges", () => {
  292. const cards = buildAssistantPreviewCards(
  293. "README.md report.docx page.html deck.pptx data.xlsx paper.pdf missing.md old.html",
  294. "/workspace",
  295. {
  296. changedFilePaths: ["/workspace/README.md", "/workspace/page.html"],
  297. },
  298. );
  299. expect(cards.map((card) => card.title)).toEqual([
  300. "paper.pdf",
  301. "data.xlsx",
  302. "deck.pptx",
  303. "page.html",
  304. "report.docx",
  305. "README.md",
  306. ]);
  307. });
  308. it("Web 远控不展示本地 URL 和 HTML 卡片,但保留 Markdown、Office 与 PDF", () => {
  309. const cards = buildAssistantPreviewCards(
  310. [
  311. "预览 http://localhost:5173 和 http://127.0.0.1:4173。",
  312. "生成 README.md、index.html、report.docx、data.xlsx、deck.pptx 和 paper.pdf。",
  313. ].join(" "),
  314. "/workspace",
  315. {
  316. changedFilePaths: ["/workspace/README.md", "/workspace/index.html"],
  317. suppressWebRemoteCards: true,
  318. },
  319. );
  320. expect(cards.map(({ title }) => title)).toEqual([
  321. "paper.pdf",
  322. "deck.pptx",
  323. "data.xlsx",
  324. "report.docx",
  325. "README.md",
  326. ]);
  327. expect(cards.every((card) => card.type !== "website")).toBe(true);
  328. });
  329. it("预抽取引用可直接复用于构卡,不再重新扫描正文", () => {
  330. const cards = buildAssistantPreviewCardsFromReferences("文件已经生成。", "/workspace", [
  331. {
  332. kind: "pdf",
  333. path: "/workspace/年度 报告.pdf",
  334. raw: "年度 报告.pdf",
  335. start: 0,
  336. end: 9,
  337. },
  338. ]);
  339. expect(cards.map(({ title }) => title)).toEqual(["年度 报告.pdf"]);
  340. });
  341. it("只把逆序后的前 15 个候选交给存在性校验", () => {
  342. const content = Array.from({ length: 16 }, (_, index) => `file-${index + 1}.pdf`).join(" ");
  343. const cards = buildAssistantPreviewCards(content, "/workspace");
  344. expect(ASSISTANT_PREVIEW_CARD_CANDIDATE_LIMIT).toBe(15);
  345. expect(cards).toHaveLength(15);
  346. expect(cards[0]?.title).toBe("file-16.pdf");
  347. expect(cards.at(-1)?.title).toBe("file-2.pdf");
  348. });
  349. });
  350. describe("assistant preview validation", () => {
  351. it("单次批量校验并最多返回 10 个有效卡片", async () => {
  352. const cards = buildAssistantPreviewCards(
  353. Array.from({ length: 15 }, (_, index) => `file-${index + 1}.pdf`).join(" "),
  354. "/workspace",
  355. );
  356. const checkFilesExist = vi.fn(async ({ paths }: { paths: string[] }) =>
  357. paths.map((path) => ({ path, exists: true })),
  358. );
  359. const result = await resolveValidatedAssistantPreviewCards(cards, {
  360. checkFilesExist,
  361. });
  362. expect(ASSISTANT_PREVIEW_CARD_VISIBLE_LIMIT).toBe(10);
  363. expect(checkFilesExist).toHaveBeenCalledTimes(1);
  364. expect(checkFilesExist.mock.calls[0]?.[0].paths).toHaveLength(15);
  365. expect(result).toHaveLength(10);
  366. expect(result[0]?.title).toBe("file-15.pdf");
  367. expect(result.at(-1)?.title).toBe("file-6.pdf");
  368. });
  369. it("校验失败不从第 16 个候选回填", async () => {
  370. const cards = buildAssistantPreviewCards(
  371. Array.from({ length: 16 }, (_, index) => `file-${index + 1}.pdf`).join(" "),
  372. "/workspace",
  373. );
  374. const result = await resolveValidatedAssistantPreviewCards(cards, {
  375. checkFilesExist: async ({ paths }) =>
  376. paths.map((path, index) => ({ path, exists: index >= 6 })),
  377. });
  378. expect(result).toHaveLength(9);
  379. expect(result.some((card) => card.title === "file-1.pdf")).toBe(false);
  380. });
  381. });
  382. describe("assistant HTML open target", () => {
  383. it("本地桌面使用 Browser,远程 workspace 和手机远控降级为 Preview Pane", () => {
  384. expect(shouldOpenAssistantHtmlInBrowser({ path: "/workspace/page.html" })).toBe(true);
  385. expect(
  386. shouldOpenAssistantHtmlInBrowser({
  387. path: "/workspace/page.html",
  388. workspaceIdentity: "remote:ssh:host:/workspace",
  389. workspaceRemoteSessionId: "remote-1",
  390. }),
  391. ).toBe(false);
  392. expect(
  393. shouldOpenAssistantHtmlInBrowser({
  394. path: "/workspace/page.html",
  395. compactForRemoteControl: true,
  396. }),
  397. ).toBe(false);
  398. });
  399. it("远程只降级 file:// HTML,localhost HTTP 仍使用 Browser", () => {
  400. const remoteScope = {
  401. workspaceIdentity: "remote:ssh:host:/workspace",
  402. workspaceRemoteSessionId: "remote-1",
  403. };
  404. expect(
  405. shouldRenderAssistantPreviewCardAsFile(
  406. {
  407. id: "website:file:///workspace/page.html",
  408. type: "website",
  409. title: "page.html",
  410. subtitleId: "chat.previewCards.htmlWebsite",
  411. url: "file:///workspace/page.html",
  412. filePath: "/workspace/page.html",
  413. },
  414. remoteScope,
  415. ),
  416. ).toBe(true);
  417. expect(
  418. shouldRenderAssistantPreviewCardAsFile(
  419. {
  420. id: "website:http://localhost:5173/page.html",
  421. type: "website",
  422. title: "page.html",
  423. subtitleId: "chat.previewCards.website",
  424. url: "http://localhost:5173/page.html",
  425. filePath: "/workspace/page.html",
  426. },
  427. remoteScope,
  428. ),
  429. ).toBe(false);
  430. });
  431. });