Files
NextFloorSymcon/tests/validate-package.mjs
T

76 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Checks the installable library layout and module metadata used by Symcon's Module Control.
// Module schema: https://www.symcon.de/assets/files/validation/moduleSchema.json
// Library layout: https://www.symcon.de/de/service/dokumentation/entwicklerbereich/sdk-tools/sdk-php/struktur/
import assert from "node:assert/strict";
import { readFileSync, readdirSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const guid = /^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$/;
const name = /^[A-Za-z0-9](?:[A-Za-z0-9 _]*[A-Za-z0-9])?$/;
const moduleFields = ["id", "name", "type", "vendor", "aliases", "url", "parentRequirements", "childRequirements", "implemented", "prefix"];
const ignoredDirectories = new Set(["libs", "docs", "imgs", "tests", "actions"]);
function readJson(file) {
return JSON.parse(readFileSync(file, "utf8"));
}
/** Validate the complete public module manifest, including fields that JSON/PHP lint cannot check. */
export function validateModule(module) {
for (const field of moduleFields) assert.ok(Object.hasOwn(module, field), `module.json: missing required field '${field}'`);
for (const field of Object.keys(module)) assert.ok(field === "$schema" || moduleFields.includes(field), `module.json: unknown field '${field}'`);
assert.match(module.id, guid, "module.json: invalid module GUID");
assert.match(module.name, name, "module.json: invalid module name");
// HTML-SDK tiles belong to normal device modules (3). Their visualization is enabled in PHP;
// declaring a visualization module (6) prevents it from being offered as a normal instance.
assert.ok([0, 1, 2, 3, 4, 5].includes(module.type), "module.json: type must be a supported PHP module type (0–5); use type 3 for an HTML-SDK device");
assert.equal(typeof module.vendor, "string", "module.json: vendor must be a string");
assert.match(module.url, /^https?:\/\/.+/, "module.json: url must be an HTTP(S) documentation URL");
assert.match(module.prefix, /^[A-Z0-9]+$/, "module.json: invalid function prefix");
for (const field of ["aliases", "parentRequirements", "childRequirements", "implemented"]) {
assert.ok(Array.isArray(module[field]), `module.json: ${field} must be an array`);
assert.equal(new Set(module[field]).size, module[field].length, `module.json: duplicate ${field}`);
for (const item of module[field]) {
assert.equal(typeof item, "string", `module.json: ${field} entries must be strings`);
if (field === "aliases") assert.ok(item.length > 0, "module.json: aliases must not be empty");
else assert.match(item, guid, `module.json: invalid GUID in ${field}`);
}
}
}
/** The argument is a library root, never the source repository containing its symcon subdirectory. */
export function validateSymconPackage(root = fileURLToPath(new URL("../", import.meta.url))) {
const library = readJson(join(root, "library.json"));
assert.match(library.id, guid, "library.json: invalid library GUID");
assert.match(library.name, name, "library.json: invalid library name");
assert.equal(library.compatibility.version, "9.0", "library.json: this port targets Symcon 9.0");
const ids = new Set([library.id]);
const modules = [];
for (const entry of readdirSync(root, { withFileTypes: true })) {
if (!entry.isDirectory() || entry.name.startsWith(".") || ignoredDirectories.has(entry.name)) continue;
const directory = join(root, entry.name);
const module = readJson(join(directory, "module.json"));
validateModule(module);
assert.ok(!ids.has(module.id), `Duplicate library/module GUID: ${module.id}`);
ids.add(module.id);
assert.equal(entry.name, module.name.replaceAll(" ", ""), "Module directory must match its PHP class name");
const php = readFileSync(join(directory, "module.php"), "utf8");
assert.match(php, new RegExp(`class\\s+${entry.name}\\s+extends\\s+IPSModuleStrict\\b`), "Module PHP class is missing or mismatched");
const form = readJson(join(directory, "form.json"));
assert.ok(Array.isArray(form.elements) && Array.isArray(form.actions), "Module form must contain elements and actions");
for (const asset of ["nextfloor-designer.js", "nextfloor-3d.js", "nextfloor-editor.js", "nextfloor-card-editor.js", "nextfloor-trend.js"]) {
assert.ok(readFileSync(join(directory, "assets", asset)).length > 0, `Missing build asset: ${asset}`);
}
modules.push(module);
}
assert.ok(modules.length > 0, "No modules found beside library.json; install the contents of symcon, not the full source repository");
return { library, modules };
}
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
const root = process.argv[2] ? resolve(process.argv[2]) : undefined;
const { library, modules } = validateSymconPackage(root);
console.log(`OK ${library.name} ${library.version}: ${modules.map((module) => module.name).join(", ")} (${basename(root ?? fileURLToPath(new URL("../", import.meta.url)))})`);
}