conversationShareImportNotice.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ConversationShareImportNotice } from "@/v4/ConversationShareImportNotice.js";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. vi.mock("@/OpenSplitButton.js", async () => {
  7. const React = await import("react");
  8. return {
  9. OpenSplitButton: (props: Record<string, unknown>) => {
  10. const target = props.target as { path?: string } | undefined;
  11. return React.createElement("button", {
  12. type: "button",
  13. "data-open-split-button": "true",
  14. "data-target-path": target?.path ?? "",
  15. });
  16. },
  17. };
  18. });
  19. const rows = [
  20. {
  21. rowId: 1,
  22. turnId: "turn-1",
  23. productTurnId: "product-turn-1",
  24. createdAt: 1,
  25. createdAtSeq: 1,
  26. kind: "turnHeader" as const,
  27. origin: "userInput" as const,
  28. state: "completedSuccess" as const,
  29. startedAt: 1,
  30. },
  31. {
  32. rowId: 2,
  33. turnId: "turn-1",
  34. productTurnId: "product-turn-1",
  35. createdAt: 2,
  36. createdAtSeq: 2,
  37. kind: "userInput" as const,
  38. origin: "realUser" as const,
  39. text: "帮我看下这个报错",
  40. },
  41. {
  42. rowId: 3,
  43. turnId: "turn-1",
  44. productTurnId: "product-turn-1",
  45. createdAt: 3,
  46. createdAtSeq: 3,
  47. kind: "assistantText" as const,
  48. text: "根因是 signer 被提前释放",
  49. state: "complete" as const,
  50. },
  51. ];
  52. function render(props: Parameters<typeof ConversationShareImportNotice>[0]) {
  53. return renderToStaticMarkup(
  54. createElement(
  55. ZCodeIntlProvider,
  56. { initialLocale: props.locale },
  57. createElement(ConversationShareImportNotice, props),
  58. ),
  59. );
  60. }
  61. describe("conversation share import notice", () => {
  62. it("可点击与静态分割线均使用导入图标,避免与 Fork 混淆", () => {
  63. for (const onOpenShareUrl of [undefined, () => {}]) {
  64. const html = render({ rows, locale: "en-US", onOpenShareUrl });
  65. expect(html).toContain("lucide-square-arrow-right-enter");
  66. expect(html).not.toContain("lucide-git-branch");
  67. }
  68. });
  69. it("渲染只读的分享对话与分割线", () => {
  70. const html = render({ rows, locale: "zh-CN", onOpenShareUrl: () => {} });
  71. expect(html).toContain('data-conversation-share-import-notice="true"');
  72. expect(html).toContain('data-conversation-share-import-divider="true"');
  73. // 复用分享页渲染器:用户气泡与助手正文都应出现。
  74. expect(html).toContain("帮我看下这个报错");
  75. expect(html).toContain("根因是 signer 被提前释放");
  76. expect(html).toContain("已从分享导入");
  77. });
  78. it("可点击时分割线是 button,不可点击时退化为静态行", () => {
  79. expect(render({ rows, locale: "zh-CN", onOpenShareUrl: () => {} })).toContain(
  80. '<button type="button" data-conversation-share-import-divider="true"',
  81. );
  82. const staticHtml = render({ rows, locale: "zh-CN" });
  83. expect(staticHtml).toContain('data-conversation-share-import-divider="true"');
  84. expect(staticHtml).not.toContain(
  85. '<button type="button" data-conversation-share-import-divider="true"',
  86. );
  87. });
  88. it("分割线文案跟随界面语言", () => {
  89. expect(render({ rows, locale: "en-US" })).toContain("Imported from share");
  90. });
  91. it("不给 artifactUrls:结果物不出现点不开的预览按钮", () => {
  92. // 本地结果物在 .zcode-share/<share-id>/shared-artifacts/ 下,渲染器的预览按钮走
  93. // window.open,给本地路径会点不开;这里与实时时间线的 ArtifactRowView 保持一致。
  94. const html = render({
  95. rows: [
  96. ...rows,
  97. {
  98. rowId: 4,
  99. turnId: "turn-1",
  100. productTurnId: "product-turn-1",
  101. createdAt: 4,
  102. createdAtSeq: 4,
  103. kind: "artifact" as const,
  104. artifactVersionId: "artifact-1",
  105. logicalArtifactKey: "report",
  106. displayName: "报告.pdf",
  107. artifactType: "pdf" as const,
  108. mimeType: "application/pdf",
  109. sizeBytes: 1024,
  110. sha256: "a".repeat(64),
  111. ref: "zcode-artifact://share/artifact-1",
  112. state: "current" as const,
  113. },
  114. ],
  115. locale: "zh-CN",
  116. });
  117. expect(html).toContain("报告.pdf");
  118. expect(html).not.toContain("预览文件");
  119. });
  120. it("给到本地导入路径时,结果物使用正常预览卡片的打开控件", () => {
  121. const html = render({
  122. rows: [
  123. ...rows,
  124. {
  125. rowId: 4,
  126. turnId: "turn-1",
  127. productTurnId: "product-turn-1",
  128. createdAt: 4,
  129. createdAtSeq: 4,
  130. kind: "artifact" as const,
  131. artifactVersionId: "artifact-1",
  132. logicalArtifactKey: "report",
  133. displayName: "报告.pdf",
  134. artifactType: "pdf" as const,
  135. mimeType: "application/pdf",
  136. sizeBytes: 1024,
  137. sha256: "a".repeat(64),
  138. ref: "zcode-artifact://share/artifact-1",
  139. state: "current" as const,
  140. },
  141. ],
  142. locale: "zh-CN",
  143. workspacePath: "/workspace",
  144. artifactWorkspaceRelativePaths: new Map([
  145. ["artifact-1", ".zcode-share/share-1/shared-artifacts/报告.pdf"],
  146. ]),
  147. onOpenCodeViewer: () => undefined,
  148. });
  149. expect(html).toContain('data-open-split-button="true"');
  150. expect(html).toContain(
  151. 'data-target-path="/workspace/.zcode-share/share-1/shared-artifacts/报告.pdf"',
  152. );
  153. });
  154. });