test: expand approval context and gemini usage coverage

This commit is contained in:
Peter Steinberger
2026-03-13 19:13:01 +00:00
parent dfcbfcfcc9
commit 09fd72bc5b
2 changed files with 163 additions and 1 deletions

View File

@@ -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 },
]);
});
});

View File

@@ -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" },
});
});
});