"use strict"; // Regression tests for the node-relay timeout parity fix. // // nodes.js relays file operations to the node daemon through httpJson(), whose // default timeoutMs is 5000. The Go daemon deliberately ignores the client's // disconnect (its handlers are not tied to r.Context() — see the comment at // main.go's self-update handler), so a 5s relay timeout aborts the relay while // the daemon still completes the operation: the user saw node_read_failed / // node_write_failed / node_delete_failed / node_extract_failed and a retry // RE-EXECUTED the operation (double extract, double delete). index.js's // equivalents use 60-630s, so every relay must pass an explicit timeoutMs at // parity: // read → 120000 write → 120000 delete → 120000 extract → 630000 // list → 60000 rename → 120000 // // extract/archive run 30s PAST the daemon's own caller-wait budgets // (extractCallerWaitBudget 10m / archiveCallerWaitBudget 5m) — the same slack // doctrine the backup relays keep: a copy completing just past the daemon's // boundary still receives its success response instead of being reported as a // panel-side failure. // // adpanel:relay-helper-consolidation (Wave 2 Step B1): index.js's GET // /files/list, /files/read and /files/stream dials moved off nodeFsPost onto // the shared services/node-fs-relay.js relayFsJson core — SAME parity values, // passed at the index.js call sites (the regexes below match the endpoint // literal followed by the timeout within the call window, so they pin the // shared-core calls exactly as they pinned the nodeFsPost calls). // (Wave 2 Step B2) the eight MUTATING fs routes (write/mkdir/delete/rename/ // extract/archive + legacy create/rename) moved the same way — their // route-window pins are added below. index.js's remaining nodeFsPost callers // (socket handlers, deferred import, scheduler) are unchanged and still // pinned by the global PARITY loop. // // (Wave 2 Step B3) nodes.js's nine server-scoped fs relays (entries, file // GET, file POST, delete, delete-batch, rename + its destination probe, // extract, archive, create) moved off httpJson onto the SAME relayFsJson // core, dialed through the NODES_FS_RELAY_DEPS binding. The PARITY loop's // nodes.js side was therefore re-pointed from the former // `nodeUrl(ctx.node, "")` httpJson anchors onto per-route windows // (same technique as index.js's B2 table): every window must dial through // relayFsJson and keep the parity timeout, so a drifted value on a migrated // relay cannot hide behind nodes.js's remaining httpJson callers (the // replacement-bytes probe, base-dir resolution, upload-url, backups // performers — all still pinned by the "every httpJson() call" test). // // index.js cannot be required in-process (it boots servers on require), so — // same technique as server-suspension-rest-bridge.test.js — these are // source-level assertions. They pin that (a) EVERY httpJson() call in nodes.js // passes an explicit timeoutMs (a new relay that inherits the 5s default fails // here), and (b) the operation relays keep the exact index.js parity // values, on both sides, so future drift on either side trips a test. const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const ROOT = path.join(__dirname, ".."); const NODES_JS = path.join(ROOT, "nodes.js"); const INDEX_JS = path.join(ROOT, "index.js"); // Strip full-line comments so prose mentioning "timeoutMs" cannot satisfy the // assertions and prose mentioning "httpJson(" cannot create false call sites. function stripFullLineComments(src) { return src .split("\n") .filter((line) => !/^\s*\/\//.test(line)) .join("\n"); } const nodesSrc = stripFullLineComments(fs.readFileSync(NODES_JS, "utf8")); const indexSrc = stripFullLineComments(fs.readFileSync(INDEX_JS, "utf8")); test("both generated proxy vhosts wait longer than the backup create and restore relays", () => { const installer = fs.readFileSync(path.join(ROOT, "initialize.sh"), "utf8"); const location = installer.match(/backup_proxy_location=\$\(cat < relayTimeout, `${relay} must finish before Nginx gives up`); } }); // How far past "httpJson(" the options object may extend. The widest relay // options object (upload-url) spans a handful of short lines; 900 characters // covers every real call with margin. const CALL_WINDOW = 900; function httpJsonCallSites(src) { const sites = []; const re = /httpJson\(/g; let m; while ((m = re.exec(src)) !== null) { // Skip the function definition itself (function httpJson(fullUrl, ...)). if (/function\s+$/.test(src.slice(Math.max(0, m.index - 12), m.index))) continue; sites.push(m.index); } return sites; } test("every httpJson() relay call in nodes.js passes an explicit timeoutMs", () => { const sites = httpJsonCallSites(nodesSrc); assert.ok(sites.length > 5, `expected the real relay call sites in nodes.js, found ${sites.length}`); for (const at of sites) { const window = nodesSrc.slice(at, at + CALL_WINDOW); assert.match( window, /timeoutMs/, `httpJson() call at byte offset ${at} of nodes.js inherits the 5s default — ` + "every node relay must pass an explicit timeoutMs (the daemon completes the " + "operation regardless of the relay's abort, so a short timeout makes the " + "user retry an already-executed operation)" ); } }); // Parity table: endpoint → [nodes.js expected timeoutMs, index.js expected // timeoutMs]. index.js writes some literals as 600_000 / 60_000, so the // matcher tolerates underscores. The list endpoint is pinned separately below // (it must not ride this loop: nodes.js's FIRST "/v1/fs/list" call is the 5s // replacement-bytes probe in fetchRemoteReplacementBytes — mirroring index.js's // fetchNodeReplacementBytes — so the loop's indexOf anchor would resolve to the // wrong relay). // // Wave 2 Step B3 re-point: nodes.js's relays dial through the shared // relayFsJson core instead of httpJson, so the nodes.js anchor is now the // ROUTE REGISTRATION (same technique as index.js's B2 table) — the window from // each anchor to the next app. registration must dial relayFsJson and // carry the parity timeout at the call site. const PARITY = [ ["/v1/fs/read", 120000, 'app.get("/api/nodes/server/:name/file"'], ["/v1/fs/write", 120000, 'app.post("/api/nodes/server/:name/file"'], ["/v1/fs/delete", 120000, 'app.post("/api/nodes/server/:name/delete"'], ["/v1/fs/extract", 630000, 'app.post("/api/nodes/server/:name/extract"'], ["/v1/fs/rename", 120000, 'app.post("/api/nodes/server/:name/rename"'], ]; // Route window in nodes.js: from the registration literal to the next // app. registration (mirrors indexRouteWindow below). function nodesRouteWindow(regPrefix, label) { const start = nodesSrc.indexOf(regPrefix); assert.notStrictEqual(start, -1, `${label}: route anchor not found: ${regPrefix}`); const re = /^app\.(?:get|post|put|delete|patch|all|use)\s*\(/gm; re.lastIndex = start + 1; let end = nodesSrc.length; let m; while ((m = re.exec(nodesSrc)) !== null) { end = m.index; break; } return nodesSrc.slice(start, end); } function relayTimeoutPattern(ms) { // relayFsJson passes the timeout as a trailing positional argument (no // `timeoutMs:` label) — match the underscore-tolerant literal. return new RegExp(`[,\\s]${String(ms).replace(/000$/, "_?000")}\\s*[,)]`); } for (const [endpoint, ms, routeAnchor] of PARITY) { test(`nodes.js ${endpoint} relay passes timeoutMs ${ms} (index.js parity)`, () => { const w = nodesRouteWindow(routeAnchor, endpoint); assert.match(w, /relayFsJson\(/, `the ${endpoint} relay must dial through the shared services/node-fs-relay.js core`); const endpointRe = endpoint.replace(/\//g, "\\/"); assert.match( w, new RegExp(`"${endpointRe}"[\\s\\S]{0,400}?${relayTimeoutPattern(ms).source}`), `the ${endpoint} relay must pass timeoutMs ${ms} — parity with index.js's fs-relay dial (relayFsJson/nodeFsPost) for the same daemon endpoint` ); }); test(`index.js keeps the ${ms} parity value for ${endpoint}`, () => { // index.js dial calls look like nodeFsPost(ctx.node, "/v1/fs/read", // {...}, 120000) for the routes still on nodeFsPost, and // relayFsJson(FS_RELAY_DEPS, ctx.node, "/v1/fs/list", {...}, 60000) for // the list/read/stream routes that moved onto the shared services/ // node-fs-relay.js core — the timeout is the trailing argument in both, // so the same endpoint-literal window pins both shapes. const pattern = new RegExp( `"${endpoint.replace(/\//g, "\\/")}"[\\s\\S]{0,400}?,\\s*${String(ms).replace(/000$/, "_?000")}\\s*[,)]` ); assert.match( indexSrc, pattern, `index.js's fs-relay dial timeout for ${endpoint} drifted from ${ms} — nodes.js relays were aligned to this value; update both sides together` ); }); } test("httpJson's 5s default remains the fallback for non-relay callers", () => { assert.match( nodesSrc, /function httpJson\(fullUrl, \{ method = "GET", headers = \{\}, body = null, timeoutMs = 5000 \}/, "the 5s default should stay as the httpJson signature default; relays opt out explicitly" ); }); // The list relays: 60000 on BOTH sides. Wave 2 Step B1 moved the index.js // /files/list dial onto services/node-fs-relay.js's relayFsJson core — the // timeout is passed at the index.js call site, so the invariant is pinned // there (the shared core carries no timeout constants of its own). // The nodes.js /entries relay uses the same core; pin the timeout to its // route so another list caller cannot accidentally satisfy the assertion. test("the list relay keeps the 60000 parity on both sides", () => { const w = nodesRouteWindow('app.get("/api/nodes/server/:name/entries"', "entries list"); assert.match(w, /relayFsJson\(/, "the /entries relay must dial through the shared services/node-fs-relay.js core"); assert.match( w, new RegExp(`"\\/v1\\/fs\\/list"[\\s\\S]{0,400}?${relayTimeoutPattern(60000).source}`), "the /entries list relay must pass timeoutMs 60000" ); const routeAt = indexSrc.indexOf('app.get("/api/servers/:name/files/list"'); assert.notStrictEqual(routeAt, -1, "expected the /files/list route in index.js"); const dialAt = indexSrc.indexOf('"/v1/fs/list"', routeAt); assert.notStrictEqual(dialAt, -1, "expected the /v1/fs/list dial in the /files/list route"); assert.match( indexSrc.slice(dialAt, dialAt + CALL_WINDOW), /,\s*60_?000\s*[,)]/, "index.js's /files/list relay must keep the 60000 parity through the shared relayFsJson dial" ); }); // ── Wave 2 Step B2: the eight MUTATING fs routes moved off nodeFsPost onto // the shared services/node-fs-relay.js relayFsJson core — same parity values, // passed at the index.js call sites (the shared core carries no timeout // constants of its own). The PARITY loop above still passes through index.js's // remaining nodeFsPost callers (socket handlers, deferred import, scheduler), // so these ROUTE-WINDOW pins exist so a drifted timeout on a migrated route // cannot hide behind those callers. Every window also asserts the route dials // through relayFsJson and keeps no parallel nodeFsPost dial of its endpoint. function indexRouteWindow(regPrefix) { const start = indexSrc.indexOf(regPrefix); assert.notStrictEqual(start, -1, `route anchor not found: ${regPrefix}`); const re = /^app\.(?:get|post|put|delete|patch|all|use)\s*\(/gm; re.lastIndex = start + 1; let end = indexSrc.length; let m; while ((m = re.exec(indexSrc)) !== null) { end = m.index; break; } return indexSrc.slice(start, end); } // [label, route anchor, endpoint literal regex source, parity timeoutMs]. // /archive dials the per-server template literal (the URL segment is the // canonical panel name, spec D23), so its pin matches the path fragment. // The read/stream rows (Wave 2 review) close the gap where the global // "/v1/fs/read" pin was satisfiable by the socket handler / AI-agent reader // even if the routes dropped their timeout — route windows pin the routes. const B2_ROUTE_PARITY = [ ["files write", 'app.put("/api/servers/:name/files/write"', '"\\/v1\\/fs\\/write"', 120000], ["files mkdir", 'app.post("/api/servers/:name/files/mkdir"', '"\\/v1\\/fs\\/write"', 120000], ["files delete", 'app.delete("/api/servers/:name/files/delete"', '"\\/v1\\/fs\\/delete"', 120000], ["files rename", 'app.post("/api/servers/:name/files/rename"', '"\\/v1\\/fs\\/rename"', 120000], ["files read", 'app.get("/api/servers/:name/files/read"', '"\\/v1\\/fs\\/read"', 120000], ["files stream", 'app.get("/api/servers/:name/files/stream"', '"\\/v1\\/fs\\/read"', 120000], ["/extract", 'app.post("/extract"', '"\\/v1\\/fs\\/extract"', 630000], ["/archive", 'app.post("/archive"', "\\/v1\\/servers\\/\\$\\{botSeg\\}\\/files\\/archive", 330000], ["legacy /create", 'app.post("/create"', '"\\/v1\\/fs\\/write"', 120000], ["legacy /rename", 'app.post("/rename"', '"\\/v1\\/fs\\/rename"', 120000], ]; for (const [label, reg, endpointRe, ms] of B2_ROUTE_PARITY) { test(`index.js ${label} keeps the ${ms} parity through the shared relayFsJson dial`, () => { const w = indexRouteWindow(reg); assert.match(w, /relayFsJson\(/, `${label} must dial through the shared services/node-fs-relay.js core`); assert.match( w, new RegExp(`${endpointRe}[\\s\\S]{0,400}?,\\s*${String(ms).replace(/000$/, "_?000")}\\s*[,)]`), `${label}'s fs-relay dial timeout drifted from ${ms} — nodes.js relays were aligned to this value; update both sides together` ); }); } // The rename destination probes ride the same shared core at the list relay's // 60000 parity. (These pins are NEW with B2: the probe windows were never // pinned before — the "list relay" test above pins only the /files/list // route's dial — so preserving them here closes that gap consciously.) for (const [label, reg] of [ ["files rename", 'app.post("/api/servers/:name/files/rename"'], ["legacy /rename", 'app.post("/rename"'], ]) { test(`index.js ${label}'s destination probe keeps the 60000 list parity`, () => { const w = indexRouteWindow(reg); const at = w.indexOf('"/v1/fs/list"'); assert.notStrictEqual(at, -1, `${label}: expected the /v1/fs/list destination probe`); assert.match( w.slice(at, at + CALL_WINDOW), /,\s*60_?000\s*[,)]/, `${label}'s destination probe must keep timeoutMs 60000 (parity with the /files/list relay)` ); }); } // ── Wave 2 Step B3: nodes.js's remaining fs relays (no PARITY row above) ──── // The delete-batch, archive and create relays plus the rename destination // probe moved onto the shared relayFsJson core in the same commit as the // PARITY rows; their windows are pinned here so a drifted timeout on any of // them cannot hide behind nodes.js's remaining httpJson callers. [label, // route anchor, endpoint literal regex source, parity timeoutMs]. /archive // dials the per-server template literal (the URL segment is the canonical // name, spec D23), so its pin matches the path fragment. const B3_ROUTE_PARITY = [ ["nodes delete-batch", 'app.post("/api/nodes/server/:name/delete-batch"', '"\\/v1\\/fs\\/delete-batch"', 120000], ["nodes /archive", 'app.post("/api/nodes/server/:name/archive"', "\\/v1\\/servers\\/\\$\\{encodeURIComponent\\(canonicalArchiveName\\)\\}\\/files\\/archive", 330000], ["nodes /create", 'app.post("/api/nodes/server/:name/create"', '"\\/v1\\/fs\\/write"', 120000], ]; for (const [label, reg, endpointRe, ms] of B3_ROUTE_PARITY) { test(`${label} keeps the ${ms} parity through the shared relayFsJson dial`, () => { const w = nodesRouteWindow(reg, label); assert.match(w, /relayFsJson\(/, `${label} must dial through the shared services/node-fs-relay.js core`); assert.match( w, new RegExp(`${endpointRe}[\\s\\S]{0,400}?,\\s*${String(ms).replace(/000$/, "_?000")}\\s*[,)]`), `${label}'s fs-relay dial timeout drifted from ${ms} — nodes.js relays were aligned to this value; update both sides together` ); }); } test("nodes.js rename's destination probe keeps the 60000 list parity", () => { const w = nodesRouteWindow('app.post("/api/nodes/server/:name/rename"', "nodes rename probe"); const at = w.indexOf('"/v1/fs/list"'); assert.notStrictEqual(at, -1, "nodes rename: expected the /v1/fs/list destination probe"); assert.match( w.slice(at, at + CALL_WINDOW), /,\s*60_?000\s*[,)]/, "the rename destination probe must keep timeoutMs 60000 (parity with the /entries list relay)" ); });