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

ContextWindowMeter.logic.test.ts

288 lines8.3 KB

1import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { deriveProviderInstanceEntries } from "../../providerInstances";
4import {
5 formatContextWindowCompactionMessage,
6 hasAvailableCompactionProvider,
7 hasDismissedResumeCompaction,
8 resolveContextWindowModelDisplayName,
9 shouldOfferResumeCompaction,
10 shouldReserveContextWindowMeter,
11} from "./ContextWindowMeter.logic";
12​
13function claudeProvider(input: {
14 instanceId: string;
15 continuationGroupKey: string;
16 enabled?: boolean;
17}): ServerProvider {
18 return {
19 instanceId: ProviderInstanceId.make(input.instanceId),
20 driver: ProviderDriverKind.make("claudeAgent"),
21 continuation: { groupKey: input.continuationGroupKey },
22 enabled: input.enabled ?? true,
23 installed: true,
24 version: null,
25 status: "ready",
26 auth: { status: "authenticated" },
27 checkedAt: "2026-08-24T12:00:00.000Z",
28 models: [],
29 slashCommands: [{ name: "compact", description: "" }],
30 skills: [],
31 };
33​
34describe("hasAvailableCompactionProvider", () => {
35 const originalInstanceId = ProviderInstanceId.make("claude_original");
36​
37 it("rejects a fallback in a different locked continuation group", () => {
38 const providers = deriveProviderInstanceEntries([
39 claudeProvider({
40 instanceId: originalInstanceId,
41 continuationGroupKey: "claude:home:/original",
42 enabled: false,
43 }),
44 claudeProvider({
45 instanceId: "claude_other",
46 continuationGroupKey: "claude:home:/other",
47 }),
48 ]);
49​
50 expect(
51 hasAvailableCompactionProvider({
52 providers,
53 driverKind: ProviderDriverKind.make("claudeAgent"),
54 instanceId: originalInstanceId,
55 lockedInstanceId: originalInstanceId,
56 }),
57 ).toBe(false);
58 });
59​
60 it("accepts an enabled fallback in the locked continuation group", () => {
61 const providers = deriveProviderInstanceEntries([
62 claudeProvider({
63 instanceId: originalInstanceId,
64 continuationGroupKey: "claude:home:/original",
65 enabled: false,
66 }),
67 claudeProvider({
68 instanceId: "claude_fallback",
69 continuationGroupKey: "claude:home:/original",
70 }),
71 ]);
72​
73 expect(
74 hasAvailableCompactionProvider({
75 providers,
76 driverKind: ProviderDriverKind.make("claudeAgent"),
77 instanceId: originalInstanceId,
78 lockedInstanceId: originalInstanceId,
79 }),
80 ).toBe(true);
81 });
82});
83​
84describe("resolveContextWindowModelDisplayName", () => {
85 it("uses the selected model from the exact provider instance", () => {
86 const primaryInstanceId = ProviderInstanceId.make("codex");
87 const selectedInstanceId = ProviderInstanceId.make("codex-work");
88 const modelOptionsByInstance = new Map([
89 [
90 primaryInstanceId,
91 [{ slug: "gpt-5.6-sol", name: "Primary profile model", shortName: "Primary" }],
92 ],
93 [selectedInstanceId, [{ slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", shortName: "5.6 Sol" }]],
94 ]);
95​
96 expect(
97 resolveContextWindowModelDisplayName(
98 {
99 instanceId: selectedInstanceId,
100 model: "gpt-5.6-sol",
101 },
102 modelOptionsByInstance,
103 ),
104 ).toBe("5.6 Sol");
105 });
106​
107 it("falls back to the selected model slug when model metadata is unavailable", () => {
108 const selectedInstanceId = ProviderInstanceId.make("codex-work");
109​
110 expect(
111 resolveContextWindowModelDisplayName(
112 {
113 instanceId: selectedInstanceId,
114 model: "custom-model",
115 },
116 new Map(),
117 ),
118 ).toBe("custom-model");
119 });
120});
121​
122describe("formatContextWindowCompactionMessage", () => {
123 it("describes compaction in terms of the selected model", () => {
124 expect(formatContextWindowCompactionMessage("GPT-5.6 Sol")).toBe(
125 "Context for GPT-5.6 Sol compacts automatically when needed.",
126 );
127 });
128​
129 it("uses neutral copy when the model is unavailable", () => {
130 expect(formatContextWindowCompactionMessage(null)).toBe(
131 "Context compacts automatically when needed.",
132 );
133 });
134​
135 it("shows the configured auto-compaction threshold", () => {
136 expect(formatContextWindowCompactionMessage("Claude Sonnet 5", 300_000)).toBe(
137 "Compacts automatically at 300,000 tokens.",
138 );
139 });
140});
141​
142describe("shouldOfferResumeCompaction", () => {
143 const now = "2026-08-24T12:00:00.000Z";
144​
145 it("matches Claude's old-session age and context thresholds", () => {
146 expect(
147 shouldOfferResumeCompaction({
148 provider: "claudeAgent",
149 usedTokens: 100_000,
150 updatedAt: "2026-08-24T10:50:00.000Z",
151 now,
152 }),
153 ).toBe(true);
154 });
155​
156 it("does not prompt for recent or smaller sessions", () => {
157 expect(
158 shouldOfferResumeCompaction({
159 provider: "claudeAgent",
160 usedTokens: 99_999,
161 updatedAt: "2026-08-24T10:00:00.000Z",
162 now,
163 }),
164 ).toBe(false);
165 expect(
166 shouldOfferResumeCompaction({
167 provider: "claudeAgent",
168 usedTokens: 200_000,
169 updatedAt: "2026-08-24T10:51:00.000Z",
170 now,
171 }),
172 ).toBe(false);
173 });
174​
175 it("does not show Claude's resume prompt for another provider", () => {
176 expect(
177 shouldOfferResumeCompaction({
178 provider: "codex",
179 usedTokens: 300_000,
180 updatedAt: "2026-08-24T09:00:00.000Z",
181 now,
182 }),
183 ).toBe(false);
184 });
185});
186​
187describe("hasDismissedResumeCompaction", () => {
188 it("recognizes the native resume dialog's permanent dismissal", () => {
189 expect(
190 hasDismissedResumeCompaction([
191 {
192 kind: "user-input.resolved",
193 payload: {
194 answers: {
195 "This session is 2h 0m old and uses 250,000 tokens. Compact it before continuing?":
196 "Don't ask again",
197 },
198 },
199 },
200 ]),
201 ).toBe(true);
202 });
203​
204 it("ignores the same answer on an unrelated question", () => {
205 expect(
206 hasDismissedResumeCompaction([
207 {
208 kind: "user-input.resolved",
209 payload: { answers: { "Show this setup reminder?": "Don't ask again" } },
210 },
211 ]),
212 ).toBe(false);
213 });
214​
215 it("ignores unrelated questions that end with Claude's compaction prompt", () => {
216 expect(
217 hasDismissedResumeCompaction([
218 {
219 kind: "user-input.resolved",
220 payload: {
221 answers: {
222 "The build cache is large. Compact it before continuing?": "Don't ask again",
223 },
224 },
225 },
226 ]),
227 ).toBe(false);
228 });
229​
230 it("ignores pending questions and malformed resolved payloads", () => {
231 expect(
232 hasDismissedResumeCompaction([
233 { kind: "user-input.requested", payload: { answers: { question: "Don't ask again" } } },
234 { kind: "user-input.resolved", payload: null },
235 { kind: "user-input.resolved", payload: { answers: ["Don't ask again"] } },
236 ]),
237 ).toBe(false);
238 });
239});
240​
241describe("shouldReserveContextWindowMeter", () => {
242 const loadingStartedThread = {
243 meterEnabled: true,
244 detailLoading: true,
245 threadStarted: true,
246 providerReportsContextWindow: true,
247 };
248​
249 it("holds the meter's slot while a started thread's detail loads", () => {
250 expect(shouldReserveContextWindowMeter(loadingStartedThread)).toBe(true);
251 });
252​
253 it("reserves nothing once the detail is in", () => {
254 expect(shouldReserveContextWindowMeter({ ...loadingStartedThread, detailLoading: false })).toBe(
255 false,
256 );
257 });
258​
259 it("reserves nothing for a thread that never ran a turn", () => {
260 expect(shouldReserveContextWindowMeter({ ...loadingStartedThread, threadStarted: false })).toBe(
261 false,
262 );
263 });
264​
265 it("reserves while the thread's provider is not in the catalog yet", () => {
266 expect(
267 shouldReserveContextWindowMeter({
268 ...loadingStartedThread,
269 providerReportsContextWindow: null,
270 }),
271 ).toBe(true);
272 });
273​
274 it("reserves nothing for a provider that does not stream usage", () => {
275 expect(
276 shouldReserveContextWindowMeter({
277 ...loadingStartedThread,
278 providerReportsContextWindow: false,
279 }),
280 ).toBe(false);
281 });
282​
283 it("reserves nothing while the meter is switched off", () => {
284 expect(shouldReserveContextWindowMeter({ ...loadingStartedThread, meterEnabled: false })).toBe(
285 false,
286 );
287 });
288});