Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/activity/__tests__/view.test.ts

Raw
import headless from "@xterm/headless";
import { expect, it } from "vitest";
import { terminalView } from "../view.ts";

it("keeps style reuse local and distinguishes every projected attribute and color mode", async () => {
	const terminal = new headless.Terminal({
		allowProposedApi: true,
		cols: 200,
		rows: 5,
		scrollback: 0,
	});
	const styles = [
		"",
		"1",
		"2",
		"3",
		"4",
		"7",
		"8",
		"9",
		"38;5;1",
		"38;2;0;0;1",
		"48;5;1",
		"48;2;0;0;1",
		"1;2;3;4;7;8;9;38;5;123;48;2;1;2;3",
	];
	try {
		for (const order of [styles, [...styles].reverse()]) {
			const input = order
				.map((style) => `\x1b[0${style ? `;${style}` : ""}m界e\u0301`)
				.join("");
			await new Promise<void>((resolve) =>
				terminal.write(`\x1b[2J\x1b[H${input}\x1b[0m`, resolve),
			);
			const view = terminalView(terminal, true);
			expect(view.lines[0]).toBe(
				input +
					(order.at(-1) ? "\x1b[0m" : "") +
					" ".repeat(200 - order.length * 3) +
					"\x1b[0m",
			);
			expect(view.cursor).toMatchObject({
				x: order.length * 3,
				y: 0,
				text: " ",
				width: 1,
				style: "\x1b[0m",
				inverse: false,
			});
			expect(terminalView(terminal, false).cursor).toBeUndefined();
		}
	} finally {
		terminal.dispose();
	}
});