Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/chrome-cdp/__tests__/lib.test.ts

Raw
import assert from "node:assert/strict";
import { test } from "vitest";
import {
	artifactPath,
	binaryKey,
	dataRoot,
	downloads,
	type Frame,
	profileName,
	ref,
	validateFlags,
} from "../lib.ts";

test("names and managed file destinations reject traversal and aliases", () => {
	for (const bad of ["A", "../bad", "con", "foo/bar", "x\\y", "", "."])
		assert.throws(() => profileName(bad));
	assert.equal(profileName("pi-cdp-test"), "pi-cdp-test");
	assert.throws(() => artifactPath("/tmp/a", "../secret"));
	assert.throws(() => artifactPath("/tmp/a", "nested/file.png"));
	assert.equal(artifactPath("/tmp/a", "shot.png"), "/tmp/a/shot.png");
	for (const flag of [
		"--user-data-dir=/tmp/other",
		"--remote-debugging-port=9222",
		"--remote-debugging-address=0.0.0.0",
		"--remote-debugging-pipe",
	])
		assert.throws(() => validateFlags([flag]));
});

test("typed result references and literal escape", () => {
	const results = new Map<string, Frame>([
		[
			"tab",
			{ id: 1, result: { targetId: "T", size: 42, nested: { a: true } } },
		],
	]);
	assert.deepEqual(
		ref(
			{
				id: "{{tab.result.targetId}}",
				size: "{{tab.result.size}}",
				object: "{{tab.result.nested}}",
				literal: "\\{{tab.result.size}}",
			},
			results,
		),
		{ id: "T", size: 42, object: { a: true }, literal: "{{tab.result.size}}" },
	);
	assert.throws(() => ref("{{tab.result.missing}}", results));
});

test("platform data roots and binary namespaces", () => {
	assert.equal(
		dataRoot("linux", { XDG_DATA_HOME: "/home/a/data" }, "/home/a"),
		"/home/a/data/chrome-cdp/profiles",
	);
	assert.equal(
		dataRoot("darwin", {}, "/Users/a"),
		"/Users/a/Library/Application Support/chrome-cdp/profiles",
	);
	assert.equal(
		dataRoot(
			"win32",
			{ LOCALAPPDATA: "C:\\Users\\a\\AppData\\Local" },
			"C:\\Users\\a",
		).endsWith("chrome-cdp\\profiles"),
		true,
	);
	assert.equal(downloads("darwin", {}, "/Users/a"), "/Users/a/Downloads");
	assert.equal(binaryKey("chrome-for-testing"), "chrome-for-testing");
	assert.equal(binaryKey("chromium"), "chromium");
});