repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/session-rename/__tests__/settings.test.ts
Rawimport { 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_SESSION_RENAME_IDLE_DELAY_MS";
type Case = {
user?: unknown;
project?: unknown;
env?: string;
untrusted?: boolean;
};
describe("session-rename.idleDelayMs 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-session-rename-settings-"));
const write = (dir: string, value: unknown) => {
mkdirSync(dir, { recursive: true });
writeFileSync(
path.join(dir, "settings.json"),
JSON.stringify({ "session-rename": { idleDelayMs: 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: { [ENV]: input.env },
});
if (input.untrusted) t.session.settingsManager.setProjectTrusted(false);
const runner = t.session.extensionRunner;
// scheduleRename needs a session file; the harness session is in-memory.
vi.spyOn(t.session.sessionManager, "getSessionFile").mockReturnValue(
path.join(root, "session.jsonl"),
);
const notify = t.events.uiCallsFor("notify").length;
await runner.emit({ type: "session_start", reason: "startup" });
const warnings = t.events
.uiCallsFor("notify")
.slice(notify)
.filter((call) => call.args[1] === "warning")
.map((call) => call.args[0]);
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
await runner.emit({
type: "turn_end",
turnIndex: 0,
message: { role: "assistant", content: [] },
toolResults: [],
} as never);
const delays = setTimeoutSpy.mock.calls.map((call) => call[1]);
setTimeoutSpy.mockRestore();
return { delays, warnings };
}
it.each([
["default unchanged", {}, 10_000],
["user value", { user: 2_500 }, 2_500],
["zero allowed", { user: 0 }, 0],
["env over settings", { user: 2_500, project: 3_000, env: "4000" }, 4_000],
["trusted project over user", { user: 2_500, project: 3_000 }, 3_000],
[
"untrusted project ignored",
{ user: 2_500, project: 3_000, untrusted: true },
2_500,
],
] satisfies [string, Case, number][])(
"%s",
async (_name, input, expected) => {
const { delays, warnings } = await start(input);
expect(delays).toContain(expected);
expect(warnings).toEqual([]);
},
);
it.each([
[
"string project",
{ user: 2_500, project: "3000" },
"Invalid session-rename.idleDelayMs from project settings",
],
[
"negative user",
{ user: -1 },
"Invalid session-rename.idleDelayMs from user settings: expected an integer from 0 to 2147483647",
],
[
"fractional project",
{ user: 2_500, project: 1.5 },
"Invalid session-rename.idleDelayMs from project settings: expected an integer from 0 to 2147483647",
],
[
"non-numeric env",
{ user: 2_500, env: "soon" },
`Invalid session-rename.idleDelayMs from environment variable ${ENV}`,
],
] satisfies [string, Case, string][])(
"rejects %s without falling through",
async (_name, input, error) => {
const { delays, warnings } = await start(input);
expect(delays).toContain(10_000);
expect(delays).not.toContain(2_500);
expect(warnings).toEqual([`${error}; using 10000 ms`]);
},
);
});