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