remotePromptAttachments.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { describe, expect, it, vi } from "vitest";
  2. import { PassThrough } from "node:stream";
  3. import type { IDisposable, Event } from "@zcode/rpc";
  4. import type { ZCodePromptAttachment } from "@zcode/shared";
  5. import {
  6. REMOTE_PROMPT_ATTACHMENT_ROOT,
  7. buildRemotePromptAttachmentPath,
  8. materializeRemotePromptAttachments,
  9. } from "../src/host/remotePromptAttachments.js";
  10. function createClosedStream(stdoutText = "") {
  11. const stdin = new PassThrough();
  12. const stdout = new PassThrough();
  13. const stderr = new PassThrough();
  14. const onClose: Event<number> = (listener: (code: number) => void): IDisposable => {
  15. queueMicrotask(() => {
  16. if (stdoutText) {
  17. stdout.write(stdoutText);
  18. }
  19. stdout.end();
  20. listener(0);
  21. });
  22. return { dispose() {} };
  23. };
  24. return { stdin, stdout, stderr, onClose };
  25. }
  26. describe("remote prompt attachment materialization", () => {
  27. it("uploads host localPath attachments and rewrites prompt paths", async () => {
  28. const upload = vi.fn(async (_localPath: string, _remotePath: string) => undefined);
  29. const exec = vi
  30. .fn()
  31. .mockResolvedValueOnce(createClosedStream("/home/tester"))
  32. .mockResolvedValueOnce(createClosedStream())
  33. .mockResolvedValueOnce(createClosedStream());
  34. const hostPath = "C:\\Users\\tester\\.zcode\\tmp\\paste-attachments\\pasted-text.txt";
  35. const attachments: ZCodePromptAttachment[] = [
  36. {
  37. kind: "file",
  38. filename: "pasted text.txt",
  39. localPath: hostPath,
  40. mimeType: "text/plain",
  41. sizeBytes: 8192,
  42. sourceKind: "clipboard-text",
  43. },
  44. {
  45. kind: "image",
  46. filename: "inline.png",
  47. mimeType: "image/png",
  48. dataBase64: "aW1hZ2U=",
  49. sizeBytes: 5,
  50. },
  51. ];
  52. const result = await materializeRemotePromptAttachments(
  53. {
  54. content: `请读取 ${hostPath}`,
  55. traceId: "trace:abc/123",
  56. attachments,
  57. },
  58. { backend: { exec, upload } },
  59. );
  60. const remotePath = upload.mock.calls[0]?.[1] ?? "";
  61. expect(remotePath).toMatch(
  62. /^\/home\/tester\/\.zcode\/tmp\/prompt-attachments\/trace-abc-123\/[A-Za-z0-9._-]+\/01-pasted-text\.txt$/,
  63. );
  64. expect(upload).toHaveBeenCalledWith(hostPath, remotePath);
  65. expect(exec.mock.calls[0]?.[0]).toBe('printf %s "$HOME"');
  66. expect(exec.mock.calls[1]?.[0]).toContain("mkdir -p");
  67. expect(exec.mock.calls[1]?.[0]).toContain("command chmod 700");
  68. expect(exec.mock.calls[1]?.[0]).not.toContain('"$HOME"');
  69. expect(exec.mock.calls[2]?.[0]).toContain("command chmod 600");
  70. expect(exec.mock.calls[2]?.[0]).not.toContain('"$HOME"');
  71. expect(result.uploadedCount).toBe(1);
  72. expect(result.content).toBe(`请读取 ${remotePath}`);
  73. expect(result.attachments?.[0]).toEqual({
  74. ...attachments[0],
  75. localPath: remotePath,
  76. });
  77. expect(result.attachments?.[1]).toBe(attachments[1]);
  78. });
  79. it("skips attachments that already point at the remote materialization directory", async () => {
  80. const upload = vi.fn(async (_localPath: string, _remotePath: string) => undefined);
  81. const exec = vi.fn(async (_command: string) => createClosedStream());
  82. const remotePath = `${REMOTE_PROMPT_ATTACHMENT_ROOT}/trace/01-image.png`;
  83. const attachments: ZCodePromptAttachment[] = [
  84. {
  85. kind: "image",
  86. filename: "image.png",
  87. localPath: remotePath,
  88. mimeType: "image/png",
  89. },
  90. ];
  91. const result = await materializeRemotePromptAttachments(
  92. { content: remotePath, traceId: "trace", attachments },
  93. { backend: { exec, upload } },
  94. );
  95. expect(exec).not.toHaveBeenCalled();
  96. expect(upload).not.toHaveBeenCalled();
  97. expect(result.uploadedCount).toBe(0);
  98. expect(result.attachments).toBe(attachments);
  99. expect(result.content).toBe(remotePath);
  100. });
  101. it("skips attachments that already point at the resolved remote materialization directory", async () => {
  102. const upload = vi.fn(async (_localPath: string, _remotePath: string) => undefined);
  103. const exec = vi.fn(async (_command: string) => createClosedStream("/home/tester"));
  104. const remotePath = "/home/tester/.zcode/tmp/prompt-attachments/trace/01-image.png";
  105. const attachments: ZCodePromptAttachment[] = [
  106. {
  107. kind: "image",
  108. filename: "image.png",
  109. localPath: remotePath,
  110. mimeType: "image/png",
  111. },
  112. ];
  113. const result = await materializeRemotePromptAttachments(
  114. { content: remotePath, traceId: "trace", attachments },
  115. { backend: { exec, upload } },
  116. );
  117. expect(exec).toHaveBeenCalledTimes(1);
  118. expect(upload).not.toHaveBeenCalled();
  119. expect(result.uploadedCount).toBe(0);
  120. expect(result.attachments).toBe(attachments);
  121. expect(result.content).toBe(remotePath);
  122. });
  123. it("fails before sending a prompt with an unreadable host attachment path", async () => {
  124. const upload = vi.fn(async (_localPath: string, _remotePath: string) => {
  125. throw new Error("EACCES");
  126. });
  127. const exec = vi
  128. .fn()
  129. .mockResolvedValueOnce(createClosedStream("/home/tester"))
  130. .mockResolvedValueOnce(createClosedStream());
  131. await expect(
  132. materializeRemotePromptAttachments(
  133. {
  134. content: "inspect",
  135. traceId: "trace",
  136. attachments: [
  137. {
  138. kind: "file",
  139. filename: "secret.txt",
  140. localPath: "C:\\secret.txt",
  141. mimeType: "text/plain",
  142. sizeBytes: 1,
  143. },
  144. ],
  145. },
  146. { backend: { exec, upload } },
  147. ),
  148. ).rejects.toThrow("远端附件上传失败:secret.txt");
  149. });
  150. it("builds private safe remote paths", () => {
  151. expect(
  152. buildRemotePromptAttachmentPath({
  153. filename: "../../api 文档.txt",
  154. index: 2,
  155. nonce: "nonce:with/slash",
  156. traceId: "trace/id:456",
  157. }),
  158. ).toBe(`${REMOTE_PROMPT_ATTACHMENT_ROOT}/trace-id-456/nonce-with-slash/03-api-.txt`);
  159. });
  160. });