T-16: M0.1b — Make repo-wide bun test green: fix queue, shutdown-signal and research-workflow test failures
This commit is contained in:
parent
890761e0d5
commit
9c4e87cdfd
17 changed files with 366 additions and 266 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { asFunction, createContainer, type AwilixContainer } from 'awilix';
|
||||
import { beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import type { HandlerRegistry } from '@stock-bot/handler-registry';
|
||||
import * as logger from '@stock-bot/logger';
|
||||
import type { ExecutionContext, IHandler } from '@stock-bot/types';
|
||||
|
|
@ -55,6 +55,7 @@ describe('HandlerScanner', () => {
|
|||
let mockRegistry: HandlerRegistry;
|
||||
let container: AwilixContainer;
|
||||
let mockLogger: any;
|
||||
let getLoggerSpy: ReturnType<typeof spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create mock logger
|
||||
|
|
@ -63,10 +64,11 @@ describe('HandlerScanner', () => {
|
|||
debug: mock(() => {}),
|
||||
error: mock(() => {}),
|
||||
warn: mock(() => {}),
|
||||
trace: mock(() => {}),
|
||||
};
|
||||
|
||||
// Mock getLogger to return our mock logger
|
||||
spyOn(logger, 'getLogger').mockReturnValue(mockLogger);
|
||||
getLoggerSpy = spyOn(logger, 'getLogger').mockReturnValue(mockLogger);
|
||||
|
||||
// Create mock registry
|
||||
mockRegistry = {
|
||||
|
|
@ -87,6 +89,10 @@ describe('HandlerScanner', () => {
|
|||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
getLoggerSpy?.mockRestore();
|
||||
});
|
||||
|
||||
describe('scanHandlers', () => {
|
||||
it('should handle empty patterns gracefully', async () => {
|
||||
await scanner.scanHandlers([]);
|
||||
|
|
|
|||
|
|
@ -1,21 +1,28 @@
|
|||
import { asFunction, asValue, createContainer } from 'awilix';
|
||||
import { describe, expect, it, mock } from 'bun:test';
|
||||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import * as queueModule from '@stock-bot/queue';
|
||||
import {
|
||||
registerApplicationServices,
|
||||
registerCacheServices,
|
||||
registerDatabaseServices,
|
||||
} from '../src/registrations';
|
||||
|
||||
// Mock the queue module
|
||||
mock.module('@stock-bot/queue', () => ({
|
||||
createServiceCache: mock(() => ({
|
||||
get: mock(() => Promise.resolve(null)),
|
||||
set: mock(() => Promise.resolve()),
|
||||
del: mock(() => Promise.resolve()),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('DI Registrations', () => {
|
||||
let createServiceCacheSpy: ReturnType<typeof spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
createServiceCacheSpy = spyOn(queueModule, 'createServiceCache').mockImplementation(
|
||||
(() => ({
|
||||
get: mock(() => Promise.resolve(null)),
|
||||
set: mock(() => Promise.resolve()),
|
||||
del: mock(() => Promise.resolve()),
|
||||
})) as any
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
createServiceCacheSpy?.mockRestore();
|
||||
});
|
||||
describe('registerCacheServices', () => {
|
||||
it('should register null cache when redis disabled', () => {
|
||||
const container = createContainer();
|
||||
|
|
@ -108,22 +115,6 @@ describe('DI Registrations', () => {
|
|||
it('should register MongoDB when config exists', () => {
|
||||
const container = createContainer();
|
||||
|
||||
// Mock MongoDB client
|
||||
const mockMongoClient = {
|
||||
connect: mock(() => Promise.resolve()),
|
||||
disconnect: mock(() => Promise.resolve()),
|
||||
getDb: mock(() => ({})),
|
||||
};
|
||||
|
||||
// Mock the MongoDB factory
|
||||
mock.module('@stock-bot/mongodb', () => ({
|
||||
MongoDBClient: class {
|
||||
constructor() {
|
||||
return mockMongoClient;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const config = {
|
||||
mongodb: {
|
||||
enabled: true,
|
||||
|
|
@ -140,22 +131,6 @@ describe('DI Registrations', () => {
|
|||
it('should register PostgreSQL when config exists', () => {
|
||||
const container = createContainer();
|
||||
|
||||
// Mock Postgres client
|
||||
const mockPostgresClient = {
|
||||
connect: mock(() => Promise.resolve()),
|
||||
disconnect: mock(() => Promise.resolve()),
|
||||
query: mock(() => Promise.resolve({ rows: [] })),
|
||||
};
|
||||
|
||||
// Mock the Postgres factory
|
||||
mock.module('@stock-bot/postgres', () => ({
|
||||
PostgresClient: class {
|
||||
constructor() {
|
||||
return mockPostgresClient;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const config = {
|
||||
postgres: {
|
||||
enabled: true,
|
||||
|
|
@ -175,22 +150,6 @@ describe('DI Registrations', () => {
|
|||
it('should register QuestDB when config exists', () => {
|
||||
const container = createContainer();
|
||||
|
||||
// Mock QuestDB client
|
||||
const mockQuestdbClient = {
|
||||
connect: mock(() => Promise.resolve()),
|
||||
disconnect: mock(() => Promise.resolve()),
|
||||
query: mock(() => Promise.resolve({ data: [] })),
|
||||
};
|
||||
|
||||
// Mock the QuestDB factory
|
||||
mock.module('@stock-bot/questdb', () => ({
|
||||
QuestDBClient: class {
|
||||
constructor() {
|
||||
return mockQuestdbClient;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const config = {
|
||||
questdb: {
|
||||
enabled: true,
|
||||
|
|
@ -281,21 +240,6 @@ describe('DI Registrations', () => {
|
|||
}),
|
||||
});
|
||||
|
||||
// Mock queue manager
|
||||
const mockQueueManager = {
|
||||
getQueue: mock(() => ({})),
|
||||
startAllWorkers: mock(() => {}),
|
||||
shutdown: mock(() => Promise.resolve()),
|
||||
};
|
||||
|
||||
mock.module('@stock-bot/queue', () => ({
|
||||
QueueManager: class {
|
||||
constructor() {
|
||||
return mockQueueManager;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const config = {
|
||||
service: {
|
||||
name: 'test-service',
|
||||
|
|
|
|||
|
|
@ -1,25 +1,23 @@
|
|||
import { afterEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import type { BaseAppConfig } from '@stock-bot/config';
|
||||
import * as loggerModule from '@stock-bot/logger';
|
||||
import { Shutdown } from '@stock-bot/shutdown';
|
||||
import { ServiceApplication } from '../src/service-application';
|
||||
import type { ServiceApplicationConfig, ServiceLifecycleHooks } from '../src/service-application';
|
||||
|
||||
// Mock logger module
|
||||
const mockLogger = {
|
||||
// Mock logger object used via spyOn in beforeEach (restored in afterEach)
|
||||
const mockLogger: any = {
|
||||
info: mock(() => {}),
|
||||
error: mock(() => {}),
|
||||
warn: mock(() => {}),
|
||||
debug: mock(() => {}),
|
||||
child: mock(() => mockLogger),
|
||||
trace: mock(() => {}),
|
||||
};
|
||||
mockLogger.child = mock(() => mockLogger);
|
||||
|
||||
mock.module('@stock-bot/logger', () => ({
|
||||
getLogger: () => mockLogger,
|
||||
setLoggerConfig: mock(() => {}),
|
||||
shutdownLoggers: mock(() => Promise.resolve()),
|
||||
}));
|
||||
|
||||
// Mock shutdown module
|
||||
// Mock shutdown instance used via spyOn(Shutdown, 'getInstance') in beforeEach
|
||||
const mockShutdownInstance = {
|
||||
shutdown: mock(() => Promise.resolve()),
|
||||
onShutdown: mock(() => {}),
|
||||
onShutdownHigh: mock(() => {}),
|
||||
onShutdownMedium: mock(() => {}),
|
||||
|
|
@ -30,24 +28,13 @@ const mockShutdownInstance = {
|
|||
executeCallbacks: mock(() => Promise.resolve()),
|
||||
};
|
||||
|
||||
const mockShutdown = {
|
||||
getInstance: mock(() => mockShutdownInstance),
|
||||
};
|
||||
|
||||
mock.module('@stock-bot/shutdown', () => ({
|
||||
Shutdown: mockShutdown,
|
||||
}));
|
||||
|
||||
// Mock Bun.serve
|
||||
// Mock server returned by spyOn(Bun, 'serve')
|
||||
const mockServer = {
|
||||
stop: mock(() => {}),
|
||||
port: 3000,
|
||||
hostname: '0.0.0.0',
|
||||
};
|
||||
|
||||
const _originalBunServe = Bun.serve;
|
||||
Bun.serve = mock(() => mockServer);
|
||||
|
||||
const mockConfig: BaseAppConfig = {
|
||||
name: 'test-service',
|
||||
version: '1.0.0',
|
||||
|
|
@ -75,13 +62,44 @@ const mockConfig: BaseAppConfig = {
|
|||
|
||||
describe('ServiceApplication', () => {
|
||||
let app: ServiceApplication;
|
||||
let getLoggerSpy: ReturnType<typeof spyOn>;
|
||||
let setLoggerConfigSpy: ReturnType<typeof spyOn>;
|
||||
let shutdownLoggersSpy: ReturnType<typeof spyOn>;
|
||||
let shutdownGetInstanceSpy: ReturnType<typeof spyOn>;
|
||||
let bunServeSpy: ReturnType<typeof spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
getLoggerSpy = spyOn(loggerModule, 'getLogger').mockReturnValue(mockLogger);
|
||||
setLoggerConfigSpy = spyOn(loggerModule, 'setLoggerConfig').mockImplementation(() => {});
|
||||
shutdownLoggersSpy = spyOn(loggerModule, 'shutdownLoggers').mockResolvedValue(undefined);
|
||||
shutdownGetInstanceSpy = spyOn(Shutdown, 'getInstance').mockReturnValue(
|
||||
mockShutdownInstance as any
|
||||
);
|
||||
bunServeSpy = spyOn(Bun, 'serve').mockImplementation((() => mockServer) as any);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up app if it exists before restoring spies
|
||||
if (app) {
|
||||
app.stop().catch(() => {});
|
||||
app = null as any;
|
||||
}
|
||||
|
||||
// Restore module/global spies so no state leaks to other test files
|
||||
getLoggerSpy?.mockRestore();
|
||||
setLoggerConfigSpy?.mockRestore();
|
||||
shutdownLoggersSpy?.mockRestore();
|
||||
shutdownGetInstanceSpy?.mockRestore();
|
||||
bunServeSpy?.mockRestore();
|
||||
|
||||
// Reset mocks
|
||||
mockLogger.info.mockReset();
|
||||
mockLogger.error.mockReset();
|
||||
mockLogger.warn.mockReset();
|
||||
mockLogger.debug.mockReset();
|
||||
mockLogger.trace.mockReset();
|
||||
mockShutdownInstance.shutdown.mockReset();
|
||||
mockShutdownInstance.shutdown.mockImplementation(() => Promise.resolve());
|
||||
mockShutdownInstance.onShutdown.mockReset();
|
||||
mockShutdownInstance.onShutdownHigh.mockReset();
|
||||
mockShutdownInstance.onShutdownMedium.mockReset();
|
||||
|
|
@ -90,12 +108,6 @@ describe('ServiceApplication', () => {
|
|||
mockShutdownInstance.registerAsync.mockReset();
|
||||
mockShutdownInstance.handleTermination.mockReset();
|
||||
mockShutdownInstance.executeCallbacks.mockReset();
|
||||
|
||||
// Clean up app if it exists
|
||||
if (app) {
|
||||
app.stop().catch(() => {});
|
||||
app = null as any;
|
||||
}
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
|
|
@ -136,7 +148,7 @@ describe('ServiceApplication', () => {
|
|||
};
|
||||
|
||||
app = new ServiceApplication(mockConfig, serviceConfig);
|
||||
expect(mockShutdown.getInstance).toHaveBeenCalledWith({
|
||||
expect(shutdownGetInstanceSpy).toHaveBeenCalledWith({
|
||||
timeout: 30000,
|
||||
});
|
||||
});
|
||||
|
|
@ -605,19 +617,14 @@ describe('ServiceApplication', () => {
|
|||
|
||||
describe('stop method', () => {
|
||||
it('should trigger shutdown', async () => {
|
||||
const mockShutdownInstance = {
|
||||
const localShutdownInstance = {
|
||||
shutdown: mock(() => Promise.resolve()),
|
||||
onShutdown: mock(() => {}),
|
||||
onShutdownHigh: mock(() => {}),
|
||||
onShutdownMedium: mock(() => {}),
|
||||
onShutdownLow: mock(() => {}),
|
||||
};
|
||||
|
||||
mock.module('@stock-bot/shutdown', () => ({
|
||||
Shutdown: {
|
||||
getInstance: () => mockShutdownInstance,
|
||||
},
|
||||
}));
|
||||
shutdownGetInstanceSpy.mockReturnValue(localShutdownInstance as any);
|
||||
|
||||
const serviceConfig: ServiceApplicationConfig = {
|
||||
serviceName: 'test-service',
|
||||
|
|
@ -627,7 +634,7 @@ describe('ServiceApplication', () => {
|
|||
|
||||
await app.stop();
|
||||
|
||||
expect(mockShutdownInstance.shutdown).toHaveBeenCalled();
|
||||
expect(localShutdownInstance.shutdown).toHaveBeenCalled();
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('Stopping test-service service...');
|
||||
});
|
||||
});
|
||||
|
|
@ -665,19 +672,14 @@ describe('ServiceApplication', () => {
|
|||
|
||||
describe('shutdown handlers', () => {
|
||||
it('should register all shutdown handlers during start', async () => {
|
||||
const mockShutdownInstance = {
|
||||
const localShutdownInstance = {
|
||||
shutdown: mock(() => Promise.resolve()),
|
||||
onShutdown: mock(() => {}),
|
||||
onShutdownHigh: mock(() => {}),
|
||||
onShutdownMedium: mock(() => {}),
|
||||
onShutdownLow: mock(() => {}),
|
||||
};
|
||||
|
||||
mock.module('@stock-bot/shutdown', () => ({
|
||||
Shutdown: {
|
||||
getInstance: () => mockShutdownInstance,
|
||||
},
|
||||
}));
|
||||
shutdownGetInstanceSpy.mockReturnValue(localShutdownInstance as any);
|
||||
|
||||
const serviceConfig: ServiceApplicationConfig = {
|
||||
serviceName: 'test-service',
|
||||
|
|
@ -731,10 +733,10 @@ describe('ServiceApplication', () => {
|
|||
);
|
||||
|
||||
// Should have registered shutdown handlers
|
||||
expect(mockShutdownInstance.onShutdown).toHaveBeenCalledTimes(5); // Queue, HTTP, Custom, Services, Loggers
|
||||
expect(localShutdownInstance.onShutdown).toHaveBeenCalledTimes(5); // Queue, HTTP, Custom, Services, Loggers
|
||||
|
||||
// Test the handlers by calling them
|
||||
const shutdownHandlers = (mockShutdownInstance.onShutdown as any).mock.calls;
|
||||
const shutdownHandlers = (localShutdownInstance.onShutdown as any).mock.calls;
|
||||
|
||||
// Find and execute queue shutdown handler (priority 9)
|
||||
const queueHandler = shutdownHandlers.find(call => call[2] === 'Queue System');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue