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