repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/vcs-status/__tests__/settings.test.ts
Rawimport {
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 {
createPiSession,
type PiSession,
} from "../../../test/fixture/create-pi-session.ts";
import { writeWindowsExecutable } from "../../../test/fixture/windows-executable.ts";
const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
type Case = {
user?: unknown;
project?: unknown;
env?: string;
untrusted?: boolean;
};
interface UiCall {
args: unknown[];
}
/**
* Install a recording UI context so `ctx.ui.setStatus` / `ctx.ui.notify` calls
* are captured for assertions.
*/
function installRecordingUi(session: PiSession): {
setStatusCalls: UiCall[];
notifyCalls: UiCall[];
} {
const setStatusCalls: UiCall[] = [];
const notifyCalls: UiCall[] = [];
session.session.extensionRunner.setUIContext(
{
setStatus: (...args: unknown[]) => setStatusCalls.push({ args }),
notify: (...args: unknown[]) => notifyCalls.push({ args }),
} as never,
"print",
);
return { setStatusCalls, notifyCalls };
}
// 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 session: PiSession | undefined;
let root: string | undefined;
afterEach(async () => {
await session?.dispose();
session = 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-"));
if ("project" in input) {
mkdirSync(path.join(root, ".pi"), { recursive: true });
writeFileSync(
path.join(root, ".pi", "settings.json"),
JSON.stringify({ "vcs-status": { timeoutMs: 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);
if (process.platform === "win32")
writeWindowsExecutable(bin, "git", "# branch.head main\n? new\n", 500);
else {
writeFileSync(
path.join(bin, "git"),
"#!/bin/sh\nsleep 0.5 </dev/null >/dev/null 2>&1\nprintf '# branch.head main\\n? new\\n'\n",
);
chmodSync(path.join(bin, "git"), 0o755);
}
session = await createPiSession({
cwd: root,
extensionPaths: [extensionPath],
...("user" in input
? { userSettings: { "vcs-status": { timeoutMs: input.user } } }
: {}),
env: {
PATH: `${bin}${path.delimiter}${process.env["PATH"]}`,
PI_VCS_STATUS_TIMEOUT_MS: input.env,
},
});
if (input.untrusted)
session.session.settingsManager.setProjectTrusted(false);
const { setStatusCalls, notifyCalls } = installRecordingUi(session);
const vcsCalls = () =>
setStatusCalls.filter((call) => call.args[0] === "vcs" && call.args[1]);
const notifyBefore = notifyCalls.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 = notifyCalls
.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`,
]);
},
);
});