conversationShareReadonlyTimeline.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it } from "vitest";
  4. import { ConversationShareReadonlyTimeline } from "@/v4/ConversationShareReadonlyTimeline.js";
  5. // SG-01 注入边界:时间线不再静态 import OpenSplitButton(避免 open-with 子树进公开页 bundle),
  6. // 改由消费方通过 artifactOpenAction 注入;测试用 stub 组件验证同一注入契约。
  7. const openActionProps: { values: Array<Record<string, unknown>> } = { values: [] };
  8. function StubArtifactOpenAction(props: Record<string, unknown>) {
  9. openActionProps.values.push(props);
  10. const target = props.target as
  11. | {
  12. path?: string;
  13. previewSource?: { workspacePath?: string };
  14. }
  15. | undefined;
  16. return createElement(
  17. "button",
  18. {
  19. type: "button",
  20. "data-open-split-button": "true",
  21. "data-target-path": target?.path ?? "",
  22. "data-preview-workspace": target?.previewSource?.workspacePath ?? "",
  23. },
  24. "Open",
  25. );
  26. }
  27. const rows = [
  28. {
  29. rowId: 1,
  30. turnId: "turn-1",
  31. productTurnId: "product-turn-1",
  32. createdAt: 1,
  33. createdAtSeq: 1,
  34. kind: "turnHeader" as const,
  35. origin: "userInput" as const,
  36. state: "completedSuccess" as const,
  37. startedAt: 1,
  38. },
  39. {
  40. rowId: 2,
  41. turnId: "turn-1",
  42. productTurnId: "product-turn-1",
  43. createdAt: 2,
  44. createdAtSeq: 2,
  45. kind: "userInput" as const,
  46. origin: "realUser" as const,
  47. text: "请总结今天的新闻",
  48. },
  49. {
  50. rowId: 3,
  51. turnId: "turn-1",
  52. productTurnId: "product-turn-1",
  53. createdAt: 3,
  54. createdAtSeq: 3,
  55. kind: "assistantText" as const,
  56. text: "这是 **摘要**",
  57. state: "complete" as const,
  58. },
  59. {
  60. rowId: 4,
  61. turnId: "turn-1",
  62. productTurnId: "product-turn-1",
  63. createdAt: 4,
  64. createdAtSeq: 4,
  65. kind: "toolCall" as const,
  66. toolCallId: "tool-1",
  67. toolName: "WebSearch",
  68. status: "success" as const,
  69. inputText: "query",
  70. output: { text: "result" },
  71. },
  72. ];
  73. const buildArtifactRow = (rowId: number, artifactVersionId: string) => ({
  74. rowId,
  75. turnId: "turn-1",
  76. productTurnId: "product-turn-1",
  77. createdAt: rowId,
  78. createdAtSeq: rowId,
  79. kind: "artifact" as const,
  80. artifactVersionId,
  81. logicalArtifactKey: "report",
  82. displayName: "晨报.pdf",
  83. artifactType: "pdf" as const,
  84. mimeType: "application/pdf",
  85. sizeBytes: 172_974,
  86. sha256: "a".repeat(64),
  87. ref: `zcode-artifact://share/${artifactVersionId}`,
  88. state: "current" as const,
  89. });
  90. describe("ConversationShareReadonlyTimeline", () => {
  91. beforeEach(() => {
  92. openActionProps.values = [];
  93. });
  94. it("uses Desktop user bubble, assistant markdown, turn grouping, and tool summary", () => {
  95. const html = renderToStaticMarkup(
  96. createElement(ConversationShareReadonlyTimeline, {
  97. rows,
  98. locale: "zh-CN",
  99. }),
  100. );
  101. expect(html).toContain('data-v4-user-input-bubble="true"');
  102. expect(html).toContain("rounded-tr-xs");
  103. expect(html).toContain("这是");
  104. expect(html).toContain('data-conversation-share-turn="turn-1"');
  105. expect(html).toContain("搜索");
  106. expect(html).toContain("tool-summary");
  107. expect(html).not.toContain("<details");
  108. expect(html).not.toContain("canEdit");
  109. expect(html).not.toContain("turn · userInput · completedSuccess");
  110. });
  111. it("renders shared user input attachments as downloadable file pills", () => {
  112. const attachmentRows = [
  113. rows[0],
  114. {
  115. ...rows[1],
  116. attachments: [
  117. {
  118. ref: "zcode-artifact://share/share-input-text-1",
  119. previewRef: "zcode-artifact://share/share-input-text-1",
  120. fileName: "pasted-text.txt",
  121. mime: "text/plain",
  122. bytes: 245_760,
  123. },
  124. ],
  125. },
  126. rows[2],
  127. ];
  128. const html = renderToStaticMarkup(
  129. createElement(ConversationShareReadonlyTimeline, {
  130. rows: attachmentRows,
  131. artifactUrls: new Map([
  132. ["share-input-text-1", "https://share.example.test/artifacts/text-1"],
  133. ]),
  134. locale: "en-US",
  135. }),
  136. );
  137. expect(html).toContain("pasted-text.txt");
  138. expect(html).toContain("https://share.example.test/artifacts/text-1");
  139. expect(html).toContain('data-v4-user-input-attachment-pill="true"');
  140. });
  141. it("keeps the user prompt before the history status and formats work duration like Desktop", () => {
  142. const historyRows = [
  143. {
  144. ...rows[0],
  145. activeMs: 165_000,
  146. },
  147. rows[1],
  148. {
  149. rowId: 7,
  150. turnId: "turn-1",
  151. productTurnId: "product-turn-1",
  152. createdAt: 7,
  153. createdAtSeq: 7,
  154. kind: "reasoning" as const,
  155. text: "先整理数据,再输出结论。",
  156. state: "complete" as const,
  157. durationMs: 2_000,
  158. },
  159. rows[2],
  160. ];
  161. const html = renderToStaticMarkup(
  162. createElement(ConversationShareReadonlyTimeline, {
  163. rows: historyRows,
  164. locale: "zh-CN",
  165. }),
  166. );
  167. expect(html.indexOf('data-v4-user-input-bubble="true"')).toBeLessThan(
  168. html.indexOf('data-conversation-share-history-trigger="true"'),
  169. );
  170. expect(html).toContain("已工作 2 分 45 秒");
  171. });
  172. it("renders headings and tables through the Desktop MessageResponse pipeline", () => {
  173. const markdownRows = [
  174. rows[0],
  175. rows[1],
  176. {
  177. ...rows[2],
  178. text: [
  179. "普通段落",
  180. "",
  181. "---",
  182. "",
  183. "# 标题",
  184. "",
  185. "## 二级标题",
  186. "",
  187. "| 项目 | 内容 |",
  188. "| --- | --- |",
  189. "| 公司 | **ZCode** |",
  190. "",
  191. "```ts",
  192. "const answer = 42;",
  193. "```",
  194. ].join("\n"),
  195. },
  196. ];
  197. const html = renderToStaticMarkup(
  198. createElement(ConversationShareReadonlyTimeline, {
  199. rows: markdownRows,
  200. locale: "zh-CN",
  201. }),
  202. );
  203. expect(html).toContain('data-streamdown="heading-1"');
  204. expect(html).toContain('data-streamdown="heading-2"');
  205. expect(html).toContain("<table");
  206. expect(html).toContain("data-markdown-table-toolbar");
  207. expect(html).not.toContain("# 标题");
  208. expect(html).not.toContain("| 项目 | 内容 |");
  209. });
  210. it("uses Desktop execute grouping for consecutive shell tool rows", () => {
  211. const executeRows = [
  212. rows[0],
  213. rows[1],
  214. rows[2],
  215. {
  216. rowId: 5,
  217. turnId: "turn-1",
  218. productTurnId: "product-turn-1",
  219. createdAt: 5,
  220. createdAtSeq: 5,
  221. kind: "toolCall" as const,
  222. toolCallId: "execute-1",
  223. toolName: "Bash",
  224. status: "success" as const,
  225. inputText: "echo one",
  226. input: { command: "echo one" },
  227. output: { text: "one" },
  228. },
  229. {
  230. rowId: 6,
  231. turnId: "turn-1",
  232. productTurnId: "product-turn-1",
  233. createdAt: 6,
  234. createdAtSeq: 6,
  235. kind: "toolCall" as const,
  236. toolCallId: "execute-2",
  237. toolName: "Bash",
  238. status: "success" as const,
  239. inputText: "echo two",
  240. input: { command: "echo two" },
  241. output: { text: "two" },
  242. },
  243. ];
  244. const html = renderToStaticMarkup(
  245. createElement(ConversationShareReadonlyTimeline, {
  246. rows: executeRows,
  247. locale: "zh-CN",
  248. }),
  249. );
  250. expect(html).toContain('data-conversation-share-work-group="executeGroup"');
  251. expect(html).toContain("执行");
  252. expect(html).toContain("lucide-square-terminal");
  253. });
  254. it("uses the Desktop tool family icon and public summary for search rows", () => {
  255. const searchRows = [
  256. rows[0],
  257. rows[1],
  258. rows[2],
  259. {
  260. ...rows[3],
  261. input: { query: "AI coding" },
  262. },
  263. ];
  264. const html = renderToStaticMarkup(
  265. createElement(ConversationShareReadonlyTimeline, {
  266. rows: searchRows,
  267. locale: "zh-CN",
  268. }),
  269. );
  270. expect(html).toContain("lucide-search");
  271. expect(html).toContain("AI coding");
  272. expect(html).not.toContain("lucide-wrench");
  273. });
  274. it("marker 不再把枚举名当文案印出来", () => {
  275. // Bug 根因:原本直接渲染 row.marker.type,分享页会出现一条写着字面量 compact 的分割线。
  276. const markerRows = [
  277. rows[0],
  278. rows[1],
  279. {
  280. rowId: 90,
  281. turnId: "turn-1",
  282. productTurnId: "product-turn-1",
  283. createdAt: 90,
  284. createdAtSeq: 90,
  285. kind: "timelineMarker" as const,
  286. lane: "turnTailBoundary" as const,
  287. marker: { type: "compact" as const, origin: "auto" as const, status: "success" as const },
  288. },
  289. {
  290. rowId: 91,
  291. turnId: "turn-1",
  292. productTurnId: "product-turn-1",
  293. createdAt: 91,
  294. createdAtSeq: 91,
  295. kind: "timelineMarker" as const,
  296. lane: "turnTailBoundary" as const,
  297. marker: { type: "goalVerify" as const, iteration: 1, outcome: "pass" as const },
  298. },
  299. ];
  300. const html = renderToStaticMarkup(
  301. createElement(ConversationShareReadonlyTimeline, {
  302. rows: markerRows as never,
  303. locale: "zh-CN",
  304. }),
  305. );
  306. expect(html).toContain("上下文已压缩");
  307. expect(html).not.toContain(">compact<");
  308. // 识别不了的类型整行不渲染,而不是退化成打印枚举名。
  309. expect(html).not.toContain("goalVerify");
  310. });
  311. it("artifact 卡片与正文的 AssistantPreviewCards 对齐", () => {
  312. const artifactRows = [
  313. rows[0],
  314. rows[1],
  315. {
  316. rowId: 80,
  317. turnId: "turn-1",
  318. productTurnId: "product-turn-1",
  319. createdAt: 80,
  320. createdAtSeq: 80,
  321. kind: "artifact" as const,
  322. artifactVersionId: "artifact-1",
  323. logicalArtifactKey: "report",
  324. displayName: "晨报.pdf",
  325. artifactType: "pdf" as const,
  326. mimeType: "application/pdf",
  327. sizeBytes: 172_974,
  328. sha256: "a".repeat(64),
  329. ref: "zcode-artifact://share/artifact-1",
  330. state: "current" as const,
  331. },
  332. ];
  333. const html = renderToStaticMarkup(
  334. createElement(ConversationShareReadonlyTimeline, {
  335. rows: artifactRows as never,
  336. locale: "zh-CN",
  337. artifactUrls: new Map([["artifact-1", "https://example.invalid/a.pdf"]]),
  338. }),
  339. );
  340. // 与 AssistantPreviewCards.tsx 的容器一致:rounded-xl / p-3 pr-4 / gap-3。
  341. expect(html).toContain(
  342. "flex w-full items-center gap-3 rounded-xl border border-card-border bg-card p-3 pr-4",
  343. );
  344. // 时间线 turn 已提供正文 inset,artifact 行不能再叠加水平 padding,否则卡片会比正文窄。
  345. expect(html).toContain('data-conversation-share-row-kind="artifact" class="py-1"');
  346. // 44px 图标底板 + 真实文件类型图标,而不是一个通用的 size-4 FileIcon。
  347. expect(html).toContain("flex size-11 shrink-0 items-center justify-center rounded-md");
  348. expect(html).toContain("pdf.svg");
  349. // 副标题复用正文同一套词汇,并且不再直接印裸字节数。
  350. expect(html).toContain("文档 · PDF");
  351. expect(html).toContain("169 KB");
  352. expect(html).not.toContain("172974 bytes");
  353. // 动作按钮与 OpenSplitButton 同一视觉,不再是蓝色文字链;语义锁定用结构性标记,
  354. // 不锁 Tailwind 类字符串(类序/尺寸微调不应打断这条回归)。
  355. expect(html).toContain('data-share-preview-button="true"');
  356. expect(html).not.toContain("text-icon-blue");
  357. });
  358. it("为 Desktop 导入副本的本地 artifact 渲染 OpenSplitButton", () => {
  359. const artifactRows = [
  360. rows[0],
  361. rows[1],
  362. {
  363. rowId: 80,
  364. turnId: "turn-1",
  365. productTurnId: "product-turn-1",
  366. createdAt: 80,
  367. createdAtSeq: 80,
  368. kind: "artifact" as const,
  369. artifactVersionId: "artifact-1",
  370. logicalArtifactKey: "report",
  371. displayName: "晨报.pdf",
  372. artifactType: "pdf" as const,
  373. mimeType: "application/pdf",
  374. sizeBytes: 172_974,
  375. sha256: "a".repeat(64),
  376. ref: "zcode-artifact://share/artifact-1",
  377. state: "current" as const,
  378. },
  379. ];
  380. const html = renderToStaticMarkup(
  381. createElement(ConversationShareReadonlyTimeline, {
  382. rows: artifactRows as never,
  383. locale: "zh-CN",
  384. workspacePath: "/workspace",
  385. artifactWorkspaceRelativePaths: new Map([
  386. ["artifact-1", ".zcode-share/share-1/shared-artifacts/晨报.pdf"],
  387. ]),
  388. onOpenCodeViewer: () => undefined,
  389. artifactOpenAction: StubArtifactOpenAction,
  390. }),
  391. );
  392. expect(html).toContain('data-open-split-button="true"');
  393. expect(html).toContain(
  394. 'data-target-path="/workspace/.zcode-share/share-1/shared-artifacts/晨报.pdf"',
  395. );
  396. expect(openActionProps.values).toHaveLength(1);
  397. expect(openActionProps.values[0]?.target).toMatchObject({
  398. type: "file",
  399. path: "/workspace/.zcode-share/share-1/shared-artifacts/晨报.pdf",
  400. title: "晨报.pdf",
  401. label: "晨报.pdf",
  402. previewSource: {
  403. type: "file",
  404. title: "晨报.pdf",
  405. path: "/workspace/.zcode-share/share-1/shared-artifacts/晨报.pdf",
  406. workspacePath: "/workspace",
  407. },
  408. });
  409. });
  410. it("不注入 artifactOpenAction 时本地 artifact 不渲染任何打开控件", () => {
  411. // 与静态守卫(conversationShareBundleBoundary.test.ts)互补的运行时负例:公开页
  412. // (packages/web/src/share)不传 artifactOpenAction。锁住 ArtifactOpenContext 门控里的
  413. // !artifactOpenAction 条件——即使 workspacePath、合法相对路径、打开回调齐备,也必须
  414. // 退回纯只读卡片,防止后续加默认回退实现让公开页重新获得本地打开入口。
  415. const artifactRows = [rows[0], rows[1], buildArtifactRow(80, "artifact-1")];
  416. const html = renderToStaticMarkup(
  417. createElement(ConversationShareReadonlyTimeline, {
  418. rows: artifactRows as never,
  419. locale: "zh-CN",
  420. workspacePath: "/workspace",
  421. artifactWorkspaceRelativePaths: new Map([
  422. ["artifact-1", ".zcode-share/share-1/shared-artifacts/晨报.pdf"],
  423. ]),
  424. onOpenCodeViewer: () => undefined,
  425. }),
  426. );
  427. expect(html).toContain("晨报.pdf");
  428. expect(html).not.toContain('data-open-split-button="true"');
  429. expect(html).not.toContain('data-share-preview-button="true"');
  430. expect(openActionProps.values).toHaveLength(0);
  431. });
  432. it("拒绝导入副本里越界的 artifact path", () => {
  433. const artifactRows = [
  434. rows[0],
  435. rows[1],
  436. {
  437. rowId: 80,
  438. turnId: "turn-1",
  439. productTurnId: "product-turn-1",
  440. createdAt: 80,
  441. createdAtSeq: 80,
  442. kind: "artifact" as const,
  443. artifactVersionId: "artifact-1",
  444. logicalArtifactKey: "report",
  445. displayName: "晨报.pdf",
  446. artifactType: "pdf" as const,
  447. mimeType: "application/pdf",
  448. sizeBytes: 172_974,
  449. sha256: "a".repeat(64),
  450. ref: "zcode-artifact://share/artifact-1",
  451. state: "current" as const,
  452. },
  453. ];
  454. const html = renderToStaticMarkup(
  455. createElement(ConversationShareReadonlyTimeline, {
  456. rows: artifactRows as never,
  457. locale: "zh-CN",
  458. workspacePath: "/workspace",
  459. artifactWorkspaceRelativePaths: new Map([["artifact-1", "../secret.pdf"]]),
  460. onOpenCodeViewer: () => undefined,
  461. artifactOpenAction: StubArtifactOpenAction,
  462. }),
  463. );
  464. expect(html).toContain("晨报.pdf");
  465. expect(html).not.toContain('data-open-split-button="true"');
  466. expect(openActionProps.values).toHaveLength(0);
  467. });
  468. it("拒绝导入副本里的绝对路径 artifact path", () => {
  469. // 公开页隐私边界:posix / Windows 盘符 / UNC 三种绝对路径形态都必须退回纯只读卡片。
  470. const artifactRows = [
  471. rows[0],
  472. rows[1],
  473. buildArtifactRow(80, "artifact-1"),
  474. buildArtifactRow(81, "artifact-2"),
  475. buildArtifactRow(82, "artifact-3"),
  476. ];
  477. const html = renderToStaticMarkup(
  478. createElement(ConversationShareReadonlyTimeline, {
  479. rows: artifactRows as never,
  480. locale: "zh-CN",
  481. workspacePath: "/workspace",
  482. artifactWorkspaceRelativePaths: new Map([
  483. ["artifact-1", "/etc/passwd"],
  484. ["artifact-2", "C:\\x\\a.pdf"],
  485. ["artifact-3", "\\\\srv\\share\\a.pdf"],
  486. ]),
  487. onOpenCodeViewer: () => undefined,
  488. artifactOpenAction: StubArtifactOpenAction,
  489. }),
  490. );
  491. expect(html).toContain("晨报.pdf");
  492. expect(html).not.toContain('data-open-split-button="true"');
  493. expect(openActionProps.values).toHaveLength(0);
  494. });
  495. it("拒绝导入副本里段数不为 4 或含 ./.. 段的 artifact path", () => {
  496. // "../secret.pdf" 在段数校验就被拒了,这里专门补 4 段但段内含 .. 的逃逸形态。
  497. const artifactRows = [
  498. rows[0],
  499. rows[1],
  500. buildArtifactRow(80, "artifact-1"),
  501. buildArtifactRow(81, "artifact-2"),
  502. buildArtifactRow(82, "artifact-3"),
  503. buildArtifactRow(83, "artifact-4"),
  504. buildArtifactRow(84, "artifact-5"),
  505. ];
  506. const html = renderToStaticMarkup(
  507. createElement(ConversationShareReadonlyTimeline, {
  508. rows: artifactRows as never,
  509. locale: "zh-CN",
  510. workspacePath: "/workspace",
  511. artifactWorkspaceRelativePaths: new Map([
  512. // 嵌套子目录:5 段。
  513. ["artifact-1", ".zcode-share/share-1/shared-artifacts/sub/a.pdf"],
  514. // 浅路径:3 段。
  515. ["artifact-2", ".zcode-share/share-1/a.pdf"],
  516. // 4 段但第 2 段是 ..:等价于跳出本 share 目录。
  517. ["artifact-3", ".zcode-share/../shared-artifacts/a.pdf"],
  518. // 4 段但文件名是 .:不是合法产物文件。
  519. ["artifact-4", ".zcode-share/share-1/shared-artifacts/."],
  520. // 反斜杠分隔不能绕过段校验。
  521. ["artifact-5", ".zcode-share\\share-1\\shared-artifacts\\a.pdf\\..\\b.pdf"],
  522. ]),
  523. onOpenCodeViewer: () => undefined,
  524. artifactOpenAction: StubArtifactOpenAction,
  525. }),
  526. );
  527. expect(html).toContain("晨报.pdf");
  528. expect(html).not.toContain('data-open-split-button="true"');
  529. expect(openActionProps.values).toHaveLength(0);
  530. });
  531. it.each([
  532. ["zh-CN", "下载文件"],
  533. ["en-US", "Download file"],
  534. ])("artifactUrls 与本地路径同时存在时 url 必须胜出(%s)", (locale, label) => {
  535. // 公开页隐私边界的关键前提:ArtifactPresentation 只在 !url 时解析本地路径;
  536. // url 存在时必须渲染 window.open 按钮,而不是能触达本地文件服务的 OpenSplitButton。
  537. const artifactRows = [rows[0], rows[1], buildArtifactRow(80, "artifact-1")];
  538. const html = renderToStaticMarkup(
  539. createElement(ConversationShareReadonlyTimeline, {
  540. rows: artifactRows as never,
  541. locale,
  542. workspacePath: "/workspace",
  543. artifactUrls: new Map([["artifact-1", "https://example.invalid/a.pdf"]]),
  544. artifactWorkspaceRelativePaths: new Map([
  545. ["artifact-1", ".zcode-share/share-1/shared-artifacts/晨报.pdf"],
  546. ]),
  547. onOpenCodeViewer: () => undefined,
  548. artifactOpenAction: StubArtifactOpenAction,
  549. }),
  550. );
  551. // 资源 URL 实际下载文件,按钮文案必须对应下载,且注入的本地打开动作完全未挂载。
  552. expect(html).toContain(label);
  553. expect(html).not.toContain("预览文件");
  554. expect(html).not.toContain("Preview file");
  555. expect(html).toContain('data-share-preview-button="true"');
  556. expect(html).not.toContain('data-open-split-button="true"');
  557. expect(openActionProps.values).toHaveLength(0);
  558. });
  559. // Bug 回归:公开分享页不传 workspacePath/readAttachment,MarkdownImage 会 fallback 成
  560. // <img src={远程}>,匿名访客一打开就自动向第三方发请求(泄露 IP/UA/Referer)。
  561. // 正文与工具输出都必须只剩链接,不能出现自动加载的 <img>。
  562. it("不为正文与工具输出里的远程图片渲染自动加载的 img", () => {
  563. const remoteImageRows = [
  564. {
  565. rowId: 1,
  566. turnId: "turn-1",
  567. productTurnId: "product-turn-1",
  568. createdAt: 1,
  569. createdAtSeq: 1,
  570. kind: "turnHeader" as const,
  571. origin: "userInput" as const,
  572. state: "completedSuccess" as const,
  573. startedAt: 1,
  574. },
  575. {
  576. rowId: 2,
  577. turnId: "turn-1",
  578. productTurnId: "product-turn-1",
  579. createdAt: 2,
  580. createdAtSeq: 2,
  581. kind: "assistantText" as const,
  582. text: "看图 ![tracker](https://tracker.invalid/pixel.png)",
  583. state: "complete" as const,
  584. },
  585. {
  586. rowId: 3,
  587. turnId: "turn-1",
  588. productTurnId: "product-turn-1",
  589. createdAt: 3,
  590. createdAtSeq: 3,
  591. kind: "toolCall" as const,
  592. toolCallId: "tool-1",
  593. toolName: "WebSearch",
  594. status: "success" as const,
  595. inputText: "query",
  596. output: { text: "结果 ![tracker](https://tracker.invalid/tool.png)" },
  597. },
  598. ];
  599. const html = renderToStaticMarkup(
  600. createElement(ConversationShareReadonlyTimeline, {
  601. rows: remoteImageRows as never,
  602. locale: "zh-CN",
  603. }),
  604. );
  605. // 关键不是 URL 是否出现在 HTML 里(链接的 title 属性带着它无害),
  606. // 而是不能有任何自动发起请求的 <img src>。
  607. expect(html).not.toContain("<img");
  608. expect(html).not.toContain('src="https://tracker.invalid');
  609. // 信息不丢:降级成外链后 URL 仍可被访客显式点击。
  610. expect(html).toContain('title="https://tracker.invalid/pixel.png"');
  611. expect(html).toContain("tracker");
  612. });
  613. });