io-boundary.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { readdir, readFile } from "node:fs/promises";
  2. import { join, relative } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. const repoRoot = join(import.meta.dirname, "..", "..", "..");
  5. const allowedNetworkBoundaryFiles = new Set([
  6. "packages/adapters/src/auth/localhost-callback.ts",
  7. "packages/adapters/src/http/index.ts",
  8. // 仅使用 node:http 的状态码/类型,不执行网络请求。
  9. "packages/adapters/src/http/public-egress-policy.ts",
  10. // 通过调用方注入的 fetch 执行正式 Adapter 网络边界。
  11. "packages/adapters/src/model/model-option-map-fetch.ts",
  12. "packages/adapters/src/model/provider-endpoint-routing-fetch.ts",
  13. // 只读 node:http 的 IncomingMessage 类型,把已到手的响应交给 http/index.ts 与 proxy-fetch.ts,不发请求。
  14. "packages/adapters/src/network/incoming-response.ts",
  15. "packages/adapters/src/network/proxy-fetch.ts",
  16. "packages/cli/src/provider-runtime-env.ts",
  17. // WebFetch 工具只读取 node:http 状态码,真实请求由注入的执行端口完成。
  18. "packages/core/src/tool/handlers/webfetch.ts",
  19. ]);
  20. async function collectTypeScriptFiles(directory: string): Promise<string[]> {
  21. const entries = await readdir(directory, { withFileTypes: true });
  22. const files: string[] = [];
  23. for (const entry of entries) {
  24. const path = join(directory, entry.name);
  25. if (entry.isDirectory()) {
  26. files.push(...(await collectTypeScriptFiles(path)));
  27. continue;
  28. }
  29. if (entry.isFile() && path.endsWith(".ts")) {
  30. files.push(path);
  31. }
  32. }
  33. return files;
  34. }
  35. describe("core I/O boundaries", () => {
  36. it("keeps file tools and context assembly free of direct host I/O", async () => {
  37. const files = [
  38. "packages/core/src/runtime.ts",
  39. "packages/core/src/context/sections/request-user-context.ts",
  40. "packages/core/src/tool/handlers/read.ts",
  41. "packages/core/src/tool/handlers/write.ts",
  42. "packages/core/src/tool/handlers/edit.ts",
  43. "packages/core/src/tool/handlers/skill.ts",
  44. ];
  45. for (const file of files) {
  46. const content = await readFile(join(repoRoot, file), "utf8");
  47. expect(content, file).not.toMatch(
  48. /from\s+["'](?:node:fs|node:fs\/promises|fs\/promises)["']/,
  49. );
  50. expect(content, file).not.toMatch(/process\.(?:cwd|env|platform)/);
  51. }
  52. });
  53. it("keeps in-process HTTP egress behind adapter network boundaries", async () => {
  54. const packageSourceRoots = [
  55. "packages/adapters/src",
  56. "packages/bootstrap/src",
  57. "packages/cli/src",
  58. "packages/contracts/src",
  59. "packages/core/src",
  60. "packages/tui/src",
  61. ];
  62. const files = (
  63. await Promise.all(
  64. packageSourceRoots.map((root) => collectTypeScriptFiles(join(repoRoot, root))),
  65. )
  66. ).flat();
  67. const violations: string[] = [];
  68. for (const file of files) {
  69. const relativePath = relative(repoRoot, file);
  70. if (allowedNetworkBoundaryFiles.has(relativePath)) {
  71. continue;
  72. }
  73. const content = await readFile(file, "utf8");
  74. if (
  75. /\bfetch\s*\(/.test(content) ||
  76. /\bhttps?\.request\s*\(/.test(content) ||
  77. /from\s+["']node:https?["']/.test(content)
  78. ) {
  79. violations.push(relativePath);
  80. }
  81. }
  82. expect(violations).toEqual([]);
  83. });
  84. });