Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 EventId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type OrchestrationThreadActivity,
7 TurnId,
8} from "@t3tools/contracts";
9
10import {
11 deriveKnownContextWindowSnapshot,
12 deriveLatestContextWindowSnapshot,
13 formatContextWindowTokens,
14} from "./contextWindow";
15
16function makeActivity(id: string, kind: string, payload: unknown): OrchestrationThreadActivity {
17 return {
18 id: EventId.make(id),
19 tone: "info",
20 kind,
21 summary: kind,
22 payload,
23 turnId: TurnId.make("turn-1"),
24 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("formats compact token counts", () => {
77 expect(formatContextWindowTokens(999)).toBe("999");
78 expect(formatContextWindowTokens(1400)).toBe("1.4k");
79 expect(formatContextWindowTokens(14_000)).toBe("14k");
80 expect(formatContextWindowTokens(258_000)).toBe("258k");
81 });
82
83 it("includes total processed tokens when available", () => {
84 const snapshot = deriveLatestContextWindowSnapshot([
85 makeActivity("activity-1", "context-window.updated", {
86 usedTokens: 81_659,
87 totalProcessedTokens: 748_126,
88 maxTokens: 258_400,
89 lastUsedTokens: 81_659,
90 }),
91 ]);
92
93 expect(snapshot?.usedTokens).toBe(81_659);
94 expect(snapshot?.totalProcessedTokens).toBe(748_126);
95 });
96
97 it("uses the selected Devin model catalog limit before ACP usage arrives", () => {
98 const instanceId = ProviderInstanceId.make("devin");
99 const snapshot = deriveKnownContextWindowSnapshot({
100 selection: { instanceId, model: "glm-5-2" },
101 providers: [
102 {
103 instanceId,
104 driver: ProviderDriverKind.make("devin"),
105 enabled: true,
106 installed: true,
107 version: null,
108 status: "ready",
109 auth: { status: "authenticated" },
110 checkedAt: "2026-03-23T00:00:00.000Z",
111 models: [
112 {
113 slug: "glm-5-2",
114 name: "GLM-5.2",
115 isCustom: false,
116 capabilities: { optionDescriptors: [] },
117 contextWindowTokens: 200_000,
118 },
119 ],
120 slashCommands: [],
121 skills: [],
122 },
123 ],
124 updatedAt: "2026-03-23T00:00:00.000Z",
125 });
126
127 expect(snapshot).toMatchObject({
128 usedTokens: 0,
129 maxTokens: 200_000,
130 remainingTokens: 200_000,
131 usedPercentage: 0,
132 model: "glm-5-2",
133 });
134 });
135
136 it("uses the selected context-window option instead of the catalog maximum", () => {
137 const instanceId = ProviderInstanceId.make("devin");
138 const snapshot = deriveKnownContextWindowSnapshot({
139 selection: {
140 instanceId,
141 model: "glm-5-2",
142 options: [{ id: "contextWindow", value: "200k" }],
143 },
144 providers: [
145 {
146 instanceId,
147 driver: ProviderDriverKind.make("devin"),
148 enabled: true,
149 installed: true,
150 version: null,
151 status: "ready",
152 auth: { status: "authenticated" },
153 checkedAt: "2026-03-23T00:00:00.000Z",
154 models: [
155 {
156 slug: "glm-5-2",
157 name: "GLM-5.2",
158 isCustom: false,
159 capabilities: {
160 optionDescriptors: [
161 {
162 id: "contextWindow",
163 label: "Context window",
164 type: "select",
165 options: [
166 { id: "200k", label: "200K" },
167 { id: "1m", label: "1M" },
168 ],
169 },
170 ],
171 },
172 contextWindowTokens: 1_000_000,
173 },
174 ],
175 slashCommands: [],
176 skills: [],
177 },
178 ],
179 updatedAt: "2026-03-23T00:00:00.000Z",
180 });
181
182 expect(snapshot?.maxTokens).toBe(200_000);
183 expect(snapshot?.remainingTokens).toBe(200_000);
184 });
185
186 it("does not invent a context meter when the catalog has no limit", () => {
187 const instanceId = ProviderInstanceId.make("devin");
188 expect(
189 deriveKnownContextWindowSnapshot({
190 selection: { instanceId, model: "custom" },
191 providers: [],
192 updatedAt: "2026-03-23T00:00:00.000Z",
193 }),
194 ).toBeNull();
195 });
196});