Open History to see the last commit
1import { EventId, TurnId, type OrchestrationThreadActivity } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3
4import {
5 derivePendingApprovals,
6 deriveWorkLogEntries,
7 hasToolActivityForTurn,
8 isLatestTurnSettled,
9} from "./session-logic";
10
11function makeActivity(overrides: {
12 id?: string;
13 createdAt?: string;
14 kind?: string;
15 summary?: string;
16 tone?: OrchestrationThreadActivity["tone"];
17 payload?: Record<string, unknown>;
18 turnId?: string;
19}): OrchestrationThreadActivity {
20 const payload = overrides.payload ?? {};
21 return {
22 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
23 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
24 kind: overrides.kind ?? "tool.started",
25 summary: overrides.summary ?? "Tool call",
26 tone: overrides.tone ?? "tool",
27 payload,
28 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
29 };
30}
31
32describe("derivePendingApprovals", () => {
33 it("tracks open approvals and removes resolved ones", () => {
34 const activities: OrchestrationThreadActivity[] = [
35 makeActivity({
36 id: "approval-open",
37 createdAt: "2026-02-23T00:00:01.000Z",
38 kind: "approval.requested",
39 summary: "Command approval requested",
40 tone: "approval",
41 payload: {
42 requestId: "req-1",
43 requestKind: "command",
44 detail: "bun run lint",
45 },
46 }),
47 makeActivity({
48 id: "approval-close",
49 createdAt: "2026-02-23T00:00:02.000Z",
50 kind: "approval.resolved",
51 summary: "Approval resolved",
52 tone: "info",
53 payload: { requestId: "req-2" },
54 }),
55 makeActivity({
56 id: "approval-closed-request",
57 createdAt: "2026-02-23T00:00:01.500Z",
58 kind: "approval.requested",
59 summary: "File-change approval requested",
60 tone: "approval",
61 payload: { requestId: "req-2", requestKind: "file-change" },
62 }),
63 ];
64
65 expect(derivePendingApprovals(activities)).toEqual([
66 {
67 requestId: "req-1",
68 requestKind: "command",
69 createdAt: "2026-02-23T00:00:01.000Z",
70 detail: "bun run lint",
71 },
72 ]);
73 });
74});
75
76describe("deriveWorkLogEntries", () => {
77 it("omits tool started entries and keeps completed entries", () => {
78 const activities: OrchestrationThreadActivity[] = [
79 makeActivity({
80 id: "tool-complete",
81 createdAt: "2026-02-23T00:00:03.000Z",
82 summary: "Tool call complete",
83 kind: "tool.completed",
84 }),
85 makeActivity({
86 id: "tool-start",
87 createdAt: "2026-02-23T00:00:02.000Z",
88 summary: "Tool call",
89 kind: "tool.started",
90 }),
91 ];
92
93 const entries = deriveWorkLogEntries(activities, undefined);
94 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
95 });
96
97 it("filters by turn id when provided", () => {
98 const activities: OrchestrationThreadActivity[] = [
99 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
100 makeActivity({
101 id: "turn-2",
102 turnId: "turn-2",
103 summary: "Tool call complete",
104 kind: "tool.completed",
105 }),
106 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
107 ];
108
109 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
110 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
111 });
112
113 it("omits checkpoint captured info entries", () => {
114 const activities: OrchestrationThreadActivity[] = [
115 makeActivity({
116 id: "checkpoint",
117 createdAt: "2026-02-23T00:00:01.000Z",
118 summary: "Checkpoint captured",
119 tone: "info",
120 }),
121 makeActivity({
122 id: "tool-complete",
123 createdAt: "2026-02-23T00:00:02.000Z",
124 summary: "Command run complete",
125 tone: "tool",
126 kind: "tool.completed",
127 }),
128 ];
129
130 const entries = deriveWorkLogEntries(activities, undefined);
131 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
132 });
133});
134
135describe("hasToolActivityForTurn", () => {
136 it("returns false when turn id is missing", () => {
137 const activities: OrchestrationThreadActivity[] = [
138 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
139 ];
140
141 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
142 expect(hasToolActivityForTurn(activities, null)).toBe(false);
143 });
144
145 it("returns true only for matching tool activity in the target turn", () => {
146 const activities: OrchestrationThreadActivity[] = [
147 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
148 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
149 ];
150
151 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
152 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
153 });
154});
155
156describe("isLatestTurnSettled", () => {
157 const latestTurn = {
158 turnId: TurnId.makeUnsafe("turn-1"),
159 startedAt: "2026-02-27T21:10:00.000Z",
160 completedAt: "2026-02-27T21:10:06.000Z",
161 } as const;
162
163 it("returns false while the same turn is still active in a running session", () => {
164 expect(
165 isLatestTurnSettled(latestTurn, {
166 orchestrationStatus: "running",
167 activeTurnId: TurnId.makeUnsafe("turn-1"),
168 }),
169 ).toBe(false);
170 });
171
172 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
173 expect(
174 isLatestTurnSettled(latestTurn, {
175 orchestrationStatus: "running",
176 activeTurnId: TurnId.makeUnsafe("turn-2"),
177 }),
178 ).toBe(false);
179 });
180
181 it("returns true once the session is no longer running that turn", () => {
182 expect(
183 isLatestTurnSettled(latestTurn, {
184 orchestrationStatus: "ready",
185 activeTurnId: undefined,
186 }),
187 ).toBe(true);
188 });
189
190 it("returns false when turn timestamps are incomplete", () => {
191 expect(
192 isLatestTurnSettled(
193 {
194 turnId: TurnId.makeUnsafe("turn-1"),
195 startedAt: null,
196 completedAt: "2026-02-27T21:10:06.000Z",
197 },
198 null,
199 ),
200 ).toBe(false);
201 });
202});