From 09fd72bc5ba312b59b16c04aaad492fb7e2ce3ff Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 19:13:01 +0000 Subject: [PATCH] test: expand approval context and gemini usage coverage --- src/infra/provider-usage.fetch.gemini.test.ts | 35 +++++ src/infra/system-run-approval-context.test.ts | 129 +++++++++++++++++- 2 files changed, 163 insertions(+), 1 deletion(-) diff --git a/src/infra/provider-usage.fetch.gemini.test.ts b/src/infra/provider-usage.fetch.gemini.test.ts index ea713478011..c21292ebf97 100644 --- a/src/infra/provider-usage.fetch.gemini.test.ts +++ b/src/infra/provider-usage.fetch.gemini.test.ts @@ -36,4 +36,39 @@ describe("fetchGeminiUsage", () => { expect(result.windows[1]?.label).toBe("Flash"); expect(result.windows[1]?.usedPercent).toBeCloseTo(30, 6); }); + + it("returns no windows when the response has no recognized model families", async () => { + const mockFetch = createProviderUsageFetch(async () => + makeResponse(200, { + buckets: [{ modelId: "gemini-unknown", remainingFraction: 0.5 }], + }), + ); + + const result = await fetchGeminiUsage("token", 5000, mockFetch, "google-gemini-cli"); + + expect(result).toEqual({ + provider: "google-gemini-cli", + displayName: "Gemini", + windows: [], + }); + }); + + it("defaults missing fractions to fully available and clamps invalid fractions", async () => { + const mockFetch = createProviderUsageFetch(async () => + makeResponse(200, { + buckets: [ + { modelId: "gemini-pro" }, + { modelId: "gemini-pro-latest", remainingFraction: -0.5 }, + { modelId: "gemini-flash", remainingFraction: 1.2 }, + ], + }), + ); + + const result = await fetchGeminiUsage("token", 5000, mockFetch, "google-gemini-cli"); + + expect(result.windows).toEqual([ + { label: "Pro", usedPercent: 100 }, + { label: "Flash", usedPercent: 0 }, + ]); + }); }); diff --git a/src/infra/system-run-approval-context.test.ts b/src/infra/system-run-approval-context.test.ts index fbd0e805a22..1dc98eea200 100644 --- a/src/infra/system-run-approval-context.test.ts +++ b/src/infra/system-run-approval-context.test.ts @@ -1,5 +1,9 @@ import { describe, expect, test } from "vitest"; -import { resolveSystemRunApprovalRequestContext } from "./system-run-approval-context.js"; +import { + parsePreparedSystemRunPayload, + resolveSystemRunApprovalRequestContext, + resolveSystemRunApprovalRuntimeContext, +} from "./system-run-approval-context.js"; describe("resolveSystemRunApprovalRequestContext", () => { test("uses full approval text and separate preview for node system.run plans", () => { @@ -37,4 +41,127 @@ describe("resolveSystemRunApprovalRequestContext", () => { expect(context.commandText).toBe('./env sh -c "jq --version"'); expect(context.commandPreview).toBe("jq --version"); }); + + test("falls back to explicit request params for non-node hosts", () => { + const context = resolveSystemRunApprovalRequestContext({ + host: "gateway", + command: "jq --version", + commandArgv: ["jq", "--version"], + cwd: "/tmp", + agentId: "main", + sessionKey: "agent:main:main", + systemRunPlan: { + argv: ["ignored"], + commandText: "ignored", + }, + }); + + expect(context.plan).toBeNull(); + expect(context.commandArgv).toEqual(["jq", "--version"]); + expect(context.commandText).toBe("jq --version"); + expect(context.commandPreview).toBeNull(); + expect(context.cwd).toBe("/tmp"); + expect(context.agentId).toBe("main"); + expect(context.sessionKey).toBe("agent:main:main"); + }); +}); + +describe("parsePreparedSystemRunPayload", () => { + test("parses legacy prepared payloads via top-level fallback command text", () => { + expect( + parsePreparedSystemRunPayload({ + plan: { + argv: ["bash", "-lc", "jq --version"], + cwd: "/tmp", + agentId: "main", + sessionKey: "agent:main:main", + }, + commandText: 'bash -lc "jq --version"', + }), + ).toEqual({ + plan: { + argv: ["bash", "-lc", "jq --version"], + cwd: "/tmp", + commandText: 'bash -lc "jq --version"', + commandPreview: null, + agentId: "main", + sessionKey: "agent:main:main", + }, + }); + }); + + test("rejects legacy payloads missing argv or command text", () => { + expect(parsePreparedSystemRunPayload({ plan: { argv: [] }, commandText: "jq --version" })).toBe( + null, + ); + expect( + parsePreparedSystemRunPayload({ + plan: { argv: ["jq", "--version"] }, + }), + ).toBeNull(); + }); +}); + +describe("resolveSystemRunApprovalRuntimeContext", () => { + test("uses normalized plan runtime metadata when available", () => { + expect( + resolveSystemRunApprovalRuntimeContext({ + plan: { + argv: ["jq", "--version"], + cwd: "/tmp", + commandText: "jq --version", + commandPreview: "jq --version", + agentId: "main", + sessionKey: "agent:main:main", + }, + }), + ).toEqual({ + ok: true, + plan: { + argv: ["jq", "--version"], + cwd: "/tmp", + commandText: "jq --version", + commandPreview: "jq --version", + agentId: "main", + sessionKey: "agent:main:main", + }, + argv: ["jq", "--version"], + cwd: "/tmp", + agentId: "main", + sessionKey: "agent:main:main", + commandText: "jq --version", + }); + }); + + test("falls back to command/rawCommand validation without a plan", () => { + expect( + resolveSystemRunApprovalRuntimeContext({ + command: ["bash", "-lc", "jq --version"], + rawCommand: 'bash -lc "jq --version"', + cwd: "/tmp", + agentId: "main", + sessionKey: "agent:main:main", + }), + ).toEqual({ + ok: true, + plan: null, + argv: ["bash", "-lc", "jq --version"], + cwd: "/tmp", + agentId: "main", + sessionKey: "agent:main:main", + commandText: 'bash -lc "jq --version"', + }); + }); + + test("returns request validation errors from command fallback", () => { + expect( + resolveSystemRunApprovalRuntimeContext({ + rawCommand: "jq --version", + }), + ).toEqual({ + ok: false, + message: "rawCommand requires params.command", + details: { code: "MISSING_COMMAND" }, + }); + }); });