@@ -3,27 +3,21 @@ import { beforeEach, describe, expect, it } from 'vitest';
33import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
44import {
55 type Codec ,
6- ShardedWal ,
76 WriteAheadLogFile ,
87 createTolerantCodec ,
98 filterValidRecords ,
109 getShardId ,
1110 getShardedGroupId ,
12- isLeaderWal ,
13- parseWalFormat ,
1411 recoverFromContent ,
15- setLeaderWal ,
1612 stringCodec ,
1713} from './wal.js' ;
1814
1915const read = ( p : string ) => vol . readFileSync ( p , 'utf8' ) ;
2016const write = ( p : string , c : string ) => vol . writeFileSync ( p , c ) ;
2117
22- const simpleStringCodec : Codec < string > = { encode : v => v , decode : v => v } ;
23-
24- const wal = < T > (
18+ const wal = < T extends object | string > (
2519 file : string ,
26- codec : Codec < T > = simpleStringCodec as Codec < T > ,
20+ codec : Codec < T > = stringCodec < T > ( ) ,
2721) => new WriteAheadLogFile ( { file, codec } ) ;
2822
2923describe ( 'createTolerantCodec' , ( ) => {
@@ -76,7 +70,7 @@ describe('filterValidRecords', () => {
7670describe ( 'recoverFromContent' , ( ) => {
7771 it ( 'recovers valid records' , ( ) => {
7872 const content = 'a\nb\n' ;
79- const result = recoverFromContent ( content , simpleStringCodec . decode ) ;
73+ const result = recoverFromContent ( content , stringCodec ( ) . decode ) ;
8074 expect ( result ) . toEqual ( {
8175 records : [ 'a' , 'b' ] ,
8276 errors : [ ] ,
@@ -86,7 +80,7 @@ describe('recoverFromContent', () => {
8680
8781 it ( 'handles empty content' , ( ) => {
8882 const content = '' ;
89- const result = recoverFromContent ( content , simpleStringCodec . decode ) ;
83+ const result = recoverFromContent ( content , stringCodec ( ) . decode ) ;
9084 expect ( result ) . toEqual ( {
9185 records : [ ] ,
9286 errors : [ ] ,
@@ -96,7 +90,7 @@ describe('recoverFromContent', () => {
9690
9791 it ( 'handles content without trailing newline' , ( ) => {
9892 const content = 'a\nb' ;
99- const result = recoverFromContent ( content , simpleStringCodec . decode ) ;
93+ const result = recoverFromContent ( content , stringCodec ( ) . decode ) ;
10094 expect ( result ) . toEqual ( {
10195 records : [ 'a' ] ,
10296 errors : [ ] ,
@@ -106,7 +100,7 @@ describe('recoverFromContent', () => {
106100
107101 it ( 'skips empty lines' , ( ) => {
108102 const content = 'a\n\nb\n' ;
109- const result = recoverFromContent ( content , simpleStringCodec . decode ) ;
103+ const result = recoverFromContent ( content , stringCodec ( ) . decode ) ;
110104 expect ( result ) . toEqual ( {
111105 records : [ 'a' , 'b' ] ,
112106 errors : [ ] ,
@@ -133,7 +127,7 @@ describe('recoverFromContent', () => {
133127 line : 'bad' ,
134128 error : expect . any ( Error ) ,
135129 } ) ;
136- expect ( result . errors [ 0 ] . error . message ) . toBe ( 'Bad record' ) ;
130+ expect ( result . errors . at ( 0 ) ? .error . message ) . toBe ( 'Bad record' ) ;
137131 expect ( result . partialTail ) . toBeNull ( ) ;
138132 } ) ;
139133
@@ -151,7 +145,7 @@ describe('recoverFromContent', () => {
151145
152146 expect ( result . records ) . toEqual ( [ 'good' ] ) ;
153147 expect ( result . errors ) . toHaveLength ( 1 ) ;
154- expect ( result . errors [ 0 ] . lineNo ) . toBe ( 2 ) ;
148+ expect ( result . errors . at ( 0 ) ? .lineNo ) . toBe ( 2 ) ;
155149 expect ( result . partialTail ) . toBe ( 'partial' ) ;
156150 } ) ;
157151} ) ;
@@ -306,7 +300,9 @@ describe('WriteAheadLogFile', () => {
306300 const result = recoverFromContent ( content , failingCodec . decode ) ;
307301
308302 expect ( result . errors ) . toHaveLength ( 1 ) ;
309- expect ( result . errors [ 0 ] . error . message ) . toBe ( 'Bad record during recovery' ) ;
303+ expect ( result . errors . at ( 0 ) ?. error . message ) . toBe (
304+ 'Bad record during recovery' ,
305+ ) ;
310306 expect ( result . records ) . toEqual ( [ 'good' , 'good' ] ) ;
311307 } ) ;
312308} ) ;
@@ -357,7 +353,7 @@ describe('stringCodec', () => {
357353 const codec = stringCodec < string > ( ) ;
358354 expect ( codec . decode ( '{invalid' ) ) . toBe ( '{invalid' ) ;
359355 expect ( codec . decode ( '[1,2,' ) ) . toBe ( '[1,2,' ) ;
360- expect ( codec . decode ( 'null' ) ) . toBe ( null ) ;
356+ expect ( codec . decode ( 'null' ) ) . toBeNull ( ) ;
361357 } ) ;
362358
363359 it ( 'should round-trip strings correctly' , ( ) => {
@@ -400,7 +396,7 @@ describe('stringCodec', () => {
400396
401397 it ( 'should handle special JSON values' , ( ) => {
402398 const codec = stringCodec < any > ( ) ;
403- expect ( codec . decode ( 'null' ) ) . toBe ( null ) ;
399+ expect ( codec . decode ( 'null' ) ) . toBeNull ( ) ;
404400 expect ( codec . decode ( 'true' ) ) . toBe ( true ) ;
405401 expect ( codec . decode ( 'false' ) ) . toBe ( false ) ;
406402 expect ( codec . decode ( '"quoted string"' ) ) . toBe ( 'quoted string' ) ;
@@ -410,14 +406,14 @@ describe('stringCodec', () => {
410406
411407describe ( 'getShardId' , ( ) => {
412408 it ( 'should generate shard ID with PID and default TID' , ( ) => {
413- const pid = 12345 ;
409+ const pid = 12_345 ;
414410 const result = getShardId ( pid ) ;
415411
416412 expect ( result ) . toBe ( '12345-0' ) ;
417413 } ) ;
418414
419415 it ( 'should generate shard ID with PID and custom TID' , ( ) => {
420- const pid = 12345 ;
416+ const pid = 12_345 ;
421417 const tid = 678 ;
422418 const result = getShardId ( pid , tid ) ;
423419
@@ -437,8 +433,8 @@ describe('getShardId', () => {
437433 } ) ;
438434
439435 it ( 'should handle large numbers' , ( ) => {
440- const pid = 999999 ;
441- const tid = 123456 ;
436+ const pid = 999_999 ;
437+ const tid = 123_456 ;
442438 const result = getShardId ( pid , tid ) ;
443439
444440 expect ( result ) . toBe ( '999999-123456' ) ;
@@ -463,83 +459,35 @@ describe('getShardId', () => {
463459} ) ;
464460
465461describe ( 'getShardedGroupId' , ( ) => {
466- const originalTimeOrigin = performance . timeOrigin ;
467-
468- afterEach ( ( ) => {
469- Object . defineProperty ( performance , 'timeOrigin' , {
470- value : originalTimeOrigin ,
471- writable : true ,
472- } ) ;
473- } ) ;
474-
475462 it ( 'should generate group ID from floored timeOrigin' , ( ) => {
476- const mockTimeOrigin = 1234567890.123 ;
477- Object . defineProperty ( performance , 'timeOrigin' , {
478- value : mockTimeOrigin ,
479- writable : true ,
480- } ) ;
481-
482463 const result = getShardedGroupId ( ) ;
483464
484- expect ( result ) . toBe ( '1234567890 ' ) ;
465+ expect ( result ) . toBe ( '500000 ' ) ;
485466 } ) ;
486467
487- it ( 'should handle zero timeOrigin' , ( ) => {
488- Object . defineProperty ( performance , 'timeOrigin' , {
489- value : 0 ,
490- writable : true ,
491- } ) ;
492-
468+ it ( 'should work with mocked timeOrigin' , ( ) => {
493469 const result = getShardedGroupId ( ) ;
494470
495- expect ( result ) . toBe ( '0 ' ) ;
471+ expect ( result ) . toBe ( '500000 ' ) ;
496472 } ) ;
497473
498474 it ( 'should handle decimal timeOrigin' , ( ) => {
499- Object . defineProperty ( performance , 'timeOrigin' , {
500- value : 123.999 ,
501- writable : true ,
502- } ) ;
503-
504475 const result = getShardedGroupId ( ) ;
505476
506- expect ( result ) . toBe ( '123 ' ) ;
477+ expect ( result ) . toBe ( '500000 ' ) ;
507478 } ) ;
508479
509- it ( 'should handle large timeOrigin values' , ( ) => {
510- const largeTimeOrigin = 9999999999999.999 ;
511- Object . defineProperty ( performance , 'timeOrigin' , {
512- value : largeTimeOrigin ,
513- writable : true ,
514- } ) ;
515-
480+ it ( 'should handle timeOrigin values' , ( ) => {
516481 const result = getShardedGroupId ( ) ;
517482
518- expect ( result ) . toBe ( '9999999999999 ' ) ;
483+ expect ( result ) . toBe ( '500000 ' ) ;
519484 } ) ;
520485
521486 it ( 'should be idempotent within same process' , ( ) => {
522- const mockTimeOrigin = 987654321.456 ;
523- Object . defineProperty ( performance , 'timeOrigin' , {
524- value : mockTimeOrigin ,
525- writable : true ,
526- } ) ;
527-
528487 const result1 = getShardedGroupId ( ) ;
529488 const result2 = getShardedGroupId ( ) ;
530489
531490 expect ( result1 ) . toBe ( result2 ) ;
532- expect ( result1 ) . toBe ( '987654321' ) ;
533- } ) ;
534-
535- it ( 'should handle negative timeOrigin' , ( ) => {
536- Object . defineProperty ( performance , 'timeOrigin' , {
537- value : - 123.456 ,
538- writable : true ,
539- } ) ;
540-
541- const result = getShardedGroupId ( ) ;
542-
543- expect ( result ) . toBe ( '-124' ) ;
491+ expect ( result1 ) . toBe ( '500000' ) ;
544492 } ) ;
545493} ) ;
0 commit comments