Files
AgentHub/server/__tests__/dashboard-runs.test.js
T
nntrivi2001 22de5009ff feat: v2.7.8 — Artifacts hub, clone related repos as extra folders, quieter resume prompt
- Artifacts (/artifacts): claude.ai pages sessions publish (captured from the
  Artifact tool's hook), pinned workspace files, skills and plugins, each
  kept as a snapshot; shared ones appear in a Hub and a skill or plugin
  installs into the viewer's own account (server/lib/artifacts.js,
  /api/artifacts, docs/plans/artifacts/).
- Workspace extra folders: clone one or more related repos beside the
  workspace and add them in one step (POST /api/lanes/:id/sources/clone).
- A pane that ended by itself while tmux kept running is no longer offered
  for resume (tmux.serverStartedAt in reconcileOrphans).
- Changes panel: a folder that is not a git repo opens on Files, and its
  files open from there.
- Plus the parallel sessions' work in this tree: File Explorer button in the
  panel header, user delete, RSA SSH keys, chat/terminal and pet fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 16:06:54 +07:00

217 lines
9.8 KiB
JavaScript

/**
* @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ĩ <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 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 }));
// Long enough ago that no reconcile treats the run as just started.
const start = (id, extra = {}) =>
runs.recordRun({
id,
mode: "interactive",
cwd: "/r",
status: "running",
laneId: 4,
startedAt: Date.parse("2026-01-01T00:00:00Z"),
...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("narrows a lane's history to one tool", () => {
start("agenthub-lane-8", { laneId: 8, tool: "claude" });
start("agenthub-lane-8-1", { laneId: 8, tool: "shell" });
const shells = runs.listRuns({ laneId: 8, tool: "shell" });
assert.deepEqual(
shells.map((r) => r.id),
["agenthub-lane-8-1"]
);
assert.equal(runs.listRuns({ laneId: 8 }).length, 2);
});
it("forgets a closed Terminal tab and nothing of another tool", () => {
start("agenthub-lane-9", { laneId: 9, tool: "claude" });
start("agenthub-lane-9-1", { laneId: 9, tool: "shell" });
assert.equal(runs.forgetShellRun("agenthub-lane-9"), false);
assert.ok(runs.getRun("agenthub-lane-9"));
assert.equal(runs.forgetShellRun("agenthub-lane-9-1"), true);
assert.equal(runs.getRun("agenthub-lane-9-1"), null);
});
it("drops every closed Terminal tab of a lane, live ones untouched", () => {
for (let i = 1; i <= 5; i++) {
start(`agenthub-lane-11-${i}`, {
laneId: 11,
tool: "shell",
startedAt: Date.parse(`2026-10-0${i}T10:00:00Z`),
});
}
start("agenthub-lane-11", { laneId: 11, tool: "claude", startedAt: Date.parse("2026-09-01") });
// Pane 5 still runs; every other one has closed.
runs.reconcileOrphans((id) => id === "agenthub-lane-11-5");
const shells = runs.listRuns({ laneId: 11, tool: "shell" }).map((r) => r.id);
assert.deepEqual(shells, ["agenthub-lane-11-5"]);
assert.ok(runs.getRun("agenthub-lane-11"), "another tool's row stays");
});
it("never ends a run started a moment ago: another process's tmux list may predate it", () => {
start("agenthub-lane-30", { laneId: 30, startedAt: Date.now() - 2000 });
start("agenthub-lane-31", { laneId: 31, startedAt: Date.now() - 60_000 });
runs.reconcileOrphans(() => false);
assert.equal(runs.getRun("agenthub-lane-30").status, "running");
assert.equal(runs.getRun("agenthub-lane-31").status, "abandoned");
});
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");
});
it("offers for resume only the sessions whose pane died under them", () => {
const { db } = require("../db");
db.prepare("INSERT INTO lanes (id, title, cwd) VALUES (20, 'w', '/i')").run();
const session = db.prepare(
"INSERT INTO sessions (id, name, status, cwd) VALUES (?, ?, 'active', '/i')"
);
for (const id of ["s-boot", "s-exit", "s-signal", "s-codex"]) session.run(id, id);
const end = db.prepare(
"INSERT INTO events (session_id, event_type, data) VALUES (?, 'SessionEnd', ?)"
);
end.run("s-exit", JSON.stringify({ reason: "prompt_input_exit" }));
end.run("s-signal", JSON.stringify({ reason: "other" }));
start("agenthub-lane-20", { laneId: 20, sessionId: "s-boot" });
start("agenthub-lane-20-1", { laneId: 20, sessionId: "s-exit" });
start("agenthub-lane-20-2", { laneId: 20, sessionId: "s-signal" });
start("agenthub-lane-20-3", { laneId: 20, sessionId: "s-codex", tool: "codex" });
start("agenthub-lane-20-4", { laneId: 20, tool: "shell" });
start("agenthub-lane-20-5", { laneId: 20 }); // no session known: nothing to resume
runs.reconcileOrphans((id) => !id.startsWith("agenthub-lane-20"));
const status = (id) => runs.getRun(id).status;
assert.equal(status("agenthub-lane-20"), "interrupted");
assert.equal(status("agenthub-lane-20-1"), "abandoned", "the user's own /exit");
assert.equal(status("agenthub-lane-20-2"), "interrupted", "a signal is not the user");
assert.equal(status("agenthub-lane-20-3"), "interrupted");
assert.equal(status("agenthub-lane-20-5"), "abandoned");
const offered = () =>
runs
.listInterrupted()
.map((r) => r.session_id)
.sort();
assert.deepEqual(offered(), ["s-boot", "s-codex", "s-signal"]);
assert.deepEqual(runs.listInterrupted(99), [], "another owner's workspace");
// Resumed by any path: a newer run of the session takes it off the list.
start("agenthub-lane-20-6", {
laneId: 20,
sessionId: "s-signal",
startedAt: Date.now() + 1000,
});
assert.deepEqual(offered(), ["s-boot", "s-codex"]);
assert.equal(runs.dismissInterrupted(["s-boot"], 99), 0, "not the caller's");
assert.equal(runs.dismissInterrupted(["s-boot", "s-codex"]), 2);
assert.deepEqual(offered(), []);
});
it("does not offer a pane that ended by itself while its tmux server ran on", () => {
const { db } = require("../db");
db.prepare("INSERT INTO lanes (id, title, cwd) VALUES (21, 'w', '/j')").run();
const session = db.prepare(
"INSERT INTO sessions (id, name, status, cwd) VALUES (?, ?, 'active', '/j')"
);
for (const id of ["q-codex", "q-signal", "q-crash", "q-reboot"]) session.run(id, id);
db.prepare(
"INSERT INTO events (session_id, event_type, data) VALUES ('q-signal', 'SessionEnd', ?)"
).run(JSON.stringify({ reason: "other" }));
start("agenthub-lane-21", { laneId: 21, sessionId: "q-codex", tool: "codex" });
start("agenthub-lane-21-1", { laneId: 21, sessionId: "q-signal" });
start("agenthub-lane-21-2", { laneId: 21, sessionId: "q-crash" });
start("agenthub-lane-21-3", { laneId: 21, sessionId: "q-reboot", tool: "codex" });
// The host's tmux has run since long before; lane 21-3's server is gone.
const since = (id) => (id === "agenthub-lane-21-3" ? null : 0);
runs.reconcileOrphans((id) => !id.startsWith("agenthub-lane-21"), since);
const status = (id) => runs.getRun(id).status;
assert.equal(status("agenthub-lane-21"), "abandoned", "Codex quit by the user");
assert.equal(status("agenthub-lane-21-1"), "abandoned", "closed, tmux ran on");
assert.equal(status("agenthub-lane-21-2"), "interrupted", "Claude never said it ended");
assert.equal(status("agenthub-lane-21-3"), "interrupted", "died with its server");
// A server newer than the run restarted under it: interrupted too.
start("agenthub-lane-21-4", { laneId: 21, sessionId: "q-codex", tool: "codex" });
runs.reconcileOrphans((id) => !id.startsWith("agenthub-lane-21"), () => Date.now());
assert.equal(status("agenthub-lane-21-4"), "interrupted");
});
it("gives a hookless tool's session the lane of the run it started in", () => {
const { db } = require("../db");
start("agenthub-lane-7-1", {
laneId: 7,
cwd: "/w",
tool: "antigravity",
startedAt: Date.parse("2026-10-05T08:49:57.145Z"),
});
runs.patchRun({
id: "agenthub-lane-7-1",
status: "killed",
endedAt: Date.parse("2026-10-05T08:50:10.740Z"),
});
const add = db.prepare(
"INSERT INTO sessions (id, name, status, cwd, started_at, tool) VALUES (?, 'x', 'completed', ?, ?, ?)"
);
add.run("in", "/w", "2026-10-05T08:50:01Z", "antigravity");
add.run("before", "/w", "2026-10-05T08:40:00Z", "antigravity");
add.run("after", "/w", "2026-10-05T09:00:00Z", "antigravity");
add.run("elsewhere", "/x", "2026-10-05T08:50:01Z", "antigravity");
add.run("other-tool", "/w", "2026-10-05T08:50:01Z", "codex");
add.run("claude", "/w", "2026-10-05T08:50:01Z", "claude");
runs.claimToolSessions();
const lane = (id) => db.prepare("SELECT lane_id FROM sessions WHERE id = ?").get(id).lane_id;
assert.equal(lane("in"), 7);
for (const id of ["before", "after", "elsewhere", "other-tool", "claude"]) {
assert.equal(lane(id), null, id);
}
// One session at a time also becomes its run's session.
assert.equal(runs.getRun("agenthub-lane-7-1").session_id, null);
runs.claimToolSessions("in");
assert.equal(runs.getRun("agenthub-lane-7-1").session_id, "in");
});
});