// Tests for the Foundry manifest the frontend serves (Syndria98/dragonroll#111): Foundry installs a // module from a manifest URL and then downloads a zip, so the zip has to hold `module.json` and every // module file at its root, and the manifest has to be the source module.json with absolute URLs. // // `frontend/server/utils/foundry-module.js` builds both. It is loaded straight from source, with its // `node:` imports, the way the other tests load frontend code; the routes on top of it only add the // request origin. // // Run with `npm test` from the repository root, or `node --test tests/`. const { test } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const { pathToFileURL } = require("node:url"); const moduleDir = path.join(__dirname, "..", "dragonroll-foundry"); const moduleUtil = pathToFileURL( path.join(__dirname, "..", "frontend", "server", "utils", "foundry-module.js"), ).href; function readSourceFiles(dir, base = dir) { return fs.readdirSync(dir, { withFileTypes: true }) .flatMap((entry) => { const full = path.join(dir, entry.name); return entry.isDirectory() ? readSourceFiles(full, base) : [path.relative(base, full).split(path.sep).join("/")]; }) .sort(); } // Reads a stored (uncompressed) zip through its central directory, checking each local header. function readZipEntries(zip) { const eocd = zip.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06])); assert.ok(eocd >= 0, "the zip has an end of central directory record"); const count = zip.readUInt16LE(eocd + 10); let offset = zip.readUInt32LE(eocd + 16); const entries = []; for (let index = 0; index < count; index++) { assert.equal(zip.readUInt32LE(offset), 0x02014b50, "central directory header"); assert.equal(zip.readUInt16LE(offset + 10), 0, "stored, not deflated"); const nameLength = zip.readUInt16LE(offset + 28); const extraLength = zip.readUInt16LE(offset + 30); const commentLength = zip.readUInt16LE(offset + 32); const localOffset = zip.readUInt32LE(offset + 42); const name = zip.toString("utf8", offset + 46, offset + 46 + nameLength); assert.equal(zip.readUInt32LE(localOffset), 0x04034b50, "local file header"); const dataStart = localOffset + 30 + zip.readUInt16LE(localOffset + 26) + zip.readUInt16LE(localOffset + 28); const size = zip.readUInt32LE(localOffset + 18); entries.push({ name, data: zip.subarray(dataStart, dataStart + size) }); offset += 46 + nameLength + extraLength + commentLength; } return entries; } test("the manifest is the module's module.json", async () => { const { readModuleManifest } = await import(moduleUtil); const manifest = readModuleManifest(); const source = JSON.parse(fs.readFileSync(path.join(moduleDir, "module.json"), "utf8")); assert.deepEqual(manifest, source); assert.equal(manifest.id, "dragonroll-foundry"); assert.ok(manifest.version); }); test("the zip installs every module file at its root, byte for byte", async () => { const { buildFoundryModuleZip } = await import(moduleUtil); const entries = readZipEntries(buildFoundryModuleZip()); assert.deepEqual(entries.map((entry) => entry.name), readSourceFiles(moduleDir)); assert.ok(entries.some((entry) => entry.name === "module.json")); assert.ok(entries.some((entry) => entry.name === "scripts/api.js")); assert.ok(entries.some((entry) => entry.name === "styles/dragonroll.css")); for (const entry of entries) { assert.deepEqual(entry.data, fs.readFileSync(path.join(moduleDir, entry.name)), entry.name); } });