import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createTestSession, type TestSession } from "../../../test/harness"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); type Case = { user?: unknown; project?: unknown; env?: string; untrusted?: boolean; }; describe("quota.refreshIntervalMs resolution", () => { let t: TestSession | undefined; let root: string | undefined; afterEach(async () => { await t?.session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); t?.dispose(); t = undefined; vi.restoreAllMocks(); if (root) rmSync(root, { recursive: true, force: true }); root = undefined; }); async function start(input: Case) { root = mkdtempSync(path.join(tmpdir(), "pi-ext-quota-settings-")); const write = (dir: string, value: unknown) => { mkdirSync(dir, { recursive: true }); writeFileSync( path.join(dir, "settings.json"), JSON.stringify({ quota: { refreshIntervalMs: value } }), ); }; if ("user" in input) write(path.join(root, ".test-home/.pi/agent"), input.user); if ("project" in input) write(path.join(root, ".pi"), input.project); t = await createTestSession({ cwd: root, extensions: [extensionPath], env: { PI_QUOTA_REFRESH_INTERVAL_MS: input.env }, }); if (input.untrusted) t.session.settingsManager.setProjectTrusted(false); const runner = t.session.extensionRunner; // Drop the timer left by the harness's own session_start. await runner.emit({ type: "session_shutdown", reason: "quit" }); const before = t.events.uiCallsFor("notify").length; await runner.emit({ type: "session_start", reason: "startup" }); const setInterval = vi.spyOn(globalThis, "setInterval"); await runner.emit({ type: "agent_start" }); const warnings = t.events .uiCallsFor("notify") .slice(before) .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { interval: setInterval.mock.calls.at(-1)?.[1], warnings }; } it.each([ ["default unchanged", {}, 300_000], ["user value", { user: 60_000 }, 60_000], ["env over settings", { user: 60_000, env: "20000" }, 20_000], ["trusted project over user", { user: 60_000, project: 30_000 }, 30_000], [ "untrusted project ignored", { user: 60_000, project: 30_000, untrusted: true }, 60_000, ], ] satisfies [string, Case, number][])( "%s", async (_name, input, expected) => { const { interval, warnings } = await start(input); expect(interval).toBe(expected); expect(warnings).toEqual([]); }, ); it.each([ [ "string user", { user: "60000" }, "Invalid quota.refreshIntervalMs from user settings", ], [ "too small project", { user: 60_000, project: 5_000 }, "Invalid quota.refreshIntervalMs from project settings: expected an integer from 10000 to 2147483647", ], [ "fractional user", { user: 10_000.5 }, "Invalid quota.refreshIntervalMs from user settings: expected an integer from 10000 to 2147483647", ], [ "non-numeric env", { user: 60_000, env: "soon" }, "Invalid quota.refreshIntervalMs from environment variable PI_QUOTA_REFRESH_INTERVAL_MS", ], ] satisfies [string, Case, string][])( "rejects %s without falling through", async (_name, input, error) => { const { interval, warnings } = await start(input); expect(interval).toBe(300_000); expect(warnings).toEqual([`${error}; using 300000 ms`]); }, ); });