11import { describe , test , expect } from "bun:test" ;
2- import { shouldAutoCompact } from "./autoCompactionCheck" ;
2+ import { checkAutoCompaction } from "./autoCompactionCheck" ;
33import type { WorkspaceUsageState } from "@/browser/stores/WorkspaceStore" ;
44import type { ChatUsageDisplay } from "@/common/utils/tokens/usageAggregator" ;
55import { KNOWN_MODELS } from "@/common/constants/knownModels" ;
@@ -43,14 +43,14 @@ const createMockUsage = (
4343 return { usageHistory, totalTokens : 0 } ;
4444} ;
4545
46- describe ( "shouldAutoCompact " , ( ) => {
46+ describe ( "checkAutoCompaction " , ( ) => {
4747 const SONNET_MAX_TOKENS = 200_000 ;
4848 const SONNET_70_PERCENT = SONNET_MAX_TOKENS * 0.7 ; // 140,000
4949 const SONNET_60_PERCENT = SONNET_MAX_TOKENS * 0.6 ; // 120,000
5050
5151 describe ( "Basic Functionality" , ( ) => {
5252 test ( "returns false when no usage data (first message)" , ( ) => {
53- const result = shouldAutoCompact ( undefined , KNOWN_MODELS . SONNET . id , false ) ;
53+ const result = checkAutoCompaction ( undefined , KNOWN_MODELS . SONNET . id , false , true ) ;
5454
5555 expect ( result . shouldShowWarning ) . toBe ( false ) ;
5656 expect ( result . usagePercentage ) . toBe ( 0 ) ;
@@ -59,7 +59,7 @@ describe("shouldAutoCompact", () => {
5959
6060 test ( "returns false when usage history is empty" , ( ) => {
6161 const usage : WorkspaceUsageState = { usageHistory : [ ] , totalTokens : 0 } ;
62- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
62+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
6363
6464 expect ( result . shouldShowWarning ) . toBe ( false ) ;
6565 expect ( result . usagePercentage ) . toBe ( 0 ) ;
@@ -68,7 +68,7 @@ describe("shouldAutoCompact", () => {
6868
6969 test ( "returns false when model has no max_input_tokens (unknown model)" , ( ) => {
7070 const usage = createMockUsage ( 50_000 ) ;
71- const result = shouldAutoCompact ( usage , "unknown-model" , false ) ;
71+ const result = checkAutoCompaction ( usage , "unknown-model" , false , true ) ;
7272
7373 expect ( result . shouldShowWarning ) . toBe ( false ) ;
7474 expect ( result . usagePercentage ) . toBe ( 0 ) ;
@@ -77,7 +77,7 @@ describe("shouldAutoCompact", () => {
7777
7878 test ( "returns false when usage is low (10%)" , ( ) => {
7979 const usage = createMockUsage ( 20_000 ) ; // 10% of 200k
80- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
80+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
8181
8282 expect ( result . shouldShowWarning ) . toBe ( false ) ;
8383 expect ( result . usagePercentage ) . toBe ( 10 ) ;
@@ -86,7 +86,7 @@ describe("shouldAutoCompact", () => {
8686
8787 test ( "returns true at warning threshold (60% with default 10% advance)" , ( ) => {
8888 const usage = createMockUsage ( SONNET_60_PERCENT ) ;
89- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
89+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
9090
9191 expect ( result . shouldShowWarning ) . toBe ( true ) ;
9292 expect ( result . usagePercentage ) . toBe ( 60 ) ;
@@ -95,7 +95,7 @@ describe("shouldAutoCompact", () => {
9595
9696 test ( "returns true at compaction threshold (70%)" , ( ) => {
9797 const usage = createMockUsage ( SONNET_70_PERCENT ) ;
98- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
98+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
9999
100100 expect ( result . shouldShowWarning ) . toBe ( true ) ;
101101 expect ( result . usagePercentage ) . toBe ( 70 ) ;
@@ -104,7 +104,7 @@ describe("shouldAutoCompact", () => {
104104
105105 test ( "returns true above threshold (80%)" , ( ) => {
106106 const usage = createMockUsage ( 160_000 ) ; // 80% of 200k
107- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
107+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
108108
109109 expect ( result . shouldShowWarning ) . toBe ( true ) ;
110110 expect ( result . usagePercentage ) . toBe ( 80 ) ;
@@ -115,7 +115,7 @@ describe("shouldAutoCompact", () => {
115115 describe ( "Usage Calculation (Critical for infinite loop fix)" , ( ) => {
116116 test ( "uses last usage entry tokens, not cumulative sum" , ( ) => {
117117 const usage = createMockUsage ( 10_000 ) ; // Only 5% of context
118- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
118+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
119119
120120 // Should be 5%, not counting historical
121121 expect ( result . usagePercentage ) . toBe ( 5 ) ;
@@ -126,7 +126,7 @@ describe("shouldAutoCompact", () => {
126126 // Scenario: After compaction, historical = 70K, recent = 5K
127127 // Should calculate based on 5K (2.5%), not 75K (37.5%)
128128 const usage = createMockUsage ( 5_000 , 70_000 ) ;
129- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
129+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
130130
131131 expect ( result . usagePercentage ) . toBe ( 2.5 ) ;
132132 expect ( result . shouldShowWarning ) . toBe ( false ) ;
@@ -148,7 +148,7 @@ describe("shouldAutoCompact", () => {
148148 totalTokens : 0 ,
149149 } ;
150150
151- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
151+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
152152
153153 // Total: 10k + 5k + 2k + 3k + 1k = 21k tokens = 10.5%
154154 expect ( result . usagePercentage ) . toBe ( 10.5 ) ;
@@ -158,23 +158,23 @@ describe("shouldAutoCompact", () => {
158158 describe ( "1M Context Mode" , ( ) => {
159159 test ( "uses 1M tokens when use1M=true and model supports it (Sonnet 4)" , ( ) => {
160160 const usage = createMockUsage ( 600_000 ) ; // 60% of 1M
161- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , true ) ;
161+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , true , true ) ;
162162
163163 expect ( result . usagePercentage ) . toBe ( 60 ) ;
164164 expect ( result . shouldShowWarning ) . toBe ( true ) ;
165165 } ) ;
166166
167167 test ( "uses 1M tokens for Sonnet with use1M=true (model is claude-sonnet-4-5)" , ( ) => {
168168 const usage = createMockUsage ( 700_000 ) ; // 70% of 1M
169- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , true ) ;
169+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , true , true ) ;
170170
171171 expect ( result . usagePercentage ) . toBe ( 70 ) ;
172172 expect ( result . shouldShowWarning ) . toBe ( true ) ;
173173 } ) ;
174174
175175 test ( "uses standard max_input_tokens when use1M=false" , ( ) => {
176176 const usage = createMockUsage ( 140_000 ) ; // 70% of 200k
177- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
177+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
178178
179179 expect ( result . usagePercentage ) . toBe ( 70 ) ;
180180 expect ( result . shouldShowWarning ) . toBe ( true ) ;
@@ -183,7 +183,7 @@ describe("shouldAutoCompact", () => {
183183 test ( "ignores use1M for models that don't support it (GPT)" , ( ) => {
184184 const usage = createMockUsage ( 100_000 , undefined , KNOWN_MODELS . GPT_MINI . id ) ;
185185 // GPT Mini has 272k context, so 100k = 36.76%
186- const result = shouldAutoCompact ( usage , KNOWN_MODELS . GPT_MINI . id , true ) ;
186+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . GPT_MINI . id , true , true ) ;
187187
188188 // Should use standard 272k, not 1M (use1M ignored for GPT)
189189 expect ( result . usagePercentage ) . toBeCloseTo ( 36.76 , 1 ) ;
@@ -194,7 +194,7 @@ describe("shouldAutoCompact", () => {
194194 describe ( "Edge Cases" , ( ) => {
195195 test ( "empty usageHistory array returns safe defaults" , ( ) => {
196196 const usage : WorkspaceUsageState = { usageHistory : [ ] , totalTokens : 0 } ;
197- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
197+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
198198
199199 expect ( result . shouldShowWarning ) . toBe ( false ) ;
200200 expect ( result . usagePercentage ) . toBe ( 0 ) ;
@@ -203,15 +203,15 @@ describe("shouldAutoCompact", () => {
203203
204204 test ( "single entry in usageHistory works correctly" , ( ) => {
205205 const usage = createMockUsage ( 140_000 ) ;
206- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
206+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
207207
208208 expect ( result . shouldShowWarning ) . toBe ( true ) ;
209209 expect ( result . usagePercentage ) . toBe ( 70 ) ;
210210 } ) ;
211211
212212 test ( "custom threshold parameter (80%)" , ( ) => {
213213 const usage = createMockUsage ( 140_000 ) ; // 70% of context
214- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false , 0.8 ) ; // 80% threshold
214+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true , 0.8 ) ; // 80% threshold
215215
216216 // At 70%, should NOT show warning for 80% threshold (needs 70% advance = 10%)
217217 expect ( result . shouldShowWarning ) . toBe ( true ) ; // 70% >= (80% - 10% = 70%)
@@ -221,7 +221,7 @@ describe("shouldAutoCompact", () => {
221221
222222 test ( "custom warning advance (5% instead of 10%)" , ( ) => {
223223 const usage = createMockUsage ( 130_000 ) ; // 65% of context
224- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false , 0.7 , 5 ) ;
224+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true , 0.7 , 5 ) ;
225225
226226 // At 65%, should show warning with 5% advance (70% - 5% = 65%)
227227 expect ( result . shouldShowWarning ) . toBe ( true ) ;
@@ -244,15 +244,15 @@ describe("shouldAutoCompact", () => {
244244 totalTokens : 0 ,
245245 } ;
246246
247- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
247+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
248248
249249 expect ( result . shouldShowWarning ) . toBe ( false ) ;
250250 expect ( result . usagePercentage ) . toBe ( 0 ) ;
251251 } ) ;
252252
253253 test ( "handles usage at exactly 100% of context" , ( ) => {
254254 const usage = createMockUsage ( SONNET_MAX_TOKENS ) ;
255- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
255+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
256256
257257 expect ( result . shouldShowWarning ) . toBe ( true ) ;
258258 expect ( result . usagePercentage ) . toBe ( 100 ) ;
@@ -261,7 +261,7 @@ describe("shouldAutoCompact", () => {
261261
262262 test ( "handles usage beyond 100% of context" , ( ) => {
263263 const usage = createMockUsage ( SONNET_MAX_TOKENS + 50_000 ) ;
264- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
264+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
265265
266266 expect ( result . shouldShowWarning ) . toBe ( true ) ;
267267 expect ( result . usagePercentage ) . toBe ( 125 ) ;
@@ -284,14 +284,14 @@ describe("shouldAutoCompact", () => {
284284
285285 for ( const { tokens, expectedPercent } of testCases ) {
286286 const usage = createMockUsage ( tokens ) ;
287- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
287+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
288288 expect ( result . usagePercentage ) . toBe ( expectedPercent ) ;
289289 }
290290 } ) ;
291291
292292 test ( "handles fractional percentages correctly" , ( ) => {
293293 const usage = createMockUsage ( 123_456 ) ; // 61.728%
294- const result = shouldAutoCompact ( usage , KNOWN_MODELS . SONNET . id , false ) ;
294+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , true ) ;
295295
296296 expect ( result . usagePercentage ) . toBeCloseTo ( 61.728 , 2 ) ;
297297 expect ( result . shouldShowWarning ) . toBe ( true ) ; // Above 60%
0 commit comments