| 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 { |
| 7 | narrowed, |
| 8 | onItemSessionTitle, |
| 9 | peopleIn, |
| 10 | repoName, |
| 11 | runsIn, |
| 12 | sessionCost, |
| 13 | sessionHref, |
| 14 | sessionsFilters, |
| 15 | sessionsQuery, |
| 16 | splitLive, |
| 17 | workspaceSessionTitle, |
| 18 | } from "./sessions.ts"; |
| 19 | |
| 20 | function listed(id: string, more: Partial<WorkspaceSession> = {}): WorkspaceSession { |
| 21 | return { |
| 22 | kind: "harness", |
| 23 | id, |
| 24 | repo: "acme/web", |
| 25 | repoId: "rep_1", |
| 26 | title: null, |
| 27 | actor: "ana", |
| 28 | harness: "claude_code", |
| 29 | agent: null, |
| 30 | branch: "main", |
| 31 | item: null, |
| 32 | itemTitle: null, |
| 33 | pull: null, |
| 34 | pullTitle: null, |
| 35 | pullStatus: null, |
| 36 | state: "active", |
| 37 | startedAt: "2026-10-17T10:00:00Z", |
| 38 | lastActivityAt: "2026-10-17T11:00:00Z", |
| 39 | subagents: 0, |
| 40 | step: null, |
| 41 | session: null, |
| 42 | ...more, |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | const own = (more: Partial<WorkSession> = {}): WorkSession => |
| 47 | ({ |
| 48 | id: "ses_1", |
| 49 | repoId: "rep_1", |
| 50 | item: null, |
| 51 | pull: null, |
| 52 | branch: "fix-it", |
| 53 | actor: { id: "u", username: "ana" }, |
| 54 | harness: "codex", |
| 55 | harnessSessionId: null, |
| 56 | title: null, |
| 57 | state: "active", |
| 58 | headCommit: null, |
| 59 | startedAt: "", |
| 60 | lastSeenAt: "", |
| 61 | endedAt: null, |
| 62 | endReason: null, |
| 63 | ...more, |
| 64 | }) as WorkSession; |
| 65 | |
| 66 | test("every kind of session opens where it is shown", () => { |
| 67 | assert.equal(sessionHref("Acme", listed("ses_1")), "/acme/-/work/sessions/ses_1"); |
| 68 | assert.equal(sessionHref("acme", listed("ses_2", { kind: "hosted", harness: "claude_managed_agents" })), "/acme/-/work/sessions/ses_2"); |
| 69 | // g1t's agent and a recorded session are their pull request's Session tab. |
| 70 | assert.equal(sessionHref("acme", listed("run_1", { kind: "agent", repo: "acme/api", pull: 8 })), "/acme/api/pull/8/session"); |
| 71 | assert.equal(sessionHref("acme", listed("pr_1", { kind: "recorded", pull: 7 })), "/acme/web/pull/7/session"); |
| 72 | assert.equal(sessionHref("acme", listed("run_2", { kind: "agent", pull: null })), null); |
| 73 | }); |
| 74 | |
| 75 | test("a session is named by its title, its subagent task, its pull request, never nothing", () => { |
| 76 | assert.equal(workspaceSessionTitle(listed("a", { session: own({ title: "Fix login" }) })), "Fix login"); |
| 77 | assert.equal(workspaceSessionTitle(listed("b", { session: own() })), "Session on fix-it"); |
| 78 | assert.equal( |
| 79 | workspaceSessionTitle(listed("c", { session: own({ parentId: "ses_0", agentType: "Explore", task: "Find endpoints" }) })), |
| 80 | "Explore: Find endpoints", |
| 81 | ); |
| 82 | assert.equal(workspaceSessionTitle(listed("d", { kind: "recorded", pull: 7, pullTitle: "Retry hooks" })), "Retry hooks"); |
| 83 | assert.equal(workspaceSessionTitle(listed("e", { kind: "agent", pull: 7 })), "Pull request #7"); |
| 84 | assert.equal(runsIn(listed("f")), "Claude Code"); |
| 85 | assert.equal(runsIn(listed("g", { kind: "agent", harness: null, agent: "g1t" })), "g1t's agent"); |
| 86 | assert.equal(runsIn(listed("h", { kind: "recorded", harness: null, agent: "cursor" })), "cursor, recorded"); |
| 87 | }); |
| 88 | |
| 89 | test("on a work item's card, a session with no title says what it is on, never a bare pull request number", () => { |
| 90 | assert.equal(onItemSessionTitle(listed("a", { session: own({ title: "Fix login" }) })), "Fix login"); |
| 91 | assert.equal(onItemSessionTitle(listed("d", { kind: "recorded", pull: 7, pullTitle: "Retry hooks" })), "Retry hooks"); |
| 92 | assert.equal(onItemSessionTitle(listed("e", { kind: "agent", pull: 158 })), "On pull request #158"); |
| 93 | assert.equal(onItemSessionTitle(listed("f", { kind: "agent", pull: null })), "Working on it"); |
| 94 | }); |
| 95 | |
| 96 | test("Sessions keeps its filters in the address and drops what it does not know", () => { |
| 97 | const filters = sessionsFilters(new URLSearchParams("repo=web&person=@bo&harness=codex&kind=bogus&state=recent")); |
| 98 | assert.deepEqual(filters, { state: "recent", repo: "web", person: "bo", harness: "codex", kind: null, item: null }); |
| 99 | assert.equal(sessionsQuery(filters), "?state=recent&repo=web&person=bo&harness=codex"); |
| 100 | assert.equal(sessionsQuery({ state: "live" }), ""); |
| 101 | assert.equal(narrowed(filters), true); |
| 102 | assert.equal(narrowed(sessionsFilters(new URLSearchParams("state=recent"))), false); |
| 103 | }); |
| 104 | |
| 105 | test("Sessions can be a work item's own: ?item=12", () => { |
| 106 | const filters = sessionsFilters(new URLSearchParams("state=recent&item=12")); |
| 107 | assert.equal(filters.item, 12); |
| 108 | assert.equal(narrowed(filters), true); |
| 109 | assert.equal(sessionsQuery(filters), "?state=recent&item=12"); |
| 110 | for (const bad of ["0", "-3", "x", "1.5"]) assert.equal(sessionsFilters(new URLSearchParams(`item=${bad}`)).item, null, bad); |
| 111 | }); |
| 112 | |
| 113 | test("live sessions come apart from those that ended, most recently heard from first", () => { |
| 114 | const sessions = [ |
| 115 | listed("old", { state: "ended" }), |
| 116 | listed("idle", { state: "idle" }), |
| 117 | listed("a", { lastActivityAt: "2026-10-17T09:00:00Z", actor: "bo" }), |
| 118 | listed("b", { lastActivityAt: "2026-10-17T11:30:00Z", actor: "bo" }), |
| 119 | ]; |
| 120 | const { active, idle, ended } = splitLive(sessions); |
| 121 | assert.deepEqual( |
| 122 | active.map((session) => session.id), |
| 123 | ["b", "a"], |
| 124 | ); |
| 125 | assert.deepEqual( |
| 126 | idle.map((session) => session.id), |
| 127 | ["idle"], |
| 128 | ); |
| 129 | assert.deepEqual( |
| 130 | ended.map((session) => session.id), |
| 131 | ["old"], |
| 132 | ); |
| 133 | assert.deepEqual(peopleIn(sessions), ["ana", "bo"]); |
| 134 | assert.equal(repoName("acme/web", "Acme"), "web"); |
| 135 | assert.equal(repoName("other/web", "acme"), "other/web"); |
| 136 | }); |
| 137 | |
| 138 | test("a session's cost reads as an estimate at list price, and says when some usage is priced as unknown", () => { |
| 139 | assert.equal(sessionCost({ costUsd: 4.1234 }), "≈ $4.12 at list price"); |
| 140 | assert.equal(sessionCost({ costUsd: 1234.5, costPartial: true }), "≈ $1,234.50 at list price, some usage priced as unknown"); |
| 141 | assert.equal(sessionCost({ costUsd: 0.004 }), "≈ under $0.01 at list price"); |
| 142 | assert.equal(sessionCost({ costUsd: 0 }), "≈ $0.00 at list price"); |
| 143 | assert.equal(sessionCost({ costUsd: null, costPartial: true }), "Usage priced as unknown"); |
| 144 | assert.equal(sessionCost({ costUsd: null }), null); |
| 145 | assert.equal(sessionCost({}), null); |
| 146 | assert.equal(sessionCost(null), null); |
| 147 | }); |