/** * @file Tests for the persisted run history (`server/lib/dashboard-runs.js`). * A run's id is its tmux pane name, which every Start in that pane reuses, so * a new run must ARCHIVE the previous row under its own id instead of * overwriting it; and a row is flagged dead only when its tmux session is * really gone — panes outlive a server restart. * @author Nguyễn Ngọc Trí Vĩ */ 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 ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "dashboard-runs-")); process.env.DASHBOARD_DB_PATH = path.join(ROOT, "t.db"); let runs; before(() => { runs = require("../lib/dashboard-runs"); }); after(() => fs.rmSync(ROOT, { recursive: true, force: true })); const start = (id, extra = {}) => runs.recordRun({ id, mode: "interactive", cwd: "/r", status: "running", laneId: 4, ...extra }); describe("dashboard-runs", () => { it("keeps the previous run of a pane as history when the pane starts again", () => { start("agenthub-lane-4", { startedAt: Date.parse("2026-10-01T10:00:00Z"), sessionId: "s-old" }); start("agenthub-lane-4", { startedAt: Date.parse("2026-10-02T10:00:00Z"), sessionId: "s-new" }); const current = runs.getRun("agenthub-lane-4"); assert.equal(current.session_id, "s-new"); const history = runs.listRuns({ laneId: 4 }); assert.equal(history.length, 2); const old = history.find((r) => r.session_id === "s-old"); assert.notEqual(old.id, "agenthub-lane-4"); assert.ok(old.id.startsWith("agenthub-lane-4@"), old.id); // A pane that is being replaced is not running any more. assert.equal(old.status, "abandoned"); assert.ok(old.ended_at); }); it("flags as abandoned only the running rows whose pane is gone", () => { start("agenthub-lane-5", { laneId: 5 }); start("agenthub-lane-6", { laneId: 6 }); const n = runs.reconcileOrphans((id) => id !== "agenthub-lane-6"); assert.equal(n, 1); assert.equal(runs.getRun("agenthub-lane-5").status, "running"); assert.equal(runs.getRun("agenthub-lane-6").status, "abandoned"); }); });