repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/llmiterate/__tests__/ui.test.ts

Raw
File outline
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
import { describe, expect, test } from "vitest";
import { defaultConfig, normalizeConfig, parseMarkerPrompts } from "../core";
import type { LlmiterateRun } from "../types";
import { renderLlmiterateStatus } from "../ui";

interface UiCall {
	key: string;
	value: string | undefined;
}

const theme = {
	fg: (_color: string, text: string) => text,
	bold: (text: string) => text,
} as unknown as ExtensionContext["ui"]["theme"];

describe("llmiterate UI", () => {
	test("status line states", () => {
		const calls: UiCall[] = [];
		const ctx = fakeContext({
			setStatus: (key, value) => calls.push({ key, value }),
		});
		const activeConfig = normalizeConfig({ include: ["src/**/*.ts"] });

		renderLlmiterateStatus(ctx, {
			watchErrors: 0,
			watchDirs: 0,
			queuedRuns: 0,
		});
		renderLlmiterateStatus(ctx, {
			config: defaultConfig(),
			watchErrors: 0,
			watchDirs: 0,
			queuedRuns: 0,
		});
		renderLlmiterateStatus(ctx, {
			config: activeConfig,
			lockError: "held",
			watchErrors: 0,
			watchDirs: 0,
			queuedRuns: 0,
		});
		renderLlmiterateStatus(ctx, {
			config: activeConfig,
			watchErrors: 2,
			watchDirs: 0,
			queuedRuns: 0,
		});
		renderLlmiterateStatus(ctx, {
			config: activeConfig,
			watchErrors: 0,
			watchDirs: 2,
			queuedRuns: 3,
		});
		renderLlmiterateStatus(ctx, {
			config: activeConfig,
			watchErrors: 0,
			watchDirs: 2,
			queuedRuns: 0,
			activeRun: sampleRun(),
		});

		expect(calls.map((c) => c.value)).toEqual([
			undefined,
			"llmiterate watching .",
			"llmiterate standby",
			"llmiterate 2 watch error(s)",
			"llmiterate queued 3",
			"llmiterate running src/main.ts:1",
		]);
	});
});

function fakeContext(overrides: {
	setStatus?: (key: string, value: string | undefined) => void;
}): ExtensionContext {
	return {
		hasUI: true,
		ui: {
			theme,
			setStatus: overrides.setStatus ?? (() => {}),
		},
	} as unknown as ExtensionContext;
}

function sampleRun(): LlmiterateRun {
	const [block] = parseMarkerPrompts(
		"src/main.ts",
		"// llmiterate: implement foo and bar",
	);
	if (!block) throw new Error("expected llmiterate block");
	return {
		id: 1,
		block,
		projectFile: "src/main.ts",
		absoluteFile: "/tmp/project/src/main.ts",
		fullPrompt: "prompt",
		status: "running",
		queuedAt: 1,
		updatedAt: 2,
		assistantText: "I am editing the file now and will run tests afterwards.",
		toolText: "running edit",
	};
}