1import { assert, describe, it } from "@effect/vitest";
2
3import { encodePngIco, readPngDimensions } from "./icon-export.ts";
4
5const pngHeader = (width: number, height: number) => {
6 const contents = Buffer.alloc(24);
7 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(contents);
8 contents.write("IHDR", 12, "ascii");
9 contents.writeUInt32BE(width, 16);
10 contents.writeUInt32BE(height, 20);
11 return contents;
12};
13
14describe("icon export", () => {
15 it("reads dimensions from a PNG IHDR chunk", () => {
16 assert.deepEqual(readPngDimensions(pngHeader(1024, 512)), { width: 1024, height: 512 });
17 });
18
19 it("encodes PNG renditions into an ICO directory", () => {
20 const small = pngHeader(16, 16);
21 const large = pngHeader(256, 256);
22 const ico = encodePngIco([
23 { size: 16, contents: small },
24 { size: 256, contents: large },
25 ]);
26
27 assert.equal(ico.readUInt16LE(2), 1);
28 assert.equal(ico.readUInt16LE(4), 2);
29 assert.equal(ico.readUInt8(6), 16);
30 assert.equal(ico.readUInt8(22), 0);
31 assert.equal(ico.readUInt32LE(18), 38);
32 assert.equal(ico.readUInt32LE(34), 38 + small.length);
33 assert.deepEqual(ico.subarray(38, 38 + small.length), small);
34 assert.deepEqual(ico.subarray(38 + small.length), large);
35 });
36
37 it("rejects duplicate ICO rendition sizes", () => {
38 assert.throws(
39 () =>
40 encodePngIco([
41 { size: 32, contents: pngHeader(32, 32) },
42 { size: 32, contents: pngHeader(32, 32) },
43 ]),
44 /provided more than once/,
45 );
46 });
47});