T-16: M0.1b — Make repo-wide bun test green: fix queue, shutdown-signal and research-workflow test failures

This commit is contained in:
Studio 2026-11-27 12:01:06 -05:00
parent 890761e0d5
commit 9c4e87cdfd
17 changed files with 366 additions and 266 deletions

View file

@ -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');