| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { WorkSession, WorkspaceSession } from "@g1t/contracts"; |
| 5 | |
| 6 | import { narrowed, peopleIn, repoName, runsIn, sessionHref, sessionsFilters, sessionsQuery, splitLive, workspaceSessionTitle } from "./sessions.ts"; |
| 7 | |
| 8 | function listed(id: string, more: Partial<WorkspaceSession> = {}): WorkspaceSession { |
| 9 | return { |
| 10 | kind: "harness", |
| 11 | id, |
| 12 | repo: "acme/web", |
| 13 | repoId: "rep_1", |
| 14 | title: null, |
| 15 | actor: "ana", |
| 16 | harness: "claude_code", |
| 17 | agent: null, |
| 18 | branch: "main", |
| 19 | item: null, |
| 20 | itemTitle: null, |
| 21 | pull: null, |
| 22 | pullTitle: null, |
| 23 | pullStatus: null, |
| 24 | state: "active", |
| 25 | startedAt: "2026-10-17T10:00:00Z", |
| 26 | lastActivityAt: "2026-10-17T11:00:00Z", |
| 27 | subagents: 0, |
| 28 | step: null, |
| 29 | session: null, |
| 30 | ...more, |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | const own = (more: Partial<WorkSession> = {}): WorkSession => |
| 35 | ({ id: "ses_1", repoId: "rep_1", item: null, pull: null, branch: "fix-it", actor: { id: "u", username: "ana" }, harness: "codex", harnessSessionId: null, title: null, state: "active", headCommit: null, startedAt: "", lastSeenAt: "", endedAt: null, endReason: null, ...more }) as WorkSession; |
| 36 | |
| 37 | test("every kind of session opens where it is shown", () => { |
| 38 | assert.equal(sessionHref("Acme", listed("ses_1")), "/acme/-/sessions/ses_1"); |
| 39 | assert.equal(sessionHref("acme", listed("ses_2", { kind: "hosted", harness: "claude_managed_agents" })), "/acme/-/sessions/ses_2"); |
| 40 | // g1t's agent and a recorded session are their pull request's Session tab. |
| 41 | assert.equal(sessionHref("acme", listed("run_1", { kind: "agent", repo: "acme/api", pull: 8 })), "/acme/api/pull/8/session"); |
| 42 | assert.equal(sessionHref("acme", listed("pr_1", { kind: "recorded", pull: 7 })), "/acme/web/pull/7/session"); |
| 43 | assert.equal(sessionHref("acme", listed("run_2", { kind: "agent", pull: null })), null); |
| 44 | }); |
| 45 | |
| 46 | test("a session is named by its title, its subagent task, its pull request, never nothing", () => { |
| 47 | assert.equal(workspaceSessionTitle(listed("a", { session: own({ title: "Fix login" }) })), "Fix login"); |
| 48 | assert.equal(workspaceSessionTitle(listed("b", { session: own() })), "Session on fix-it"); |
| 49 | assert.equal(workspaceSessionTitle(listed("c", { session: own({ parentId: "ses_0", agentType: "Explore", task: "Find endpoints" }) })), "Explore: Find endpoints"); |
| 50 | assert.equal(workspaceSessionTitle(listed("d", { kind: "recorded", pull: 7, pullTitle: "Retry hooks" })), "Retry hooks"); |
| 51 | assert.equal(workspaceSessionTitle(listed("e", { kind: "agent", pull: 7 })), "Pull request #7"); |
| 52 | assert.equal(runsIn(listed("f")), "Claude Code"); |
| 53 | assert.equal(runsIn(listed("g", { kind: "agent", harness: null, agent: "g1t" })), "g1t's agent"); |
| 54 | assert.equal(runsIn(listed("h", { kind: "recorded", harness: null, agent: "cursor" })), "cursor, recorded"); |
| 55 | }); |
| 56 | |
| 57 | test("Sessions keeps its filters in the address and drops what it does not know", () => { |
| 58 | const filters = sessionsFilters(new URLSearchParams("repo=web&person=@bo&harness=codex&kind=bogus&state=recent")); |
| 59 | assert.deepEqual(filters, { state: "recent", repo: "web", person: "bo", harness: "codex", kind: null }); |
| 60 | assert.equal(sessionsQuery(filters), "?state=recent&repo=web&person=bo&harness=codex"); |
| 61 | assert.equal(sessionsQuery({ state: "live" }), ""); |
| 62 | assert.equal(narrowed(filters), true); |
| 63 | assert.equal(narrowed(sessionsFilters(new URLSearchParams("state=recent"))), false); |
| 64 | }); |
| 65 | |
| 66 | test("live sessions come apart from those that ended, most recently heard from first", () => { |
| 67 | const sessions = [ |
| 68 | listed("old", { state: "ended" }), |
| 69 | listed("idle", { state: "idle" }), |
| 70 | listed("a", { lastActivityAt: "2026-10-17T09:00:00Z", actor: "bo" }), |
| 71 | listed("b", { lastActivityAt: "2026-10-17T11:30:00Z", actor: "bo" }), |
| 72 | ]; |
| 73 | const { active, idle, ended } = splitLive(sessions); |
| 74 | assert.deepEqual(active.map((session) => session.id), ["b", "a"]); |
| 75 | assert.deepEqual(idle.map((session) => session.id), ["idle"]); |
| 76 | assert.deepEqual(ended.map((session) => session.id), ["old"]); |
| 77 | assert.deepEqual(peopleIn(sessions), ["ana", "bo"]); |
| 78 | assert.equal(repoName("acme/web", "Acme"), "web"); |
| 79 | assert.equal(repoName("other/web", "acme"), "other/web"); |
| 80 | }); |