Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

test: cover ICO binary frames and icon-source heuristics

Tw937a7c8047e93b
diff --git a/tests/unit/ico.test.ts b/tests/unit/ico.test.ts
new file mode 100644
index 00000000..318f0831
--- /dev/null
+++ b/tests/unit/ico.test.ts
@@ -0,0 +1,168 @@
+import fs from 'fs';
+import os from 'os';
+import path from 'path';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { buildIcoFromPngBuffers, writeIcoWithPreferredSize } from '@/utils/ico';
+
+const ICO_HEADER_SIZE = 6;
+const ICO_DIR_ENTRY_SIZE = 16;
+
+type ParsedEntry = {
+ width: number;
+ height: number;
+ bytesInRes: number;
+ imageOffset: number;
+ data: Buffer;
+};
+
+function decodeDim(byte: number): number {
+ return byte === 0 ? 256 : byte;
+}
+
+function parseIcoHeader(buffer: Buffer): ParsedEntry[] {
+ expect(buffer.readUInt16LE(0)).toBe(0);
+ expect(buffer.readUInt16LE(2)).toBe(1);
+ const count = buffer.readUInt16LE(4);
+ const entries: ParsedEntry[] = [];
+ for (let i = 0; i < count; i++) {
+ const offset = ICO_HEADER_SIZE + i * ICO_DIR_ENTRY_SIZE;
+ const bytesInRes = buffer.readUInt32LE(offset + 8);
+ const imageOffset = buffer.readUInt32LE(offset + 12);
+ entries.push({
+ width: decodeDim(buffer.readUInt8(offset)),
+ height: decodeDim(buffer.readUInt8(offset + 1)),
+ bytesInRes,
+ imageOffset,
+ data: buffer.subarray(imageOffset, imageOffset + bytesInRes),
+ });
+ }
+ return entries;
+}
+
+function fakePng(tag: number, length: number): Buffer {
+ const buf = Buffer.alloc(length);
+ buf.writeUInt32BE(0x89504e47, 0);
+ buf.writeUInt8(tag, 4);
+ return buf;
+}
+
+const tempDirs: string[] = [];
+
+function makeTempDir(): string {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pake-ico-test-'));
+ tempDirs.push(dir);
+ return dir;
+}
+
+afterEach(() => {
+ while (tempDirs.length > 0) {
+ const dir = tempDirs.pop();
+ if (dir && fs.existsSync(dir)) {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ }
+});
+
+describe('buildIcoFromPngBuffers', () => {
+ it('writes header, directory entries, and payload for a single frame', () => {
+ const png = fakePng(1, 40);
+ const ico = buildIcoFromPngBuffers([{ size: 16, png }]);
+
+ const entries = parseIcoHeader(ico);
+ expect(entries).toHaveLength(1);
+ expect(entries[0].width).toBe(16);
+ expect(entries[0].height).toBe(16);
+ expect(entries[0].bytesInRes).toBe(png.length);
+ expect(entries[0].imageOffset).toBe(ICO_HEADER_SIZE + ICO_DIR_ENTRY_SIZE);
+ expect(entries[0].data.equals(png)).toBe(true);
+ });
+
+ it('encodes size>=256 as the sentinel byte 0', () => {
+ const png = fakePng(2, 64);
+ const ico = buildIcoFromPngBuffers([{ size: 256, png }]);
+
+ expect(ico.readUInt8(ICO_HEADER_SIZE)).toBe(0);
+ expect(ico.readUInt8(ICO_HEADER_SIZE + 1)).toBe(0);
+ const entries = parseIcoHeader(ico);
+ expect(entries[0].width).toBe(256);
+ expect(entries[0].height).toBe(256);
+ });
+
+ it('lays frames out back to back with monotonic offsets', () => {
+ const a = fakePng(1, 20);
+ const b = fakePng(2, 35);
+ const c = fakePng(3, 50);
+ const ico = buildIcoFromPngBuffers([
+ { size: 16, png: a },
+ { size: 32, png: b },
+ { size: 64, png: c },
+ ]);
+
+ const entries = parseIcoHeader(ico);
+ expect(entries.map((e) => e.width)).toEqual([16, 32, 64]);
+ expect(entries[0].imageOffset).toBe(
+ ICO_HEADER_SIZE + 3 * ICO_DIR_ENTRY_SIZE,
+ );
+ expect(entries[1].imageOffset).toBe(
+ entries[0].imageOffset + entries[0].bytesInRes,
+ );
+ expect(entries[2].imageOffset).toBe(
+ entries[1].imageOffset + entries[1].bytesInRes,
+ );
+ expect(entries[0].data.equals(a)).toBe(true);
+ expect(entries[1].data.equals(b)).toBe(true);
+ expect(entries[2].data.equals(c)).toBe(true);
+ });
+
+ it('emits a header-only buffer for zero frames', () => {
+ const ico = buildIcoFromPngBuffers([]);
+ expect(ico.length).toBe(ICO_HEADER_SIZE);
+ expect(ico.readUInt16LE(4)).toBe(0);
+ });
+});
+
+describe('writeIcoWithPreferredSize', () => {
+ it('moves the frame matching preferredSize to the front', async () => {
+ const dir = makeTempDir();
+ const source = path.join(dir, 'source.ico');
+ const output = path.join(dir, 'reordered.ico');
+
+ const ico = buildIcoFromPngBuffers([
+ { size: 32, png: fakePng(1, 20) },
+ { size: 16, png: fakePng(2, 24) },
+ { size: 64, png: fakePng(3, 28) },
+ ]);
+ fs.writeFileSync(source, ico);
+
+ const ok = await writeIcoWithPreferredSize(source, output, 16);
+
+ expect(ok).toBe(true);
+ const result = parseIcoHeader(fs.readFileSync(output));
+ expect(result[0].width).toBe(16);
+ expect(result[0].data.readUInt8(4)).toBe(2);
+ });
+
+ it('returns false when the source file is missing', async () => {
+ const dir = makeTempDir();
+ const ok = await writeIcoWithPreferredSize(
+ path.join(dir, 'does-not-exist.ico'),
+ path.join(dir, 'out.ico'),
+ 32,
+ );
+ expect(ok).toBe(false);
+ });
+
+ it('returns false on a malformed ICO header', async () => {
+ const dir = makeTempDir();
+ const source = path.join(dir, 'bad.ico');
+ fs.writeFileSync(source, Buffer.from([0, 0]));
+
+ const ok = await writeIcoWithPreferredSize(
+ source,
+ path.join(dir, 'out.ico'),
+ 32,
+ );
+ expect(ok).toBe(false);
+ });
+});
diff --git a/tests/unit/icon-source.test.ts b/tests/unit/icon-source.test.ts
new file mode 100644
index 00000000..d34d5e22
--- /dev/null
+++ b/tests/unit/icon-source.test.ts
@@ -0,0 +1,114 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ generateDashboardIconSlugs,
+ getIconSourcePriority,
+ isLikelyLocalHostname,
+ shouldPreferDashboardIcons,
+} from '@/utils/icon-source';
+
+describe('isLikelyLocalHostname', () => {
+ it('recognises the literal localhost label', () => {
+ expect(isLikelyLocalHostname('localhost')).toBe(true);
+ expect(isLikelyLocalHostname('LOCALHOST')).toBe(true);
+ });
+
+ it('recognises IPv4 literals', () => {
+ expect(isLikelyLocalHostname('127.0.0.1')).toBe(true);
+ expect(isLikelyLocalHostname('10.0.0.5')).toBe(true);
+ });
+
+ it('recognises IPv6 literals via the colon heuristic', () => {
+ expect(isLikelyLocalHostname('::1')).toBe(true);
+ expect(isLikelyLocalHostname('fe80::1')).toBe(true);
+ });
+
+ it('treats dotless hostnames as local', () => {
+ expect(isLikelyLocalHostname('myhost')).toBe(true);
+ });
+
+ it('recognises typical local-network suffixes', () => {
+ expect(isLikelyLocalHostname('router.local')).toBe(true);
+ expect(isLikelyLocalHostname('nas.lan')).toBe(true);
+ expect(isLikelyLocalHostname('svc.internal')).toBe(true);
+ expect(isLikelyLocalHostname('box.home')).toBe(true);
+ expect(isLikelyLocalHostname('pi.localdomain')).toBe(true);
+ });
+
+ it('rejects public hostnames and empty input', () => {
+ expect(isLikelyLocalHostname('example.com')).toBe(false);
+ expect(isLikelyLocalHostname('a.b.example.com')).toBe(false);
+ expect(isLikelyLocalHostname('')).toBe(false);
+ expect(isLikelyLocalHostname(' ')).toBe(false);
+ });
+});
+
+describe('generateDashboardIconSlugs', () => {
+ it('lowercases and deduplicates a single-word name', () => {
+ expect(generateDashboardIconSlugs('GitHub')).toEqual(['github']);
+ });
+
+ it('emits both spaced and hyphenated variants for multi-word names', () => {
+ expect(generateDashboardIconSlugs('Notion AI')).toEqual([
+ 'notion ai',
+ 'notion-ai',
+ ]);
+ });
+
+ it('returns an empty list for blank input', () => {
+ expect(generateDashboardIconSlugs('')).toEqual([]);
+ expect(generateDashboardIconSlugs(' ')).toEqual([]);
+ });
+});
+
+describe('shouldPreferDashboardIcons', () => {
+ it('returns false when the app name is empty', () => {
+ expect(shouldPreferDashboardIcons('https://github.com/', '')).toBe(false);
+ });
+
+ it('returns false when the URL cannot be parsed', () => {
+ expect(shouldPreferDashboardIcons('not a url', 'GitHub')).toBe(false);
+ });
+
+ it('returns false for root domains that already host the brand', () => {
+ expect(shouldPreferDashboardIcons('https://github.com/', 'GitHub')).toBe(
+ false,
+ );
+ });
+
+ it('returns false when the subdomain label does not match the app name', () => {
+ expect(
+ shouldPreferDashboardIcons('https://mail.google.com/', 'Gmail'),
+ ).toBe(false);
+ });
+
+ it('returns true for product subdomains matching the app name', () => {
+ expect(
+ shouldPreferDashboardIcons(
+ 'https://notebooklm.google.com/',
+ 'NotebookLM',
+ ),
+ ).toBe(true);
+ });
+
+ it('returns true for self-hosted local hostnames', () => {
+ expect(
+ shouldPreferDashboardIcons('https://grafana.mylab.local/', 'Grafana'),
+ ).toBe(true);
+ });
+});
+
+describe('getIconSourcePriority', () => {
+ it('orders domain-first for plain root domains', () => {
+ expect(getIconSourcePriority('https://github.com/', 'GitHub')).toEqual([
+ 'domain',
+ 'dashboard',
+ ]);
+ });
+
+ it('orders dashboard-first for product subdomains', () => {
+ expect(
+ getIconSourcePriority('https://notebooklm.google.com/', 'NotebookLM'),
+ ).toEqual(['dashboard', 'domain']);
+ });
+});