BlueGit

timfa questward

Files

questward / client / tests / sprite-size.test.js

sprite-size.test.js

sprite-size.test.js 1.5 KiB Download Raw
1import { readdirSync, readFileSync } from "node:fs";
2import { extname, join } from "node:path";
3import { fileURLToPath } from "node:url";
4import test from "node:test";
5import assert from "node:assert/strict";
6
7const SPRITE_SIZE = 16;
8const ASSET_ROOT = join(fileURLToPath(new URL("..", import.meta.url)), "assets");
9const SPRITE_FOLDERS = ["grounds", "blocks", "characters", "items", "equipment", "mobs"];
10
11function pngSize (buffer)
12{
13 if (buffer.length < 24 || buffer.toString("ascii", 1, 4) !== "PNG")
14 { throw new Error("Not a PNG"); }
15 return {
16 width: buffer.readUInt32BE(16),
17 height: buffer.readUInt32BE(20)
18 };
19}
20
21function listPngs (dir)
22{
23 const files = [];
24 for (const entry of readdirSync(dir, { withFileTypes: true }))
25 {
26 const path = join(dir, entry.name);
27 if (entry.isDirectory())
28 { files.push(...listPngs(path)); }
29 else if (extname(entry.name).toLowerCase() === ".png")
30 { files.push(path); }
31 }
32 return files;
33}
34
35test("every world sprite is exactly 16×16", () =>
36{
37 const offenders = [];
38 for (const folder of SPRITE_FOLDERS)
39 {
40 const dir = join(ASSET_ROOT, folder);
41 let files = [];
42 try
43 { files = listPngs(dir); }
44 catch (error)
45 {
46 if (error.code === "ENOENT")
47 { continue; }
48 throw error;
49 }
50 for (const file of files)
51 {
52 const { width, height } = pngSize(readFileSync(file));
53 if (width !== SPRITE_SIZE || height !== SPRITE_SIZE)
54 { offenders.push(`${file} is ${width}×${height}`); }
55 }
56 }
57 assert.equal(offenders.length, 0, offenders.join("\n"));
58});