|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import type { ToolCall } from "@intx/types/runtime"; |
| 3 | +import type { Approval, PermissionRequest } from "./types.js"; |
| 4 | +import { evaluateApprovals, grantScopeMatches, type GrantWorkspace } from "./authz-grants.js"; |
| 5 | +import { createPermissionGate, isRequestCoveredByGrant } from "./gate.js"; |
| 6 | + |
| 7 | +// evaluateApprovals and isRequestCoveredByGrant each decide, independently, |
| 8 | +// whether a grant's tool/providerModel/cwd scope covers a request. Both are |
| 9 | +// expected to delegate to the same shared predicate (grantScopeMatches) |
| 10 | +// rather than reimplementing the condition. This test drives the same |
| 11 | +// grant+request pairs through all three and asserts they agree — a |
| 12 | +// regression where one call site reimplements the check with subtly |
| 13 | +// different semantics would fail here even if each function still "looks |
| 14 | +// right" in isolation. |
| 15 | +describe("grant tool/providerModel/cwd scoping agrees across call sites", () => { |
| 16 | + const workspace: GrantWorkspace = { resolvedCwd: "/proj", roots: ["/proj"] }; |
| 17 | + const noopRestricted = () => false; |
| 18 | + |
| 19 | + const grants: Approval[] = [ |
| 20 | + { tool: "run_shell", pattern: "npm test" }, |
| 21 | + { tool: "run_shell", pattern: "npm test", providerModel: "openai:gpt-5" }, |
| 22 | + { tool: "run_shell", pattern: "npm test", cwd: "/proj" }, |
| 23 | + { tool: "write_file", pattern: "npm test" }, |
| 24 | + ]; |
| 25 | + |
| 26 | + const requests: Array<{ tool: string; cwd?: string | undefined; activeProviderModel?: string | undefined }> = [ |
| 27 | + { tool: "run_shell", cwd: "/proj", activeProviderModel: "openai:gpt-5" }, |
| 28 | + { tool: "run_shell", cwd: "/proj", activeProviderModel: "anthropic:opus" }, |
| 29 | + { tool: "run_shell", cwd: "/other", activeProviderModel: "openai:gpt-5" }, |
| 30 | + { tool: "run_shell", cwd: undefined, activeProviderModel: undefined }, |
| 31 | + { tool: "write_file", cwd: "/proj", activeProviderModel: undefined }, |
| 32 | + ]; |
| 33 | + |
| 34 | + for (const grant of grants) { |
| 35 | + for (const req of requests) { |
| 36 | + test(`grant ${JSON.stringify(grant)} vs request ${JSON.stringify(req)}`, async () => { |
| 37 | + const expected = grantScopeMatches(grant, req.tool, req.activeProviderModel, req.cwd, workspace); |
| 38 | + |
| 39 | + const viaEvaluateApprovals = await evaluateApprovals({ |
| 40 | + tool: req.tool, |
| 41 | + subject: "npm test", |
| 42 | + approvals: [grant], |
| 43 | + activeProviderModel: req.activeProviderModel, |
| 44 | + requestCwd: req.cwd, |
| 45 | + workspace, |
| 46 | + }); |
| 47 | + |
| 48 | + const request: PermissionRequest = { |
| 49 | + tool: req.tool, |
| 50 | + action: req.tool, |
| 51 | + subject: "npm test", |
| 52 | + scopes: [], |
| 53 | + ...(req.cwd !== undefined ? { cwd: req.cwd } : {}), |
| 54 | + }; |
| 55 | + const viaGate = isRequestCoveredByGrant(request, grant, req.activeProviderModel, noopRestricted, workspace); |
| 56 | + |
| 57 | + // Both live call sites additionally require the pattern to match the |
| 58 | + // subject, which is true for every case here ("npm test" grants an |
| 59 | + // exact "npm test" subject), so a scope mismatch is the only thing |
| 60 | + // that can make either disagree with the shared predicate. |
| 61 | + expect(viaEvaluateApprovals).toBe(expected); |
| 62 | + expect(viaGate).toBe(expected); |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +// hasExactFullCommandGrant (gate.ts) is the third live call site grantScopeMatches |
| 69 | +// unifies, but it is not exported — it only surfaces through the exact-full-command |
| 70 | +// replay path inside evaluate(). This drives that path directly with grants that |
| 71 | +// grantScopeMatches would refuse (wrong cwd, wrong providerModel) to confirm the |
| 72 | +// replay never fires when the shared predicate says no, matching the coverage the |
| 73 | +// other two call sites get above. |
| 74 | +describe("hasExactFullCommandGrant agrees with grantScopeMatches", () => { |
| 75 | + const full = "npm i && curl x"; |
| 76 | + const shellCall = (command: string): ToolCall => ({ id: "c", name: "run_shell", arguments: { command } }); |
| 77 | + |
| 78 | + test("does not replay a grant scoped to a different cwd", async () => { |
| 79 | + let asked = 0; |
| 80 | + const gate = createPermissionGate({ |
| 81 | + approvals: [{ tool: "run_shell", pattern: full, cwd: "/other-project" }], |
| 82 | + requestApproval: async () => { asked++; return { allow: true }; }, |
| 83 | + interactive: true, |
| 84 | + skipPermissions: false, |
| 85 | + }); |
| 86 | + expect((await gate.evaluate(shellCall(full))).allowed).toBe(true); |
| 87 | + // grantScopeMatches would refuse this grant (cwd mismatch), so the |
| 88 | + // exact-full-command shortcut must not fire — the operator is still asked. |
| 89 | + expect(asked).toBeGreaterThan(0); |
| 90 | + }); |
| 91 | + |
| 92 | + test("does not replay a grant scoped to a different provider model", async () => { |
| 93 | + let asked = 0; |
| 94 | + const gate = createPermissionGate({ |
| 95 | + approvals: [{ tool: "run_shell", pattern: full, providerModel: "openai:gpt-5" }], |
| 96 | + providerName: "anthropic", |
| 97 | + model: "opus", |
| 98 | + requestApproval: async () => { asked++; return { allow: true }; }, |
| 99 | + interactive: true, |
| 100 | + skipPermissions: false, |
| 101 | + }); |
| 102 | + expect((await gate.evaluate(shellCall(full))).allowed).toBe(true); |
| 103 | + expect(asked).toBeGreaterThan(0); |
| 104 | + }); |
| 105 | + |
| 106 | + test("replays a grant whose scope grantScopeMatches accepts", async () => { |
| 107 | + let asked = 0; |
| 108 | + const gate = createPermissionGate({ |
| 109 | + approvals: [{ tool: "run_shell", pattern: full }], |
| 110 | + requestApproval: async () => { asked++; return { allow: true }; }, |
| 111 | + interactive: true, |
| 112 | + skipPermissions: false, |
| 113 | + }); |
| 114 | + expect((await gate.evaluate(shellCall(full))).allowed).toBe(true); |
| 115 | + expect(asked).toBe(0); |
| 116 | + }); |
| 117 | +}); |
0 commit comments