conversationShareImportNotice.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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("渲染只读的分享对话与分割线", () => {
  63. const html = render({ rows, locale: "zh-CN", onOpenShareUrl: () => {} });
  64. expect(html).toContain('data-conversation-share-import-notice="true"');
  65. expect(html).toContain('data-conversation-share-import-divider="true"');
  66. // 复用分享页渲染器:用户气泡与助手正文都应出现。
  67. expect(html).toContain("帮我看下这个报错");
  68. expect(html).toContain("根因是 signer 被提前释放");
  69. expect(html).toContain("已从分享导入");
  70. });
  71. it("可点击时分割线是 button,不可点击时退化为静态行", () => {
  72. expect(render({ rows, locale: "zh-CN", onOpenShareUrl: () => {} })).toContain(
  73. '<button type="button" data-conversation-share-import-divider="true"',
  74. );
  75. const staticHtml = render({ rows, locale: "zh-CN" });
  76. expect(staticHtml).toContain('data-conversation-share-import-divider="true"');
  77. expect(staticHtml).not.toContain(
  78. '<button type="button" data-conversation-share-import-divider="true"',
  79. );
  80. });
  81. it("分割线文案跟随界面语言", () => {
  82. expect(render({ rows, locale: "en-US" })).toContain("Imported from share");
  83. });
  84. it("不给 artifactUrls:结果物不出现点不开的预览按钮", () => {
  85. // 本地结果物在 .zcode-share/<share-id>/shared-artifacts/ 下,渲染器的预览按钮走
  86. // window.open,给本地路径会点不开;这里与实时时间线的 ArtifactRowView 保持一致。
  87. const html = render({
  88. rows: [
  89. ...rows,
  90. {
  91. rowId: 4,
  92. turnId: "turn-1",
  93. productTurnId: "product-turn-1",
  94. createdAt: 4,
  95. createdAtSeq: 4,
  96. kind: "artifact" as const,
  97. artifactVersionId: "artifact-1",
  98. logicalArtifactKey: "report",
  99. displayName: "报告.pdf",
  100. artifactType: "pdf" as const,
  101. mimeType: "application/pdf",
  102. sizeBytes: 1024,
  103. sha256: "a".repeat(64),
  104. ref: "zcode-artifact://share/artifact-1",
  105. state: "current" as const,
  106. },
  107. ],
  108. locale: "zh-CN",
  109. });
  110. expect(html).toContain("报告.pdf");
  111. expect(html).not.toContain("预览文件");
  112. });
  113. it("给到本地导入路径时,结果物使用正常预览卡片的打开控件", () => {
  114. const html = render({
  115. rows: [
  116. ...rows,
  117. {
  118. rowId: 4,
  119. turnId: "turn-1",
  120. productTurnId: "product-turn-1",
  121. createdAt: 4,
  122. createdAtSeq: 4,
  123. kind: "artifact" as const,
  124. artifactVersionId: "artifact-1",
  125. logicalArtifactKey: "report",
  126. displayName: "报告.pdf",
  127. artifactType: "pdf" as const,
  128. mimeType: "application/pdf",
  129. sizeBytes: 1024,
  130. sha256: "a".repeat(64),
  131. ref: "zcode-artifact://share/artifact-1",
  132. state: "current" as const,
  133. },
  134. ],
  135. locale: "zh-CN",
  136. workspacePath: "/workspace",
  137. artifactWorkspaceRelativePaths: new Map([
  138. ["artifact-1", ".zcode-share/share-1/shared-artifacts/报告.pdf"],
  139. ]),
  140. onOpenCodeViewer: () => undefined,
  141. });
  142. expect(html).toContain('data-open-split-button="true"');
  143. expect(html).toContain(
  144. 'data-target-path="/workspace/.zcode-share/share-1/shared-artifacts/报告.pdf"',
  145. );
  146. });
  147. });