|
1 | 1 | import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | +import os from 'os'; |
2 | 4 | import SharedStateService from '../../services/shared-state-service'; |
3 | | -import { |
4 | | - setupTestDir, cleanupTestFiles, createMockElement, TEST_SHARED_STATE_PATH, |
5 | | -} from '../test-helpers'; |
| 5 | +import { createStateV1, createStateV2, createLegacyElement } from '../factories/shared-state-factory'; |
| 6 | + |
| 7 | +jest.mock('../../logger', () => ({ |
| 8 | + debug: jest.fn(), |
| 9 | + error: jest.fn(), |
| 10 | +})); |
6 | 11 |
|
7 | 12 | describe('SharedStateService', () => { |
8 | 13 | let service: SharedStateService; |
| 14 | + let testPath: string; |
9 | 15 |
|
10 | | - beforeAll(async () => { |
11 | | - await setupTestDir(); |
| 16 | + beforeEach(async () => { |
| 17 | + testPath = path.join(os.tmpdir(), `test-${Date.now()}.json`); |
| 18 | + (SharedStateService as any).SHARED_STATE_PATH = testPath; |
| 19 | + service = new SharedStateService(); |
| 20 | + }); |
12 | 21 |
|
13 | | - // Monkey-patch the constant for testing |
14 | | - const SharedStateServiceModule = await import('../../services/shared-state-service'); |
15 | | - const SharedStateServiceClass = SharedStateServiceModule.default; |
| 22 | + afterEach(async () => { |
| 23 | + try { |
| 24 | + await fs.unlink(testPath); |
| 25 | + } catch { |
| 26 | + // ignore |
| 27 | + } |
| 28 | + }); |
16 | 29 |
|
17 | | - // Override the static constant |
18 | | - (SharedStateServiceClass as any).SHARED_STATE_PATH = TEST_SHARED_STATE_PATH; |
| 30 | + describe('saveState', () => { |
| 31 | + it('writes state to file', async () => { |
| 32 | + const state = createStateV2(); |
19 | 33 |
|
20 | | - service = new SharedStateServiceClass(); |
21 | | - }); |
| 34 | + await service.saveState(state); |
22 | 35 |
|
23 | | - afterAll(async () => { |
24 | | - await cleanupTestFiles(); |
25 | | - }); |
| 36 | + const content = await fs.readFile(testPath, 'utf8'); |
| 37 | + const parsed = JSON.parse(content); |
| 38 | + expect(parsed.stateVersion).toBe(state.stateVersion); |
| 39 | + expect(parsed.data.processedPointedDOMElement).toEqual(state.data.processedPointedDOMElement); |
| 40 | + }); |
26 | 41 |
|
27 | | - it('should save and load current element', async () => { |
28 | | - const mockElement = createMockElement(); |
29 | | - |
30 | | - await service.saveCurrentElement(mockElement); |
31 | | - const loadedElement = await service.getCurrentElement(); |
32 | | - |
33 | | - expect(loadedElement).toBeTruthy(); |
34 | | - expect(loadedElement!.selector).toBe(mockElement.selector); |
35 | | - expect(loadedElement!.tagName).toBe(mockElement.tagName); |
36 | | - expect(loadedElement!.id).toBe(mockElement.id); |
37 | | - expect(loadedElement!.classes).toEqual(mockElement.classes); |
38 | | - expect(loadedElement!.innerText).toBe(mockElement.innerText); |
39 | | - expect(loadedElement!.attributes).toEqual(mockElement.attributes); |
40 | | - expect(loadedElement!.position).toEqual(mockElement.position); |
41 | | - expect(loadedElement!.cssProperties).toEqual(mockElement.cssProperties); |
42 | | - expect(loadedElement!.url).toBe(mockElement.url); |
43 | | - expect(loadedElement!.tabId).toBe(mockElement.tabId); |
44 | | - }); |
| 42 | + it('overwrites corrupted file', async () => { |
| 43 | + await fs.writeFile(testPath, 'invalid json'); |
| 44 | + const state = createStateV1(); |
45 | 45 |
|
46 | | - it('should handle null element', async () => { |
47 | | - await service.saveCurrentElement(null); |
48 | | - const loadedElement = await service.getCurrentElement(); |
| 46 | + await service.saveState(state); |
49 | 47 |
|
50 | | - expect(loadedElement).toBeNull(); |
| 48 | + const content = await fs.readFile(testPath, 'utf8'); |
| 49 | + const parsed = JSON.parse(content); |
| 50 | + expect(parsed.stateVersion).toBe(state.stateVersion); |
| 51 | + expect(parsed.data.processedPointedDOMElement).toEqual(state.data.processedPointedDOMElement); |
| 52 | + }); |
51 | 53 | }); |
52 | 54 |
|
53 | | - it('should return null for missing file', async () => { |
54 | | - // Delete the file if it exists |
55 | | - try { |
56 | | - await fs.unlink(TEST_SHARED_STATE_PATH); |
57 | | - } catch { |
58 | | - // File doesn't exist, which is fine |
59 | | - } |
| 55 | + describe('getPointedElement', () => { |
| 56 | + it('returns processed element from v2 state', async () => { |
| 57 | + const state = createStateV2(); |
| 58 | + await fs.writeFile(testPath, JSON.stringify(state)); |
60 | 59 |
|
61 | | - const loadedElement = await service.getCurrentElement(); |
62 | | - expect(loadedElement).toBeNull(); |
63 | | - }); |
| 60 | + const result = await service.getPointedElement(); |
64 | 61 |
|
65 | | - it('should handle corrupted file gracefully', async () => { |
66 | | - // Write invalid JSON to the file |
67 | | - await fs.writeFile(TEST_SHARED_STATE_PATH, 'invalid json content'); |
| 62 | + expect(result).toEqual(state.data.processedPointedDOMElement); |
| 63 | + }); |
68 | 64 |
|
69 | | - const loadedElement = await service.getCurrentElement(); |
70 | | - expect(loadedElement).toBeNull(); |
71 | | - }); |
| 65 | + it('returns processed element from v1 state', async () => { |
| 66 | + const state = createStateV1(); |
| 67 | + await fs.writeFile(testPath, JSON.stringify(state)); |
72 | 68 |
|
73 | | - it('should save element over corrupted file', async () => { |
74 | | - // Write invalid JSON to the file |
75 | | - await fs.writeFile(TEST_SHARED_STATE_PATH, 'invalid json content'); |
| 69 | + const result = await service.getPointedElement(); |
76 | 70 |
|
77 | | - const mockElement = createMockElement(); |
78 | | - await service.saveCurrentElement(mockElement); |
| 71 | + expect(result).toEqual(state.data.processedPointedDOMElement); |
| 72 | + }); |
79 | 73 |
|
80 | | - const loadedElement = await service.getCurrentElement(); |
81 | | - expect(loadedElement).toBeTruthy(); |
82 | | - expect(loadedElement!.selector).toBe(mockElement.selector); |
83 | | - }); |
| 74 | + it('returns legacy element as-is', async () => { |
| 75 | + const legacyElement = createLegacyElement(); |
| 76 | + await fs.writeFile(testPath, JSON.stringify(legacyElement)); |
| 77 | + |
| 78 | + const result = await service.getPointedElement(); |
| 79 | + |
| 80 | + expect(result).toEqual(legacyElement); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns null for invalid json', async () => { |
| 84 | + await fs.writeFile(testPath, 'invalid json'); |
84 | 85 |
|
85 | | - it('should overwrite previous element', async () => { |
86 | | - const firstElement = createMockElement(); |
87 | | - firstElement.selector = 'div.first-element'; |
| 86 | + const result = await service.getPointedElement(); |
88 | 87 |
|
89 | | - const secondElement = createMockElement(); |
90 | | - secondElement.selector = 'div.second-element'; |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
91 | 90 |
|
92 | | - await service.saveCurrentElement(firstElement); |
93 | | - await service.saveCurrentElement(secondElement); |
| 91 | + it('returns null when file does not exist', async () => { |
| 92 | + const result = await service.getPointedElement(); |
94 | 93 |
|
95 | | - const loadedElement = await service.getCurrentElement(); |
96 | | - expect(loadedElement).toBeTruthy(); |
97 | | - expect(loadedElement!.selector).toBe('div.second-element'); |
| 94 | + expect(result).toBeNull(); |
| 95 | + }); |
98 | 96 | }); |
99 | 97 | }); |
0 commit comments