repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/terminal-widget.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import { osc8Link, renderUiWidget } from "../terminal-widget";

const OSC8_RE = /\u001b]8;;.*?\u001b\\|\u001b]8;;\u001b\\/g;

function visibleLength(line: string): number {
	return line.replace(OSC8_RE, "").length;
}

describe("ui terminal widget", () => {
	it("renders OSC 8 links", () => {
		expect(osc8Link("http://127.0.0.1:123/open", "open ui")).toBe(
			"\u001b]8;;http://127.0.0.1:123/open\u001b\\open ui\u001b]8;;\u001b\\",
		);
	});

	it("renders a closed bounded widget box", () => {
		const lines = renderUiWidget({
			url: "http://127.0.0.1:123/?token=abc",
			qr: Array.from({ length: 30 }, (_, index) => `QR ${index}`).join("\n"),
			state: "connected",
			sessionLabel: "ui: pi-ext",
			clients: 1,
		});

		const text = lines.join("\n");
		const visibleText = text.replace(OSC8_RE, "");
		expect(lines).toHaveLength(9);
		expect(lines[0]).toMatch(/^┏━+┓$/);
		expect(lines.at(-1)).toMatch(/^┗━+┛$/);
		expect(lines.slice(1, -1).every((line) => /^┃.*┃$/.test(line))).toBe(true);
		expect(new Set(lines.map((line) => visibleLength(line))).size).toBe(1);
		expect(text).toContain("pi ui");
		expect(visibleText).toContain("url: http://127.0.0.1:123/");
		expect(visibleText).not.toContain("token=abc");
		expect(text).toContain("connected");
		expect(text).toContain("ui: pi-ext");
		expect(text).toContain("press ESC to stop");
		expect(text).toContain("/ui off");
		expect(text).toContain("/ui qr");
		expect(text).not.toContain("widget truncated");
		expect(text).not.toContain("QR 29");
	});

	it("wraps long content inside the box without truncating", () => {
		const longLabel = "ui: " + "x".repeat(90);
		const lines = renderUiWidget({
			url: "http://127.0.0.1:123/?token=abc",
			qr: "QR",
			state: "connected",
			sessionLabel: longLabel,
			clients: 1,
		});

		const text = lines.join("\n");
		expect(text).toContain("session: ui: ");
		expect(text.match(/x/g)).toHaveLength(90);
		expect(text).not.toContain("…");
		expect(new Set(lines.map((line) => visibleLength(line))).size).toBe(1);
	});
});