test(cosmos): add init path coverage for platform-service
This commit is contained in:
parent
e3f173c164
commit
1df94103ea
67
services/platform-service/src/lib/cosmos-init.test.ts
Normal file
67
services/platform-service/src/lib/cosmos-init.test.ts
Normal file
@ -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');
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user