repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/web-composer.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import {
	composerKeyAction,
	filterCommandItems,
	moveMenuIndex,
} from "../web/components/composer.js";

describe("ui web composer autocomplete", () => {
	const commands = [
		{ name: "mode", description: "Switch mode" },
		{ name: "skill:brutal", description: "Brutal style" },
		{ name: "skill:wiki", description: "Wiki research" },
		{ name: "ui", description: "Browser UI" },
	];

	it("filters slash command items", () => {
		expect(filterCommandItems(commands, "/skill:b")).toEqual([
			{ name: "skill:brutal", description: "Brutal style" },
		]);
	});

	it("returns extension, skill, and prompt command items for empty slash prefix", () => {
		expect(filterCommandItems(commands, "/").map((item) => item.name)).toEqual([
			"mode",
			"skill:brutal",
			"skill:wiki",
			"ui",
		]);
	});

	it("wraps keyboard menu selection", () => {
		expect(moveMenuIndex(0, 1, 3)).toBe(1);
		expect(moveMenuIndex(2, 1, 3)).toBe(0);
		expect(moveMenuIndex(0, -1, 3)).toBe(2);
		expect(moveMenuIndex(0, 1, 0)).toBe(0);
	});

	it("maps composer keybindings", () => {
		expect(composerKeyAction({ key: "Enter" })).toBe("submit");
		expect(composerKeyAction({ key: "Enter", shiftKey: true })).toBe("newline");
		expect(composerKeyAction({ key: "Escape" })).toBe("abort");
		expect(composerKeyAction({ key: "o", ctrlKey: true })).toBe(
			"toggle-expand",
		);
		expect(composerKeyAction({ key: "p", ctrlKey: true })).toBe("cycle-model");
		expect(
			composerKeyAction({ key: "Arrow" + "Down", menuVisible: true }),
		).toBe("menu-next");
	});
});