conversationSharePublicProjection.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import type { ConversationRow } from "@zcode/shared";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. assertConversationSharePublicProjection,
  5. buildConversationSharePublicProjection,
  6. } from "../src/conversation-share/conversationSharePublicProjection.js";
  7. function rowsWithCompletedSubagent(): ConversationRow[] {
  8. return [
  9. {
  10. rowId: 101,
  11. turnId: "local-turn-a",
  12. entityId: "local-entity-header",
  13. productTurnId: "local-product-turn-a",
  14. createdAt: 1_000,
  15. createdAtSeq: 10,
  16. actions: { canFork: true },
  17. kind: "turnHeader",
  18. origin: "userInput",
  19. executionKind: "agent",
  20. state: "completedSuccess",
  21. startedAt: 1_000,
  22. endedAt: 1_900,
  23. activeMs: 700,
  24. workSegments: [
  25. {
  26. segmentId: "segment-1",
  27. triggerEntityId: "local-entity-tool",
  28. startedAt: 1_100,
  29. endedAt: 1_800,
  30. activeMs: 700,
  31. },
  32. ],
  33. originMeta: {
  34. backgroundSource: "subagent",
  35. workId: "local-work-header",
  36. title: "后台结果",
  37. },
  38. fileChanges: { additions: 3, deletions: 1, files: 1 },
  39. },
  40. {
  41. rowId: 102,
  42. turnId: "local-turn-a",
  43. entityId: "local-entity-user",
  44. productTurnId: "local-product-turn-a",
  45. createdAt: 1_010,
  46. createdAtSeq: 11,
  47. actions: { canEdit: true },
  48. kind: "userInput",
  49. text: "请分析并生成报告",
  50. origin: "backgroundResult",
  51. originMeta: {
  52. backgroundSource: "subagent",
  53. workId: "local-work-user",
  54. senderSessionId: "local-child-session",
  55. senderLabel: "研究助手",
  56. },
  57. sourceCommandId: "source-command",
  58. rootSourceCommandId: "root-source-command",
  59. clientId: "client-desktop",
  60. },
  61. {
  62. rowId: 103,
  63. turnId: "local-turn-a",
  64. entityId: "local-entity-tool",
  65. productTurnId: "local-product-turn-a",
  66. createdAt: 1_020,
  67. createdAtSeq: 12,
  68. actions: { canRetry: true },
  69. kind: "toolCall",
  70. toolCallId: "local-tool-call",
  71. toolName: "Agent",
  72. status: "success",
  73. inputText: '{"description":"研究"}',
  74. input: { description: "研究" },
  75. output: { text: "子任务已完成" },
  76. progress: { bytes: 12, previewLine: "running", updatedAt: 1_500 },
  77. approvalInteractionId: "approval-1",
  78. backgrounded: true,
  79. workId: "local-work-tool",
  80. startedAt: 1_100,
  81. endedAt: 1_700,
  82. },
  83. {
  84. rowId: 104,
  85. turnId: "local-turn-a",
  86. entityId: "local-entity-subagent",
  87. productTurnId: "local-product-turn-a",
  88. createdAt: 1_030,
  89. createdAtSeq: 13,
  90. kind: "subagent",
  91. parentToolCallId: "local-tool-call",
  92. subagentType: "research",
  93. status: "success",
  94. summaryText: "研究完成",
  95. childSessionId: "local-child-session",
  96. startedAt: 1_100,
  97. endedAt: 1_700,
  98. },
  99. {
  100. rowId: 105,
  101. turnId: "local-turn-a",
  102. entityId: "local-entity-assistant",
  103. productTurnId: "local-product-turn-a",
  104. createdAt: 1_040,
  105. createdAtSeq: 14,
  106. actions: { canFork: true },
  107. kind: "assistantText",
  108. text: "报告已经生成",
  109. state: "complete",
  110. model: "glm",
  111. feedback: "like",
  112. },
  113. ];
  114. }
  115. function build(rows = rowsWithCompletedSubagent()) {
  116. return buildConversationSharePublicProjection({
  117. rows,
  118. selectedProductTurnIds: ["local-product-turn-a"],
  119. });
  120. }
  121. function expectBuildFailure(rows: ConversationRow[], kind: string): void {
  122. try {
  123. build(rows);
  124. throw new Error("Expected public projection construction to fail");
  125. } catch (error) {
  126. expect(error).toMatchObject({ kind });
  127. }
  128. }
  129. describe("conversation share public projection", () => {
  130. it("SHARE02:重建 artifact 公开身份并保留本地读取引用在 sidecar", () => {
  131. const rows = rowsWithCompletedSubagent();
  132. rows.push({
  133. rowId: 106,
  134. turnId: "local-turn-a",
  135. entityId: "local-artifact-entity",
  136. productTurnId: "local-product-turn-a",
  137. createdAt: 1_050,
  138. createdAtSeq: 15,
  139. kind: "artifact",
  140. artifactVersionId: "local-artifact-version",
  141. logicalArtifactKey: "local-report-key",
  142. displayName: "report.pdf",
  143. artifactType: "pdf",
  144. mimeType: "application/pdf",
  145. sizeBytes: 4,
  146. sha256: "a".repeat(64),
  147. ref: "/workspace/report.pdf",
  148. state: "current",
  149. });
  150. const result = build(rows);
  151. expect(result.rows.at(-1)).toMatchObject({
  152. kind: "artifact",
  153. artifactVersionId: "share-artifact-1",
  154. logicalArtifactKey: "share-artifact-key-1",
  155. ref: "zcode-artifact://share/share-artifact-1",
  156. productTurnId: "share-product-turn-1",
  157. });
  158. expect(result.artifacts).toEqual([
  159. {
  160. sourceRef: "/workspace/report.pdf",
  161. descriptor: {
  162. artifact_id: "share-artifact-1",
  163. logical_artifact_key: "share-artifact-key-1",
  164. producer_product_turn_id: "share-product-turn-1",
  165. artifact_version: 1,
  166. state: "current",
  167. ref: "zcode-artifact://share/share-artifact-1",
  168. artifact_type: "pdf",
  169. display_name: "report.pdf",
  170. extension: "pdf",
  171. mime_type: "application/pdf",
  172. size_bytes: 4,
  173. sha256: "a".repeat(64),
  174. },
  175. },
  176. ]);
  177. expect(JSON.stringify(result.rows)).not.toContain("/workspace/report.pdf");
  178. });
  179. it("filters completed subagent details while preserving Agent toolCall and public content", () => {
  180. const result = build();
  181. expect(result.selectedProductTurnIds).toEqual(["share-product-turn-1"]);
  182. expect(result.rows.map((row) => row.kind)).toEqual([
  183. "turnHeader",
  184. "userInput",
  185. "toolCall",
  186. "assistantText",
  187. ]);
  188. expect(result.rows.map((row) => row.rowId)).toEqual([1, 2, 3, 4]);
  189. expect(result.rows.map((row) => row.entityId)).toEqual([
  190. "share-entity-1",
  191. "share-entity-2",
  192. "share-entity-3",
  193. "share-entity-4",
  194. ]);
  195. expect(result.rows.every((row) => row.productTurnId === "share-product-turn-1")).toBe(true);
  196. expect(result.rows[0]).toMatchObject({
  197. kind: "turnHeader",
  198. turnId: "share-turn-1",
  199. workSegments: [expect.objectContaining({ triggerEntityId: "share-entity-3" })],
  200. });
  201. expect(result.rows[0]).not.toHaveProperty("originMeta");
  202. expect(result.rows[1]).toMatchObject({
  203. kind: "userInput",
  204. originMeta: {
  205. backgroundSource: "subagent",
  206. senderLabel: "研究助手",
  207. },
  208. });
  209. expect(result.rows[2]).toMatchObject({
  210. kind: "toolCall",
  211. toolName: "Agent",
  212. toolCallId: "share-tool-call-1",
  213. status: "success",
  214. output: { text: "子任务已完成" },
  215. });
  216. expect(result.rows[2]).not.toHaveProperty("progress");
  217. expect(result.rows[2]).not.toHaveProperty("approvalInteractionId");
  218. expect(result.rows[2]).not.toHaveProperty("workId");
  219. expect(result.rows[3]).not.toHaveProperty("feedback");
  220. expect(JSON.stringify(result.rows)).not.toMatch(
  221. /local-product-turn-a|local-turn-a|local-entity|local-tool-call|source-command|client-desktop|approval-1|local-work/u,
  222. );
  223. });
  224. it("allows rows to share one persistent entity identity", () => {
  225. const rows = rowsWithCompletedSubagent();
  226. rows[1] = { ...rows[1]!, entityId: rows[0]!.entityId } as ConversationRow;
  227. const result = build(rows);
  228. expect(result.rows[0]?.entityId).toBe("share-entity-1");
  229. expect(result.rows[1]?.entityId).toBe("share-entity-1");
  230. });
  231. it("is deterministic and rejects a dangling retained entity reference", () => {
  232. const first = build();
  233. const second = build();
  234. expect(second).toEqual(first);
  235. const header = first.rows[0];
  236. if (header?.kind !== "turnHeader" || !header.workSegments?.[0]) {
  237. throw new Error("Expected projected turn header work segment");
  238. }
  239. header.workSegments[0].triggerEntityId = "share-entity-999";
  240. expect(() =>
  241. assertConversationSharePublicProjection({
  242. rows: first.rows,
  243. selectedProductTurnIds: first.selectedProductTurnIds,
  244. }),
  245. ).toThrowError(expect.objectContaining({ kind: "invalid_conversation" }));
  246. });
  247. it("rejects active rows before preparing a share", () => {
  248. const rows = rowsWithCompletedSubagent();
  249. rows[0] = { ...rows[0]!, kind: "turnHeader", state: "running" };
  250. expectBuildFailure(rows, "invalid_conversation");
  251. });
  252. it("drops local attachment refs until the publication stage rewrites them", () => {
  253. const rows = rowsWithCompletedSubagent();
  254. rows[1] = {
  255. ...rows[1]!,
  256. kind: "userInput",
  257. attachments: [
  258. {
  259. ref: "attachment/local-file",
  260. fileName: "report.pdf",
  261. mime: "application/pdf",
  262. bytes: 10,
  263. },
  264. ],
  265. };
  266. const result = build(rows);
  267. expect(result.rows.find((row) => row.kind === "userInput")).not.toHaveProperty("attachments");
  268. });
  269. it("preserves share attachment refs in the public projection", () => {
  270. const rows = rowsWithCompletedSubagent();
  271. rows[1] = {
  272. ...rows[1]!,
  273. kind: "userInput",
  274. attachments: [
  275. {
  276. ref: "zcode-artifact://share/share-input-artifact-1",
  277. previewRef: "zcode-artifact://share/share-input-artifact-1",
  278. fileName: "pasted-text.txt",
  279. mime: "text/plain",
  280. bytes: 10,
  281. },
  282. ],
  283. };
  284. const result = build(rows);
  285. expect(result.rows.find((row) => row.kind === "userInput")).toMatchObject({
  286. attachments: [
  287. expect.objectContaining({ ref: "zcode-artifact://share/share-input-artifact-1" }),
  288. ],
  289. });
  290. });
  291. it.each([
  292. ["data:text/plain;base64,YQ==", "unsafe_structure"],
  293. ["file:///tmp/report.html", "unsafe_structure"],
  294. ["zcode-artifact://share/report", "artifact_protocol_not_ready"],
  295. ] as const)("rejects unsupported local reference %s", (text, kind) => {
  296. const rows = rowsWithCompletedSubagent();
  297. rows[4] = {
  298. ...rows[4]!,
  299. kind: "assistantText",
  300. text,
  301. state: "complete",
  302. };
  303. expectBuildFailure(rows, kind);
  304. });
  305. it("SHARE02:移除本地 truncated 引用但保留可公开的工具输出预览", () => {
  306. const rows = rowsWithCompletedSubagent();
  307. rows[2] = {
  308. ...rows[2]!,
  309. kind: "toolCall",
  310. output: {
  311. text: "head...tail",
  312. truncated: { totalBytes: 100_000, ref: "tool-output/local-tool-call" },
  313. },
  314. };
  315. const projection = build(rows);
  316. expect(projection.rows.find((row) => row.kind === "toolCall")?.output).toEqual({
  317. text: "head...tail",
  318. });
  319. });
  320. });