repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ssh-tools/index.test.ts

Raw
File outline
/**
 * Smoke tests for ssh-tools pure functions.
 * Run: bun test extensions/ssh-tools/index.test.ts
 */
import { describe, expect, test } from "bun:test";

test("shellQuote escapes single quotes", () => {
	function shellQuote(s: string) {
		return `'${s.replaceAll("'", `'"'"'`)}'`;
	}
	expect(shellQuote("foo")).toBe("'foo'");
	expect(shellQuote("it's")).toBe("'it'\"'\"'s'");
	expect(shellQuote("")).toBe("''");
	expect(shellQuote('it\'s a "test"')).toBe("'it'\"'\"'s a \"test\"'");
});

test("normalizeTarget handles simple host", () => {
	function normalizeTarget(arg: string, profiles: { name: string }[]) {
		const t = arg.trim();
		const found = profiles.find((p) => p.name === t);
		if (found) return found;
		const i = t.indexOf(":");
		return i > 0
			? { name: t, remote: t.slice(0, i), cwd: t.slice(i + 1) }
			: { name: t, remote: t };
	}
	expect(normalizeTarget("server", [])).toEqual({
		name: "server",
		remote: "server",
	});
});

test("normalizeTarget handles host:path", () => {
	function normalizeTarget(arg: string, profiles: { name: string }[]) {
		const t = arg.trim();
		const found = profiles.find((p) => p.name === t);
		if (found) return found;
		const i = t.indexOf(":");
		return i > 0
			? { name: t, remote: t.slice(0, i), cwd: t.slice(i + 1) }
			: { name: t, remote: t };
	}
	expect(normalizeTarget("server:/home/me", [])).toEqual({
		name: "server:/home/me",
		remote: "server",
		cwd: "/home/me",
	});
});

test("normalizeTarget prefers profile match", () => {
	function normalizeTarget(
		arg: string,
		profiles: { name: string; remote: string; cwd?: string }[],
	) {
		const t = arg.trim();
		const found = profiles.find((p) => p.name === t);
		if (found) return found;
		const i = t.indexOf(":");
		return i > 0
			? { name: t, remote: t.slice(0, i), cwd: t.slice(i + 1) }
			: { name: t, remote: t };
	}
	const profiles = [{ name: "prod", remote: "prod.example.com", cwd: "/app" }];
	expect(normalizeTarget("prod", profiles)).toEqual({
		name: "prod",
		remote: "prod.example.com",
		cwd: "/app",
	});
});

test("normalizeTarget profile beats host:path", () => {
	function normalizeTarget(
		arg: string,
		profiles: { name: string; remote: string; cwd?: string }[],
	) {
		const t = arg.trim();
		const found = profiles.find((p) => p.name === t);
		if (found) return found;
		const i = t.indexOf(":");
		return i > 0
			? { name: t, remote: t.slice(0, i), cwd: t.slice(i + 1) }
			: { name: t, remote: t };
	}
	const profiles = [
		{ name: "prod:/custom", remote: "prod.example.com", cwd: "/opt" },
	];
	expect(normalizeTarget("prod:/custom", profiles)).toEqual({
		name: "prod:/custom",
		remote: "prod.example.com",
		cwd: "/opt",
	});
});

test("IMAGE_MIMES lookup", () => {
	const IMAGE_MIMES: Record<string, string> = {
		".jpg": "image/jpeg",
		".jpeg": "image/jpeg",
		".png": "image/png",
		".gif": "image/gif",
		".webp": "image/webp",
	};
	expect(IMAGE_MIMES[".png"]).toBe("image/png");
	expect(IMAGE_MIMES[".PNG"]).toBe(undefined);
	expect(IMAGE_MIMES[".xyz"]).toBe(undefined);
	expect(IMAGE_MIMES[".jpg"]).toBe("image/jpeg");
	expect(IMAGE_MIMES[".jpeg"]).toBe("image/jpeg");
});

test("TOOL_SET membership", () => {
	const TOOL_NAMES = ["ssh_read", "ssh_write", "ssh_edit", "ssh_bash"] as const;
	const TOOL_SET = new Set(TOOL_NAMES);
	expect(TOOL_SET.has("ssh_read")).toBe(true);
	expect(TOOL_SET.has("ssh_write")).toBe(true);
	expect(TOOL_SET.has("ssh_edit")).toBe(true);
	expect(TOOL_SET.has("ssh_bash")).toBe(true);
	expect(TOOL_SET.has("bash")).toBe(false);
});

// Regression: helpers must not reference outer 'pi' scope
test("makeHelpers creates closures without outer pi reference", () => {
	// Simulate the pattern: helpers defined inside function, not outside
	let called = false;
	function makeHelpers(_pi: { setActiveTools: (t: string[]) => void }) {
		function enableTools() {
			called = true;
		}
		function disableTools() {
			called = false;
		}
		return { enableTools, disableTools };
	}
	const mockPi = { setActiveTools: (_t: string[]) => {} };
	const { enableTools } = makeHelpers(mockPi);
	enableTools();
	expect(called).toBe(true);
});