botGroupAttachmentUpload.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { describe, expect, it, vi } from "vitest";
  2. import { uploadBotGroupAttachments } from "../src/zcode-agent/groupAttachmentUpload.js";
  3. describe("group attachment transaction", () => {
  4. it("does not release history refs until the target commits the text bytes", async () => {
  5. let complete!: (value: { ref: string }) => void;
  6. const committed = new Promise<{ ref: string }>((resolve) => {
  7. complete = resolve;
  8. });
  9. const agent = {
  10. attachmentBeginV4: vi.fn(async () => ({ state: "uploading", nextChunkIndex: 0 })),
  11. attachmentChunkV4: vi.fn(async (_params: { dataBase64: string }) => ({ nextChunkIndex: 1 })),
  12. attachmentCommitV4: vi.fn(() => committed),
  13. attachmentAbortV4: vi.fn(),
  14. };
  15. let released = false;
  16. const upload = uploadBotGroupAttachments(
  17. agent as never,
  18. { sessionId: "task", workspacePath: "/work" },
  19. [
  20. {
  21. kind: "file",
  22. filename: "history.txt",
  23. mimeType: "text/plain",
  24. sourceKind: "topic-history",
  25. messageCount: 1,
  26. textContent: "北京\n历史原文",
  27. sizeBytes: 19,
  28. },
  29. ],
  30. ).then((refs) => {
  31. released = true;
  32. return refs;
  33. });
  34. await vi.waitFor(() => expect(agent.attachmentCommitV4).toHaveBeenCalledOnce());
  35. expect(released).toBe(false);
  36. expect(
  37. Buffer.from(agent.attachmentChunkV4.mock.calls[0]![0].dataBase64, "base64").toString(),
  38. ).toBe("北京\n历史原文");
  39. complete({ ref: "zcode-artifact://history" });
  40. expect(await upload).toEqual([
  41. expect.objectContaining({ ref: "zcode-artifact://history", sourceKind: "topic-history" }),
  42. ]);
  43. });
  44. it("aborts a cancelled upload before committing it", async () => {
  45. const controller = new AbortController();
  46. const agent = {
  47. attachmentBeginV4: vi.fn(async () => ({ state: "uploading", nextChunkIndex: 0 })),
  48. attachmentChunkV4: vi.fn(async () => {
  49. controller.abort();
  50. return { nextChunkIndex: 1 };
  51. }),
  52. attachmentCommitV4: vi.fn(),
  53. attachmentAbortV4: vi.fn(async () => undefined),
  54. };
  55. await expect(
  56. uploadBotGroupAttachments(
  57. agent as never,
  58. { workspacePath: "/work", sessionId: "task" },
  59. [
  60. {
  61. kind: "file",
  62. filename: "a.txt",
  63. mimeType: "text/plain",
  64. sizeBytes: 3,
  65. dataBase64: "YWJj",
  66. },
  67. ],
  68. controller.signal,
  69. ),
  70. ).rejects.toThrow();
  71. expect(agent.attachmentCommitV4).not.toHaveBeenCalled();
  72. expect(agent.attachmentAbortV4).toHaveBeenCalledOnce();
  73. });
  74. it("commits a content reference on the target runtime before admission", async () => {
  75. const agent = {
  76. attachmentBeginV4: vi.fn(async () => ({ state: "uploading", nextChunkIndex: 0 })),
  77. attachmentChunkV4: vi.fn(async () => ({ nextChunkIndex: 1 })),
  78. attachmentCommitV4: vi.fn(async () => ({ ref: "zcode-artifact://file" })),
  79. attachmentAbortV4: vi.fn(async () => undefined),
  80. };
  81. const refs = await uploadBotGroupAttachments(
  82. agent as never,
  83. {
  84. workspacePath: "/work",
  85. workspaceIdentity: "ssh://host/work",
  86. remoteSessionId: "remote",
  87. sessionId: "task",
  88. },
  89. [
  90. {
  91. kind: "file",
  92. filename: "a.txt",
  93. sourceKind: "topic-history",
  94. messageCount: 2,
  95. mimeType: "text/plain",
  96. sizeBytes: 3,
  97. dataBase64: "YWJj",
  98. },
  99. ],
  100. );
  101. expect(refs[0]?.ref).toBe("zcode-artifact://file");
  102. expect(refs[0]).toMatchObject({ sourceKind: "topic-history", messageCount: 2 });
  103. expect(agent.attachmentBeginV4).toHaveBeenCalledWith(
  104. expect.objectContaining({
  105. workspaceIdentity: "ssh://host/work",
  106. remoteSessionId: "remote",
  107. totalBytes: 3,
  108. checksum: expect.stringMatching(/^sha256:/),
  109. }),
  110. );
  111. });
  112. it("aborts on chunk failure instead of submitting incomplete data", async () => {
  113. const agent = {
  114. attachmentBeginV4: vi.fn(async () => ({ state: "uploading", nextChunkIndex: 0 })),
  115. attachmentChunkV4: vi.fn(async () => {
  116. throw new Error("offline");
  117. }),
  118. attachmentCommitV4: vi.fn(),
  119. attachmentAbortV4: vi.fn(async () => undefined),
  120. };
  121. await expect(
  122. uploadBotGroupAttachments(agent as never, { workspacePath: "/work", sessionId: "task" }, [
  123. { kind: "image", filename: "a.png", mimeType: "image/png", dataBase64: "YWJj" },
  124. ]),
  125. ).rejects.toThrow("offline");
  126. expect(agent.attachmentAbortV4).toHaveBeenCalledOnce();
  127. expect(agent.attachmentCommitV4).not.toHaveBeenCalled();
  128. });
  129. });