Files
RustProxy/tests/panel_refresh_contract.js
T
2026-11-08 21:26:30 +01:00

56 lines
3.1 KiB
JavaScript

const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const root = path.join(__dirname, "..");
const read = relative => fs.readFileSync(path.join(root, relative), "utf8");
const settings = read("src/admin/web/settings.js");
const certificateApi = read("src/admin/web/certificate-api.js");
const core = read("src/admin/web/app/core.js");
const cache = read("src/admin/web/cache-panel.js");
const transform = read("src/admin/web/transform-panel.js");
const traffic = read("src/admin/web/traffic-panel.js");
const refresh = read("src/admin/web/app/refresh.js");
const management = read("src/admin/web/management.js");
const loggingApi = read("src/admin/logging_api.rs");
const openapi = JSON.parse(read("src/admin/openapi.json"));
assert.match(settings, /authenticated && name === "certificate-api"[^\n]+rustProxyCertificateApi\?\.load/);
assert.match(settings, /authenticated && active === "certificate-api"[^\n]+rustProxyCertificateApi\?\.load/);
assert.ok(!/renderCertificateApi\(\);\s*loadCertificateApi\(\);\s*\}\)\(\);\s*$/.test(certificateApi),
"Certificate API must not fetch credentials when its module is initialized");
assert.match(core, /async function refresh\(force = false\)/);
assert.doesNotMatch(core, /includeLogs/);
assert.match(core, /new EventSource\(`\/api\/events\?interval=\$\{seconds\}&logs=false`\)/);
assert.match(loggingApi, /logs: Option<bool>/);
assert.match(loggingApi, /let include_logs = query\.logs\.unwrap_or\(true\)/);
assert.match(loggingApi, /if include_logs[\s\S]+logs_payload\(&state\)[\s\S]+else[\s\S]+live_status_payload/);
assert.match(core, /if \(payload\.logs\) renderLogs\(payload\.logs\)/);
assert.match(refresh, /active: \(\) => !!window\.rustProxyLogs\?\.active\?\.\(\)/);
assert.doesNotMatch(refresh, /manual\("logs-fallback"\).*manual\("system-info"\)/);
assert.match(management, /function active\(\) \{ return dialog\.open && !paused && !expanded; \}/);
assert.match(management, /syncLivePolling\(true\)/);
for (const [name, source, endpoint] of [
["cache", cache, "/api/cache"],
["transform", transform, "/api/transform"],
["traffic", traffic, "/api/traffic"],
]) {
assert.ok(/view=live/.test(source) || /set\(\"view\", \"live\"\)/.test(source), `${name} panel must use the lightweight polling view`);
assert.match(source, /data\.revision !== staticRevision/, `${name} panel must invalidate static data on config revision changes`);
assert.match(source, /reset\(\)[\s\S]{0,220}staticData = null[\s\S]{0,120}staticRevision = null/, `${name} panel reset must discard static cached API data`);
const operation = openapi.paths[endpoint].get;
const view = (operation.parameters || []).find(parameter => parameter.name === "view");
assert.deepEqual(view?.schema?.enum, ["live"], `${endpoint} must document view=live`);
}
const events = openapi.paths["/api/events"].get;
const logsFlag = (events.parameters || []).find(parameter => parameter.name === "logs");
assert.equal(logsFlag?.schema?.type, "boolean");
assert.equal(logsFlag?.schema?.default, true);
assert.equal(openapi.info.version, "0.36.7");
console.log("Panel refresh/API contract checks: OK");