repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/uv/__tests__/harness.test.ts

Raw
File outline
import { afterEach, describe, expect, it } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
import uvExtension, { __test } from "../index";

let t: TestSession | undefined;

afterEach(() => {
	t?.dispose();
	t = undefined;
});

describe("uv extension", () => {
	it("loads in real Pi extension runtime", async () => {
		t = await createTestSession({ extensionFactories: [uvExtension] });

		expect(t.session.extensionRunner.extensions).toHaveLength(1);
	});

	it("blocks pip-family commands with uv guidance", () => {
		expect(__test.getBlockedCommandMessage("pip install rich")).toContain(
			"uv add PACKAGE",
		);
		expect(
			__test.getBlockedCommandMessage("python -m pip install rich"),
		).toContain("python -m pip");
	});

	it("blocks venv and py_compile bytecode writes", () => {
		expect(__test.getBlockedCommandMessage("python3 -m venv .venv")).toContain(
			"uv venv",
		);
		expect(
			__test.getBlockedCommandMessage(".venv/bin/python -m py_compile x.py"),
		).toContain("__pycache__");
	});

	it("leaves normal Python execution alone", () => {
		expect(__test.getBlockedCommandMessage("python script.py")).toBeNull();
		expect(
			__test.getBlockedCommandMessage("uv run python script.py"),
		).toBeNull();
	});
});