Files
AgentHub/server/__tests__/tmux.test.js
T
nntrivi2001 198fbb7980 fix: event-loop freezes, plugin-install boot crash, tmux attach/scrollbar, Codex transcripts
- GET /api/workflows: one LEAD() window pass in a read worker (read-pool.js,
  workflow-stats.js); idx_events_session restored, idx_events_tool added
- compat-sqlite gains function/pluck/readonly so a plugin install without a
  better-sqlite3 build boots again; antigravity-usage uses db.js's Database
- install-hooks adds no checkout hooks while the agenthub plugin is enabled
  (events were doubled)
- terminal attach runs the live tmux server's own binary; the pane scrollbar
  gets a visible 2-column style, and claude panes start with
  CLAUDE_CODE_DISABLE_ALTERNATE_SCREEN=1 so tmux keeps history to scroll
- Codex sessions' conversation in GET /api/sessions/:id/transcript
- Codex/Antigravity sessions claimed by their workspace without hooks
- UI-audit follow-ups, docs and OpenAPI
2026-10-05 17:01:05 +07:00

139 lines
4.2 KiB
JavaScript

/**
* @file tmux.test.js
* @description Unit tests for the tmux command wrapper. Injects a fake exec
* implementation so the suite never shells out to a real `tmux` binary (CI
* has none installed).
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { describe, it, beforeEach } = require("node:test");
const assert = require("node:assert/strict");
const tmux = require("../lib/tmux");
describe("tmux wrapper", () => {
beforeEach(() => {
tmux.__reset();
});
it("hasSession returns true when execFileSync exits 0", () => {
tmux.__setExecImpl(() => "");
assert.equal(tmux.hasSession("agenthub-lane-1"), true);
});
it("hasSession returns false when execFileSync throws", () => {
tmux.__setExecImpl(() => {
const e = new Error("no such session");
e.status = 1;
throw e;
});
assert.equal(tmux.hasSession("agenthub-lane-1"), false);
});
it("newSession builds the correct argv", () => {
const calls = [];
tmux.__setExecImpl((args) => {
calls.push(args);
return "";
});
tmux.newSession({
name: "agenthub-lane-1",
cwd: "/tmp/repo",
argv: ["claude", "--model", "opus"],
});
assert.deepEqual(calls[0], [
"new-session",
"-d",
"-s",
"agenthub-lane-1",
"-c",
"/tmp/repo",
"--",
"claude",
"--model",
"opus",
]);
assert.deepEqual(calls[1], ["set-option", "-t", "=agenthub-lane-1:", "mouse", "on"]);
assert.deepEqual(calls[2], ["set-option", "-t", "=agenthub-lane-1:", "status", "off"]);
assert.deepEqual(calls[3], ["set-option", "-t", "=agenthub-lane-1:", "pane-scrollbars", "on"]);
assert.deepEqual(calls[4], [
"set-option",
"-t",
"=agenthub-lane-1:",
"pane-scrollbars-style",
"fg=brightwhite,bg=black,width=2,pad=0",
]);
});
it("newSession still succeeds on a tmux older than 3.7 (no pane-scrollbars)", () => {
tmux.__setExecImpl((args) => {
if (args.includes("pane-scrollbars")) throw new Error("invalid option: pane-scrollbars");
return "";
});
assert.doesNotThrow(() =>
tmux.newSession({ name: "agenthub-lane-1", cwd: "/tmp/repo", argv: ["claude"] })
);
});
it("sendText types the literal string and presses Enter, with no quoting", () => {
const calls = [];
tmux.__setExecImpl((args) => {
calls.push(args);
return "";
});
tmux.sendText("agenthub-lane-1", 'Reminder: run "agenthub stage implement"');
assert.deepEqual(calls[0], [
"send-keys",
"-t",
"=agenthub-lane-1:",
"-l",
'Reminder: run "agenthub stage implement"',
]);
assert.deepEqual(calls[1], ["send-keys", "-t", "=agenthub-lane-1:", "Enter"]);
});
it("targets a session exactly, so lane 1 never resolves to lane 12 by prefix", () => {
const calls = [];
tmux.__setExecImpl((args) => {
calls.push(args);
return "";
});
tmux.hasSession("agenthub-lane-1");
tmux.killSession("agenthub-lane-1");
assert.deepEqual(calls, [
["has-session", "-t", "=agenthub-lane-1"],
["kill-session", "-t", "=agenthub-lane-1"],
]);
});
it("killSession never throws when the session is already gone", () => {
tmux.__setExecImpl(() => {
const e = new Error("no such session");
e.status = 1;
throw e;
});
assert.doesNotThrow(() => tmux.killSession("agenthub-lane-1"));
});
it("listSessions filters by prefix and ignores unrelated sessions", () => {
tmux.__setExecImpl(() => "agenthub-lane-1\nagenthub-lane-2\nsome-other-session\n");
assert.deepEqual(tmux.listSessions("agenthub-lane-"), ["agenthub-lane-1", "agenthub-lane-2"]);
});
it("listSessions returns [] when tmux has no sessions at all (exit 1)", () => {
tmux.__setExecImpl(() => {
const e = new Error("no server running");
e.status = 1;
throw e;
});
assert.deepEqual(tmux.listSessions("agenthub-lane-"), []);
});
it("isTmuxAvailable reflects whether the binary resolves on PATH", () => {
tmux.__setExecImpl(() => "tmux 3.4");
assert.equal(tmux.isTmuxAvailable(), true);
tmux.__setExecImpl(() => {
throw new Error("ENOENT");
});
assert.equal(tmux.isTmuxAvailable(), false);
});
});