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:14:24 -05:00
parent e89aa6cf2c
commit 512be9d95a
2 changed files with 49 additions and 22 deletions

View file

@ -27,11 +27,13 @@ if (!testRedisUrl) {
describe.skipIf(!testRedisUrl)('RedisCache', () => {
let cache: RedisCache;
let redisConfig: RedisConfig;
let testPrefix: string;
beforeEach(() => {
redisConfig = parseTestRedisConfig(testRedisUrl!);
testPrefix = `test:${crypto.randomUUID()}:`;
const options: CacheOptions = {
keyPrefix: 'test:',
keyPrefix: testPrefix,
ttl: 3600,
shared: false,
redisConfig,
@ -40,7 +42,14 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => {
});
afterEach(async () => {
await cache?.disconnect().catch(() => {});
if (!cache) return;
try {
await cache.clear();
} catch {
// Ignore cleanup errors if Redis is unreachable or already disconnected
} finally {
await cache.disconnect().catch(() => {});
}
});
describe('Core functionality', () => {
@ -174,7 +183,7 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => {
it('should handle errors gracefully in get', async () => {
// Force an error by using invalid JSON
const badCache = new RedisCache({
keyPrefix: 'bad:',
keyPrefix: `bad:${crypto.randomUUID()}:`,
shared: false,
redisConfig,
});
@ -188,6 +197,7 @@ describe.skipIf(!testRedisUrl)('RedisCache', () => {
const stats = badCache.getStats();
expect(stats.misses).toBe(1);
} finally {
await badCache.clear().catch(() => {});
await badCache.disconnect().catch(() => {});
}
});

View file

@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { Shutdown } from '../src/shutdown';
describe('Shutdown', () => {
@ -126,28 +126,45 @@ describe('Shutdown', () => {
});
it('should respect timeout', async () => {
let slowTimer: ReturnType<typeof setTimeout> | undefined;
const slowHandler = mock(async () => {
await new Promise(resolve => {
slowTimer = setTimeout(resolve, 2000);
});
});
let releaseHandler!: () => void;
const slowHandler = mock(
() =>
new Promise<void>(resolve => {
releaseHandler = resolve;
})
);
Shutdown.reset();
shutdown = Shutdown.getInstance({ timeout: 100, autoRegister: false });
shutdown.onShutdown(slowHandler, 10, 'Slow Handler');
let scheduledCallback: (() => void) | undefined;
let scheduledDelay: number | undefined;
const fakeTimer = {} as ReturnType<typeof setTimeout>;
const setTimeoutSpy = spyOn(globalThis, 'setTimeout').mockImplementation(((
cb: (...args: unknown[]) => void,
delay?: number
) => {
scheduledCallback = () => cb();
scheduledDelay = delay;
return fakeTimer;
}) as unknown as typeof setTimeout);
const clearTimeoutSpy = spyOn(globalThis, 'clearTimeout').mockImplementation(() => {});
const start = Date.now();
try {
await expect(shutdown.shutdown()).rejects.toThrow('Shutdown timeout');
} finally {
clearTimeout(slowTimer);
}
const duration = Date.now() - start;
Shutdown.reset();
shutdown = Shutdown.getInstance({ timeout: 100, autoRegister: false });
shutdown.onShutdown(slowHandler, 10, 'Slow Handler');
// Shutdown waits for timeout (100ms) plus some processing time
expect(duration).toBeGreaterThan(90);
expect(duration).toBeLessThan(500);
const shutdownPromise = shutdown.shutdown();
expect(scheduledDelay).toBe(100);
expect(scheduledCallback).toBeDefined();
scheduledCallback!();
await expect(shutdownPromise).rejects.toThrow('Shutdown timeout');
expect(clearTimeoutSpy).toHaveBeenCalledWith(fakeTimer);
} finally {
releaseHandler?.();
setTimeoutSpy.mockRestore();
clearTimeoutSpy.mockRestore();
}
});
});