Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/ephemlets/__tests__/look.test.ts

Raw
import { colorToHex, type TUI } from "@earendil-works/pi-tui";
import { describe, expect, it, vi } from "vitest";
import { icon, Look, Palette, parsePaletteReply } from "../src/look.ts";
import { indexedTheme, plainTheme } from "./theme.ts";

describe("ephemlet look", () => {
	it("blends tints in OKLCH when theme colors are concrete", () => {
		const look = new Look(plainTheme, undefined, "nerd");
		expect(colorToHex(look.surface)).toBe("#242424");
		for (const tint of [look.band, look.focus, look.hover!, look.pill])
			expect(colorToHex(tint)).not.toBe("#242424");
		expect(colorToHex(look.urgentBand)).not.toBe(colorToHex(look.band));
		expect(look.focusText).toBeUndefined();
	});
	it("falls back to plain theme tokens for unresolved palette indices", () => {
		const look = new Look(indexedTheme, undefined, "nerd");
		expect(look.band).toEqual(look.surface);
		expect(look.urgentBand).toEqual(look.surface);
		expect(look.hover).toBeUndefined();
		expect(colorToHex(look.focus)).toBe("#c9b6eb");
		expect(look.focusText).toEqual(look.surface);
		expect(look.pill).toEqual(look.muted);
	});
	it("asks the terminal for palette colors once and blends with its reply", async () => {
		let listener: ((data: string) => unknown) | undefined;
		const written: string[] = [];
		const changed = vi.fn();
		const tui = {
			addInputListener(next: (data: string) => unknown) {
				listener = next;
				return () => {
					listener = undefined;
				};
			},
			terminal: { write: (data: string) => written.push(data) },
		} as unknown as TUI;
		const palette = new Palette(tui, changed);
		try {
			expect(new Look(indexedTheme, palette, "nerd").hover).toBeUndefined();
			await Promise.resolve();
			expect(written).toEqual(["\x1b]4;18;?\x1b\\", "\x1b]4;7;?\x1b\\"]);
			expect(listener?.("x")).toBeUndefined();
			expect(listener?.("\x1b]4;18;rgb:2424/2424/2424\x1b\\")).toEqual({
				consume: true,
			});
			expect(listener?.("\x1b]4;7;rgb:ed/ed/ed\x07")).toEqual({
				consume: true,
			});
			expect(listener).toBeUndefined();
			expect(changed).toHaveBeenCalledTimes(2);
			const look = new Look(indexedTheme, palette, "nerd");
			expect(look.hover).toBeDefined();
			expect(colorToHex(look.band)).not.toBe("#242424");
			expect(written).toHaveLength(2);
		} finally {
			palette.dispose();
		}
	});
	it("parses OSC 4 replies with any channel width", () => {
		expect(parsePaletteReply("\x1b]4;3;rgb:f/0/80\x07")).toEqual({
			index: 3,
			color: { kind: "rgb", r: 255, g: 0, b: 128 },
		});
		expect(parsePaletteReply("\x1b]11;rgb:0/0/0\x07")).toBeUndefined();
	});
	it("uses verified Nerd Font Material Design code points", () => {
		expect(
			[
				icon("nerd", "checkOn"),
				icon("nerd", "checkOff"),
				icon("nerd", "radioOn"),
				icon("nerd", "radioOff"),
				icon("nerd", "read"),
				icon("nerd", "now"),
				icon("nerd", "next"),
			].map((glyph) => glyph.codePointAt(0)?.toString(16)),
		).toEqual(["f0132", "f0131", "f043e", "f043d", "f012d", "f0241", "f1164"]);
		expect(icon("unicode", "checkOn")).toBe("☑");
	});
});