"use strict"; // Behavioral tests for the write_file create-only backstop (utils/ // dashboard-assistant-tools.js): a user with files_create but NOT files_write // must never reach /v1/fs/write through an older daemon that ignores `mode` // unless the panel could verify the target name is free. The probe contract // pinned here: // - healthy listing with the name present -> rejected, write NOT forwarded; // - listing failure (status 0 / malformed / other statuses) -> FAIL CLOSED, // write NOT forwarded, retriable message; // - definitive 400 "not a directory" (missing parent; /v1/fs/write creates // parents) -> write forwarded with mode "create"; // - healthy listing, free name -> write forwarded with mode "create"; // - a user with neither files_create nor files_write is denied outright; // - 409 on create and 404 existed:false on update map to the distinct // create/overwrite permission messages. // // The runner is exercised directly with contract-compatible dep stubs (no // network, no service/router stack) — the same behavioral style as the // delete-flow suite, minus HTTP. const test = require("node:test"); const assert = require("node:assert"); const { createDashboardAssistantToolRunner } = require("../utils/dashboard-assistant-tools.js"); const USER_EMAIL = "user@test.local"; const SERVER_NAME = "alpha"; function makeDeps({ perms, listResult, writeResult, readResult } = {}) { const calls = []; return { calls, loadServersIndex: async () => [{ name: SERVER_NAME, displayName: "Alpha" }], findUserByEmail: async () => ({ email: USER_EMAIL, admin: false }), getAccessListForEmail: async () => [SERVER_NAME], getEffectivePermsForUserOnServer: async () => perms || {}, resolveRemoteFsContext: async () => ({ remote: true, node: { id: "node-1", name: "node-1" }, baseDir: `/srv/${SERVER_NAME}`, serverName: SERVER_NAME, }), nodeFsPost: async (node, endpoint, payload) => { calls.push({ endpoint, payload }); if (endpoint === "/v1/fs/list") return listResult || { status: 0, json: null }; if (endpoint === "/v1/fs/write") return writeResult || { status: 0, json: null }; if (endpoint === "/v1/fs/read") return readResult || { status: 0, json: null }; return { status: 0, json: null }; }, }; } function writeArgs(overrides = {}) { return { server: SERVER_NAME, path: "config.txt", content: "hello", ...overrides }; } test("create-only user: name taken in healthy listing is rejected and the write is not forwarded", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 200, json: { ok: true, entries: [{ name: "config.txt", type: "file" }] } }, }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /already exists/); assert.match(result.error, /files_write/); assert.equal(deps.calls.filter((call) => call.endpoint === "/v1/fs/write").length, 0); }); test("create-only user: failed listing fails closed with a retriable message and no write", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 0, json: null }, // node unreachable / timeout shape }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /Could not verify that this file is new/); assert.equal(deps.calls.filter((call) => call.endpoint === "/v1/fs/write").length, 0); }); test("create-only user: malformed listing body fails closed too", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 200, json: { ok: true } }, // ok without entries }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /Could not verify that this file is new/); assert.equal(deps.calls.filter((call) => call.endpoint === "/v1/fs/write").length, 0); }); test("create-only user: definitive 400 not-a-directory (missing parent) forwards the write with mode create", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 400, json: { ok: false, error: "not a directory" } }, writeResult: { status: 200, json: { ok: true } }, readResult: { status: 200, json: { ok: true, content: "hello" } }, }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, true); assert.equal(result.verified, true); const writes = deps.calls.filter((call) => call.endpoint === "/v1/fs/write"); assert.equal(writes.length, 1); assert.equal(writes[0].payload.mode, "create"); }); test("create-only user: free listing forwards the write with mode create", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 200, json: { ok: true, entries: [{ name: "other.txt", type: "file" }] } }, writeResult: { status: 200, json: { ok: true } }, readResult: { status: 200, json: { ok: true, content: "hello" } }, }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, true); const writes = deps.calls.filter((call) => call.endpoint === "/v1/fs/write"); assert.equal(writes.length, 1); assert.equal(writes[0].payload.mode, "create"); assert.equal(writes[0].payload.server, SERVER_NAME); }); test("user with neither files_create nor files_write is denied before any node call", async () => { const deps = makeDeps({ perms: { files_read: true } }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /permission to write files/); assert.equal(deps.calls.length, 0); }); test("overwrite-only user hitting a missing file gets the files_create message (404 existed:false)", async () => { const deps = makeDeps({ perms: { files_create: false, files_write: true }, writeResult: { status: 404, json: { ok: false, error: "file not found", existed: false } }, }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /does not exist/); assert.match(result.error, /files_create/); }); test("create-only user racing past the probe gets the files_write message (409 on create)", async () => { const deps = makeDeps({ perms: { files_create: true, files_write: false }, listResult: { status: 200, json: { ok: true, entries: [] } }, writeResult: { status: 409, json: { ok: false, error: "file already exists", existed: true } }, }); const runTool = createDashboardAssistantToolRunner(deps); const result = await runTool({ name: "write_file", args: writeArgs(), userEmail: USER_EMAIL }); assert.equal(result.ok, false); assert.match(result.error, /already exists/); assert.match(result.error, /files_write/); });