|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import type { Approval, PermissionRequest } from "./types.js"; |
| 3 | +import { evaluateApprovals, grantScopeMatches, type GrantWorkspace } from "./authz-grants.js"; |
| 4 | +import { isRequestCoveredByGrant } from "./gate.js"; |
| 5 | + |
| 6 | +// evaluateApprovals and isRequestCoveredByGrant each decide, independently, |
| 7 | +// whether a grant's tool/providerModel/cwd scope covers a request. Both are |
| 8 | +// expected to delegate to the same shared predicate (grantScopeMatches) |
| 9 | +// rather than reimplementing the condition. This test drives the same |
| 10 | +// grant+request pairs through all three and asserts they agree — a |
| 11 | +// regression where one call site reimplements the check with subtly |
| 12 | +// different semantics would fail here even if each function still "looks |
| 13 | +// right" in isolation. |
| 14 | +describe("grant tool/providerModel/cwd scoping agrees across call sites", () => { |
| 15 | + const workspace: GrantWorkspace = { resolvedCwd: "/proj", roots: ["/proj"] }; |
| 16 | + const noopRestricted = () => false; |
| 17 | + |
| 18 | + const grants: Approval[] = [ |
| 19 | + { tool: "run_shell", pattern: "npm test" }, |
| 20 | + { tool: "run_shell", pattern: "npm test", providerModel: "openai:gpt-5" }, |
| 21 | + { tool: "run_shell", pattern: "npm test", cwd: "/proj" }, |
| 22 | + { tool: "write_file", pattern: "npm test" }, |
| 23 | + ]; |
| 24 | + |
| 25 | + const requests: Array<{ tool: string; cwd?: string | undefined; activeProviderModel?: string | undefined }> = [ |
| 26 | + { tool: "run_shell", cwd: "/proj", activeProviderModel: "openai:gpt-5" }, |
| 27 | + { tool: "run_shell", cwd: "/proj", activeProviderModel: "anthropic:opus" }, |
| 28 | + { tool: "run_shell", cwd: "/other", activeProviderModel: "openai:gpt-5" }, |
| 29 | + { tool: "run_shell", cwd: undefined, activeProviderModel: undefined }, |
| 30 | + { tool: "write_file", cwd: "/proj", activeProviderModel: undefined }, |
| 31 | + ]; |
| 32 | + |
| 33 | + for (const grant of grants) { |
| 34 | + for (const req of requests) { |
| 35 | + test(`grant ${JSON.stringify(grant)} vs request ${JSON.stringify(req)}`, async () => { |
| 36 | + const expected = grantScopeMatches(grant, req.tool, req.activeProviderModel, req.cwd, workspace); |
| 37 | + |
| 38 | + const viaEvaluateApprovals = await evaluateApprovals({ |
| 39 | + tool: req.tool, |
| 40 | + subject: "npm test", |
| 41 | + approvals: [grant], |
| 42 | + activeProviderModel: req.activeProviderModel, |
| 43 | + requestCwd: req.cwd, |
| 44 | + workspace, |
| 45 | + }); |
| 46 | + |
| 47 | + const request: PermissionRequest = { |
| 48 | + tool: req.tool, |
| 49 | + action: req.tool, |
| 50 | + subject: "npm test", |
| 51 | + scopes: [], |
| 52 | + ...(req.cwd !== undefined ? { cwd: req.cwd } : {}), |
| 53 | + }; |
| 54 | + const viaGate = isRequestCoveredByGrant(request, grant, req.activeProviderModel, noopRestricted, workspace); |
| 55 | + |
| 56 | + // Both live call sites additionally require the pattern to match the |
| 57 | + // subject, which is true for every case here ("npm test" grants an |
| 58 | + // exact "npm test" subject), so a scope mismatch is the only thing |
| 59 | + // that can make either disagree with the shared predicate. |
| 60 | + expect(viaEvaluateApprovals).toBe(expected); |
| 61 | + expect(viaGate).toBe(expected); |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | +}); |
0 commit comments