|
| 1 | +// @vitest-environment node |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { createRequire } from 'module'; |
| 4 | + |
| 5 | +// ─── Setup CJS mocks via require cache ─────────────────────────────────────── |
| 6 | +const require_ = createRequire(import.meta.url); |
| 7 | +const readFileSyncMock = vi.fn(); |
| 8 | +const handleMock = vi.fn(); |
| 9 | +const mockAppDelete = vi.fn().mockResolvedValue(undefined); |
| 10 | +const mockSettings = vi.fn(); |
| 11 | + |
| 12 | +// Inject electron mock into require cache |
| 13 | +require_.cache[require_.resolve('electron')] = { |
| 14 | + id: 'electron', |
| 15 | + filename: require_.resolve('electron'), |
| 16 | + loaded: true, |
| 17 | + exports: { |
| 18 | + ipcMain: { handle: handleMock }, |
| 19 | + dialog: { showOpenDialog: vi.fn() }, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +// Inject fs mock |
| 24 | +require_.cache[require_.resolve('fs')] = { |
| 25 | + id: 'fs', |
| 26 | + filename: require_.resolve('fs'), |
| 27 | + loaded: true, |
| 28 | + exports: { |
| 29 | + readFileSync: readFileSyncMock, |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +// Inject firebase-admin mock |
| 34 | +const firebaseAdminPath = require_.resolve('firebase-admin'); |
| 35 | +require_.cache[firebaseAdminPath] = { |
| 36 | + id: 'firebase-admin', |
| 37 | + filename: firebaseAdminPath, |
| 38 | + loaded: true, |
| 39 | + exports: { |
| 40 | + initializeApp: vi.fn(), |
| 41 | + credential: { cert: vi.fn().mockReturnValue('mock-credential') }, |
| 42 | + firestore: vi.fn().mockReturnValue({ settings: mockSettings }), |
| 43 | + app: vi.fn().mockReturnValue({ delete: mockAppDelete }), |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +// Now load the controller — it will pick up our cached mocks |
| 48 | +// We must delete any cached version first |
| 49 | +const controllerPath = require_.resolve('./firebaseController'); |
| 50 | +delete require_.cache[controllerPath]; |
| 51 | +const { registerHandlers } = require_(controllerPath); |
| 52 | +registerHandlers(); |
| 53 | + |
| 54 | +// Capture the handler functions |
| 55 | +const handlers = {}; |
| 56 | +for (const [channel, handler] of handleMock.mock.calls) { |
| 57 | + handlers[channel] = handler; |
| 58 | +} |
| 59 | + |
| 60 | +describe('firebaseController', () => { |
| 61 | + beforeEach(() => { |
| 62 | + readFileSyncMock.mockReset(); |
| 63 | + mockAppDelete.mockClear(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('connects with a valid service account path', async () => { |
| 67 | + const serviceAccount = { project_id: 'test-project' }; |
| 68 | + readFileSyncMock.mockReturnValue(JSON.stringify(serviceAccount)); |
| 69 | + |
| 70 | + const result = await handlers['firebase:connect'](null, { |
| 71 | + serviceAccountPath: '/path/to/sa.json', |
| 72 | + }); |
| 73 | + |
| 74 | + expect(result).toEqual({ success: true, projectId: 'test-project', databaseId: undefined }); |
| 75 | + expect(readFileSyncMock).toHaveBeenCalledWith('/path/to/sa.json', 'utf8'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('connects with databaseId', async () => { |
| 79 | + const serviceAccount = { project_id: 'test-project' }; |
| 80 | + readFileSyncMock.mockReturnValue(JSON.stringify(serviceAccount)); |
| 81 | + |
| 82 | + const result = await handlers['firebase:connect'](null, { |
| 83 | + serviceAccountPath: '/path/to/sa.json', |
| 84 | + databaseId: 'my-database', |
| 85 | + }); |
| 86 | + |
| 87 | + expect(result).toEqual({ success: true, projectId: 'test-project', databaseId: 'my-database' }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('supports backward compat with string param', async () => { |
| 91 | + const serviceAccount = { project_id: 'legacy-project' }; |
| 92 | + readFileSyncMock.mockReturnValue(JSON.stringify(serviceAccount)); |
| 93 | + |
| 94 | + const result = await handlers['firebase:connect'](null, '/legacy/path.json'); |
| 95 | + |
| 96 | + expect(result.success).toBe(true); |
| 97 | + expect(result.projectId).toBe('legacy-project'); |
| 98 | + expect(result.databaseId).toBeUndefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns error for invalid file', async () => { |
| 102 | + readFileSyncMock.mockImplementation(() => { |
| 103 | + throw new Error('ENOENT: no such file'); |
| 104 | + }); |
| 105 | + |
| 106 | + const result = await handlers['firebase:connect'](null, { |
| 107 | + serviceAccountPath: '/bad/path.json', |
| 108 | + }); |
| 109 | + |
| 110 | + expect(result.success).toBe(false); |
| 111 | + expect(result.error).toContain('ENOENT'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('disconnects when connected', async () => { |
| 115 | + const serviceAccount = { project_id: 'test-project' }; |
| 116 | + readFileSyncMock.mockReturnValue(JSON.stringify(serviceAccount)); |
| 117 | + |
| 118 | + await handlers['firebase:connect'](null, { serviceAccountPath: '/path/to/sa.json' }); |
| 119 | + const result = await handlers['firebase:disconnect'](); |
| 120 | + |
| 121 | + expect(result).toEqual({ success: true }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('disconnects gracefully when not connected', async () => { |
| 125 | + const result = await handlers['firebase:disconnect'](); |
| 126 | + |
| 127 | + expect(result).toEqual({ success: true }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments