import { chmodSync, 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; }; // Fake git answers after 500 ms: a short timeout drops the porcelain flags. const SLOW = "main [?]"; const TIMED_OUT = "main"; describe("vcs-status.timeoutMs 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; if (root) rmSync(root, { recursive: true, force: true }); root = undefined; }); async function start(input: Case) { root = mkdtempSync(path.join(tmpdir(), "pi-ext-vcs-status-settings-")); const write = (dir: string, value: unknown) => { mkdirSync(dir, { recursive: true }); writeFileSync( path.join(dir, "settings.json"), JSON.stringify({ "vcs-status": { timeoutMs: 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); mkdirSync(path.join(root, ".git")); writeFileSync(path.join(root, ".git/HEAD"), "ref: refs/heads/main\n"); const bin = path.join(root, "bin"); mkdirSync(bin); writeFileSync( path.join(bin, "git"), "#!/bin/sh\nsleep 0.5 /dev/null 2>&1\nprintf '# branch.head main\\n? new\\n'\n", ); chmodSync(path.join(bin, "git"), 0o755); t = await createTestSession({ cwd: root, extensions: [extensionPath], env: { PATH: `${bin}${path.delimiter}${process.env.PATH}`, PI_VCS_STATUS_TIMEOUT_MS: input.env, }, }); if (input.untrusted) t.session.settingsManager.setProjectTrusted(false); const session = t; const vcsCalls = () => session.events .uiCallsFor("setStatus") .filter((call) => call.args[0] === "vcs" && call.args[1]); const notifyBefore = session.events.uiCallsFor("notify").length; const vcsBefore = vcsCalls().length; await session.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); await vi.waitFor( () => expect(vcsCalls().length).toBeGreaterThan(vcsBefore), { timeout: 5000 }, ); const label = String(vcsCalls().at(-1)?.args[1]).split(" ").slice(1); const warnings = session.events .uiCallsFor("notify") .slice(notifyBefore) .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { label: label.join(" "), warnings }; } it.each([ ["default 2500 ms", {}, SLOW], ["user value", { user: 100 }, TIMED_OUT], ["env over settings", { user: 5000, env: "100" }, TIMED_OUT], ["trusted project over user", { user: 5000, project: 100 }, TIMED_OUT], [ "untrusted project ignored", { user: 5000, project: 100, untrusted: true }, SLOW, ], ] satisfies [string, Case, string][])( "%s", async (_name, input, expected) => { const { label, warnings } = await start(input); expect(label).toBe(expected); expect(warnings).toEqual([]); }, ); it.each([ ["string user", { user: "100" }, "user settings"], ["fractional project", { user: 100, project: 1.5 }, "project settings"], ["zero user", { user: 0 }, "user settings"], [ "non-numeric env", { user: 100, env: "fast" }, "environment variable PI_VCS_STATUS_TIMEOUT_MS", ], ] satisfies [string, Case, string][])( "rejects %s without falling through", async (_name, input, source) => { const { label, warnings } = await start(input); expect(label).toBe(SLOW); expect(warnings).toEqual([ `Invalid vcs-status.timeoutMs from ${source}; using default 2500 ms`, ]); }, ); });