|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +// Hoisted admin mock & dotenv noop |
| 4 | +const adminMock = vi.hoisted(() => ({ |
| 5 | + instanceId: vi.fn(() => ({ |
| 6 | + app: { options: { projectId: 'mock-project' } } |
| 7 | + })) |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('dotenv', () => ({ config: vi.fn() })); |
| 11 | + |
| 12 | +vi.mock('firebase-admin', () => ({ |
| 13 | + default: { |
| 14 | + instanceId: adminMock.instanceId |
| 15 | + }, |
| 16 | + instanceId: adminMock.instanceId |
| 17 | +})); |
| 18 | + |
| 19 | +const envBackup: NodeJS.ProcessEnv = { ...process.env }; |
| 20 | + |
| 21 | +describe('config.ts', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.resetModules(); |
| 24 | + Object.assign(process.env, { |
| 25 | + SUUNTOAPP_CLIENT_ID: 'suunto-id', |
| 26 | + SUUNTOAPP_CLIENT_SECRET: 'suunto-secret', |
| 27 | + SUUNTOAPP_SUBSCRIPTION_KEY: 'suunto-sub', |
| 28 | + COROSAPI_CLIENT_ID: 'coros-id', |
| 29 | + COROSAPI_CLIENT_SECRET: 'coros-secret', |
| 30 | + GARMINAPI_CLIENT_ID: 'garmin-id', |
| 31 | + GARMINAPI_CLIENT_SECRET: 'garmin-secret', |
| 32 | + }); |
| 33 | + delete process.env.GCLOUD_PROJECT; // force fallback to admin.instanceId |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + process.env = { ...envBackup }; |
| 38 | + vi.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns configured values and derives cloudtasks defaults from admin project', async () => { |
| 42 | + const { config } = await import('./config'); |
| 43 | + |
| 44 | + expect(config.suuntoapp.client_id).toBe('suunto-id'); |
| 45 | + expect(config.suuntoapp.subscription_key).toBe('suunto-sub'); |
| 46 | + expect(config.corosapi.client_secret).toBe('coros-secret'); |
| 47 | + expect(config.garminapi.client_id).toBe('garmin-id'); |
| 48 | + |
| 49 | + expect(config.cloudtasks.projectId).toBe('mock-project'); |
| 50 | + expect(config.cloudtasks.serviceAccountEmail).toBe('[email protected]'); |
| 51 | + expect(config.debug.bucketName).toBe('quantified-self-io-debug-files'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('throws when a required env var is missing', async () => { |
| 55 | + delete process.env.SUUNTOAPP_CLIENT_ID; |
| 56 | + const { config } = await import('./config'); |
| 57 | + |
| 58 | + expect(() => config.suuntoapp.client_id).toThrow(/Missing required environment variable: SUUNTOAPP_CLIENT_ID/); |
| 59 | + }); |
| 60 | +}); |
0 commit comments