Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { EventId, type OrchestrationThreadActivity, TurnId } from "@t3tools/contracts";
3
4import {
5 contextWindowReachedThreadLimit,
6 deriveLatestContextWindowSnapshot,
7 formatContextWindowTokens,
8} from "./contextWindow";
9
10function makeActivity(
11 id: string,
12 kind: string,
13 payload: unknown,
14 options: { readonly sequence?: number; readonly createdAt?: string } = {},
15): OrchestrationThreadActivity {
16 return {
17 id: EventId.make(id),
18 tone: "info",
19 kind,
20 summary: kind,
21 payload,
22 turnId: TurnId.make("turn-1"),
23 ...(options.sequence === undefined ? {} : { sequence: options.sequence }),
24 createdAt: options.createdAt ?? "2026-03-23T00:00:00.000Z",
25 };
26}
27
28describe("contextWindow", () => {
29 it("derives the latest valid context window snapshot", () => {
30 const snapshot = deriveLatestContextWindowSnapshot([
31 makeActivity("activity-1", "context-window.updated", {
32 usedTokens: 1000,
33 }),
34 makeActivity("activity-2", "tool.started", {}),
35 makeActivity("activity-3", "context-window.updated", {
36 usedTokens: 14_000,
37 maxTokens: 258_000,
38 compactsAutomatically: true,
39 autoCompactThreshold: 200_000,
40 }),
41 ]);
42
43 expect(snapshot).not.toBeNull();
44 expect(snapshot?.usedTokens).toBe(14_000);
45 expect(snapshot?.totalProcessedTokens).toBeNull();
46 expect(snapshot?.maxTokens).toBe(258_000);
47 expect(snapshot?.compactsAutomatically).toBe(true);
48 expect(snapshot?.autoCompactThreshold).toBe(200_000);
49 });
50
51 it("ignores malformed payloads", () => {
52 const snapshot = deriveLatestContextWindowSnapshot([
53 makeActivity("activity-1", "context-window.updated", {}),
54 ]);
55
56 expect(snapshot).toBeNull();
57 });
58
59 it("keeps valid zero-usage snapshots", () => {
60 const snapshot = deriveLatestContextWindowSnapshot([
61 makeActivity("activity-1", "context-window.updated", {
62 usedTokens: 0,
63 maxTokens: 100_000,
64 }),
65 ]);
66
67 expect(snapshot).toMatchObject({
68 usedTokens: 0,
69 maxTokens: 100_000,
70 remainingTokens: 100_000,
71 usedPercentage: 0,
72 remainingPercentage: 100,
73 });
74 });
75
76 it("uses canonical activity order after compaction instead of array order", () => {
77 const snapshot = deriveLatestContextWindowSnapshot([
78 makeActivity(
79 "activity-current",
80 "context-window.updated",
81 { usedTokens: 20_000, maxTokens: 250_000 },
82 { sequence: 12, createdAt: "2026-03-23T00:02:00.000Z" },
83 ),
84 makeActivity(
85 "activity-stale",
86 "context-window.updated",
87 { usedTokens: 250_000, maxTokens: 250_000 },
88 { sequence: 11, createdAt: "2026-03-23T00:01:00.000Z" },
89 ),
90 ]);
91
92 expect(snapshot?.usedTokens).toBe(20_000);
93 expect(contextWindowReachedThreadLimit(snapshot)).toBe(false);
94 });
95
96 it("formats compact token counts", () => {
97 expect(formatContextWindowTokens(999)).toBe("999");
98 expect(formatContextWindowTokens(1400)).toBe("1.4k");
99 expect(formatContextWindowTokens(14_000)).toBe("14k");
100 expect(formatContextWindowTokens(258_000)).toBe("258k");
101 });
102
103 it("includes total processed tokens when available", () => {
104 const snapshot = deriveLatestContextWindowSnapshot([
105 makeActivity("activity-1", "context-window.updated", {
106 usedTokens: 81_659,
107 totalProcessedTokens: 748_126,
108 maxTokens: 258_400,
109 lastUsedTokens: 81_659,
110 }),
111 ]);
112
113 expect(snapshot?.usedTokens).toBe(81_659);
114 expect(snapshot?.totalProcessedTokens).toBe(748_126);
115 });
116
117 it("recognizes the hard thread limit from the latest snapshot", () => {
118 const snapshot = deriveLatestContextWindowSnapshot([
119 makeActivity("activity-1", "context-window.updated", {
120 usedTokens: 250_000,
121 maxTokens: 400_000,
122 }),
123 ]);
124
125 expect(contextWindowReachedThreadLimit(snapshot)).toBe(true);
126 expect(contextWindowReachedThreadLimit(snapshot, 300_000)).toBe(false);
127 });
128});