fix: stabilize competition results flow

This commit is contained in:
dahyeon committed 2026-07-13 13:55:08 +00:00
1 parent 3bd3120eb1
commit a88562b208
20 files changed
+411 -65

No files matched your search

@@ -1,9 +1,75 @@
import mongoose from 'mongoose';
import {
ConnectionService,
resolveMongoAppName,
resolveMongoConnectionOptions,
resolveMongoDbName,
} from './connection.service';
function createFakeConnection(connectionId: string) {
const models: Record<string, unknown> = {};
return {
readyState: mongoose.ConnectionStates.connected,
models,
model: jest.fn((name: string) => {
const model = { connectionId, modelName: name };
models[name] = model;
return model;
}),
};
}
describe('ConnectionService', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('shares one MongoDB connection across concurrent initialization calls', async () => {
const connection = createFakeConnection('shared');
const createConnection = jest
.spyOn(mongoose, 'createConnection')
.mockReturnValue({
asPromise: jest.fn().mockResolvedValue(connection),
} as never);
const service = new ConnectionService({
get: jest.fn().mockResolvedValue('mongodb://localhost/test'),
} as never);
const [first, second] = await Promise.all([
service.getConnection(),
service.getConnection(),
]);
expect(first).toBe(connection);
expect(second).toBe(connection);
expect(createConnection).toHaveBeenCalledTimes(1);
});
it('rebuilds models from the replacement connection after disconnect', async () => {
const firstConnection = createFakeConnection('first');
const secondConnection = createFakeConnection('second');
jest
.spyOn(mongoose, 'createConnection')
.mockReturnValueOnce({
asPromise: jest.fn().mockResolvedValue(firstConnection),
} as never)
.mockReturnValueOnce({
asPromise: jest.fn().mockResolvedValue(secondConnection),
} as never);
const service = new ConnectionService({
get: jest.fn().mockResolvedValue('mongodb://localhost/test'),
} as never);
const firstModel = await service.getModel('match');
firstConnection.readyState = mongoose.ConnectionStates.disconnected;
const secondModel = await service.getModel('match');
expect(firstModel).toMatchObject({ connectionId: 'first' });
expect(secondModel).toMatchObject({ connectionId: 'second' });
expect(secondModel).not.toBe(firstModel);
});
});
describe('resolveMongoDbName', () => {
it('uses production database only for prod', () => {
expect(resolveMongoDbName('prod')).toBe('lolympus');