refactor(cli): reuse allowlist mutation flow in approvals CLI

This commit is contained in:
Peter Steinberger
2026-02-19 06:43:22 +00:00
parent 8d048d412f
commit fa31f1cad2
2 changed files with 106 additions and 44 deletions

View File

@@ -24,6 +24,10 @@ const localSnapshot = {
file: { version: 1, agents: {} },
};
function resetLocalSnapshot() {
localSnapshot.file = { version: 1, agents: {} };
}
vi.mock("./gateway-rpc.js", () => ({
callGatewayFromCli: (method: string, opts: unknown, params?: unknown) =>
callGatewayFromCli(method, opts, params),
@@ -64,6 +68,7 @@ describe("exec approvals CLI", () => {
};
it("routes get command to local, gateway, and node modes", async () => {
resetLocalSnapshot();
resetRuntimeCapture();
callGatewayFromCli.mockClear();
@@ -91,6 +96,7 @@ describe("exec approvals CLI", () => {
});
it("defaults allowlist add to wildcard agent", async () => {
resetLocalSnapshot();
resetRuntimeCapture();
callGatewayFromCli.mockClear();
@@ -116,4 +122,34 @@ describe("exec approvals CLI", () => {
}),
);
});
it("removes wildcard allowlist entry and prunes empty agent", async () => {
resetLocalSnapshot();
localSnapshot.file = {
version: 1,
agents: {
"*": {
allowlist: [{ pattern: "/usr/bin/uname", lastUsedAt: Date.now() }],
},
},
};
resetRuntimeCapture();
callGatewayFromCli.mockClear();
const saveExecApprovals = vi.mocked(execApprovals.saveExecApprovals);
saveExecApprovals.mockClear();
const program = createProgram();
await program.parseAsync(["approvals", "allowlist", "remove", "/usr/bin/uname"], {
from: "user",
});
expect(saveExecApprovals).toHaveBeenCalledWith(
expect.objectContaining({
version: 1,
agents: undefined,
}),
);
expect(runtimeErrors).toHaveLength(0);
});
});