/** * @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ĩ */ 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); }); });