Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

session-logic.test.ts

128 lines4.0 KB

1import { EventId, TurnId, type OrchestrationThreadActivity } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import { derivePendingApprovals, deriveWorkLogEntries } from "./session-logic";
5​
6function makeActivity(overrides: {
7 id?: string;
8 createdAt?: string;
9 kind?: string;
10 summary?: string;
11 tone?: OrchestrationThreadActivity["tone"];
12 payload?: Record<string, unknown>;
13 turnId?: string;
14}): OrchestrationThreadActivity {
15 const payload = overrides.payload ?? {};
16 return {
17 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
18 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
19 kind: overrides.kind ?? "tool.started",
20 summary: overrides.summary ?? "Tool call",
21 tone: overrides.tone ?? "tool",
22 payload,
23 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
24 };
26​
27describe("derivePendingApprovals", () => {
28 it("tracks open approvals and removes resolved ones", () => {
29 const activities: OrchestrationThreadActivity[] = [
30 makeActivity({
31 id: "approval-open",
32 createdAt: "2026-02-23T00:00:01.000Z",
33 kind: "approval.requested",
34 summary: "Command approval requested",
35 tone: "approval",
36 payload: {
37 requestId: "req-1",
38 requestKind: "command",
39 detail: "bun run lint",
40 },
41 }),
42 makeActivity({
43 id: "approval-close",
44 createdAt: "2026-02-23T00:00:02.000Z",
45 kind: "approval.resolved",
46 summary: "Approval resolved",
47 tone: "info",
48 payload: { requestId: "req-2" },
49 }),
50 makeActivity({
51 id: "approval-closed-request",
52 createdAt: "2026-02-23T00:00:01.500Z",
53 kind: "approval.requested",
54 summary: "File-change approval requested",
55 tone: "approval",
56 payload: { requestId: "req-2", requestKind: "file-change" },
57 }),
58 ];
59​
60 expect(derivePendingApprovals(activities)).toEqual([
61 {
62 requestId: "req-1",
63 requestKind: "command",
64 createdAt: "2026-02-23T00:00:01.000Z",
65 detail: "bun run lint",
66 },
67 ]);
68 });
69});
70​
71describe("deriveWorkLogEntries", () => {
72 it("omits tool started entries and keeps completed entries", () => {
73 const activities: OrchestrationThreadActivity[] = [
74 makeActivity({
75 id: "tool-complete",
76 createdAt: "2026-02-23T00:00:03.000Z",
77 summary: "Tool call complete",
78 kind: "tool.completed",
79 }),
80 makeActivity({
81 id: "tool-start",
82 createdAt: "2026-02-23T00:00:02.000Z",
83 summary: "Tool call",
84 kind: "tool.started",
85 }),
86 ];
87​
88 const entries = deriveWorkLogEntries(activities, undefined);
89 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
90 });
91​
92 it("filters by turn id when provided", () => {
93 const activities: OrchestrationThreadActivity[] = [
94 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
95 makeActivity({
96 id: "turn-2",
97 turnId: "turn-2",
98 summary: "Tool call complete",
99 kind: "tool.completed",
100 }),
101 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
102 ];
103​
104 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
105 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
106 });
107​
108 it("omits checkpoint captured info entries", () => {
109 const activities: OrchestrationThreadActivity[] = [
110 makeActivity({
111 id: "checkpoint",
112 createdAt: "2026-02-23T00:00:01.000Z",
113 summary: "Checkpoint captured",
114 tone: "info",
115 }),
116 makeActivity({
117 id: "tool-complete",
118 createdAt: "2026-02-23T00:00:02.000Z",
119 summary: "Command run complete",
120 tone: "tool",
121 kind: "tool.completed",
122 }),
123 ];
124​
125 const entries = deriveWorkLogEntries(activities, undefined);
126 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
127 });
128});