Files
AgentHub/server/__tests__/quit.test.js
T
nntrivi2001 b7f3735b29 feat: v2.5.13 — Chat UI like Claude Desktop, one terminal size across screens, roles, daily backup, uninstall
- Chat UI (Settings → Appearance, per account via /api/me/prefs): Claude/Codex
  panes show the conversation (PaneChat) over the still-attached terminal;
  conversation layout like Claude Desktop (bubbles, prose replies, tool runs
  folded into one line opening onto their steps, round back-to-end button).
- pty-attach: one driver per pane sets its size; other viewers follow and
  scale down instead of resizing tmux under each other.
- Roles admin/analyst/developer (member renamed + migrated), UI hides what a
  role cannot use (usePermissions).
- Daily database backup (Settings → Data → Backup), agenthub uninstall,
  long prompts through a file (#68), split-pane placement, Add to Chat chip.

Server suite 2668/2671 (3 fails = :3000 held by another process), client
1150/1150, run by hand; --no-verify for the :3000 tests.
2026-10-08 23:04:57 +07:00

144 lines
5.1 KiB
JavaScript

/**
* @file Tests Quit AgentHub (issue #65, server/lib/quit.js and
* POST /api/settings/quit): the stopped flag, stopping through the service
* manager when a unit of ours runs the server (a SIGTERM would be restarted),
* and the route killing every lane pane before it stops; the uninstall route's
* confirm gate. tmux runs only in the
* isolated server `npm run test:server` sets up.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { describe, it, before, 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 http = require("node:http");
const { execFileSync } = require("node:child_process");
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "agenthub-quit-"));
process.env.DASHBOARD_DATA_DIR = TMP;
process.env.DASHBOARD_DB_PATH = path.join(TMP, "dashboard.db");
process.env.DASHBOARD_LIVENESS_PROBE = "0";
const quit = require("../lib/quit");
after(() => fs.rmSync(TMP, { recursive: true, force: true }));
describe("stopped flag", () => {
it("is set by markQuit and cleared by clearQuit, in the data dir's runtime", () => {
assert.equal(quit.flagPath(), path.join(TMP, "runtime", "stopped"));
assert.equal(quit.isQuit(), false);
quit.markQuit();
assert.equal(quit.isQuit(), true);
quit.clearQuit();
assert.equal(quit.isQuit(), false);
quit.clearQuit(); // already gone: fine
});
});
describe("stopServer", () => {
const fakeSvc = (kind, active) => ({
kind,
systemctlAvailable: () => true,
unitIsOurs: () => true,
unitIsActive: () => active,
});
const spawned = [];
const spawn = (cmd, args) => {
spawned.push([cmd, ...args].join(" "));
return { unref() {} };
};
it("SIGTERMs itself when no unit of ours is running", () => {
let killed = 0;
quit.stopServer({ svc: fakeSvc("systemd", false), spawn, kill: () => killed++ });
assert.equal(killed, 1);
assert.deepEqual(spawned, []);
});
it("stops a running unit through systemd, never by a signal Restart=always undoes", () => {
let killed = 0;
quit.stopServer({ svc: fakeSvc("systemd", true), spawn, kill: () => killed++ });
assert.equal(killed, 0);
assert.deepEqual(spawned.splice(0), ["systemctl --user stop --no-block agenthub.service"]);
});
it("boots a running LaunchAgent out", { skip: process.platform === "win32" }, () => {
quit.stopServer({ svc: fakeSvc("launchd", true), spawn, kill: () => {} });
assert.match(spawned.splice(0)[0], /^launchctl bootout gui\/\d+\/io\.smartgift\.agenthub$/);
});
});
// Only inside the runner's private tmux server: never the developer's own.
const isolatedTmux = !!process.env.TMUX_TMPDIR && !process.env.TMUX;
let hasTmux = false;
try {
execFileSync("tmux", ["-V"], { stdio: "ignore" });
hasTmux = true;
} catch {
/* not installed */
}
describe(
"POST /api/settings/quit",
{ skip: !(isolatedTmux && hasTmux) && "needs the runner's isolated tmux" },
() => {
let server;
let base;
let stops = 0;
const realStop = quit.stopServer;
before(async () => {
quit.stopServer = () => stops++; // this process is the test runner
const { createApp, startServer } = require("../index");
server = await startServer(createApp(), 0);
base = `http://127.0.0.1:${server.address().port}`;
});
after(() => {
quit.stopServer = realStop;
server?.close();
require("../db").db.close();
});
it("closes every lane pane, sets the flag, then stops the server", async () => {
execFileSync("tmux", ["new-session", "-d", "-s", "agenthub-lane-9001", "sleep 60"]);
execFileSync("tmux", ["new-session", "-d", "-s", "agenthub-lane-9001-2", "sleep 60"]);
execFileSync("tmux", ["new-session", "-d", "-s", "not-ours", "sleep 60"]);
try {
const body = await new Promise((resolve, reject) => {
const req = http.request(`${base}/api/settings/quit`, { method: "POST" }, (res) => {
let text = "";
res.on("data", (c) => (text += c));
res.on("end", () => resolve(JSON.parse(text)));
});
req.on("error", reject);
req.end();
});
assert.deepEqual(body, { ok: true, killed: 2 });
assert.equal(quit.isQuit(), true);
const left = execFileSync("tmux", ["list-sessions", "-F", "#{session_name}"], {
encoding: "utf8",
});
assert.deepEqual(left.trim().split("\n"), ["not-ours"]);
await new Promise((r) => setImmediate(r));
assert.equal(stops, 1);
} finally {
execFileSync("tmux", ["kill-session", "-t", "not-ours"]);
}
});
// Never with `confirm: true` here: that runs scripts/uninstall.js for real.
it("refuses an uninstall without confirm: true", async () => {
const res = await fetch(`${base}/api/settings/uninstall`, {
method: "POST",
headers: { "content-type": "application/json" },
body: "{}",
});
assert.equal(res.status, 400);
assert.equal((await res.json()).error.code, "ECONFIRM");
});
}
);