|
| 1 | +import { |
| 2 | + describe, it, before, after, |
| 3 | +} from 'node:test'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import fs from 'fs/promises'; |
| 6 | +import SharedStateService from '../../services/shared-state-service'; |
| 7 | +import { |
| 8 | + setupTestDir, cleanupTestFiles, createMockElement, TEST_SHARED_STATE_PATH, |
| 9 | +} from '../test-helpers'; |
| 10 | + |
| 11 | +// Override the shared state path for testing |
| 12 | + |
| 13 | +describe('SharedStateService', () => { |
| 14 | + let service: SharedStateService; |
| 15 | + |
| 16 | + before(async () => { |
| 17 | + await setupTestDir(); |
| 18 | + |
| 19 | + // Monkey-patch the constant for testing |
| 20 | + const SharedStateServiceModule = await import('../../services/shared-state-service'); |
| 21 | + (SharedStateServiceModule.default as any).prototype.constructor = function testConstructor() { |
| 22 | + // Use test path instead of default |
| 23 | + this.filePath = TEST_SHARED_STATE_PATH; |
| 24 | + }; |
| 25 | + |
| 26 | + service = new SharedStateService(); |
| 27 | + }); |
| 28 | + |
| 29 | + after(async () => { |
| 30 | + await cleanupTestFiles(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should save and load current element', async () => { |
| 34 | + const mockElement = createMockElement(); |
| 35 | + |
| 36 | + await service.saveCurrentElement(mockElement); |
| 37 | + const loaded = await service.getCurrentElement(); |
| 38 | + |
| 39 | + assert.deepStrictEqual(loaded, mockElement); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle null element', async () => { |
| 43 | + await service.saveCurrentElement(null); |
| 44 | + const loaded = await service.getCurrentElement(); |
| 45 | + |
| 46 | + assert.strictEqual(loaded, null); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return null for missing file', async () => { |
| 50 | + await cleanupTestFiles(); |
| 51 | + await setupTestDir(); |
| 52 | + |
| 53 | + const loaded = await service.getCurrentElement(); |
| 54 | + |
| 55 | + assert.strictEqual(loaded, null); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle corrupted file gracefully', async () => { |
| 59 | + // Write corrupted data to the file |
| 60 | + await fs.writeFile(TEST_SHARED_STATE_PATH, 'not json at all', 'utf8'); |
| 61 | + |
| 62 | + const loaded = await service.getCurrentElement(); |
| 63 | + |
| 64 | + assert.strictEqual(loaded, null); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should save element over corrupted file', async () => { |
| 68 | + // First create a corrupted file |
| 69 | + await fs.writeFile(TEST_SHARED_STATE_PATH, 'corrupted content', 'utf8'); |
| 70 | + |
| 71 | + // Save a new element over it |
| 72 | + const mockElement = createMockElement(); |
| 73 | + await service.saveCurrentElement(mockElement); |
| 74 | + |
| 75 | + // Should be able to load the new element |
| 76 | + const loaded = await service.getCurrentElement(); |
| 77 | + |
| 78 | + assert.deepStrictEqual(loaded, mockElement); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should overwrite previous element', async () => { |
| 82 | + const firstElement = createMockElement(); |
| 83 | + const secondElement = { ...createMockElement(), id: 'second-element' }; |
| 84 | + |
| 85 | + await service.saveCurrentElement(firstElement); |
| 86 | + await service.saveCurrentElement(secondElement); |
| 87 | + |
| 88 | + const loaded = await service.getCurrentElement(); |
| 89 | + |
| 90 | + assert.deepStrictEqual(loaded, secondElement); |
| 91 | + }); |
| 92 | +}); |
0 commit comments