|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { mkdtemp, readFile, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import { |
| 7 | + loadProjectTrust, |
| 8 | + projectTrustPath, |
| 9 | + trustMcpServer, |
| 10 | + trustPlugin, |
| 11 | + type ProjectTrustStore, |
| 12 | +} from "./project-trust.js"; |
| 13 | +import type { MCPServerConfig } from "../config/settings.js"; |
| 14 | + |
| 15 | +async function withTempHome(fn: (home: string, cwd: string) => Promise<void>): Promise<void> { |
| 16 | + const home = await mkdtemp(join(tmpdir(), "project-trust-test-")); |
| 17 | + try { |
| 18 | + await fn(home, "/repo/under/test"); |
| 19 | + } finally { |
| 20 | + await rm(home, { recursive: true, force: true }); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const mcpServer = (name: string): MCPServerConfig => ({ name, command: "node", args: [name] }); |
| 25 | + |
| 26 | +describe("project trust store", () => { |
| 27 | + test("concurrent plugin trust grants both survive without a corrupt file", async () => { |
| 28 | + await withTempHome(async (home, cwd) => { |
| 29 | + await Promise.all([ |
| 30 | + trustPlugin(cwd, "/plugins/a", home), |
| 31 | + trustPlugin(cwd, "/plugins/b", home), |
| 32 | + ]); |
| 33 | + |
| 34 | + const store = await loadProjectTrust(cwd, home); |
| 35 | + expect(store.trustedPluginPaths.sort()).toEqual(["/plugins/a", "/plugins/b"]); |
| 36 | + |
| 37 | + // File on disk must be complete, valid JSON — not truncated by an |
| 38 | + // interleaved write. |
| 39 | + const raw = await readFile(projectTrustPath(cwd, home), "utf8"); |
| 40 | + expect(() => JSON.parse(raw)).not.toThrow(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + test("concurrent plugin-trust and MCP-trust updates both survive", async () => { |
| 45 | + await withTempHome(async (home, cwd) => { |
| 46 | + const server1 = mcpServer("server-one"); |
| 47 | + const server2 = mcpServer("server-two"); |
| 48 | + |
| 49 | + await Promise.all([ |
| 50 | + trustPlugin(cwd, "/plugins/a", home), |
| 51 | + trustMcpServer(cwd, server1, home), |
| 52 | + trustPlugin(cwd, "/plugins/b", home), |
| 53 | + trustMcpServer(cwd, server2, home), |
| 54 | + ]); |
| 55 | + |
| 56 | + const store: ProjectTrustStore = await loadProjectTrust(cwd, home); |
| 57 | + expect(store.trustedPluginPaths.sort()).toEqual(["/plugins/a", "/plugins/b"]); |
| 58 | + expect(store.trustedMcpFingerprints).toHaveLength(2); |
| 59 | + |
| 60 | + const raw = await readFile(projectTrustPath(cwd, home), "utf8"); |
| 61 | + expect(() => JSON.parse(raw)).not.toThrow(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test("many concurrent writers never drop a grant", async () => { |
| 66 | + await withTempHome(async (home, cwd) => { |
| 67 | + const pluginPaths = Array.from({ length: 20 }, (_, i) => `/plugins/p${i}`); |
| 68 | + const servers = Array.from({ length: 20 }, (_, i) => mcpServer(`server-${i}`)); |
| 69 | + |
| 70 | + await Promise.all([ |
| 71 | + ...pluginPaths.map((p) => trustPlugin(cwd, p, home)), |
| 72 | + ...servers.map((s) => trustMcpServer(cwd, s, home)), |
| 73 | + ]); |
| 74 | + |
| 75 | + const store = await loadProjectTrust(cwd, home); |
| 76 | + expect(store.trustedPluginPaths).toHaveLength(pluginPaths.length); |
| 77 | + expect(store.trustedMcpFingerprints).toHaveLength(servers.length); |
| 78 | + |
| 79 | + const raw = await readFile(projectTrustPath(cwd, home), "utf8"); |
| 80 | + const parsed = JSON.parse(raw); |
| 81 | + expect(Array.isArray(parsed.trustedPluginPaths)).toBe(true); |
| 82 | + expect(Array.isArray(parsed.trustedMcpFingerprints)).toBe(true); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test("no leftover .tmp file remains after concurrent writes settle", async () => { |
| 87 | + await withTempHome(async (home, cwd) => { |
| 88 | + await Promise.all([ |
| 89 | + trustPlugin(cwd, "/plugins/a", home), |
| 90 | + trustPlugin(cwd, "/plugins/b", home), |
| 91 | + trustMcpServer(cwd, mcpServer("s"), home), |
| 92 | + ]); |
| 93 | + const path = projectTrustPath(cwd, home); |
| 94 | + const tmp = `${path}.${process.pid}.tmp`; |
| 95 | + const exists = await readFile(tmp, "utf8").then( |
| 96 | + () => true, |
| 97 | + () => false, |
| 98 | + ); |
| 99 | + expect(exists).toBe(false); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments