conversationShareContract.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. conversationShareArtifactUploadDataSchema,
  4. conversationShareCapabilitiesDataSchema,
  5. conversationShareCapabilitiesWireSchema,
  6. narrowConversationShareCapabilities,
  7. conversationShareConfirmDataSchema,
  8. conversationShareConfirmRequestSchema,
  9. conversationShareContinuationDataSchema,
  10. conversationShareContinuationRequestSchema,
  11. conversationShareErrorEnvelopeSchema,
  12. conversationSharePreparationDataSchema,
  13. conversationSharePreparationRequestSchema,
  14. conversationSharePreviewDataSchema,
  15. createConversationShareSuccessEnvelopeSchema,
  16. decodeConversationShareRows,
  17. isConversationShareSchemaVersionSupported,
  18. localizeConversationShareUrl,
  19. parseConversationSharePathname,
  20. } from "../src/conversation-share.js";
  21. import { sharedContextImportStateSchema } from "../src/zcode-protocol-v4/shared-context-import.js";
  22. const SHA_256 = "a".repeat(64);
  23. const completedRows = [
  24. {
  25. rowId: 1,
  26. turnId: "turn-1",
  27. productTurnId: "product-turn-1",
  28. createdAt: 1_788_415_000_000,
  29. createdAtSeq: 1,
  30. kind: "turnHeader",
  31. origin: "userInput",
  32. state: "completedSuccess",
  33. startedAt: 1_788_415_000_000,
  34. endedAt: 1_788_415_001_000,
  35. },
  36. {
  37. rowId: 2,
  38. turnId: "turn-1",
  39. productTurnId: "product-turn-1",
  40. createdAt: 1_788_415_000_100,
  41. createdAtSeq: 2,
  42. kind: "assistantText",
  43. text: "已完成",
  44. state: "complete",
  45. },
  46. ] as const;
  47. const artifactDescriptor = {
  48. artifact_id: "artifact-public-id",
  49. logical_artifact_key: "report",
  50. producer_product_turn_id: "product-turn-1",
  51. artifact_version: 1,
  52. state: "current",
  53. ref: "zcode-artifact://share/artifact-public-id",
  54. artifact_type: "pptx",
  55. display_name: "季度报告.pptx",
  56. original_path: "/workspace/output/季度报告.pptx",
  57. extension: "pptx",
  58. mime_type: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  59. size_bytes: 102_400,
  60. sha256: SHA_256,
  61. } as const;
  62. describe("conversation share API revision 23 contract", () => {
  63. it("按语言前缀解析分享路径,只认已知形状", () => {
  64. expect(parseConversationSharePathname("/cn/share/abc")).toEqual({
  65. rawCode: "abc",
  66. locale: "zh-CN",
  67. });
  68. expect(parseConversationSharePathname("/share/abc")).toEqual({
  69. rawCode: "abc",
  70. locale: "en-US",
  71. });
  72. expect(parseConversationSharePathname("/share/abc/extra")).toBeNull();
  73. expect(parseConversationSharePathname("/cn/shared/abc")).toBeNull();
  74. expect(parseConversationSharePathname("/share")).toBeNull();
  75. });
  76. it("把分享链接改写到目标语言站点,且不改动 code 段", () => {
  77. expect(localizeConversationShareUrl("https://zcode.z.ai/cn/share/abc", "en-US")).toBe(
  78. "https://zcode.z.ai/share/abc",
  79. );
  80. expect(localizeConversationShareUrl("https://zcode.z.ai/share/abc", "zh-CN")).toBe(
  81. "https://zcode.z.ai/cn/share/abc",
  82. );
  83. // 已经是目标语言时保持原样,端口与 query 不受影响。
  84. expect(
  85. localizeConversationShareUrl("https://zcode.z.ai:8443/cn/share/a.b~c?x=1", "zh-CN"),
  86. ).toBe("https://zcode.z.ai:8443/cn/share/a.b~c?x=1");
  87. });
  88. it("形状不认识的链接原样返回,绝不猜着改", () => {
  89. // 服务端将来若换 URL 形状或独立域名,这里必须安静地不作为而不是改错。
  90. for (const url of [
  91. "https://zcode.z.ai/s/abc",
  92. "https://zcode.z.ai/cn/share/abc/extra",
  93. "https://zcode.z.ai/cn/share",
  94. "not-a-url",
  95. "",
  96. ]) {
  97. expect(localizeConversationShareUrl(url, "en-US")).toBe(url);
  98. }
  99. });
  100. it("sharedContextImport 只接受规范 /cn/share/ 回链", () => {
  101. // 回归守卫:导入时若把回链按界面语言本地化成 /share/<code>,这里会直接拒掉,
  102. // 英文界面的导入就会挂。所以持久化必须存规范路径,本地化只在展示时做。
  103. const base = {
  104. contextId: "shared-context-1",
  105. title: "来自分享:Shared Research",
  106. status: "pending" as const,
  107. };
  108. expect(
  109. sharedContextImportStateSchema.safeParse({
  110. ...base,
  111. shareUrl: "https://zcode.z.ai/cn/share/abc",
  112. }).success,
  113. ).toBe(true);
  114. const localized = sharedContextImportStateSchema.safeParse({
  115. ...base,
  116. shareUrl: "https://zcode.z.ai/share/abc",
  117. });
  118. // 英文站路径只能作为 legacy { title } 形状通过,v2 形状(带 contextId/status)会被拒。
  119. expect(localized.success && "contextId" in localized.data).toBe(false);
  120. });
  121. it("parses capabilities with all three access modes", () => {
  122. const parsed = conversationShareCapabilitiesDataSchema.parse({
  123. schema_version: 1,
  124. ttl_ms: 604_800_000,
  125. access_modes: ["private", "public_readonly", "public_importable"],
  126. max_rows: 500,
  127. max_payload_bytes: 4_194_304,
  128. max_artifact_count: 20,
  129. max_artifact_bytes: 20_971_520,
  130. max_total_artifact_bytes: 104_857_600,
  131. allowed_artifacts: [
  132. {
  133. type: "image",
  134. extensions: ["png", "jpg", "jpeg", "webp"],
  135. mime_types: ["image/png", "image/jpeg", "image/webp"],
  136. },
  137. ],
  138. });
  139. expect(parsed.access_modes).toEqual(["private", "public_readonly", "public_importable"]);
  140. });
  141. it("tolerates artifact types the client does not know yet", () => {
  142. // 线上故障:后端在 allowed_artifacts 里新增类型后,严格枚举让整份 capabilities 解析
  143. // 失败,发布在 collecting 阶段就抛 invalid_contract。能力发现列表必须向前兼容。
  144. const wire = conversationShareCapabilitiesWireSchema.parse({
  145. schema_version: 1,
  146. ttl_ms: 604_800_000,
  147. access_modes: ["private"],
  148. max_rows: 500,
  149. max_payload_bytes: 4_194_304,
  150. max_artifact_count: 20,
  151. max_artifact_bytes: 20_971_520,
  152. max_total_artifact_bytes: 104_857_600,
  153. allowed_artifacts: [
  154. { type: "pdf", extensions: ["pdf"], mime_types: ["application/pdf"] },
  155. { type: "some-future-type", extensions: ["fut"], mime_types: ["application/x-future"] },
  156. ],
  157. });
  158. const { capabilities, unsupportedArtifactTypes } = narrowConversationShareCapabilities(wire);
  159. expect(capabilities.allowed_artifacts.map((entry) => entry.type)).toEqual(["pdf"]);
  160. expect(unsupportedArtifactTypes).toEqual(["some-future-type"]);
  161. });
  162. it("保留服务端的 md 结果物类型,不再把它当未知类型削掉", () => {
  163. // Bug 根因:本地枚举缺 md → 服务端允许的 md 被丢弃 → 又拿被削过的白名单
  164. // 告诉用户「.md 不支持,请取消该轮」。本地能抽出 .md 候选,就必须认这个类型。
  165. const wire = conversationShareCapabilitiesWireSchema.parse({
  166. schema_version: 1,
  167. ttl_ms: 604_800_000,
  168. access_modes: ["private"],
  169. max_rows: 500,
  170. max_payload_bytes: 4_194_304,
  171. max_artifact_count: 20,
  172. max_artifact_bytes: 20_971_520,
  173. max_total_artifact_bytes: 104_857_600,
  174. allowed_artifacts: [
  175. { type: "md", extensions: ["md"], mime_types: ["text/markdown"] },
  176. { type: "html", extensions: ["html", "htm"], mime_types: ["text/html"] },
  177. { type: "text", extensions: ["txt"], mime_types: ["text/plain"] },
  178. ],
  179. });
  180. const { capabilities, unsupportedArtifactTypes } = narrowConversationShareCapabilities(wire);
  181. expect(capabilities.allowed_artifacts.map((entry) => entry.type)).toEqual([
  182. "md",
  183. "html",
  184. "text",
  185. ]);
  186. expect(unsupportedArtifactTypes).toEqual([]);
  187. });
  188. it("parses preparation request and confirmed idempotency response", () => {
  189. expect(
  190. conversationSharePreparationRequestSchema.parse({
  191. client_request_id: "request-uuid",
  192. title: "分享标题",
  193. schema_version: 1,
  194. access_mode: "public_readonly",
  195. payload_sha256: SHA_256,
  196. artifact_count: 0,
  197. }),
  198. ).toMatchObject({ artifact_count: 0, access_mode: "public_readonly" });
  199. const parsed = conversationSharePreparationDataSchema.parse({
  200. preparation_id: "preparation-id",
  201. status: "confirmed",
  202. access_mode: "public_readonly",
  203. expires_at: 1_789_020_800_000,
  204. share: {
  205. share_code: "base64url-hmac-code",
  206. share_url: "https://example.com/share/base64url-hmac-code",
  207. access_mode: "public_readonly",
  208. expires_at: 1_789_020_800_000,
  209. },
  210. });
  211. expect(parsed.status).toBe("confirmed");
  212. });
  213. it("parses artifact upload response", () => {
  214. expect(
  215. conversationShareArtifactUploadDataSchema.parse({
  216. artifact_id: artifactDescriptor.artifact_id,
  217. size_bytes: artifactDescriptor.size_bytes,
  218. sha256: SHA_256,
  219. status: "uploaded",
  220. safety_status: "pending",
  221. }),
  222. ).toEqual({
  223. artifact_id: "artifact-public-id",
  224. size_bytes: 102_400,
  225. sha256: SHA_256,
  226. status: "uploaded",
  227. safety_status: "pending",
  228. });
  229. });
  230. it.each([undefined, "", " "])(
  231. "rejects missing or empty artifact safety status: %j",
  232. (safetyStatus) => {
  233. expect(() =>
  234. conversationShareArtifactUploadDataSchema.parse({
  235. artifact_id: artifactDescriptor.artifact_id,
  236. size_bytes: artifactDescriptor.size_bytes,
  237. sha256: SHA_256,
  238. status: "uploaded",
  239. ...(safetyStatus === undefined ? {} : { safety_status: safetyStatus }),
  240. }),
  241. ).toThrow();
  242. },
  243. );
  244. it("parses confirm request with formal ConversationRow values", () => {
  245. const parsed = conversationShareConfirmRequestSchema.parse({
  246. selected_product_turn_ids: ["product-turn-1"],
  247. projection: { rows: completedRows },
  248. integrity: {
  249. projection_sha256: SHA_256,
  250. artifact_set_sha256: SHA_256,
  251. },
  252. disclosure_confirmation: {
  253. version: 1,
  254. accepted_at: 1_788_415_002_000,
  255. acknowledged_no_secret_detection: true,
  256. },
  257. });
  258. expect(Object.keys(parsed)).toEqual([
  259. "selected_product_turn_ids",
  260. "projection",
  261. "integrity",
  262. "disclosure_confirmation",
  263. ]);
  264. expect(parsed.projection.rows[0]?.kind).toBe("turnHeader");
  265. expect(
  266. conversationShareConfirmDataSchema.parse({
  267. share_code: "base64url-hmac-code",
  268. share_url: "https://example.com/share/base64url-hmac-code",
  269. access_mode: "private",
  270. expires_at: 1_789_020_800_000,
  271. }).share_code,
  272. ).toBe("base64url-hmac-code");
  273. });
  274. it("keeps preview URLs and continuation download URLs as distinct fields", () => {
  275. const preview = conversationSharePreviewDataSchema.parse({
  276. schema_version: 1,
  277. share: {
  278. title: "分享标题",
  279. access_mode: "public_importable",
  280. created_at: 1_788_415_002_000,
  281. expires_at: 1_789_020_800_000,
  282. },
  283. rows: completedRows,
  284. artifacts: [
  285. {
  286. ...artifactDescriptor,
  287. url: "https://signed.example.com/preview",
  288. url_expires_at: 1_788_415_600_000,
  289. },
  290. ],
  291. integrity: {
  292. projection_sha256: SHA_256,
  293. artifact_set_sha256: SHA_256,
  294. },
  295. });
  296. const continuationRequest = conversationShareContinuationRequestSchema.parse({
  297. schema_version: 1,
  298. client_request_id: "import-request-uuid",
  299. });
  300. const continuation = conversationShareContinuationDataSchema.parse({
  301. schema_version: 1,
  302. import_grant_id: "deterministic-grant-id",
  303. import_grant_expires_at: 1_788_415_600_000,
  304. share: {
  305. share_id: "internal-share-id",
  306. title: "分享标题",
  307. access_mode: "public_importable",
  308. created_at: 1_788_415_002_000,
  309. expires_at: 1_789_020_800_000,
  310. },
  311. rows: completedRows,
  312. artifacts: [
  313. {
  314. ...artifactDescriptor,
  315. download_url: "https://signed.example.com/download",
  316. download_url_expires_at: 1_788_415_600_000,
  317. },
  318. ],
  319. integrity: {
  320. projection_sha256: SHA_256,
  321. artifact_set_sha256: SHA_256,
  322. },
  323. });
  324. expect(preview.artifacts[0]?.url).toContain("/preview");
  325. expect(continuation.artifacts[0]?.download_url).toContain("/download");
  326. expect(continuationRequest.schema_version).toBe(1);
  327. });
  328. it("rejects the legacy payload hash in confirm integrity", () => {
  329. expect(() =>
  330. conversationShareConfirmRequestSchema.parse({
  331. selected_product_turn_ids: ["product-turn-1"],
  332. projection: { rows: completedRows },
  333. integrity: {
  334. projection_sha256: SHA_256,
  335. artifact_set_sha256: SHA_256,
  336. payload_sha256: SHA_256,
  337. },
  338. disclosure_confirmation: {
  339. version: 1,
  340. accepted_at: 1_788_415_002_000,
  341. acknowledged_no_secret_detection: true,
  342. },
  343. }),
  344. ).toThrow();
  345. });
  346. it("accepts formal artifact rows and rejects incomplete ones", () => {
  347. const artifactRow = {
  348. rowId: 3,
  349. turnId: "turn-1",
  350. productTurnId: "product-turn-1",
  351. createdAt: 1_788_415_000_200,
  352. createdAtSeq: 3,
  353. kind: "artifact",
  354. artifactVersionId: "artifact-public-id",
  355. logicalArtifactKey: "report",
  356. displayName: "季度报告.pdf",
  357. artifactType: "pdf",
  358. mimeType: "application/pdf",
  359. sizeBytes: 102_400,
  360. sha256: SHA_256,
  361. ref: "zcode-artifact://share/artifact-public-id",
  362. state: "current",
  363. } as const;
  364. expect(
  365. conversationShareConfirmRequestSchema
  366. .parse({
  367. selected_product_turn_ids: ["product-turn-1"],
  368. projection: { rows: [...completedRows, artifactRow] },
  369. integrity: { projection_sha256: SHA_256, artifact_set_sha256: SHA_256 },
  370. disclosure_confirmation: {
  371. version: 1,
  372. accepted_at: 1_788_415_002_000,
  373. acknowledged_no_secret_detection: true,
  374. },
  375. })
  376. .projection.rows.at(-1),
  377. ).toEqual(artifactRow);
  378. expect(() =>
  379. conversationShareCapabilitiesDataSchema.parse({
  380. schema_version: 1,
  381. ttl_ms: 1,
  382. access_modes: ["private"],
  383. max_rows: 1,
  384. max_payload_bytes: 1,
  385. max_artifact_count: 0,
  386. max_artifact_bytes: 1,
  387. max_total_artifact_bytes: 1,
  388. allowed_artifacts: [],
  389. unexpected: true,
  390. }),
  391. ).toThrow();
  392. // 入站 preview 的 rows 是 unknown[]:残缺行不再打死整份分享,而是由
  393. // decodeConversationShareRows 单独跳过(见「跨版本前向兼容」一节的用例)。
  394. const previewWithBrokenRow = conversationSharePreviewDataSchema.parse({
  395. schema_version: 1,
  396. share: {
  397. title: "分享标题",
  398. access_mode: "public_readonly",
  399. created_at: 1,
  400. expires_at: 2,
  401. },
  402. rows: [
  403. {
  404. rowId: 1,
  405. turnId: "turn-1",
  406. productTurnId: "product-turn-1",
  407. createdAt: 1,
  408. createdAtSeq: 1,
  409. kind: "artifact",
  410. artifactVersionId: "artifact-public-id",
  411. },
  412. ],
  413. artifacts: [],
  414. integrity: {
  415. projection_sha256: SHA_256,
  416. artifact_set_sha256: SHA_256,
  417. },
  418. });
  419. const decodedBrokenRow = decodeConversationShareRows(previewWithBrokenRow.rows);
  420. expect(decodedBrokenRow.rows).toHaveLength(0);
  421. expect(decodedBrokenRow.unsupportedCount).toBe(1);
  422. expect(decodedBrokenRow.unsupportedKinds).toEqual(["artifact"]);
  423. });
  424. it.each([
  425. 3001, 3002, 3200, 3201, 3203, 3204, 3205, 3206, 3207, 3208, 3209, 3210, 3211, 3212, 3213, 3214,
  426. 3215,
  427. ])("parses documented business error code %i", (code) => {
  428. expect(conversationShareErrorEnvelopeSchema.parse({ code, msg: "error" }).code).toBe(code);
  429. });
  430. });
  431. /**
  432. * 跨版本前向兼容。
  433. *
  434. * 分享内容在 4 个可独立升级/回滚的部件之间流动(发布端 Desktop、Share API 后端、
  435. * 独立部署的落地页镜像、导入端 Desktop),它们不可能同版本。这一节把「什么样的演进
  436. * 不许打死老消费端」钉成回归,避免以后每次改分享内容都要重新推演一遍兼容性。
  437. */
  438. describe("conversation share cross-version forward compatibility", () => {
  439. const share = {
  440. title: "分享标题",
  441. access_mode: "public_readonly",
  442. created_at: 1,
  443. expires_at: 2,
  444. } as const;
  445. const integrity = { projection_sha256: SHA_256, artifact_set_sha256: SHA_256 } as const;
  446. const assistantRow = {
  447. rowId: 10,
  448. turnId: "turn-1",
  449. createdAt: 1,
  450. createdAtSeq: 1,
  451. kind: "assistantText",
  452. text: "hi",
  453. state: "complete",
  454. } as const;
  455. const previewOf = (overrides: Record<string, unknown> = {}) => ({
  456. schema_version: 1,
  457. share,
  458. rows: [assistantRow],
  459. artifacts: [],
  460. integrity,
  461. ...overrides,
  462. });
  463. it("后端给响应加顶层字段时仍然可解析", () => {
  464. expect(() =>
  465. conversationSharePreviewDataSchema.parse(previewOf({ view_count: 3 })),
  466. ).not.toThrow();
  467. });
  468. it("后端给 share 元数据加字段时仍然可解析", () => {
  469. expect(() =>
  470. conversationSharePreviewDataSchema.parse(
  471. previewOf({ share: { ...share, owner_display_name: "someone" } }),
  472. ),
  473. ).not.toThrow();
  474. });
  475. it("后端给 artifact descriptor 加字段时仍然可解析", () => {
  476. const artifact = {
  477. artifact_id: "artifact-1",
  478. logical_artifact_key: "report",
  479. producer_product_turn_id: "product-turn-1",
  480. artifact_version: 1,
  481. state: "current",
  482. ref: "zcode-artifact://share/artifact-1",
  483. artifact_type: "md",
  484. display_name: "report.md",
  485. extension: "md",
  486. mime_type: "text/markdown",
  487. size_bytes: 1,
  488. sha256: SHA_256,
  489. url: "https://example.com/a",
  490. url_expires_at: 3,
  491. // 后端将来新增的安全扫描字段
  492. virus_scan: "clean",
  493. };
  494. expect(() =>
  495. conversationSharePreviewDataSchema.parse(previewOf({ artifacts: [artifact] })),
  496. ).not.toThrow();
  497. });
  498. it("后端上线新业务错误码时保留服务端 msg,不退化成无上下文的 HTTP 错误", () => {
  499. const parsed = conversationShareErrorEnvelopeSchema.parse({ code: 3299, msg: "新错误" });
  500. expect(parsed).toEqual({ code: 3299, msg: "新错误" });
  501. });
  502. it("后端给成功信封写非空 msg 时不判成契约错误", () => {
  503. expect(() =>
  504. createConversationShareSuccessEnvelopeSchema(conversationSharePreviewDataSchema).parse({
  505. code: 0,
  506. msg: "ok",
  507. data: previewOf(),
  508. }),
  509. ).not.toThrow();
  510. });
  511. it("后端给 capabilities 加限额字段或新 access mode 时,能力发现不被打死", () => {
  512. const wire = conversationShareCapabilitiesWireSchema.parse({
  513. schema_version: 1,
  514. ttl_ms: 1,
  515. access_modes: ["private", "public_readonly", "public_importable", "public_team"],
  516. max_rows: 1,
  517. max_payload_bytes: 1,
  518. max_artifact_count: 0,
  519. max_artifact_bytes: 1,
  520. max_total_artifact_bytes: 1,
  521. allowed_artifacts: [],
  522. // 后端将来新增的限额
  523. max_title_length: 100,
  524. });
  525. const narrowed = narrowConversationShareCapabilities(wire);
  526. expect(narrowed.capabilities.access_modes).toEqual([
  527. "private",
  528. "public_readonly",
  529. "public_importable",
  530. ]);
  531. expect(narrowed.unsupportedAccessModes).toEqual(["public_team"]);
  532. });
  533. // 这是本次加固的核心回归:additive 的 row 演进曾经会让所有老导入端算出不同的
  534. // projection_sha256,把「加一个 optional 字段」误报成「分享文件校验失败」。
  535. it("row 多一个未知 optional 字段时,原始值原样保留(哈希才能对得上)", () => {
  536. const futureRow = { ...assistantRow, futureField: "x" };
  537. const parsed = conversationSharePreviewDataSchema.parse(previewOf({ rows: [futureRow] }));
  538. expect(parsed.rows[0]).toEqual(futureRow);
  539. const decoded = decodeConversationShareRows(parsed.rows);
  540. expect(decoded.unsupportedCount).toBe(0);
  541. expect(decoded.rows).toHaveLength(1);
  542. });
  543. it.each([
  544. ["新增 row kind", { ...assistantRow, kind: "videoClip" }, "videoClip"],
  545. ["已有 kind 的新 enum 值", { ...assistantRow, state: "aborted" }, "assistantText"],
  546. [
  547. "新增 timelineMarker type",
  548. {
  549. rowId: 11,
  550. turnId: "turn-1",
  551. createdAt: 1,
  552. createdAtSeq: 2,
  553. kind: "timelineMarker",
  554. marker: { type: "brandNewMarker", detail: "x" },
  555. },
  556. "timelineMarker",
  557. ],
  558. [
  559. "新增 artifact type",
  560. {
  561. rowId: 12,
  562. turnId: "turn-1",
  563. createdAt: 1,
  564. createdAtSeq: 3,
  565. kind: "artifact",
  566. artifactVersionId: "a",
  567. logicalArtifactKey: "k",
  568. displayName: "data.csv",
  569. artifactType: "csv",
  570. mimeType: "text/csv",
  571. sizeBytes: 1,
  572. sha256: SHA_256,
  573. ref: "zcode-artifact://share/a",
  574. state: "current",
  575. },
  576. "artifact",
  577. ],
  578. ])("%s 只丢该行,其余内容照常呈现", (_label, futureRow, expectedKind) => {
  579. const parsed = conversationSharePreviewDataSchema.parse(
  580. previewOf({ rows: [assistantRow, futureRow] }),
  581. );
  582. const decoded = decodeConversationShareRows(parsed.rows);
  583. expect(decoded.rows).toHaveLength(1);
  584. expect(decoded.rows[0]?.rowId).toBe(assistantRow.rowId);
  585. expect(decoded.unsupportedCount).toBe(1);
  586. expect(decoded.unsupportedKinds).toEqual([expectedKind]);
  587. });
  588. it("schema_version 比本端新时收得下,由调用方转成「请升级」而不是「格式无效」", () => {
  589. const parsed = conversationSharePreviewDataSchema.parse(previewOf({ schema_version: 2 }));
  590. expect(parsed.schema_version).toBe(2);
  591. expect(isConversationShareSchemaVersionSupported(parsed.schema_version)).toBe(false);
  592. expect(isConversationShareSchemaVersionSupported(1)).toBe(true);
  593. });
  594. it("出站请求仍然严格:多带字段是本端 bug,必须当场炸掉", () => {
  595. expect(() =>
  596. conversationShareContinuationRequestSchema.parse({
  597. schema_version: 1,
  598. client_request_id: "request-1",
  599. unexpected: true,
  600. }),
  601. ).toThrow();
  602. });
  603. });