@@ -156,7 +156,11 @@ const THREAD_ID = "b".repeat(64);
156156test ( "trimContextsToBudget_underBudget_returnsZeroAndLeavesContextsUnchanged" , ( ) => {
157157 const contexts = { [ `msg:${ MSG_ID } ` ] : 100 } ;
158158 // A very large budget — nothing should be evicted.
159- const { evicted, fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , 1_000_000 ) ;
159+ const { evicted, fitsAfterTrim } = trimContextsToBudget (
160+ contexts ,
161+ CLIENT_ID ,
162+ 1_000_000 ,
163+ ) ;
160164 assert . equal ( evicted , 0 ) ;
161165 assert . equal ( fitsAfterTrim , true ) ;
162166 assert . ok ( `msg:${ MSG_ID } ` in contexts ) ;
@@ -171,22 +175,31 @@ test("trimContextsToBudget_overBudget_evictsMsgEntriesOldestFirst", () => {
171175 [ `msg:${ "d" . repeat ( 64 ) } ` ] : 2 ,
172176 } ;
173177 const encoder = new TextEncoder ( ) ;
174- const fullSize = encoder . encode (
175- JSON . stringify ( { v : 1 , client_id : CLIENT_ID , contexts } ) ,
176- ) . length ;
177178 // Budget that requires evicting at least one entry.
178- const budget = fullSize - 10 ;
179-
180- const { evicted, fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , budget ) ;
179+ const budget =
180+ encoder . encode ( JSON . stringify ( { v : 1 , client_id : CLIENT_ID , contexts } ) )
181+ . length - 10 ;
182+
183+ const { evicted, fitsAfterTrim } = trimContextsToBudget (
184+ contexts ,
185+ CLIENT_ID ,
186+ budget ,
187+ ) ;
181188 assert . ok ( evicted >= 1 , `expected at least 1 eviction, got ${ evicted } ` ) ;
182189 assert . equal ( fitsAfterTrim , true ) ;
183190 // The oldest entry (ts=1) must be gone.
184- assert . ok ( ! ( `msg:${ MSG_ID } ` in contexts ) , "oldest msg entry should be evicted" ) ;
191+ assert . ok (
192+ ! ( `msg:${ MSG_ID } ` in contexts ) ,
193+ "oldest msg entry should be evicted" ,
194+ ) ;
185195 // Result must fit within budget.
186196 const resultSize = encoder . encode (
187197 JSON . stringify ( { v : 1 , client_id : CLIENT_ID , contexts } ) ,
188198 ) . length ;
189- assert . ok ( resultSize <= budget , `result ${ resultSize } exceeds budget ${ budget } ` ) ;
199+ assert . ok (
200+ resultSize <= budget ,
201+ `result ${ resultSize } exceeds budget ${ budget } ` ,
202+ ) ;
190203} ) ;
191204
192205test ( "trimContextsToBudget_channelKeysNeverEvicted" , ( ) => {
@@ -206,12 +219,18 @@ test("trimContextsToBudget_channelKeysNeverEvicted", () => {
206219 const { fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , budget ) ;
207220
208221 // Channel key must survive regardless of how many msg entries were evicted.
209- assert . ok ( "channel:some-channel-id" in contexts , "channel key must not be evicted" ) ;
222+ assert . ok (
223+ "channel:some-channel-id" in contexts ,
224+ "channel key must not be evicted" ,
225+ ) ;
210226 assert . equal ( fitsAfterTrim , true ) ;
211227 const resultSize = encoder . encode (
212228 JSON . stringify ( { v : 1 , client_id : CLIENT_ID , contexts } ) ,
213229 ) . length ;
214- assert . ok ( resultSize <= budget , `result ${ resultSize } exceeds budget ${ budget } ` ) ;
230+ assert . ok (
231+ resultSize <= budget ,
232+ `result ${ resultSize } exceeds budget ${ budget } ` ,
233+ ) ;
215234} ) ;
216235
217236test ( "trimContextsToBudget_msgEvictedBeforeThread" , ( ) => {
@@ -222,9 +241,6 @@ test("trimContextsToBudget_msgEvictedBeforeThread", () => {
222241 [ `thread:${ THREAD_ID } ` ] : 2 ,
223242 } ;
224243 const encoder = new TextEncoder ( ) ;
225- const fullSize = encoder . encode (
226- JSON . stringify ( { v : 1 , client_id : CLIENT_ID , contexts } ) ,
227- ) . length ;
228244 // Tight budget: remove exactly one entry.
229245 const oneEntrySize = encoder . encode (
230246 JSON . stringify ( {
@@ -235,17 +251,28 @@ test("trimContextsToBudget_msgEvictedBeforeThread", () => {
235251 ) . length ;
236252 const budget = oneEntrySize + 5 ; // fits one entry, not two
237253
238- const { evicted, fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , budget ) ;
254+ const { evicted, fitsAfterTrim } = trimContextsToBudget (
255+ contexts ,
256+ CLIENT_ID ,
257+ budget ,
258+ ) ;
239259 assert . equal ( evicted , 1 ) ;
240260 assert . equal ( fitsAfterTrim , true ) ;
241- assert . ok ( ! ( `msg:${ MSG_ID } ` in contexts ) , "msg entry should be evicted before thread" ) ;
261+ assert . ok (
262+ ! ( `msg:${ MSG_ID } ` in contexts ) ,
263+ "msg entry should be evicted before thread" ,
264+ ) ;
242265 assert . ok ( `thread:${ THREAD_ID } ` in contexts , "thread entry should survive" ) ;
243266} ) ;
244267
245268test ( "trimContextsToBudget_emptyContexts_returnsZeroAndFits" , ( ) => {
246269 // Empty contexts: blob is just the skeleton — fits any reasonable budget.
247270 const contexts = { } ;
248- const { evicted, fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , 1_000_000 ) ;
271+ const { evicted, fitsAfterTrim } = trimContextsToBudget (
272+ contexts ,
273+ CLIENT_ID ,
274+ 1_000_000 ,
275+ ) ;
249276 assert . equal ( evicted , 0 ) ;
250277 assert . equal ( fitsAfterTrim , true ) ;
251278} ) ;
@@ -263,7 +290,11 @@ test("trimContextsToBudget_channelOnlyBlobExceedsBudget_fitsAfterTrimFalse", ()
263290 // Budget smaller than the channel-only skeleton — cannot be satisfied.
264291 const budget = skeletonSize - 1 ;
265292
266- const { evicted, fitsAfterTrim } = trimContextsToBudget ( contexts , CLIENT_ID , budget ) ;
293+ const { evicted, fitsAfterTrim } = trimContextsToBudget (
294+ contexts ,
295+ CLIENT_ID ,
296+ budget ,
297+ ) ;
267298 assert . equal ( evicted , 0 , "no evictable entries exist" ) ;
268299 assert . equal ( fitsAfterTrim , false , "channel-only blob still exceeds budget" ) ;
269300 // Channel key must still be present.
0 commit comments