conversationShareArtifactSource.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { mkdtemp, mkdir, realpath, rm, symlink, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import {
  6. createLocalConversationShareArtifactSource,
  7. createRemoteConversationShareArtifactSource,
  8. } from "../src/conversation-share/conversationShareArtifactSource.js";
  9. const temporaryDirectories: string[] = [];
  10. // Windows 无开发者模式时创建 symlink 会 EPERM;symlink 边界断言按能力探测跳过
  11. const canCreateSymlink = await (async () => {
  12. const probeRoot = await mkdtemp(join(tmpdir(), "zcode-symlink-probe-"));
  13. try {
  14. await symlink(probeRoot, join(probeRoot, "link"));
  15. return true;
  16. } catch {
  17. return false;
  18. } finally {
  19. await rm(probeRoot, { force: true, recursive: true });
  20. }
  21. })();
  22. afterEach(async () => {
  23. await Promise.all(
  24. temporaryDirectories.splice(0).map((path) => rm(path, { recursive: true, force: true })),
  25. );
  26. });
  27. describe("conversation share local artifact source", () => {
  28. it("SHARE06:异步读取 workspace 内文件并执行大小上限", async () => {
  29. const root = await mkdtemp(join(tmpdir(), "zcode-share-artifact-"));
  30. temporaryDirectories.push(root);
  31. const path = join(root, "report.pdf");
  32. await writeFile(path, "pdf!");
  33. const source = createLocalConversationShareArtifactSource();
  34. await expect(source.read({ workspacePath: root, ref: path, maxBytes: 4 })).resolves.toEqual({
  35. bytes: new TextEncoder().encode("pdf!"),
  36. canonicalPath: await realpath(path),
  37. });
  38. await expect(
  39. source.read({ workspacePath: root, ref: path, maxBytes: 3 }),
  40. ).rejects.toMatchObject({ kind: "limit_exceeded" });
  41. await expect(source.stat({ workspacePath: root, ref: path })).resolves.toEqual({
  42. canonicalPath: await realpath(path),
  43. size: 4,
  44. mtimeMs: expect.any(Number),
  45. });
  46. });
  47. it("SHARE06:拒绝 workspace 外路径和指向外部的符号链接", async () => {
  48. const parent = await mkdtemp(join(tmpdir(), "zcode-share-boundary-"));
  49. temporaryDirectories.push(parent);
  50. const root = join(parent, "workspace");
  51. await mkdir(root);
  52. const outside = join(parent, "outside.pdf");
  53. await writeFile(outside, "secret");
  54. const source = createLocalConversationShareArtifactSource();
  55. await expect(
  56. source.read({ workspacePath: root, ref: outside, maxBytes: 100 }),
  57. ).rejects.toMatchObject({ kind: "unsafe_structure" });
  58. if (!canCreateSymlink) {
  59. return;
  60. }
  61. const link = join(root, "linked.pdf");
  62. await symlink(outside, link);
  63. await expect(
  64. source.read({ workspacePath: root, ref: link, maxBytes: 100 }),
  65. ).rejects.toMatchObject({ kind: "unsafe_structure" });
  66. });
  67. it("缺失文件报 artifact_read_failed 并保留 errno 与 cause", async () => {
  68. const root = await mkdtemp(join(tmpdir(), "zcode-share-missing-"));
  69. temporaryDirectories.push(root);
  70. const source = createLocalConversationShareArtifactSource();
  71. // 正文里的模板占位符会走到这里;调用方靠 reasonCode 决定跳过而不是打死整次发布。
  72. await expect(
  73. source.read({ workspacePath: root, ref: join(root, "报告.pdf"), maxBytes: 100 }),
  74. ).rejects.toMatchObject({
  75. kind: "invalid_conversation",
  76. reasonCode: "artifact_read_failed",
  77. diagnostics: { errno: "ENOENT" },
  78. cause: { code: "ENOENT" },
  79. });
  80. });
  81. it("目录形状的引用报 artifact_read_failed 而非崩溃", async () => {
  82. const root = await mkdtemp(join(tmpdir(), "zcode-share-dir-"));
  83. temporaryDirectories.push(root);
  84. await mkdir(join(root, "report.pdf"));
  85. const source = createLocalConversationShareArtifactSource();
  86. await expect(
  87. source.read({ workspacePath: root, ref: join(root, "report.pdf"), maxBytes: 100 }),
  88. ).rejects.toMatchObject({ reasonCode: "artifact_read_failed" });
  89. });
  90. it("metadata stat 对缺失文件返回可分类的 artifact_read_failed", async () => {
  91. const root = await mkdtemp(join(tmpdir(), "zcode-share-stat-missing-"));
  92. temporaryDirectories.push(root);
  93. const source = createLocalConversationShareArtifactSource();
  94. await expect(
  95. source.stat({ workspacePath: root, ref: join(root, "不存在.pdf") }),
  96. ).rejects.toMatchObject({
  97. kind: "invalid_conversation",
  98. reasonCode: "artifact_read_failed",
  99. diagnostics: { errno: "ENOENT" },
  100. });
  101. });
  102. });
  103. describe("conversation share remote artifact source", () => {
  104. it("远端连接错误不应被伪装成可跳过的文件读取失败", async () => {
  105. const source = createRemoteConversationShareArtifactSource({
  106. resolvePath: async ({ path }) => path,
  107. stat: async () => {
  108. throw new Error("remote connection closed");
  109. },
  110. readFileRange: vi.fn(),
  111. });
  112. await expect(
  113. source.read({ workspacePath: "/workspace", ref: "report.pdf", maxBytes: 100 }),
  114. ).rejects.toThrow("remote connection closed");
  115. });
  116. it("按 range 分块读取远端文件并保留远端 canonical path", async () => {
  117. const content = new TextEncoder().encode("remote-report");
  118. const readFileRange = async ({ offset, length }: { offset: number; length: number }) =>
  119. content.slice(offset, offset + length);
  120. const source = createRemoteConversationShareArtifactSource({
  121. resolvePath: async ({ path }) => path.replace("/alias", "/workspace"),
  122. stat: async ({ path }) => ({
  123. path,
  124. type: "file" as const,
  125. size: content.byteLength,
  126. mtimeMs: 7,
  127. }),
  128. readFileRange,
  129. });
  130. await expect(
  131. source.read({ workspacePath: "/alias", ref: "report.pdf", maxBytes: 100 }),
  132. ).resolves.toEqual({ bytes: content, canonicalPath: "/workspace/report.pdf" });
  133. });
  134. it("拒绝越界路径、空 chunk 和读取期间变化", async () => {
  135. const base = {
  136. resolvePath: async ({ path }: { path: string }) => path,
  137. stat: async ({ path }: { path: string }) => ({
  138. path,
  139. type: "file" as const,
  140. size: 4,
  141. mtimeMs: 1,
  142. }),
  143. };
  144. await expect(
  145. createRemoteConversationShareArtifactSource({
  146. ...base,
  147. readFileRange: async () => new Uint8Array([1]),
  148. }).read({ workspacePath: "/workspace", ref: "/outside/report.pdf", maxBytes: 10 }),
  149. ).rejects.toMatchObject({ kind: "unsafe_structure" });
  150. await expect(
  151. createRemoteConversationShareArtifactSource({
  152. ...base,
  153. readFileRange: async () => new Uint8Array(0),
  154. }).read({ workspacePath: "/workspace", ref: "report.pdf", maxBytes: 10 }),
  155. ).rejects.toMatchObject({ kind: "invalid_conversation" });
  156. let statCalls = 0;
  157. await expect(
  158. createRemoteConversationShareArtifactSource({
  159. resolvePath: base.resolvePath,
  160. stat: async ({ path }) => ({
  161. path,
  162. type: "file" as const,
  163. size: 4,
  164. mtimeMs: ++statCalls,
  165. }),
  166. readFileRange: async () => new Uint8Array([1, 2, 3, 4]),
  167. }).read({ workspacePath: "/workspace", ref: "report.pdf", maxBytes: 10 }),
  168. ).rejects.toMatchObject({ kind: "invalid_conversation" });
  169. await expect(
  170. createRemoteConversationShareArtifactSource({
  171. ...base,
  172. stat: async ({ path }) => ({ path, type: "file" as const, size: 4, mtimeMs: 9 }),
  173. readFileRange: async () => new Uint8Array([1, 2, 3, 4]),
  174. }).stat({ workspacePath: "/workspace", ref: "report.pdf" }),
  175. ).resolves.toEqual({ canonicalPath: "/workspace/report.pdf", size: 4, mtimeMs: 9 });
  176. });
  177. });