Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/pipi/__tests__/settings.test.ts

Raw
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";

let fixture: PiSession | undefined;
afterEach(async () => {
	await fixture?.dispose();
	fixture = undefined;
});
async function setting(
	userSettings: Record<string, unknown> = {},
	project?: Record<string, unknown>,
	trusted = false,
) {
	fixture = await createPiSession({
		userSettings,
		env: { PI_PIPI_DEBUG: "0" },
	});
	if (project) {
		await mkdir(join(fixture.cwd, ".pi"), { recursive: true });
		await writeFile(
			join(fixture.cwd, ".pi", "settings.json"),
			JSON.stringify(project),
		);
	}
	const { PIPI_SETTING } = await import("../config.ts");
	const { resolveSetting } = await import("../src/pi-ext-settings.ts");
	return resolveSetting(
		{ getFlag: () => undefined },
		{ cwd: fixture.cwd, isProjectTrusted: () => trusted },
		PIPI_SETTING,
	);
}
async function deadline(
	userSettings: Record<string, unknown> = {},
	project?: Record<string, unknown>,
	trusted = false,
	env: Record<string, string> = {},
) {
	fixture = await createPiSession({
		userSettings,
		env: { PI_PIPI_DEBUG: "0", ...env },
	});
	if (project) {
		await mkdir(join(fixture.cwd, ".pi"), { recursive: true });
		await writeFile(
			join(fixture.cwd, ".pi", "settings.json"),
			JSON.stringify(project),
		);
	}
	const { TIMEOUT_SETTING } = await import("../config.ts");
	const { resolveSetting } = await import("../src/pi-ext-settings.ts");
	return resolveSetting(
		{ getFlag: () => undefined },
		{ cwd: fixture.cwd, isProjectTrusted: () => trusted },
		TIMEOUT_SETTING,
	);
}
describe("pipi tier settings", () => {
	it("defaults to parent inheritance with no tier recommendations", async () => {
		expect(await setting()).toMatchObject({
			ok: true,
			source: "default",
			value: { modelTiers: {}, providerModelTiers: {} },
		});
	});
	it("reads user recommendations and gives provider-specific recommendations priority", async () => {
		const value = await setting({
			pipi: {
				modelTiers: {
					small: {
						model: "faux/general",
						thinkingLevel: "high",
						instructions: "Bounded review",
					},
				},
				providerModelTiers: { faux: { small: { model: "faux/special" } } },
			},
		});
		expect(value.ok).toBe(true);
		if (!value.ok) throw new Error(value.error);
		const { selectModel, effectiveTiers } = await import("../config.ts");
		expect(effectiveTiers(value.value, "faux").small).toEqual({
			model: "faux/special",
			thinkingLevel: "high",
			instructions: "Bounded review",
		});
		expect(selectModel({ tier: "small" }, value.value, "faux")).toBe(
			"faux/special",
		);
		expect(selectModel({ tier: "small" }, value.value, "other")).toBe(
			"faux/general",
		);
		expect(
			selectModel(
				{ model: "explicit/model", tier: "small" },
				value.value,
				"faux",
			),
		).toBe("explicit/model");
		expect(selectModel({}, value.value, "faux")).toBeUndefined();
		expect(selectModel({ tier: "huge" }, value.value, "faux")).toBeUndefined();
	});
	it("deep-merges trusted project tiers over user tiers", async () => {
		expect(
			await setting(
				{
					pipi: {
						modelTiers: {
							small: { model: "faux/user" },
							large: { model: "faux/large" },
						},
					},
				},
				{ pipi: { modelTiers: { small: { model: "faux/project" } } } },
				true,
			),
		).toMatchObject({
			ok: true,
			source: "project",
			value: {
				modelTiers: {
					small: { model: "faux/project" },
					large: { model: "faux/large" },
				},
			},
		});
	});
	it("does not read untrusted project tiers", async () => {
		expect(
			await setting(
				{ pipi: { modelTiers: { small: { model: "faux/user" } } } },
				{ pipi: { modelTiers: { small: { model: "faux/project" } } } },
			),
		).toMatchObject({
			ok: true,
			source: "user",
			value: { modelTiers: { small: { model: "faux/user" } } },
		});
	});
	it("reports invalid configuration rather than falling through", async () => {
		expect(
			await setting(
				{ pipi: { modelTiers: { small: { model: "faux/user" } } } },
				{ pipi: { modelTiers: { small: 42 } } },
				true,
			),
		).toMatchObject({ ok: false, source: "project" });
	});
	it("rejects unknown config keys and unknown tiers", async () => {
		expect(
			await setting({ pipi: { modelTiers: { imaginary: "faux/user" } } }),
		).toMatchObject({ ok: false });
	});
	it("keeps tier parsing tolerant of the timeoutMs leaf", async () => {
		expect(await setting({ pipi: { timeoutMs: 60_000 } })).toMatchObject({
			ok: true,
			value: { modelTiers: {}, providerModelTiers: {} },
		});
	});
});
describe("pipi call deadline", () => {
	it("defaults to four hours", async () => {
		expect(await deadline()).toMatchObject({
			ok: true,
			source: "default",
			value: 14_400_000,
		});
	});
	it("reads the environment variable", async () => {
		expect(
			await deadline(undefined, undefined, false, {
				PIPI_TIMEOUT_MS: "60000",
			}),
		).toMatchObject({ ok: true, source: "env", value: 60_000 });
	});
	it("reads user settings and gives trusted project priority", async () => {
		expect(await deadline({ pipi: { timeoutMs: 120_000 } })).toMatchObject({
			ok: true,
			source: "user",
			value: 120_000,
		});
		expect(
			await deadline(
				{ pipi: { timeoutMs: 120_000 } },
				{ pipi: { timeoutMs: 60_000 } },
				true,
			),
		).toMatchObject({ ok: true, source: "project", value: 60_000 });
	});
	it("ignores untrusted project deadlines", async () => {
		expect(
			await deadline(
				{ pipi: { timeoutMs: 120_000 } },
				{ pipi: { timeoutMs: 60_000 } },
			),
		).toMatchObject({ ok: true, source: "user", value: 120_000 });
	});
	it("rejects invalid deadlines without falling through", async () => {
		expect(await deadline({ pipi: { timeoutMs: 0 } })).toMatchObject({
			ok: false,
			source: "user",
		});
		expect(
			await deadline({ pipi: { timeoutMs: 3_000_000_000 } }),
		).toMatchObject({
			ok: false,
			source: "user",
		});
		expect(
			await deadline(
				{ pipi: { timeoutMs: 120_000 } },
				{ pipi: { timeoutMs: 0 } },
				true,
			),
		).toMatchObject({ ok: false, source: "project" });
		// Runs last: the fixture environment persists until afterEach disposal.
		expect(
			await deadline(undefined, undefined, false, {
				PIPI_TIMEOUT_MS: "abc",
			}),
		).toMatchObject({ ok: false, source: "env" });
	});
});