repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/runtime/__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 { createTestSession, type TestSession } from "../../../test/harness";
const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
const ENV = "PI_RUNTIME_VERSION_TIMEOUT_MS";
type Case = {
user?: unknown;
project?: unknown;
env?: string;
untrusted?: boolean;
};
describe("runtime.versionTimeoutMs 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;
});
/** Fake `bun` answers after 500 ms; the status shows its version only within the timeout. */
async function start(input: Case) {
root = mkdtempSync(path.join(tmpdir(), "pi-ext-runtime-settings-"));
const write = (dir: string, value: unknown) => {
mkdirSync(dir, { recursive: true });
writeFileSync(
path.join(dir, "settings.json"),
JSON.stringify({ runtime: { versionTimeoutMs: 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);
const bin = path.join(root, "bin");
mkdirSync(bin);
writeFileSync(path.join(bin, "bun"), "#!/bin/sh\nsleep 0.5\necho 9.9.9\n");
chmodSync(path.join(bin, "bun"), 0o755);
writeFileSync(path.join(root, "bun.lock"), "");
t = await createTestSession({
cwd: root,
extensions: [extensionPath],
env: { PATH: `${bin}:${process.env.PATH}`, [ENV]: input.env },
});
if (input.untrusted) t.session.settingsManager.setProjectTrusted(false);
const session = t;
const calls = (name: string) => session.events.uiCallsFor(name);
const statusBefore = calls("setStatus").length;
const notifyBefore = calls("notify").length;
await session.session.extensionRunner.emit({
type: "session_start",
reason: "startup",
});
await vi.waitFor(
() => expect(calls("setStatus").length).toBeGreaterThan(statusBefore),
{ timeout: 5000 },
);
const text = calls("setStatus").at(-1)?.args[1] as string | undefined;
const warnings = calls("notify")
.slice(notifyBefore)
.filter((call) => call.args[1] === "warning")
.map((call) => call.args[0]);
return { version: text?.endsWith(" v9.9.9") ?? false, warnings };
}
it.each([
["default 2500 waits for version", {}, true],
["user value applied", { user: 100 }, false],
["env over user", { user: 100, env: "2500" }, true],
["env over project", { project: 2500, env: "100" }, false],
["trusted project over user", { user: 100, project: 2500 }, true],
[
"untrusted project ignored",
{ user: 100, project: 2500, untrusted: true },
false,
],
] satisfies [string, Case, boolean][])(
"%s",
async (_name, input, expected) => {
const { version, warnings } = await start(input);
expect(version).toBe(expected);
expect(warnings).toEqual([]);
},
);
it.each([
["string project", { user: 100, project: "100" }, "project settings", ""],
[
"zero user",
{ user: 0 },
"user settings",
": expected an integer from 1 to 2147483647",
],
[
"fractional user",
{ user: 100.5 },
"user settings",
": expected an integer from 1 to 2147483647",
],
[
"oversized project",
{ user: 100, project: 2 ** 31 },
"project settings",
": expected an integer from 1 to 2147483647",
],
[
"non-numeric env",
{ user: 100, env: "fast" },
`environment variable ${ENV}`,
"",
],
] satisfies [string, Case, string, string][])(
"rejects %s without falling through",
async (_name, input, source, detail) => {
const { version, warnings } = await start(input);
expect(version).toBe(true);
expect(warnings).toEqual([
`Invalid runtime.versionTimeoutMs from ${source}${detail}; using 2500 ms`,
]);
},
);
});