repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/decay/__tests__/ui.test.ts

Raw
File outline
import { describe, expect, it, vi } from "vitest";
import {
	DecayComponent,
	type DecayDisplayState,
	plainFrame,
	renderDecayFrame,
} from "../ui";

function state(overrides: Partial<DecayDisplayState> = {}): DecayDisplayState {
	return {
		model: "openai-codex/gpt-5.6-luna",
		inputTokens: 118_420,
		keptTokens: 20_000,
		totalChunks: 8,
		phase: "classify",
		transfers: [],
		startedAt: 1_000,
		...overrides,
	};
}

const completedTransfers: DecayDisplayState["transfers"] = [
	{ chunk: 1, kind: "old", recurring: false, acceptedAt: 1_000 },
	{ chunk: 2, kind: "old", recurring: true, acceptedAt: 1_000 },
	{ chunk: 3, kind: "established", recurring: false, acceptedAt: 1_000 },
	{ chunk: 4, kind: "established", recurring: false, acceptedAt: 1_000 },
	{ chunk: 5, kind: "recent", recurring: false, acceptedAt: 1_000 },
	{ chunk: 6, kind: "recent", recurring: true, acceptedAt: 1_000 },
	{ chunk: 7, kind: "newest", recurring: false, acceptedAt: 1_000 },
	{ chunk: 8, kind: "newest", recurring: true, acceptedAt: 1_000 },
];

describe("Decay terminal rendering", () => {
	it.each([
		[40, 15, 4],
		[40, 24, 8],
		[59, 23, 4],
		[80, 30, 8],
		[120, 40, 8],
		[140, 45, 8],
	])(
		"renders a fixed responsive %ix%i widget with %i lines",
		(width, viewportHeight, widgetHeight) => {
			const lines = plainFrame(
				renderDecayFrame(width, viewportHeight, state(), 5_000),
			);
			expect(lines).toHaveLength(widgetHeight);
			for (const line of lines) expect([...line]).toHaveLength(width);
			expect(lines.join("\n")).toContain("DECAY");
			expect(lines.join("\n")).toContain("CLASSIFY 0/8");
			expect(lines.join("\n")).not.toMatch(/TRIGGER|CAUSE/);
			if (widgetHeight === 8) {
				expect(lines.join("\n")).toContain("TRANSFER");
				expect(lines.join("\n")).toContain("SUMMARY");
				expect(lines.join("\n")).toContain("KEPT");
			} else {
				expect(lines.join("\n")).toContain("↵ queue");
			}
		},
	);

	it("keeps widget height stable across progress and phase changes", () => {
		for (const viewportHeight of [15, 30]) {
			const initial = renderDecayFrame(80, viewportHeight, state(), 1_000);
			const active = renderDecayFrame(
				80,
				viewportHeight,
				state({ transfers: completedTransfers }),
				2_000,
			);
			const merge = renderDecayFrame(
				80,
				viewportHeight,
				state({ phase: "merge", transfers: completedTransfers }),
				3_000,
			);
			expect(active).toHaveLength(initial.length);
			expect(merge).toHaveLength(initial.length);
		}
	});

	it("vacates classified history cells and lands an old-to-new summary", () => {
		const initial = plainFrame(renderDecayFrame(80, 30, state(), 2_000));
		const completed = plainFrame(
			renderDecayFrame(80, 30, state({ transfers: completedTransfers }), 2_000),
		);
		const initialHistory = initial.slice(1, 3).join("");
		const completedHistory = completed.slice(1, 3).join("");
		expect(completedHistory).not.toBe(initialHistory);
		expect(completedHistory.split(" ").length).toBeGreaterThan(
			initialHistory.split(" ").length,
		);
		const summary = completed.find((line) => line.includes("SUMMARY ["));
		expect(summary).toBeDefined();
		expect(
			summary
				?.slice(summary.indexOf("[") + 1, summary.lastIndexOf("]"))
				.replaceAll("·", "")
				.trim(),
		).toBe("░░▒▒▓▓██");
	});

	it("shows transfer motion and indeterminate merge without fake percentage", () => {
		const moving = plainFrame(
			renderDecayFrame(
				80,
				30,
				state({
					transfers: [
						{
							chunk: 1,
							kind: "old",
							recurring: false,
							acceptedAt: 1_000,
						},
					],
				}),
				1_330,
			),
		);
		expect(moving.find((line) => line.includes("TRANSFER"))).toMatch(/[▖▘▝▗]/);
		const merging = plainFrame(
			renderDecayFrame(80, 30, state({ phase: "merge" }), 2_000),
		);
		expect(merging.join("\n")).toContain("MERGE");
		expect(merging.join("\n")).not.toContain("MERGE 0%");
	});

	it("is passive and never implements focused keyboard handling", () => {
		const tui = {
			terminal: { rows: 30 },
			requestRender: vi.fn(),
		} as never;
		const component = new DecayComponent(
			tui,
			{
				fg: (_color: string, text: string) => text,
				bold: (text: string) => text,
			} as never,
			state(),
		);
		try {
			expect("handleInput" in component).toBe(false);
			expect(component.render(80)).toHaveLength(8);
		} finally {
			component.dispose();
		}
	});
});