"use strict"; // Behavioral tests for the dashboard-assistant suspension gate (utils/ // dashboard-assistant-tools.js). The assistant's file tools reach the node // daemon directly via deps.nodeFsPost, so resolveServerOrError is the only // place the panel-wide suspension freeze is enforced on that path — the port // of resolveBotToolServerOrError from index.js. The contract pinned here: // - every direct file tool (read_file / write_file / delete_path) on a // suspended server answers the masked "is suspended." error with // code "server-suspended" and NEVER touches deps.nodeFsPost; // - a healthy server flows through the gate unchanged (read forwarded); // - batch scopes (update_minecraft_server_property // all_accessible_minecraft) skip suspended servers with reason // "server_suspended" instead of queueing a browser action that would // bounce off the route-level 423; // - list_servers exposes the suspended flag so the model can explain why a // server is unavailable. // // Same direct-runner style with contract-compatible dep stubs as the // write-probe suite — no network, no service/router stack. const test = require("node:test"); const assert = require("node:assert"); const { createDashboardAssistantToolRunner } = require("../utils/dashboard-assistant-tools.js"); const USER_EMAIL = "user@test.local"; function makeDeps({ entries, perms } = {}) { const calls = []; return { calls, loadServersIndex: async () => entries || [], findUserByEmail: async () => ({ email: USER_EMAIL, admin: false }), getAccessListForEmail: async () => (entries || []).map((entry) => entry.name), getEffectivePermsForUserOnServer: async () => perms || { files_read: true, files_create: true, files_write: true, files_delete: true, }, resolveRemoteFsContext: async (serverName) => ({ remote: true, node: { id: "node-1", name: "node-1" }, baseDir: `/srv/${serverName}`, serverName, }), nodeFsPost: async (node, endpoint, payload) => { calls.push({ endpoint, payload }); if (endpoint === "/v1/fs/read") { return { status: 200, json: { ok: true, content: Buffer.from("hello", "utf8").toString("base64") } }; } if (endpoint === "/v1/fs/list") { return { status: 200, json: { ok: true, entries: [] } }; } return { status: 200, json: { ok: true } }; }, }; } const healthyEntry = { name: "alpha", displayName: "Alpha", nodeId: "node-1", suspended: false, mcVersion: "1.20.4" }; const suspendedEntry = { name: "beta", displayName: "Beta", nodeId: "node-1", suspended: true, mcVersion: "1.20.4" }; test("read_file on a suspended server returns the suspension error and never reaches the node", async () => { const deps = makeDeps({ entries: [suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "read_file", args: { server: "beta", path: "config.txt" }, userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /Beta is suspended\./); assert.equal(result.code, "server-suspended"); assert.equal(deps.calls.length, 0); }); test("write_file on a suspended server returns the suspension error and never reaches the node", async () => { const deps = makeDeps({ entries: [suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: { server: "beta", path: "config.txt", content: "x" }, userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /Beta is suspended\./); assert.equal(result.code, "server-suspended"); assert.equal(deps.calls.length, 0); }); test("delete_path on a suspended server returns the suspension error and never reaches the node", async () => { const deps = makeDeps({ entries: [suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "delete_path", args: { server: "beta", path: "config.txt" }, userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /Beta is suspended\./); assert.equal(result.code, "server-suspended"); assert.equal(deps.calls.length, 0); }); test("read_file on a healthy server flows through the gate unchanged", async () => { const deps = makeDeps({ entries: [healthyEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "read_file", args: { server: "alpha", path: "config.txt" }, userEmail: USER_EMAIL }); assert.equal(result.ok, true); assert.equal(result.content, "hello"); assert.equal(deps.calls.filter((call) => call.endpoint === "/v1/fs/read").length, 1); }); test("update_minecraft_server_property batch scope skips suspended servers with a clean reason", async () => { const deps = makeDeps({ entries: [healthyEntry, suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const clientActions = []; const result = await runTool({ name: "update_minecraft_server_property", args: { property: "motd", value: "hello", scope: "all_accessible_minecraft" }, userEmail: USER_EMAIL, clientActions, }); assert.equal(result.ok, true); assert.equal(result.count, 1); assert.deepEqual(result.targets.map((target) => target.server), ["alpha"]); const skippedBeta = result.skipped.find((item) => item.server === "beta"); assert.ok(skippedBeta, "suspended server must appear in skipped"); assert.equal(skippedBeta.reason, "server_suspended"); assert.match(skippedBeta.message, /Beta is suspended\./); const queued = clientActions[0]; assert.ok(queued, "browser action must be queued for the healthy server"); assert.deepEqual(queued.targets.map((target) => target.server), ["alpha"]); }); test("list_servers exposes the suspended flag for each entry", async () => { const deps = makeDeps({ entries: [healthyEntry, suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "list_servers", args: {}, userEmail: USER_EMAIL }); assert.equal(result.ok, true); const byName = Object.fromEntries(result.servers.map((server) => [server.name, server])); assert.equal(byName.alpha.suspended, false); assert.equal(byName.beta.suspended, true); }); test("update_minecraft_server_property with every candidate suspended says so in the headline", async () => { const deps = makeDeps({ entries: [suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const clientActions = []; const result = await runTool({ name: "update_minecraft_server_property", args: { property: "motd", value: "hello", scope: "all_accessible_minecraft" }, userEmail: USER_EMAIL, clientActions, }); assert.equal(result.ok, false); assert.match(result.error, /All matching Minecraft servers are suspended\./); assert.ok(result.skipped?.length >= 1); assert.ok(result.skipped.every((item) => item.reason === "server_suspended")); assert.equal(clientActions.length, 0, "no browser action may be queued when every target is suspended"); assert.equal(deps.calls.length, 0); }); test("update_minecraft_server_property single_server scope hits the resolver gate, never the permission headline", async () => { // The scope resolves through resolveServerOrError first, whose suspension // gate returns before collectFileTargets runs — so the empty-targets // permission arm below is unreachable for a suspended single server. const deps = makeDeps({ entries: [suspendedEntry] }); const runTool = createDashboardAssistantToolRunner(deps); const clientActions = []; const result = await runTool({ name: "update_minecraft_server_property", args: { property: "motd", value: "hello", scope: "single_server", server: "beta" }, userEmail: USER_EMAIL, clientActions, }); assert.equal(result.ok, false); assert.match(result.error, /Beta is suspended\./); assert.equal(result.code, "server-suspended"); assert.doesNotMatch(result.error, /permission/i); assert.equal(clientActions.length, 0); assert.equal(deps.calls.length, 0); });