#!/usr/bin/env node

const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");

const root = path.join(__dirname, "..");
const script = fs.readFileSync(path.join(root, "activate-staged-unit.py"), "utf8");
const unit = fs.readFileSync(path.join(root, "hlcc-jd-app-hub-activator.service"), "utf8");

assert.match(script, /systemctl", "show", "--property=MainPID"/);
assert.match(script, /process_root\.stat\(\)\.st_uid != os\.getuid\(\)/);
assert.match(script, /"\/jd-app-hub\/server\.js" not in command/);
assert.match(script, /os\.kill\(pid, signal\.SIGTERM\)/);
assert.doesNotMatch(script, /shell=True|systemctl", "(?:restart|stop|start)"/);
assert.match(unit, /^User=guanghu$/m);
assert.match(unit, /^NoNewPrivileges=true$/m);
assert.match(unit, /^ProtectSystem=strict$/m);
assert.match(unit, /^ReadOnlyPaths=__RELEASE_ROOT__$/m);

console.log("JD app hub staged-unit activator: PASS");
