From 1df94103ea7d23e17a6cdc45c977d0afc7f933d0 Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Mon, 16 Feb 2026 15:22:38 -0800 Subject: [PATCH] test(cosmos): add init path coverage for platform-service --- .../src/lib/cosmos-init.test.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 services/platform-service/src/lib/cosmos-init.test.ts diff --git a/services/platform-service/src/lib/cosmos-init.test.ts b/services/platform-service/src/lib/cosmos-init.test.ts new file mode 100644 index 00000000..34afbe35 --- /dev/null +++ b/services/platform-service/src/lib/cosmos-init.test.ts @@ -0,0 +1,67 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +const cosmosMock = { + initializeAllContainers: vi.fn(), + registerContainers: vi.fn(), +}; + +vi.mock('@bytelyst/cosmos', () => cosmosMock); + +describe('initCosmosIfNeeded', () => { + beforeEach(() => { + vi.clearAllMocks(); + vi.unstubAllEnvs(); + }); + + afterEach(() => { + vi.resetModules(); + }); + + it('registers containers and skips init in production by default', async () => { + vi.stubEnv('COSMOS_ENDPOINT', 'https://example.documents.azure.com:443/'); + vi.stubEnv('COSMOS_KEY', 'key'); + vi.stubEnv('JWT_SECRET', 'secret'); + vi.stubEnv('NODE_ENV', 'production'); + vi.stubEnv('COSMOS_AUTO_INIT', 'false'); + + const { initCosmosIfNeeded } = await import('./cosmos-init.js'); + await initCosmosIfNeeded(); + + expect(cosmosMock.registerContainers).toHaveBeenCalledOnce(); + expect(cosmosMock.initializeAllContainers).not.toHaveBeenCalled(); + }); + + it('initializes containers outside production and logs success', async () => { + vi.stubEnv('COSMOS_ENDPOINT', 'https://example.documents.azure.com:443/'); + vi.stubEnv('COSMOS_KEY', 'key'); + vi.stubEnv('JWT_SECRET', 'secret'); + vi.stubEnv('NODE_ENV', 'test'); + + cosmosMock.initializeAllContainers.mockResolvedValue(undefined); + const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); + + const { initCosmosIfNeeded } = await import('./cosmos-init.js'); + await initCosmosIfNeeded(); + + expect(cosmosMock.registerContainers).toHaveBeenCalledOnce(); + expect(cosmosMock.initializeAllContainers).toHaveBeenCalledOnce(); + expect(infoSpy).toHaveBeenCalledWith('[platform-service] Cosmos containers ensured'); + }); + + it('logs warning when initialization fails', async () => { + vi.stubEnv('COSMOS_ENDPOINT', 'https://example.documents.azure.com:443/'); + vi.stubEnv('COSMOS_KEY', 'key'); + vi.stubEnv('JWT_SECRET', 'secret'); + vi.stubEnv('NODE_ENV', 'test'); + + cosmosMock.initializeAllContainers.mockRejectedValue(new Error('boom')); + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + const { initCosmosIfNeeded } = await import('./cosmos-init.js'); + await initCosmosIfNeeded(); + + expect(cosmosMock.registerContainers).toHaveBeenCalledOnce(); + expect(cosmosMock.initializeAllContainers).toHaveBeenCalledOnce(); + expect(warnSpy).toHaveBeenCalledWith('[platform-service] Cosmos init failed: boom'); + }); +});