const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const editor = fs.readFileSync(path.join(__dirname, "../src/admin/web/vhosts/editor.js"), "utf8"); const begin = editor.indexOf("const authMethodsByProvider = {"); const end = editor.indexOf("function load(value = defaults()", begin); assert.ok(begin >= 0 && end > begin); const values = new Map(); const context = vm.createContext({ str: key => values.get(key) || "", yes: () => false, num: () => 5, nullable: () => null, lines: () => [] }); const selection = vm.runInContext(editor.slice(begin, end) + "\n({ authMethodsByProvider, inferredAuthentication, selectedAuthentication, readForwardProvider })", context); const legacy = [ [{ basic_auth: { enabled: true } }, "basic", "basic"], [{ tinyauth: { enabled: true } }, "tinyauth", "forward_auth"], [{ authelia: { enabled: true } }, "authelia", "forward_auth"], [{ authentik: { enabled: true } }, "authentik", "forward_auth"], [{ oidc: { enabled: true } }, "custom", "oidc"], [{}, "none", "none"] ]; for (const [vhost, provider, method] of legacy) { const actual = selection.inferredAuthentication(vhost); assert.equal(actual.provider, provider); assert.equal(actual.method, method); } for (const provider of ["tinyauth", "authelia", "authentik", "pocket_id", "custom"]) { const selected = { provider, method: "oidc" }; const actual = selection.inferredAuthentication({ authentication: selected, oidc: { enabled: true } }); assert.equal(actual.provider, provider); assert.equal(actual.method, "oidc"); values.set("auth-provider", provider); values.set("auth-method", "oidc"); assert.equal(selection.selectedAuthentication().method, "oidc"); for (const forward of ["authelia", "authentik"]) assert.equal(selection.readForwardProvider(forward).enabled, false); } for (const provider of ["authelia", "authentik"]) { values.set("auth-provider", provider); values.set("auth-method", "forward_auth"); assert.equal(selection.readForwardProvider(provider).enabled, true); } values.set("auth-provider", "none"); values.set("auth-method", ""); assert.equal(selection.selectedAuthentication().method, "none"); assert.deepEqual(Array.from(selection.authMethodsByProvider.pocket_id), ["oidc"]); assert.deepEqual(Array.from(selection.authMethodsByProvider.basic), ["basic"]); assert.deepEqual(Array.from(selection.authMethodsByProvider.authelia), ["forward_auth", "oidc"]); console.log("Vhost provider/method UI selection checks: OK");