Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/__tests__/native-setup-approval.test.ts

Raw
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
import { ScrollView, type TUI } from "@earendil-works/pi-tui";
import { expect, it, vi } from "vitest";
import {
	approveNativeSetup,
	createNativeSetupApprovalView,
} from "../src/native-setup-approval.ts";

it.each([
	["ctrl+alt+v", "\u001b\u0016"],
	["ctrl+alt+x", "\u001b\u0018"],
] as const)(
	"cancels the focused setup view with configured toggle %s",
	(shortcut, input) => {
		const done = vi.fn();
		const view = createNativeSetupApprovalView(
			{ requestRender() {} } as unknown as TUI,
			{ fg: (_color, text) => text, bold: (text) => text },
			"Approve assets?",
			"No assets may be acquired before consent.",
			done,
			shortcut,
		);
		expect(view.render(100).join("\n")).toContain(shortcut);
		view.handleInput?.(input);
		expect(done).toHaveBeenCalledOnce();
		expect(done).toHaveBeenCalledWith(false);
	},
);

it("starts with a compact summary and keeps full sources, hashes and destinations behind details", () => {
	const done = vi.fn();
	const view = createNativeSetupApprovalView(
		{ requestRender: vi.fn() } as unknown as TUI,
		{ fg: (_color, text) => text, bold: (text) => text },
		"Download voice files?",
		"Source: https://example.invalid/model\nSHA-256: pinned-digest\nDestination: /voice/cache/model\n\u001b]52;c;untrusted\u0007",
		done,
		undefined,
		"84 MB download\nStorage: /voice/cache",
	);
	let text = view.render(80).join("\n");
	expect(text).toContain("84 MB download");
	expect(text).not.toContain("SHA-256");
	expect(text).not.toContain("https://");
	expect(text.match(/y continue/gu)).toHaveLength(1);
	view.handleInput?.("d");
	text = view.render(80).join("\n");
	expect(text).toContain("pinned-digest");
	expect(text).toContain("/voice/cache/model");
	expect(text).not.toContain("untrusted");
	expect(done).not.toHaveBeenCalled();
	view.handleInput?.("d");
	expect(view.render(80).join("\n")).not.toContain("SHA-256");
	view.handleInput?.("y");
	expect(done).toHaveBeenCalledWith(true);
});

it("reports setup consent as a permission and resolves approval", async () => {
	let options: unknown;
	const ctx = {
		ui: {
			custom: async (factory: any, customOptions: unknown) => {
				options = customOptions;
				return await new Promise((resolve) => {
					const view = factory(
						{ requestRender() {} },
						{
							fg: (_color: string, text: string) => text,
							bold: (text: string) => text,
						},
						{},
						resolve,
					);
					view.handleInput("y");
				});
			},
		},
	} as unknown as ExtensionContext;

	await expect(
		approveNativeSetup(ctx, "Approve voice files?", "details"),
	).resolves.toBe(true);
	expect(options).toEqual({
		blocked: { kind: "permission", message: "Approve voice files?" },
	});
});

it("cancels setup consent when its signal aborts", async () => {
	const controller = new AbortController();
	const ctx = {
		ui: {
			custom: (factory: any) =>
				new Promise((resolve) => {
					factory(
						{ requestRender() {} },
						{
							fg: (_color: string, text: string) => text,
							bold: (text: string) => text,
						},
						{},
						resolve,
					);
					controller.abort();
				}),
		},
	} as unknown as ExtensionContext;

	await expect(
		approveNativeSetup(
			ctx,
			"Approve voice files?",
			"details",
			controller.signal,
		),
	).resolves.toBe(false);
});

it("keeps the complete asset set in a real scroll viewport and never approves on Enter", () => {
	const done = vi.fn();
	const tui = { requestRender() {} } as unknown as TUI;
	const theme = {
		fg: (_color: string, text: string) => text,
		bold: (text: string) => text,
	} as unknown as Theme;
	const details = Array.from(
		{ length: 300 },
		(_, index) => `asset ${index}: destination, bytes, provenance, gap`,
	).join("\n");
	const view = createNativeSetupApprovalView(
		tui,
		theme,
		"Approve assets?",
		`${details}\n\u001b]52;c;untrusted\u0007`,
		done,
	);
	expect(view).toBeInstanceOf(ScrollView);
	const rendered = view.render(40).join("\n");
	expect(rendered).toContain("asset 299");
	expect(rendered).not.toContain("untrusted");
	view.handleInput?.("\r");
	expect(done).not.toHaveBeenCalled();
	view.handleInput?.("y");
	expect(done).toHaveBeenLastCalledWith(true);
	view.handleInput?.("\u001b");
	expect(done).toHaveBeenLastCalledWith(false);
});