|
1 | | -import { afterEach, describe, expect, mock, spyOn, test } from "bun:test"; |
| 1 | +import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"; |
2 | 2 | import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
| 5 | +import { getLogger } from "@intx/log"; |
| 6 | +import type { ToolCall } from "@intx/types/runtime"; |
5 | 7 |
|
| 8 | +import { LOG_NAMESPACE_ROOT } from "../branding.js"; |
6 | 9 | import * as permissionStore from "../permission/store.js"; |
| 10 | +import { createPermissionGate } from "../permission/gate.js"; |
| 11 | +import type { GrantScope } from "../permission/types.js"; |
7 | 12 | import { |
8 | 13 | buildSubAgentProvider, |
9 | 14 | createApprovalPersist, |
@@ -160,6 +165,12 @@ describe("loadSeededApprovals merge order", () => { |
160 | 165 | }); |
161 | 166 |
|
162 | 167 | describe("createApprovalPersist", () => { |
| 168 | + const persistLogger = getLogger([LOG_NAMESPACE_ROOT, "session", "approvals"]); |
| 169 | + |
| 170 | + beforeEach(() => { |
| 171 | + spyOn(persistLogger, "warn"); |
| 172 | + }); |
| 173 | + |
163 | 174 | afterEach(() => { |
164 | 175 | mock.restore(); |
165 | 176 | }); |
@@ -187,6 +198,97 @@ describe("createApprovalPersist", () => { |
187 | 198 | expect(global).toHaveBeenCalledTimes(1); |
188 | 199 | expect(providerModel).toHaveBeenCalledTimes(1); |
189 | 200 | }); |
| 201 | + |
| 202 | + const persistedScopes: { |
| 203 | + scope: Exclude<GrantScope, "session">; |
| 204 | + reject: (message: string) => void; |
| 205 | + }[] = [ |
| 206 | + { |
| 207 | + scope: "project", |
| 208 | + reject: (message) => { |
| 209 | + spyOn(permissionStore, "saveProjectApproval").mockRejectedValue(new Error(message)); |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + scope: "global", |
| 214 | + reject: (message) => { |
| 215 | + spyOn(permissionStore, "saveGlobalApproval").mockRejectedValue(new Error(message)); |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + scope: "provider-model", |
| 220 | + reject: (message) => { |
| 221 | + spyOn(permissionStore, "saveProviderModelApproval").mockRejectedValue(new Error(message)); |
| 222 | + }, |
| 223 | + }, |
| 224 | + ]; |
| 225 | + |
| 226 | + const shellCall = (command: string): ToolCall => ({ |
| 227 | + id: "c", |
| 228 | + name: "run_shell", |
| 229 | + arguments: { command }, |
| 230 | + }); |
| 231 | + |
| 232 | + async function flushUnhandledRejections(): Promise<unknown> { |
| 233 | + let unhandled: unknown = null; |
| 234 | + const onUnhandled = (reason: unknown): void => { |
| 235 | + unhandled = reason; |
| 236 | + }; |
| 237 | + process.on("unhandledRejection", onUnhandled); |
| 238 | + try { |
| 239 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 240 | + } finally { |
| 241 | + process.off("unhandledRejection", onUnhandled); |
| 242 | + } |
| 243 | + return unhandled; |
| 244 | + } |
| 245 | + |
| 246 | + for (const { scope, reject } of persistedScopes) { |
| 247 | + test(`a rejected ${scope} write is contained, logged, and never becomes an unhandled rejection`, async () => { |
| 248 | + const message = `${scope} disk full`; |
| 249 | + reject(message); |
| 250 | + |
| 251 | + const persist = createApprovalPersist("/tmp/proj", "openai:gpt-5"); |
| 252 | + persist({ tool: "run_shell", pattern: "npm *" }, scope); |
| 253 | + |
| 254 | + expect(await flushUnhandledRejections()).toBeNull(); |
| 255 | + expect(persistLogger.warn).toHaveBeenCalledTimes(1); |
| 256 | + expect(persistLogger.warn).toHaveBeenCalledWith( |
| 257 | + "Failed to persist {scope} approval: {error}", |
| 258 | + { |
| 259 | + scope, |
| 260 | + error: message, |
| 261 | + }, |
| 262 | + ); |
| 263 | + }); |
| 264 | + |
| 265 | + test(`an approved call still completes and the in-memory ${scope} grant still applies when persist rejects`, async () => { |
| 266 | + reject(`${scope} EACCES`); |
| 267 | + const persist = createApprovalPersist("/tmp/proj", "openai:gpt-5"); |
| 268 | + let asked = 0; |
| 269 | + const gate = createPermissionGate({ |
| 270 | + approvals: [], |
| 271 | + requestApproval: async () => { |
| 272 | + asked++; |
| 273 | + return { |
| 274 | + allow: true, |
| 275 | + persist: { id: scope, label: "", pattern: "npm *", grant: scope }, |
| 276 | + }; |
| 277 | + }, |
| 278 | + persist, |
| 279 | + interactive: true, |
| 280 | + skipPermissions: false, |
| 281 | + providerName: "openai", |
| 282 | + model: "gpt-5", |
| 283 | + }); |
| 284 | + |
| 285 | + expect((await gate.evaluate(shellCall("npm test"))).allowed).toBe(true); |
| 286 | + expect(asked).toBe(1); |
| 287 | + expect(await flushUnhandledRejections()).toBeNull(); |
| 288 | + expect((await gate.evaluate(shellCall("npm run build"))).allowed).toBe(true); |
| 289 | + expect(asked).toBe(1); |
| 290 | + }); |
| 291 | + } |
190 | 292 | }); |
191 | 293 |
|
192 | 294 | describe("skillDirsFromEnabledPlugins", () => { |
|
0 commit comments