@@ -235,31 +235,72 @@ describe("UsageEventStore", () => {
235235 expect ( events [ 1 ] . eventId ) . toBe ( "evt-2" )
236236 } )
237237
238- it ( "should skip corrupt lines and continue reading " , async ( ) => {
238+ it ( "should quarantine corrupt last line if it ends with newline " , async ( ) => {
239239 const event = makeEvent ( )
240240 await store . append ( event )
241241
242- // Manually add a corrupt line
242+ // Add a corrupt line that ends with \n (fully written, not a crash tail)
243243 const segmentPath = path . join ( store . _getStatsDir ( ) , "events-000001.ndjson" )
244244 await fs . appendFile ( segmentPath , "{invalid json line\n" )
245245
246246 const events = await store . readAll ( )
247247 expect ( events ) . toHaveLength ( 1 ) // corrupt line is skipped
248+
249+ const quarantinePath = path . join ( store . _getStatsDir ( ) , "quarantine" , "corrupt-lines.jsonl" )
250+ const quarantineExists = await fs
251+ . access ( quarantinePath )
252+ . then ( ( ) => true )
253+ . catch ( ( ) => false )
254+ expect ( quarantineExists ) . toBe ( true )
255+
256+ const quarantineContent = await fs . readFile ( quarantinePath , "utf-8" )
257+ const quarantineEntries = quarantineContent
258+ . trim ( )
259+ . split ( "\n" )
260+ . map ( ( l ) => JSON . parse ( l ) )
261+ expect ( quarantineEntries ) . toHaveLength ( 1 )
262+ expect ( quarantineEntries [ 0 ] . line ) . toBe ( 2 )
263+ } )
264+
265+ it ( "should quarantine schema-invalid last line if it ends with newline" , async ( ) => {
266+ const event = makeEvent ( )
267+ await store . append ( event )
268+
269+ // Add a schema-invalid line ending with \n
270+ const segmentPath = path . join ( store . _getStatsDir ( ) , "events-000001.ndjson" )
271+ await fs . appendFile ( segmentPath , JSON . stringify ( { schemaVersion : 999 , invalid : true } ) + "\n" )
272+
273+ const events = await store . readAll ( )
274+ expect ( events ) . toHaveLength ( 1 )
275+
276+ const quarantinePath = path . join ( store . _getStatsDir ( ) , "quarantine" , "corrupt-lines.jsonl" )
277+ const quarantineExists = await fs
278+ . access ( quarantinePath )
279+ . then ( ( ) => true )
280+ . catch ( ( ) => false )
281+ expect ( quarantineExists ) . toBe ( true )
248282 } )
249283
250- it ( "should ignore truncated last line (crash tail)" , async ( ) => {
284+ it ( "should ignore truncated last line (crash tail) without writing quarantine report " , async ( ) => {
251285 const event = makeEvent ( )
252286 await store . append ( event )
253287
254- // Manually add a truncated line (last line)
288+ // Manually add a truncated line (last line, no trailing newline )
255289 const segmentPath = path . join ( store . _getStatsDir ( ) , "events-000001.ndjson" )
256- await fs . appendFile ( segmentPath , '{"partial": tru' ) // Truncated JSON
290+ await fs . appendFile ( segmentPath , '{"partial": tru' ) // Truncated JSON without \n
257291
258292 const events = await store . readAll ( )
259293 expect ( events ) . toHaveLength ( 1 ) // crash tail is ignored
294+
295+ const quarantinePath = path . join ( store . _getStatsDir ( ) , "quarantine" , "corrupt-lines.jsonl" )
296+ const quarantineExists = await fs
297+ . access ( quarantinePath )
298+ . then ( ( ) => true )
299+ . catch ( ( ) => false )
300+ expect ( quarantineExists ) . toBe ( false )
260301 } )
261302
262- it ( "should write quarantine report for corrupt lines" , async ( ) => {
303+ it ( "should write quarantine report for corrupt lines in the middle " , async ( ) => {
263304 const event = makeEvent ( )
264305 await store . append ( event )
265306
@@ -352,7 +393,7 @@ describe("UsageEventStore", () => {
352393 expect ( result ) . toBe ( true )
353394 } )
354395
355- it ( "should move old segments to old-generation directory" , async ( ) => {
396+ it ( "should clean up old-generation directory after clear " , async ( ) => {
356397 await store . append ( makeEvent ( ) )
357398
358399 await store . clear ( )
@@ -362,11 +403,11 @@ describe("UsageEventStore", () => {
362403 . access ( oldGenDir )
363404 . then ( ( ) => true )
364405 . catch ( ( ) => false )
365- expect ( oldGenExists ) . toBe ( true )
406+ expect ( oldGenExists ) . toBe ( false )
366407 } )
367408 } )
368409
369- describe ( "idempotency recovery on restart" , ( ) => {
410+ describe ( "idempotency recovery on restart and multi-instance " , ( ) => {
370411 it ( "should rebuild idempotency set from segment scan on re-init" , async ( ) => {
371412 const event = makeEvent ( { idempotencyKey : "idem-persist" } )
372413 await store . append ( event )
@@ -379,6 +420,24 @@ describe("UsageEventStore", () => {
379420 const result = await newStore . append ( event )
380421 expect ( result ) . toBe ( false )
381422 } )
423+
424+ it ( "should double-check idempotency across concurrent store instances sharing same directory" , async ( ) => {
425+ const store2 = new UsageEventStore ( tempDir )
426+ await store2 . initialize ( )
427+
428+ const event = makeEvent ( { idempotencyKey : "idem-cross-instance" } )
429+
430+ // First append on store1 succeeds
431+ const result1 = await store . append ( event )
432+ expect ( result1 ) . toBe ( true )
433+
434+ // Second append on store2 with same idempotencyKey is rejected under lock
435+ const result2 = await store2 . append ( event )
436+ expect ( result2 ) . toBe ( false )
437+
438+ const events = await store2 . readAll ( )
439+ expect ( events ) . toHaveLength ( 1 )
440+ } )
382441 } )
383442
384443 describe ( "cache invalidation on same-segment append" , ( ) => {
@@ -419,7 +478,7 @@ describe("UsageEventStore", () => {
419478 } )
420479 } )
421480
422- describe ( "error handling" , ( ) => {
481+ describe ( "error handling and hard cap " , ( ) => {
423482 it ( "should report not capped initially" , async ( ) => {
424483 expect ( store . isCapped ( ) ) . toBe ( false )
425484 } )
@@ -431,5 +490,21 @@ describe("UsageEventStore", () => {
431490 // Re-appending the same event is not an error
432491 await expect ( store . append ( event ) ) . resolves . toBe ( false )
433492 } )
493+
494+ it ( "should prospectively reject appends when hard cap would be reached" , async ( ) => {
495+ const TOTAL_MAX_BYTES = 100 * 1024 * 1024 // 100 MiB
496+ const statsDir = store . _getStatsDir ( )
497+
498+ // Pre-fill a dummy segment file that puts total size just 50 bytes below 100 MiB
499+ const dummySegment = path . join ( statsDir , "events-000001.ndjson" )
500+ const dummySize = TOTAL_MAX_BYTES - 50
501+ const buffer = Buffer . alloc ( dummySize , " " )
502+ await fs . writeFile ( dummySegment , buffer )
503+
504+ const event = makeEvent ( )
505+ // Appending an event (which serializes to > 100 bytes) must prospectively fail
506+ await expect ( store . append ( event ) ) . rejects . toThrow ( "Storage hard cap (100 MiB) reached" )
507+ expect ( store . isCapped ( ) ) . toBe ( true )
508+ } )
434509 } )
435510} )
0 commit comments