/** * @file S3 fails closed: a server that started with S3 keeps its * server.conf when the file later fails to read (it never silently becomes * "no S3", which would run a member's pane as the host account), and a tmux * call whose account cannot be told under S3 fails instead of going to the * host's own tmux server. * @author Nguyễn Ngọc Trí Vĩ */ const { describe, it, after } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const os = require("node:os"); const path = require("node:path"); const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "s3-fail-closed-")); const CONF = path.join(TMP, "server.conf"); process.env.AGENTHUB_SERVER_CONF = CONF; process.env.DASHBOARD_DB_PATH = path.join(TMP, "t.db"); const linux = require("../lib/linux-accounts"); const tmux = require("../lib/tmux"); const writeConf = () => fs.writeFileSync(CONF, `HOST_USER=h\nHOST_UID=1000\nUSERS_ROOT=${path.join(TMP, "users")}\n`); const later = (ms) => { const real = Date.now; const t = real() + ms; Date.now = () => t; return () => (Date.now = real); }; after(() => { delete process.env.AGENTHUB_MODE; linux.__resetConfCache(); tmux.__reset(); }); describe("S3 fails closed", () => { it("keeps the last good server.conf in server mode when the file stops reading", () => { process.env.AGENTHUB_MODE = "server"; linux.__resetConfCache(); writeConf(); assert.equal(linux.s3Enabled(), true); fs.rmSync(CONF); const restore = later(10_000); // past the 5 s cache try { assert.equal(linux.s3Enabled(), true, "still S3: never fall back to the host"); assert.equal(linux.serverConf().usersRoot, path.join(TMP, "users")); } finally { restore(); } }); it("outside server mode a missing file is simply no S3", () => { delete process.env.AGENTHUB_MODE; linux.__resetConfCache(); writeConf(); linux.serverConf(); fs.rmSync(CONF); const restore = later(10_000); try { assert.equal(linux.serverConf(), null); } finally { restore(); } }); it("a tmux call whose account cannot be told under S3 fails, never reaching the host's tmux", () => { process.env.AGENTHUB_MODE = "server"; linux.__resetConfCache(); writeConf(); const calls = []; tmux.__setExecImpl((args) => { calls.push(args); return ""; }); tmux.__setAccountImpl(() => { throw Object.assign(new Error("database is locked"), { code: "SQLITE_BUSY" }); }); assert.throws(() => tmux.sendText("agenthub-lane-7", "ls"), { code: "SQLITE_BUSY" }); assert.deepEqual(calls, [], "nothing ran on the host's tmux server"); }); });