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