trust-manager.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import {
  6. getProjectTrustPath,
  7. hasProjectConfigDir,
  8. hasProjectTrustInputs,
  9. ProjectTrustStore,
  10. } from "../src/core/trust-manager.ts";
  11. describe("ProjectTrustStore", () => {
  12. let tempDir: string;
  13. let agentDir: string;
  14. let cwd: string;
  15. beforeEach(() => {
  16. tempDir = join(tmpdir(), `trust-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
  17. agentDir = join(tempDir, "agent");
  18. cwd = join(tempDir, "project");
  19. mkdirSync(agentDir, { recursive: true });
  20. mkdirSync(cwd, { recursive: true });
  21. });
  22. afterEach(() => {
  23. rmSync(tempDir, { recursive: true, force: true });
  24. });
  25. it("stores decisions per cwd", () => {
  26. const store = new ProjectTrustStore(agentDir);
  27. expect(store.get(cwd)).toBeNull();
  28. expect(store.getEntry(cwd)).toBeNull();
  29. store.set(cwd, true);
  30. expect(store.get(cwd)).toBe(true);
  31. expect(store.getEntry(cwd)).toEqual({ path: getProjectTrustPath(cwd), decision: true });
  32. store.set(cwd, false);
  33. expect(store.get(cwd)).toBe(false);
  34. expect(store.getEntry(cwd)).toEqual({ path: getProjectTrustPath(cwd), decision: false });
  35. store.set(cwd, null);
  36. expect(store.get(cwd)).toBeNull();
  37. expect(store.getEntry(cwd)).toBeNull();
  38. });
  39. it("inherits the closest saved decision from parent directories", () => {
  40. const store = new ProjectTrustStore(agentDir);
  41. const parentDir = join(tempDir, "trusted-parent");
  42. const childDir = join(parentDir, "project");
  43. const grandchildDir = join(childDir, "nested");
  44. mkdirSync(grandchildDir, { recursive: true });
  45. store.set(parentDir, true);
  46. expect(store.get(childDir)).toBe(true);
  47. expect(store.getEntry(childDir)).toEqual({ path: getProjectTrustPath(parentDir), decision: true });
  48. expect(store.get(grandchildDir)).toBe(true);
  49. expect(store.getEntry(grandchildDir)).toEqual({ path: getProjectTrustPath(parentDir), decision: true });
  50. store.set(childDir, false);
  51. expect(store.get(grandchildDir)).toBe(false);
  52. expect(store.getEntry(grandchildDir)).toEqual({ path: getProjectTrustPath(childDir), decision: false });
  53. });
  54. it("can clear a child override to inherit parent trust", () => {
  55. const store = new ProjectTrustStore(agentDir);
  56. const parentDir = join(tempDir, "trusted-parent");
  57. const childDir = join(parentDir, "project");
  58. mkdirSync(childDir, { recursive: true });
  59. store.set(parentDir, true);
  60. store.set(childDir, false);
  61. expect(store.getEntry(childDir)).toEqual({ path: getProjectTrustPath(childDir), decision: false });
  62. store.setMany([
  63. { path: parentDir, decision: true },
  64. { path: childDir, decision: null },
  65. ]);
  66. expect(store.get(childDir)).toBe(true);
  67. expect(store.getEntry(childDir)).toEqual({ path: getProjectTrustPath(parentDir), decision: true });
  68. });
  69. it("fails loudly without overwriting malformed trust stores", () => {
  70. const trustPath = join(agentDir, "trust.json");
  71. writeFileSync(trustPath, "{not json", "utf-8");
  72. const store = new ProjectTrustStore(agentDir);
  73. expect(() => store.get(cwd)).toThrow(/Failed to read trust store/);
  74. expect(() => store.set(cwd, true)).toThrow(/Failed to read trust store/);
  75. expect(readFileSync(trustPath, "utf-8")).toBe("{not json");
  76. });
  77. it("detects project trust inputs", () => {
  78. expect(hasProjectConfigDir(cwd)).toBe(false);
  79. expect(hasProjectTrustInputs(cwd)).toBe(false);
  80. mkdirSync(join(cwd, ".pi"), { recursive: true });
  81. expect(hasProjectConfigDir(cwd)).toBe(true);
  82. expect(hasProjectTrustInputs(cwd)).toBe(true);
  83. rmSync(join(cwd, ".pi"), { recursive: true, force: true });
  84. writeFileSync(join(cwd, "AGENTS.md"), "Project instructions");
  85. expect(hasProjectTrustInputs(cwd)).toBe(false);
  86. rmSync(join(cwd, "AGENTS.md"), { force: true });
  87. writeFileSync(join(cwd, "CLAUDE.md"), "Legacy project instructions");
  88. expect(hasProjectTrustInputs(cwd)).toBe(false);
  89. rmSync(join(cwd, "CLAUDE.md"), { force: true });
  90. mkdirSync(join(cwd, ".agents", "skills"), { recursive: true });
  91. expect(hasProjectTrustInputs(cwd)).toBe(true);
  92. });
  93. });