conversationShareLandingPage.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type { ConversationSharePreview } from "@zcode/shared";
  5. import {
  6. ConversationShareLandingPage,
  7. ConversationShareLandingStatus,
  8. } from "../src/share/ConversationShareLandingPage.js";
  9. const preview: ConversationSharePreview = {
  10. schema_version: 1,
  11. share: {
  12. title: "公开任务",
  13. access_mode: "public_importable",
  14. created_at: 1_000,
  15. expires_at: 2_000,
  16. },
  17. rows: [
  18. {
  19. rowId: 1,
  20. turnId: "share-turn-1",
  21. productTurnId: "share-product-turn-1",
  22. createdAt: 1_000,
  23. createdAtSeq: 1,
  24. kind: "userInput",
  25. text: "请生成一个文件",
  26. origin: "realUser",
  27. },
  28. {
  29. rowId: 2,
  30. turnId: "share-turn-1",
  31. productTurnId: "share-product-turn-1",
  32. createdAt: 1_001,
  33. createdAtSeq: 2,
  34. kind: "assistantText",
  35. text: "已完成 **文件**",
  36. state: "complete",
  37. },
  38. ],
  39. artifacts: [],
  40. integrity: {
  41. projection_sha256: "a".repeat(64),
  42. artifact_set_sha256: "b".repeat(64),
  43. },
  44. };
  45. describe("ConversationShareLandingPage", () => {
  46. it.each([
  47. ["rate_limited", "访问过于频繁", "Too many requests", true],
  48. ["service_unavailable", "分享服务暂时异常", "Sharing service unavailable", true],
  49. ["feature_disabled", "分享服务暂未开放", "Sharing is currently unavailable", false],
  50. ] as const)(
  51. "renders a specific localized action and diagnostics for %s",
  52. (error, zh, en, retryable) => {
  53. for (const locale of ["zh-CN", "en-US"] as const) {
  54. const html = renderToStaticMarkup(
  55. createElement(ConversationShareLandingStatus, {
  56. state: { kind: "error", error, status: 503, code: 3217, requestId: "web-request-123" },
  57. locale,
  58. onRetry: () => {},
  59. }),
  60. );
  61. expect(html).toContain(locale === "zh-CN" ? zh : en);
  62. expect(html).toContain("web-request-123");
  63. expect(html).toContain("3217");
  64. expect(html.includes(locale === "zh-CN" ? ">重试<" : ">Try again<")).toBe(retryable);
  65. expect(html).not.toContain("请检查网络");
  66. }
  67. },
  68. );
  69. it("renders public content and only the importable deep link", () => {
  70. const html = renderToStaticMarkup(
  71. createElement(ConversationShareLandingPage, {
  72. shareCode: "share-1",
  73. preview,
  74. locale: "zh-CN",
  75. }),
  76. );
  77. expect(html).toContain("公开任务");
  78. expect(html).toContain('title="公开任务"');
  79. expect(html).toContain('data-share-header-ready="false"');
  80. expect(html).toContain('data-share-continue-visible="true"');
  81. expect(html).toContain("请生成一个文件");
  82. expect(html).toContain("去 ZCode 继续");
  83. expect(html).toContain('data-share-theme-toggle="true"');
  84. expect(html).toContain("切换到深色主题");
  85. expect(html.match(/href="zcode:\/\/share\/import\?code=share-1"/g)?.length).toBe(2);
  86. expect(html).not.toContain("Sign out");
  87. });
  88. it("counts only artifact rows and excludes user input attachment manifests", () => {
  89. const inputRef = "zcode-artifact://share/input-1";
  90. const resultRef = "zcode-artifact://share/result-1";
  91. const mixedPreview: ConversationSharePreview = {
  92. ...preview,
  93. rows: [
  94. {
  95. ...preview.rows[0]!,
  96. kind: "userInput",
  97. attachments: [
  98. {
  99. ref: inputRef,
  100. previewRef: inputRef,
  101. fileName: "pasted-text.txt",
  102. mime: "text/plain",
  103. bytes: 16_384,
  104. },
  105. ],
  106. },
  107. preview.rows[1]!,
  108. {
  109. rowId: 3,
  110. turnId: "share-turn-1",
  111. productTurnId: "share-product-turn-1",
  112. createdAt: 1_002,
  113. createdAtSeq: 3,
  114. kind: "artifact",
  115. artifactVersionId: "result-1",
  116. logicalArtifactKey: "result-key-1",
  117. displayName: "report.pdf",
  118. artifactType: "pdf",
  119. mimeType: "application/pdf",
  120. sizeBytes: 32_768,
  121. sha256: "c".repeat(64),
  122. ref: resultRef,
  123. state: "current",
  124. },
  125. ],
  126. artifacts: [
  127. {
  128. artifact_id: "input-1",
  129. logical_artifact_key: "input-key-1",
  130. producer_product_turn_id: "share-product-turn-1",
  131. artifact_version: 1,
  132. state: "current",
  133. ref: inputRef,
  134. artifact_type: "text",
  135. display_name: "pasted-text.txt",
  136. extension: "txt",
  137. mime_type: "text/plain",
  138. size_bytes: 16_384,
  139. sha256: "b".repeat(64),
  140. url: "https://example.test/input-1",
  141. url_expires_at: 2_000,
  142. },
  143. {
  144. artifact_id: "result-1",
  145. logical_artifact_key: "result-key-1",
  146. producer_product_turn_id: "share-product-turn-1",
  147. artifact_version: 1,
  148. state: "current",
  149. ref: resultRef,
  150. artifact_type: "pdf",
  151. display_name: "report.pdf",
  152. extension: "pdf",
  153. mime_type: "application/pdf",
  154. size_bytes: 32_768,
  155. sha256: "c".repeat(64),
  156. url: "https://example.test/result-1",
  157. url_expires_at: 2_000,
  158. },
  159. ],
  160. };
  161. const html = renderToStaticMarkup(
  162. createElement(ConversationShareLandingPage, {
  163. shareCode: "share-1",
  164. preview: mixedPreview,
  165. locale: "zh-CN",
  166. }),
  167. );
  168. expect(html).toContain("1 个结果物");
  169. expect(html).not.toContain("2 个结果物");
  170. expect(html).toContain("pasted-text.txt");
  171. expect(html).toContain("report.pdf");
  172. });
  173. it("does not show a result count when the share contains only input attachments", () => {
  174. const inputOnly = {
  175. ...preview,
  176. rows: [
  177. {
  178. ...preview.rows[0]!,
  179. kind: "userInput" as const,
  180. attachments: [
  181. {
  182. ref: "zcode-artifact://share/input-only",
  183. fileName: "input.txt",
  184. mime: "text/plain",
  185. bytes: 5,
  186. },
  187. ],
  188. },
  189. preview.rows[1]!,
  190. ],
  191. artifacts: [
  192. {
  193. artifact_id: "input-only",
  194. logical_artifact_key: "input-only-key",
  195. producer_product_turn_id: "share-product-turn-1",
  196. artifact_version: 1 as const,
  197. state: "current" as const,
  198. ref: "zcode-artifact://share/input-only",
  199. artifact_type: "text" as const,
  200. display_name: "input.txt",
  201. extension: "txt",
  202. mime_type: "text/plain",
  203. size_bytes: 5,
  204. sha256: "d".repeat(64),
  205. url: "https://example.test/input-only",
  206. url_expires_at: 2_000,
  207. },
  208. ],
  209. } satisfies ConversationSharePreview;
  210. const html = renderToStaticMarkup(
  211. createElement(ConversationShareLandingPage, {
  212. shareCode: "share-1",
  213. preview: inputOnly,
  214. locale: "zh-CN",
  215. }),
  216. );
  217. expect(html).not.toContain("个结果物");
  218. });
  219. it("does not offer continuation for readonly shares", () => {
  220. const html = renderToStaticMarkup(
  221. createElement(ConversationShareLandingPage, {
  222. shareCode: "share-1",
  223. preview: { ...preview, share: { ...preview.share, access_mode: "public_readonly" } },
  224. locale: "en-US",
  225. }),
  226. );
  227. expect(html).not.toContain("zcode://share/import");
  228. expect(html).not.toContain("Open in ZCode");
  229. expect(html).not.toContain("去 ZCode 继续");
  230. expect(html).toContain('data-share-theme-toggle="true"');
  231. expect(html).not.toContain('data-share-continue-footer="true"');
  232. expect(html).toContain('data-share-continue-visible="false"');
  233. });
  234. it("renders private login-required status without exposing API details", () => {
  235. const html = renderToStaticMarkup(
  236. createElement(ConversationShareLandingStatus, {
  237. state: { kind: "login_required" },
  238. locale: "en-US",
  239. }),
  240. );
  241. expect(html).toContain("Sign in to view this share");
  242. expect(html).not.toContain("3211");
  243. });
  244. it.each([
  245. ["zh-CN", "账号数据不互通", "回到首页"],
  246. ["en-US", "do not share account data", "Back to home"],
  247. ] as const)(
  248. "offers account guidance and home navigation for not_found in %s",
  249. (locale, hint, home) => {
  250. const html = renderToStaticMarkup(
  251. createElement(ConversationShareLandingStatus, {
  252. state: { kind: "error", error: "not_found" },
  253. locale,
  254. onRetry: () => {},
  255. }),
  256. );
  257. expect(html).toContain(hint);
  258. expect(html).toContain(home);
  259. expect(html).toContain('href="https://zcode.z.ai"');
  260. expect(html).not.toContain('target="_blank"');
  261. },
  262. );
  263. it.each([
  264. "network",
  265. "expired",
  266. "authentication_required",
  267. "invalid_contract",
  268. "unsupported_schema_version",
  269. "rate_limited",
  270. "unknown",
  271. ] as const)("keeps account guidance and home navigation out of %s", (error) => {
  272. const html = renderToStaticMarkup(
  273. createElement(ConversationShareLandingStatus, {
  274. state: { kind: "error", error },
  275. locale: "zh-CN",
  276. onRetry: () => {},
  277. }),
  278. );
  279. expect(html).not.toContain("账号数据不互通");
  280. expect(html).not.toContain("回到首页");
  281. });
  282. });