Skip to content

Open-source, local-first AI agent for real work.

2.3 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
import { verifyWindowsLayoutBranding } from "./windows-executable-branding.mjs";

const vettaIconPath = join(import.meta.dirname, "..", "build", "icon.ico");
const otherIconPath = join(import.meta.dirname, "..", "build", "tray", "win", "tray-dark-taskbar.ico");

async function writeExecutable(path, { productName, iconPath }) {
	const executable = NtExecutable.createEmpty(false, false);
	const resource = NtExecutableResource.from(executable);
	const language = { lang: 1033, codepage: 1200 };
	const versionInfo = Resource.VersionInfo.createEmpty();
	versionInfo.setStringValues(language, { ProductName: productName });
	versionInfo.outputToResourceEntries(resource.entries);
	const icons = Data.IconFile.from(await readFile(iconPath)).icons.map(({ data }) => data);
	Resource.IconGroupEntry.replaceIconsForResource(resource.entries, 1, language.lang, icons);
	resource.outputResource(executable);
	await writeFile(path, Buffer.from(executable.generate()));
}

test("Windows layout branding rejects an unbranded application behind a branded launcher", async () => {
	const root = await mkdtemp(join(tmpdir(), "vetta-windows-branding-"));
	const launcherPath = join(root, "Vetta.exe");
	const applicationPath = join(root, "versions", "1.2.3", "Vetta.exe");
	try {
		await mkdir(join(root, "versions", "1.2.3"), { recursive: true });
		await writeExecutable(launcherPath, { productName: "Vetta", iconPath: vettaIconPath });
		await writeExecutable(applicationPath, { productName: "Vetta", iconPath: vettaIconPath });
		await verifyWindowsLayoutBranding(root, "1.2.3");

		await writeExecutable(applicationPath, { productName: "Electron", iconPath: vettaIconPath });
		await assert.rejects(() => verifyWindowsLayoutBranding(root, "1.2.3"), /has product name Electron/);

		await writeExecutable(applicationPath, { productName: "Vetta", iconPath: otherIconPath });
		await assert.rejects(() => verifyWindowsLayoutBranding(root, "1.2.3"), /does not use the Vetta icon/);
	} finally {
		await rm(root, { recursive: true, force: true });
	}
});