Files
FreeCADWasmDev/tools/plugin-package-smoke.js
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

60 lines
2.3 KiB
JavaScript

// SPDX-License-Identifier: LGPL-2.1-or-later
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const sampleDirectory = path.resolve(process.argv[2] || "FreeCAD/docs/plugin-sample");
const expectedApiVersion = "1";
function digest(buffer)
{
return crypto.createHash("sha256").update(buffer).digest("hex");
}
function validatePackage(directory, manifestOverride)
{
const manifest = manifestOverride || JSON.parse(fs.readFileSync(path.join(directory, "manifest.json"), "utf8"));
if (manifest.schema !== "freecad-web-plugin/v1" || manifest.apiVersion !== expectedApiVersion
|| !/^[a-z0-9-]+$/.test(manifest.id) || manifest.entry !== "plugin.js") {
throw new Error("PLUGIN_MANIFEST_INVALID");
}
const entryPath = path.join(directory, manifest.entry);
const source = fs.readFileSync(entryPath);
if (digest(source) !== manifest.sha256) {
throw new Error("PLUGIN_HASH_MISMATCH");
}
const context = vm.createContext({module: {exports: {}}, exports: {}, require: undefined, process: undefined, globalThis: {}});
new vm.Script(source.toString("utf8"), {filename: entryPath}).runInContext(context, {timeout: 100});
const exported = context.module.exports;
if (!exported || typeof exported.register !== "function") {
throw new Error("PLUGIN_ENTRY_INVALID");
}
const result = exported.register({apiVersion: expectedApiVersion});
return {manifest, result};
}
const valid = validatePackage(sampleDirectory);
let wrongHash = "none";
try {
validatePackage(sampleDirectory, {...valid.manifest, sha256: "0".repeat(64)});
}
catch (error) {
wrongHash = error.message;
}
let wrongVersion = "none";
try {
validatePackage(sampleDirectory, {...valid.manifest, apiVersion: "999"});
}
catch (error) {
wrongVersion = error.message;
}
const result = {valid: {id: valid.result.id, commands: valid.result.commands, apiVersion: valid.result.apiVersion}, wrongHash, wrongVersion};
if (result.valid.id !== "freecad-web-part-sample" || result.valid.commands.join(",") !== "box"
|| result.valid.apiVersion !== "1" || result.wrongHash !== "PLUGIN_HASH_MISMATCH"
|| result.wrongVersion !== "PLUGIN_MANIFEST_INVALID") {
throw new Error(JSON.stringify(result));
}
console.log(JSON.stringify(result));