Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

99 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { WorkSession, WorkspaceSession } from "@g1t/contracts";
5
6import { narrowed, peopleIn, repoName, runsIn, sessionCost, sessionHref, sessionsFilters, sessionsQuery, splitLive, workspaceSessionTitle } from "./sessions.ts";
7
8function 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
34const 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
37test("every kind of session opens where it is shown", () => {
38 assert.equal(sessionHref("Acme", listed("ses_1")), "/acme/-/work/sessions/ses_1");
39 assert.equal(sessionHref("acme", listed("ses_2", { kind: "hosted", harness: "claude_managed_agents" })), "/acme/-/work/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
46test("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
57test("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, item: 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
66test("Sessions can be a work item's own: ?item=12", () => {
67 const filters = sessionsFilters(new URLSearchParams("state=recent&item=12"));
68 assert.equal(filters.item, 12);
69 assert.equal(narrowed(filters), true);
70 assert.equal(sessionsQuery(filters), "?state=recent&item=12");
71 for (const bad of ["0", "-3", "x", "1.5"]) assert.equal(sessionsFilters(new URLSearchParams(`item=${bad}`)).item, null, bad);
72});
73
74test("live sessions come apart from those that ended, most recently heard from first", () => {
75 const sessions = [
76 listed("old", { state: "ended" }),
77 listed("idle", { state: "idle" }),
78 listed("a", { lastActivityAt: "2026-10-17T09:00:00Z", actor: "bo" }),
79 listed("b", { lastActivityAt: "2026-10-17T11:30:00Z", actor: "bo" }),
80 ];
81 const { active, idle, ended } = splitLive(sessions);
82 assert.deepEqual(active.map((session) => session.id), ["b", "a"]);
83 assert.deepEqual(idle.map((session) => session.id), ["idle"]);
84 assert.deepEqual(ended.map((session) => session.id), ["old"]);
85 assert.deepEqual(peopleIn(sessions), ["ana", "bo"]);
86 assert.equal(repoName("acme/web", "Acme"), "web");
87 assert.equal(repoName("other/web", "acme"), "other/web");
88});
89
90test("a session's cost reads as an estimate at list price, and says when some usage is priced as unknown", () => {
91 assert.equal(sessionCost({ costUsd: 4.1234 }), "≈ $4.12 at list price");
92 assert.equal(sessionCost({ costUsd: 1234.5, costPartial: true }), "≈ $1,234.50 at list price, some usage priced as unknown");
93 assert.equal(sessionCost({ costUsd: 0.004 }), "≈ under $0.01 at list price");
94 assert.equal(sessionCost({ costUsd: 0 }), "≈ $0.00 at list price");
95 assert.equal(sessionCost({ costUsd: null, costPartial: true }), "Usage priced as unknown");
96 assert.equal(sessionCost({ costUsd: null }), null);
97 assert.equal(sessionCost({}), null);
98 assert.equal(sessionCost(null), null);
99});