Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test: move telegram/errorHandler test cleanup into afterEach for isolation (#2550)
Adam Eivy91aac8723e58
diff --git a/server/lib/errorHandler.test.js b/server/lib/errorHandler.test.jsindex 5131025ff2..cbb21b005e 100644--- a/server/lib/errorHandler.test.js+++ b/server/lib/errorHandler.test.js@@ -252,22 +252,32 @@ describe('errorHandler.js', () => {// re-throws the argument (the classic footgun). Production always has// subscribers (autoFixer / socket.js); keep a noop attached so emitErrorEvent// doesn't throw out of asyncHandler's catch during these tests.+ // Cleanup lives in afterEach (not test bodies) so a failing assertion can't+ // leak the noop listener, any test-registered listener, or the console.error+ // spy into sibling tests.let noopErrorListener;+ let errorSpy;+ let testListeners;+ // Register an errorEvents 'error' listener that this describe will remove.+ const trackListener = (fn) => { testListeners.push(fn); errorEvents.on('error', fn); };+beforeEach(() => {+ testListeners = [];noopErrorListener = () => {};errorEvents.on('error', noopErrorListener);+ errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});});afterEach(() => {errorEvents.off('error', noopErrorListener);+ for (const fn of testListeners) errorEvents.off('error', fn);+ errorSpy.mockRestore();});it('catches a thrown ServerError, emits, and responds with status + body', async () => {const io = { emit: vi.fn() };const { req, res } = makeReqRes(io);const events = [];- const listener = (err, ctx) => events.push({ err, ctx });- errorEvents.on('error', listener);- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ trackListener((err, ctx) => events.push({ err, ctx }));const handler = asyncHandler(async () => {throw new ServerError('nope', { status: 403, code: 'FORBIDDEN', context: { detail: 'x' } });@@ -275,9 +285,6 @@ describe('errorHandler.js', () => {await handler(req, res, vi.fn());await flushMicrotasks();- errorEvents.off('error', listener);- errorSpy.mockRestore();-expect(res.status).toHaveBeenCalledWith(403);const body = res.json.mock.calls[0][0];expect(body.error).toBe('nope');@@ -295,7 +302,6 @@ describe('errorHandler.js', () => {it('normalizes a plain thrown Error to a 500 INTERNAL_ERROR response', async () => {const io = { emit: vi.fn() };const { req, res } = makeReqRes(io);- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});const handler = asyncHandler(async () => {throw new Error('kaboom');@@ -303,8 +309,6 @@ describe('errorHandler.js', () => {await handler(req, res, vi.fn());await flushMicrotasks();- errorSpy.mockRestore();-expect(res.status).toHaveBeenCalledWith(500);const body = res.json.mock.calls[0][0];expect(body.code).toBe('INTERNAL_ERROR');@@ -324,7 +328,6 @@ describe('errorHandler.js', () => {it('still responds when no io is registered on the app', async () => {const { req, res } = makeReqRes(null);- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});const handler = asyncHandler(async () => {throw new ServerError('missing', { status: 404 });@@ -332,8 +335,6 @@ describe('errorHandler.js', () => {await handler(req, res, vi.fn());await flushMicrotasks();- errorSpy.mockRestore();-expect(res.status).toHaveBeenCalledWith(404);// status→code derivation still runs even without an io channel.expect(res.json.mock.calls[0][0].code).toBe('NOT_FOUND');diff --git a/server/lib/telegramClient.test.js b/server/lib/telegramClient.test.jsindex c965027480..711f02f8f8 100644--- a/server/lib/telegramClient.test.js+++ b/server/lib/telegramClient.test.js@@ -43,6 +43,10 @@ describe('createTelegramBot — async handler rejections', () => {let onUnhandled;let originalFetch;let errorSpy;+ let bots;++ // Track polling bots so afterEach stops them even if an assertion throws first.+ const track = (bot) => { bots.push(bot); return bot; };beforeEach(() => {unhandled = null;@@ -50,9 +54,11 @@ describe('createTelegramBot — async handler rejections', () => {process.on('unhandledRejection', onUnhandled);originalFetch = global.fetch;errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ bots = [];});- afterEach(() => {+ afterEach(async () => {+ for (const bot of bots) await bot.stopPolling().catch(() => {});process.off('unhandledRejection', onUnhandled);global.fetch = originalFetch;errorSpy.mockRestore();@@ -60,7 +66,7 @@ describe('createTelegramBot — async handler rejections', () => {it('does not leak an unhandled rejection when an onText handler rejects', async () => {global.fetch = makeFetchMock();- const bot = createTelegramBot('test-token', { polling: true });+ const bot = track(createTelegramBot('test-token', { polling: true }));let called = false;bot.onText(/\/boom/, async () => {@@ -81,7 +87,7 @@ describe('createTelegramBot — async handler rejections', () => {it("does not leak an unhandled rejection when an 'message' listener rejects", async () => {global.fetch = makeFetchMock();- const bot = createTelegramBot('test-token', { polling: true });+ const bot = track(createTelegramBot('test-token', { polling: true }));let called = false;bot.on('message', async () => {