conversationShareLandingPage.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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("renders public content and only the importable deep link", () => {
  47. const html = renderToStaticMarkup(
  48. createElement(ConversationShareLandingPage, {
  49. shareCode: "share-1",
  50. preview,
  51. locale: "zh-CN",
  52. }),
  53. );
  54. expect(html).toContain("公开任务");
  55. expect(html).toContain('data-share-header="true"');
  56. expect(html).toContain('data-share-header-shell="true"');
  57. expect(html).toContain('data-share-header-row="true"');
  58. expect(html).toContain('data-share-brand="true"');
  59. expect(html.match(/data-share-content-rail="true"/g)?.length).toBe(2);
  60. expect(html.match(/data-share-content-inset="true"/g)?.length).toBe(3);
  61. expect(html).toContain('data-share-body-rail="true"');
  62. expect(html).toContain('data-share-title="true"');
  63. expect(html).toContain('data-share-continue-slot="true"');
  64. expect(html).toContain('data-share-continue-visible="true"');
  65. expect(html).toContain('data-share-continue-measure="true"');
  66. expect(html).toContain('title="公开任务"');
  67. // 首屏(SSR/未测量)先保留自然排列,客户端测量后只写入连续几何变量;
  68. // 不再在 inline 与 rail-aligned 两种布局之间切换。
  69. expect(html).toContain('data-share-header-layout="continuous"');
  70. expect(html).toContain('data-share-header-ready="false"');
  71. expect(html).not.toContain('data-share-header-layout="compact"');
  72. expect(html).not.toContain("absolute inset-x-0 bottom-0");
  73. expect(html).toContain("bg-background backdrop-blur");
  74. expect(html).not.toContain("bg-background/95");
  75. expect(html).toContain("ZCode");
  76. expect(html).toContain("sticky");
  77. expect(html).toContain("text-left");
  78. expect(html).not.toContain("grid-cols-[minmax(0,1fr)_minmax(0,2fr)_minmax(0,1fr)]");
  79. expect(html).toContain("max-w-4xl");
  80. expect(html).toContain("sm:px-8");
  81. expect(html).toContain("sm:py-10");
  82. expect(html).toContain("data-share-metadata");
  83. expect(html).toContain("请生成一个文件");
  84. expect(html).toContain("data-v4-user-input-bubble");
  85. expect(html).toContain("group/assistant-row");
  86. expect(html).toContain('data-share-scroll-viewport="true"');
  87. expect(html).toContain('data-markdown-table-layout-root="true"');
  88. expect(html).toContain("overflow-y-auto");
  89. expect(html).toContain("去 ZCode 继续");
  90. expect(html).toContain('data-share-theme-toggle="true"');
  91. expect(html).toContain("切换到深色主题");
  92. expect(html.match(/data-share-continue-link="true"/g)?.length).toBe(2);
  93. expect(html).toContain('data-share-continue-footer="true"');
  94. expect(html.match(/href="zcode:\/\/share\/import\?code=share-1"/g)?.length).toBe(2);
  95. expect(html.indexOf('data-share-continue-footer="true"')).toBeGreaterThan(
  96. html.indexOf("data-conversation-share-timeline"),
  97. );
  98. expect(html).toContain("rounded-xl");
  99. expect(html).toContain("bg-primary");
  100. expect(html).toContain("text-primary-foreground");
  101. expect(html).not.toContain("ZCode 会话分享");
  102. expect(html).not.toContain("data-share-flag");
  103. expect(html).not.toContain("下载 ZCode");
  104. expect(html).not.toContain("Sign out");
  105. expect(html).toContain("zcode://share/import?code=share-1");
  106. });
  107. it("counts only artifact rows and excludes user input attachment manifests", () => {
  108. const inputRef = "zcode-artifact://share/input-1";
  109. const resultRef = "zcode-artifact://share/result-1";
  110. const mixedPreview: ConversationSharePreview = {
  111. ...preview,
  112. rows: [
  113. {
  114. ...preview.rows[0]!,
  115. kind: "userInput",
  116. attachments: [
  117. {
  118. ref: inputRef,
  119. previewRef: inputRef,
  120. fileName: "pasted-text.txt",
  121. mime: "text/plain",
  122. bytes: 16_384,
  123. },
  124. ],
  125. },
  126. preview.rows[1]!,
  127. {
  128. rowId: 3,
  129. turnId: "share-turn-1",
  130. productTurnId: "share-product-turn-1",
  131. createdAt: 1_002,
  132. createdAtSeq: 3,
  133. kind: "artifact",
  134. artifactVersionId: "result-1",
  135. logicalArtifactKey: "result-key-1",
  136. displayName: "report.pdf",
  137. artifactType: "pdf",
  138. mimeType: "application/pdf",
  139. sizeBytes: 32_768,
  140. sha256: "c".repeat(64),
  141. ref: resultRef,
  142. state: "current",
  143. },
  144. ],
  145. artifacts: [
  146. {
  147. artifact_id: "input-1",
  148. logical_artifact_key: "input-key-1",
  149. producer_product_turn_id: "share-product-turn-1",
  150. artifact_version: 1,
  151. state: "current",
  152. ref: inputRef,
  153. artifact_type: "text",
  154. display_name: "pasted-text.txt",
  155. extension: "txt",
  156. mime_type: "text/plain",
  157. size_bytes: 16_384,
  158. sha256: "b".repeat(64),
  159. url: "https://example.test/input-1",
  160. url_expires_at: 2_000,
  161. },
  162. {
  163. artifact_id: "result-1",
  164. logical_artifact_key: "result-key-1",
  165. producer_product_turn_id: "share-product-turn-1",
  166. artifact_version: 1,
  167. state: "current",
  168. ref: resultRef,
  169. artifact_type: "pdf",
  170. display_name: "report.pdf",
  171. extension: "pdf",
  172. mime_type: "application/pdf",
  173. size_bytes: 32_768,
  174. sha256: "c".repeat(64),
  175. url: "https://example.test/result-1",
  176. url_expires_at: 2_000,
  177. },
  178. ],
  179. };
  180. const html = renderToStaticMarkup(
  181. createElement(ConversationShareLandingPage, {
  182. shareCode: "share-1",
  183. preview: mixedPreview,
  184. locale: "zh-CN",
  185. }),
  186. );
  187. expect(html).toContain("1 个结果物");
  188. expect(html).not.toContain("2 个结果物");
  189. expect(html).toContain("pasted-text.txt");
  190. expect(html).toContain("report.pdf");
  191. });
  192. it("does not show a result count when the share contains only input attachments", () => {
  193. const inputOnly = {
  194. ...preview,
  195. rows: [
  196. {
  197. ...preview.rows[0]!,
  198. kind: "userInput" as const,
  199. attachments: [
  200. {
  201. ref: "zcode-artifact://share/input-only",
  202. fileName: "input.txt",
  203. mime: "text/plain",
  204. bytes: 5,
  205. },
  206. ],
  207. },
  208. preview.rows[1]!,
  209. ],
  210. artifacts: [
  211. {
  212. artifact_id: "input-only",
  213. logical_artifact_key: "input-only-key",
  214. producer_product_turn_id: "share-product-turn-1",
  215. artifact_version: 1 as const,
  216. state: "current" as const,
  217. ref: "zcode-artifact://share/input-only",
  218. artifact_type: "text" as const,
  219. display_name: "input.txt",
  220. extension: "txt",
  221. mime_type: "text/plain",
  222. size_bytes: 5,
  223. sha256: "d".repeat(64),
  224. url: "https://example.test/input-only",
  225. url_expires_at: 2_000,
  226. },
  227. ],
  228. } satisfies ConversationSharePreview;
  229. const html = renderToStaticMarkup(
  230. createElement(ConversationShareLandingPage, {
  231. shareCode: "share-1",
  232. preview: inputOnly,
  233. locale: "zh-CN",
  234. }),
  235. );
  236. expect(html).not.toContain("个结果物");
  237. });
  238. it("does not offer continuation for readonly shares", () => {
  239. const html = renderToStaticMarkup(
  240. createElement(ConversationShareLandingPage, {
  241. shareCode: "share-1",
  242. preview: { ...preview, share: { ...preview.share, access_mode: "public_readonly" } },
  243. locale: "en-US",
  244. }),
  245. );
  246. expect(html).not.toContain("zcode://share/import");
  247. expect(html).not.toContain("Open in ZCode");
  248. expect(html).not.toContain("去 ZCode 继续");
  249. expect(html).toContain('data-share-theme-toggle="true"');
  250. expect(html).not.toContain('data-share-continue-footer="true"');
  251. expect(html).toContain('data-share-continue-measure="true"');
  252. expect(html).toContain('data-share-continue-visible="false"');
  253. });
  254. it("renders private login-required status without exposing API details", () => {
  255. const html = renderToStaticMarkup(
  256. createElement(ConversationShareLandingStatus, {
  257. state: { kind: "login_required" },
  258. locale: "en-US",
  259. }),
  260. );
  261. expect(html).toContain("Sign in to view this share");
  262. expect(html).not.toContain("3211");
  263. });
  264. it.each([
  265. ["zh-CN", "账号数据不互通", "回到首页"],
  266. ["en-US", "do not share account data", "Back to home"],
  267. ] as const)(
  268. "offers account guidance and home navigation for not_found in %s",
  269. (locale, hint, home) => {
  270. const html = renderToStaticMarkup(
  271. createElement(ConversationShareLandingStatus, {
  272. state: { kind: "error", error: "not_found" },
  273. locale,
  274. onRetry: () => {},
  275. }),
  276. );
  277. expect(html).toContain(hint);
  278. expect(html).toContain(home);
  279. expect(html).toContain('href="https://zcode.z.ai"');
  280. expect(html).not.toContain('target="_blank"');
  281. },
  282. );
  283. it.each([
  284. "network",
  285. "expired",
  286. "authentication_required",
  287. "invalid_contract",
  288. "unsupported_schema_version",
  289. "rate_limited",
  290. "unknown",
  291. ] as const)("keeps account guidance and home navigation out of %s", (error) => {
  292. const html = renderToStaticMarkup(
  293. createElement(ConversationShareLandingStatus, {
  294. state: { kind: "error", error },
  295. locale: "zh-CN",
  296. onRetry: () => {},
  297. }),
  298. );
  299. expect(html).not.toContain("账号数据不互通");
  300. expect(html).not.toContain("回到首页");
  301. });
  302. });