stock-bot/libs/core/di/test/container-builder.test.ts

116 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'bun:test';
import { ZodError } from 'zod';
import { baseAppSchema } from '@stock-bot/config';
import { ServiceContainerBuilder } from '../src/container/builder';
// Production schemas and registrations, with initialization disabled: no module
// mocks, environment credentials, sockets or a second config implementation.
function configuration() {
return baseAppSchema.parse({
name: 'di-test',
version: '1.0.0',
environment: 'test',
service: { name: 'TestService', port: 3000 },
log: { level: 'fatal' },
database: {
dragonfly: { enabled: false },
mongodb: { enabled: false, uri: 'mongodb://localhost/test', database: 'fixture' },
postgres: { enabled: false, database: 'fixture', user: 'fixture', password: 'fixture' },
questdb: { enabled: false, ilpPort: 19009 },
},
});
}
async function compose(config = configuration()) {
return new ServiceContainerBuilder().withConfig(config).skipInitialization().build();
}
describe('ServiceContainerBuilder production configuration', () => {
it('normalizes nested configuration and registers real services without connecting', async () => {
const container = await compose();
try {
const config = container.resolve('config');
expect(config.service?.serviceName).toBe('test-service');
expect(config.mongodb.database).toBe('fixture');
expect(config.postgres.database).toBe('fixture');
expect(config.questdb?.influxPort).toBe(19009);
for (const name of ['cache', 'mongoClient', 'postgresClient', 'questdbClient']) {
expect(container.resolve(name)).toBeNull();
}
expect(container.resolve('handlerRegistry')).toBeDefined();
expect(container.resolve('handlerScanner')).toBeDefined();
expect(container.hasRegistration('serviceContainer')).toBe(true);
} finally {
await container.dispose();
}
});
it('uses flat database settings in preference to nested ones', async () => {
const config = {
...configuration(),
redis: { enabled: false, host: 'flat.test', port: 16379, db: 2 },
};
const container = await compose(config);
try {
expect(container.resolve('config').redis).toMatchObject(config.redis);
} finally {
await container.dispose();
}
});
it('keeps optional service defaults and honors disabled optional services', async () => {
const defaults = await compose();
try {
expect(defaults.resolve('config').queue).toMatchObject({
enabled: true,
workers: 1,
concurrency: 1,
});
expect(defaults.resolve('config').browser).toMatchObject({ headless: true, timeout: 30000 });
} finally {
await defaults.dispose();
}
const container = await new ServiceContainerBuilder()
.withConfig(configuration())
.withOptions({
enableQueue: false,
enableBrowser: false,
enableProxy: false,
enableQuestDB: false,
})
.skipInitialization()
.build();
try {
const config = container.resolve('config');
expect(config.queue).toBeUndefined();
expect(config.browser).toBeUndefined();
expect(config.proxy).toBeUndefined();
expect(config.questdb).toBeUndefined();
} finally {
await container.dispose();
}
});
it('uses the last complete configuration when the builder is reused', async () => {
const builder = new ServiceContainerBuilder().withConfig(configuration());
const next = { ...configuration(), service: { name: 'replacement', port: 3100 } };
const container = await builder.withConfig(next).skipInitialization().build();
try {
expect(container.resolve('config').service?.name).toBe('replacement');
} finally {
await container.dispose();
}
});
it('rejects invalid settings and missing required app fields with schema errors', () => {
expect(() =>
new ServiceContainerBuilder().withConfig({
...configuration(),
redis: { enabled: 'wrong' },
} as never)
).toThrow(ZodError);
expect(() =>
new ServiceContainerBuilder().withConfig({ service: { name: 'incomplete' } } as never)
).toThrow(ZodError);
});
});