Skip to content

Commit bc86d08

Browse files
committed
refactor: wip
1 parent 4f8cd2d commit bc86d08

File tree

1 file changed

+38
-66
lines changed

1 file changed

+38
-66
lines changed

packages/utils/src/lib/wal.unit.test.ts

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ describe('WriteAheadLogFile', () => {
250250
const w = wal('/test/a.log');
251251

252252
const result = w.recover();
253-
expect(result.records).toEqual(['line1', 'line2']);
253+
expect(result.records).toStrictEqual(['line1', 'line2']);
254254
expect(result.errors).toEqual([]);
255255
});
256256

@@ -385,7 +385,6 @@ describe('WriteAheadLogFile', () => {
385385

386386
const walInstance = wal('/test/a.log');
387387

388-
// Mock the recover method to return errors
389388
const recoverSpy = vi.spyOn(walInstance, 'recover').mockReturnValue({
390389
records: ['content'],
391390
errors: [
@@ -685,85 +684,62 @@ describe('parseWalFormat', () => {
685684
});
686685

687686
it('should use default finalizer when none provided', () => {
688-
const result = parseWalFormat({ baseName: 'test' });
687+
const result = parseWalFormat<string>({ baseName: 'test' });
689688
expect(result.finalizer(['line1', 'line2'])).toBe('line1\nline2\n');
690689
expect(result.finalizer([])).toBe('\n');
691690
});
692691
});
693692

694693
describe('isLeaderWal', () => {
695-
const originalEnv = { ...process.env };
696-
697-
afterEach(() => {
698-
process.env = { ...originalEnv }; // eslint-disable-line functional/immutable-data
699-
});
700-
701694
it('should return true when env var matches current pid', () => {
702-
const envVarName = 'TEST_LEADER_PID';
703-
process.env[envVarName] = '10001'; // eslint-disable-line functional/immutable-data
695+
vi.stubEnv('TEST_LEADER_PID', '10001');
704696

705-
const result = isLeaderWal(envVarName);
697+
const result = isLeaderWal('TEST_LEADER_PID');
706698
expect(result).toBe(true);
707699
});
708700

709701
it('should return false when env var does not match current pid', () => {
710-
const envVarName = 'TEST_LEADER_PID';
711-
process.env[envVarName] = '67890'; // eslint-disable-line functional/immutable-data
702+
vi.stubEnv('TEST_LEADER_PID', '67890');
712703

713-
const result = isLeaderWal(envVarName);
704+
const result = isLeaderWal('TEST_LEADER_PID');
714705
expect(result).toBe(false);
715706
});
716707

717708
it('should return false when env var is not set', () => {
718-
const envVarName = 'NON_EXISTENT_VAR';
719-
delete process.env[envVarName]; // eslint-disable-line @typescript-eslint/no-dynamic-delete,functional/immutable-data
709+
vi.stubEnv('NON_EXISTENT_VAR', undefined as any);
720710

721-
const result = isLeaderWal(envVarName);
711+
const result = isLeaderWal('NON_EXISTENT_VAR');
722712
expect(result).toBe(false);
723713
});
724714

725715
it('should return false when env var is empty string', () => {
726-
const envVarName = 'TEST_LEADER_PID';
727-
process.env[envVarName] = ''; // eslint-disable-line functional/immutable-data
716+
vi.stubEnv('TEST_LEADER_PID', '');
728717

729-
const result = isLeaderWal(envVarName);
718+
const result = isLeaderWal('TEST_LEADER_PID');
730719
expect(result).toBe(false);
731720
});
732721
});
733722

734723
describe('setLeaderWal', () => {
735-
const originalEnv = { ...process.env };
736-
737-
afterEach(() => {
738-
process.env = { ...originalEnv }; // eslint-disable-line functional/immutable-data
739-
});
740-
741724
it('should set env var when not already set', () => {
742-
const envVarName = 'TEST_ORIGIN_PID';
743-
delete process.env[envVarName]; // eslint-disable-line @typescript-eslint/no-dynamic-delete,functional/immutable-data
744-
expect(process.env[envVarName]).toBeUndefined();
725+
expect(process.env['TEST_ORIGIN_PID']).toBeUndefined();
745726

746-
setLeaderWal(envVarName);
727+
setLeaderWal('TEST_ORIGIN_PID');
747728

748-
expect(process.env[envVarName]).toBe('10001'); // process.pid is mocked to 10001
729+
expect(process.env['TEST_ORIGIN_PID']).toBe('10001');
749730
});
750731

751732
it('should not overwrite existing env var', () => {
752-
const envVarName = 'TEST_ORIGIN_PID';
753-
const existingValue = '99999';
754-
755-
process.env[envVarName] = existingValue; // eslint-disable-line functional/immutable-data
756-
setLeaderWal(envVarName);
733+
vi.stubEnv('TEST_ORIGIN_PID', '99999');
734+
setLeaderWal('TEST_ORIGIN_PID');
757735

758-
expect(process.env[envVarName]).toBe(existingValue);
736+
expect(process.env['TEST_ORIGIN_PID']).toBe('99999');
759737
});
760738

761739
it('should set env var to current pid as string', () => {
762-
const envVarName = 'TEST_ORIGIN_PID';
763-
delete process.env[envVarName]; // eslint-disable-line @typescript-eslint/no-dynamic-delete,functional/immutable-data
764-
setLeaderWal(envVarName);
740+
setLeaderWal('TEST_ORIGIN_PID');
765741

766-
expect(process.env[envVarName]).toBe('10001');
742+
expect(process.env['TEST_ORIGIN_PID']).toBe('10001');
767743
});
768744
});
769745

@@ -787,12 +763,11 @@ describe('ShardedWal', () => {
787763

788764
const shard = sw.shard('123-456');
789765
expect(shard).toBeInstanceOf(WriteAheadLogFile);
790-
expect(shard.getPath()).toBe('/test/shards/test-wal.123-456.log');
766+
expect(shard.getPath()).toMatchPath('/test/shards/test-wal.123-456.log');
791767
});
792768

793769
it('should list no shard files when directory does not exist', () => {
794770
const sw = new ShardedWal('/nonexistent', {});
795-
// Access private method for testing
796771
const files = (sw as any).shardFiles();
797772
expect(files).toEqual([]);
798773
});
@@ -805,10 +780,11 @@ describe('ShardedWal', () => {
805780
});
806781

807782
it('should list shard files matching extension', () => {
808-
vol.mkdirSync('/shards', { recursive: true });
809-
write('/shards/wal.1.log', 'content1');
810-
write('/shards/wal.2.log', 'content2');
811-
write('/shards/other.txt', 'not a shard');
783+
vol.fromJSON({
784+
'/shards/wal.1.log': 'content1',
785+
'/shards/wal.2.log': 'content2',
786+
'/shards/other.txt': 'not a shard',
787+
});
812788

813789
const sw = new ShardedWal('/shards', { walExtension: '.log' });
814790
const files = (sw as any).shardFiles();
@@ -836,9 +812,10 @@ describe('ShardedWal', () => {
836812
});
837813

838814
it('should finalize multiple shards into single file', () => {
839-
vol.mkdirSync('/shards', { recursive: true });
840-
write('/shards/test.1.log', 'record1\n');
841-
write('/shards/test.2.log', 'record2\n');
815+
vol.fromJSON({
816+
'/shards/test.1.log': 'record1\n',
817+
'/shards/test.2.log': 'record2\n',
818+
});
842819

843820
const sw = new ShardedWal('/shards', {
844821
baseName: 'test',
@@ -854,10 +831,10 @@ describe('ShardedWal', () => {
854831
});
855832

856833
it('should handle invalid entries during finalize', () => {
857-
vol.mkdirSync('/shards', { recursive: true });
858-
write('/shards/test.1.log', 'valid\n');
859-
write('/shards/test.2.log', 'invalid\n');
860-
834+
vol.fromJSON({
835+
'/shards/test.1.log': 'valid\n',
836+
'/shards/test.2.log': 'invalid\n',
837+
});
861838
const tolerantCodec = createTolerantCodec({
862839
encode: (s: string) => s,
863840
decode: (s: string) => {
@@ -883,10 +860,10 @@ describe('ShardedWal', () => {
883860
});
884861

885862
it('should cleanup shard files', () => {
886-
vol.mkdirSync('/shards', { recursive: true });
887-
write('/shards/test.1.log', 'content1');
888-
write('/shards/test.2.log', 'content2');
889-
863+
vol.fromJSON({
864+
'/shards/test.1.log': 'content1',
865+
'/shards/test.2.log': 'content2',
866+
});
890867
const sw = new ShardedWal('/shards', {
891868
baseName: 'test',
892869
walExtension: '.log',
@@ -902,24 +879,19 @@ describe('ShardedWal', () => {
902879
});
903880

904881
it('should handle cleanup when some shard files do not exist', () => {
905-
vol.mkdirSync('/shards', { recursive: true });
906-
write('/shards/test.1.log', 'content1');
882+
vol.fromJSON({ '/shards/test.1.log': 'content1' });
907883

908884
const sw = new ShardedWal('/shards', {
909885
baseName: 'test',
910886
walExtension: '.log',
911887
});
912888

913-
// Manually delete one file to simulate race condition
914889
vol.unlinkSync('/shards/test.1.log');
915-
916-
// Should not throw
917890
expect(() => sw.cleanup()).not.toThrow();
918891
});
919892

920893
it('should use custom options in finalizer', () => {
921-
vol.mkdirSync('/shards', { recursive: true });
922-
write('/shards/test.1.log', 'record1\n');
894+
vol.fromJSON({ '/shards/test.1.log': 'record1\n' });
923895

924896
const sw = new ShardedWal('/shards', {
925897
baseName: 'test',

0 commit comments

Comments
 (0)