conversationPreviewArtifacts.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildConversationPreviewArtifactCandidates,
  4. type ConversationPreviewFileChange,
  5. } from "../src/conversation-preview-artifacts.js";
  6. const workspacePath = "/workspace";
  7. function fileChange(path: string, overrides: Partial<ConversationPreviewFileChange> = {}) {
  8. return {
  9. path,
  10. state: "active" as const,
  11. ...overrides,
  12. };
  13. }
  14. describe("conversation preview artifact candidates", () => {
  15. it("把 .md 标成 md 类型而不是 html", () => {
  16. // 之前 .md 被标成 artifactType html + mime text/markdown,报错文案因此出现
  17. // 「HTML(.md / text/markdown)」这种组合,也对不上服务端的 md 白名单条目。
  18. const candidates = buildConversationPreviewArtifactCandidates({
  19. productTurnId: "turn-1",
  20. assistantText: "已生成 报告.md",
  21. workspacePath,
  22. fileChanges: [fileChange("/workspace/报告.md")],
  23. });
  24. expect(candidates).toEqual([
  25. expect.objectContaining({
  26. artifactType: "md",
  27. mimeType: "text/markdown",
  28. previewKind: "markdown",
  29. displayName: "报告.md",
  30. }),
  31. ]);
  32. });
  33. it("uses explicit assistant references and ignores unreferenced fileChanges", () => {
  34. const candidates = buildConversationPreviewArtifactCandidates({
  35. productTurnId: "turn-1",
  36. assistantText: "已生成 report.pdf",
  37. workspacePath,
  38. fileChanges: [fileChange("/workspace/report.pdf"), fileChange("/workspace/temp.pdf")],
  39. });
  40. expect(candidates.map((candidate) => candidate.sourceRef)).toEqual(["/workspace/report.pdf"]);
  41. });
  42. it("allows Office/PDF references without fileChanges", () => {
  43. const candidates = buildConversationPreviewArtifactCandidates({
  44. productTurnId: "turn-1",
  45. assistantText: "下载 report.pdf 和 slides.pptx",
  46. workspacePath,
  47. });
  48. expect(candidates.map((candidate) => candidate.displayName)).toEqual([
  49. "slides.pptx",
  50. "report.pdf",
  51. ]);
  52. });
  53. it("只把显式预览引用纳入候选,忽略正文里的 ~/ 路径并支持视频媒体", () => {
  54. const candidates = buildConversationPreviewArtifactCandidates({
  55. productTurnId: "turn-1",
  56. assistantText:
  57. '~/Code/vibe/shows/deliver/晨报.pdf ::zcode-file-citation{path="/workspace/晨报.pdf"} out/a.mp4 out/b.mp4 out/c.mp4',
  58. workspacePath,
  59. });
  60. expect(candidates.map((candidate) => candidate.displayName)).toEqual([
  61. "c.mp4",
  62. "b.mp4",
  63. "a.mp4",
  64. "晨报.pdf",
  65. ]);
  66. expect(candidates.filter((candidate) => candidate.artifactType === "video")).toHaveLength(3);
  67. });
  68. it("requires active fileChanges for Markdown and HTML", () => {
  69. const candidates = buildConversationPreviewArtifactCandidates({
  70. productTurnId: "turn-1",
  71. assistantText: "README.md index.html report.pdf",
  72. workspacePath,
  73. fileChanges: [fileChange("/workspace/README.md"), fileChange("/workspace/index.html")],
  74. });
  75. expect(candidates.map((candidate) => candidate.displayName)).toEqual([
  76. "report.pdf",
  77. "index.html",
  78. "README.md",
  79. ]);
  80. expect(
  81. buildConversationPreviewArtifactCandidates({
  82. productTurnId: "turn-1",
  83. assistantText: "README.md index.html",
  84. workspacePath,
  85. }),
  86. ).toEqual([]);
  87. });
  88. it("rejects escaped paths and unknown extensions", () => {
  89. const candidates = buildConversationPreviewArtifactCandidates({
  90. productTurnId: "turn-1",
  91. assistantText: "../secret.pdf /tmp/other.pdf report.txt",
  92. workspacePath,
  93. });
  94. expect(candidates).toEqual([]);
  95. });
  96. it("deduplicates the same canonical path referenced multiple times", () => {
  97. const candidates = buildConversationPreviewArtifactCandidates({
  98. productTurnId: "turn-1",
  99. assistantText: "report.pdf ./report.pdf",
  100. workspacePath,
  101. });
  102. expect(candidates).toHaveLength(1);
  103. expect(candidates[0]?.sourceRef).toBe("/workspace/report.pdf");
  104. });
  105. it("matches UI order by keeping the latest duplicate before applying the candidate limit", () => {
  106. const candidates = buildConversationPreviewArtifactCandidates({
  107. productTurnId: "turn-1",
  108. assistantText: "report.pdf summary.pdf ./report.pdf",
  109. workspacePath,
  110. });
  111. expect(candidates.map((candidate) => candidate.sourceRef)).toEqual([
  112. "/workspace/report.pdf",
  113. "/workspace/summary.pdf",
  114. ]);
  115. });
  116. it("removes reverted Markdown/HTML candidates but keeps Office/PDF behavior", () => {
  117. const candidates = buildConversationPreviewArtifactCandidates({
  118. productTurnId: "turn-1",
  119. assistantText: "README.md index.html report.pdf",
  120. workspacePath,
  121. fileChanges: [
  122. fileChange("/workspace/README.md", { state: "reverted" }),
  123. fileChange("/workspace/index.html", { state: "reverted" }),
  124. ],
  125. });
  126. expect(candidates.map((candidate) => candidate.displayName)).toEqual(["report.pdf"]);
  127. });
  128. });