import 'reflect-metadata'; import { MODULE_METADATA } from '@nestjs/common/constants'; import { Test } from '@nestjs/testing'; import type { TestingModule } from '@nestjs/testing'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { AVATAR_GENERATION_REPOSITORY_TOKEN, MEDIA_REPOSITORY_TOKEN, AVATAR_REPOSITORY_TOKEN } from './MediaPersistenceTokens'; describe('MediaPersistenceModule', () => { const originalEnv = { ...process.env }; afterEach(() => { process.env = originalEnv; vi.restoreAllMocks(); }); it('uses inmemory providers when GRIDPILOT_API_PERSISTENCE=inmemory', async () => { vi.resetModules(); process.env.GRIDPILOT_API_PERSISTENCE = 'inmemory'; delete process.env.DATABASE_URL; const { MediaPersistenceModule } = await import('./MediaPersistenceModule'); const { InMemoryAvatarGenerationRepository } = await import('@adapters/media/persistence/inmemory/InMemoryAvatarGenerationRepository'); const module: TestingModule = await Test.createTestingModule({ imports: [MediaPersistenceModule], }).compile(); const avatarGenRepo = module.get(AVATAR_GENERATION_REPOSITORY_TOKEN); expect(avatarGenRepo).toBeInstanceOf(InMemoryAvatarGenerationRepository); // Media and Avatar repos are mocks in inmemory mode const mediaRepo = module.get(MEDIA_REPOSITORY_TOKEN); const avatarRepo = module.get(AVATAR_REPOSITORY_TOKEN); expect(mediaRepo).toBeDefined(); expect(avatarRepo).toBeDefined(); await module.close(); }); it('uses postgres module when GRIDPILOT_API_PERSISTENCE=postgres', async () => { vi.resetModules(); process.env.GRIDPILOT_API_PERSISTENCE = 'postgres'; delete process.env.DATABASE_URL; const { MediaPersistenceModule } = await import('./MediaPersistenceModule'); const { PostgresMediaPersistenceModule } = await import('../postgres/PostgresMediaPersistenceModule'); const imports = Reflect.getMetadata(MODULE_METADATA.IMPORTS, MediaPersistenceModule) as unknown[]; expect(imports).toContain(PostgresMediaPersistenceModule); }); it('defaults to inmemory when GRIDPILOT_API_PERSISTENCE is not set', async () => { vi.resetModules(); delete process.env.GRIDPILOT_API_PERSISTENCE; delete process.env.DATABASE_URL; const { MediaPersistenceModule } = await import('./MediaPersistenceModule'); const { InMemoryMediaPersistenceModule } = await import('../inmemory/InMemoryMediaPersistenceModule'); const imports = Reflect.getMetadata(MODULE_METADATA.IMPORTS, MediaPersistenceModule) as unknown[]; expect(imports).toContain(InMemoryMediaPersistenceModule); }); });