From afe7d8886966ad0a2cf02afc22b46ca3976bec01 Mon Sep 17 00:00:00 2001 From: Saravana Achu Mac Date: Mon, 4 May 2026 15:19:05 -0700 Subject: [PATCH] chore(monitoring): type fetch test mocks Replace test-only any casts with a small typed fetch mock helper for partial health-check responses. Verification: pnpm --filter @bytelyst/monitoring build; pnpm --filter @bytelyst/monitoring test; pnpm --filter @bytelyst/monitoring exec eslint . --ext .ts,.tsx; pnpm lint. --- .../src/__tests__/monitoring.test.ts | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/monitoring/src/__tests__/monitoring.test.ts b/packages/monitoring/src/__tests__/monitoring.test.ts index 4bb3e624..b48f4ddf 100644 --- a/packages/monitoring/src/__tests__/monitoring.test.ts +++ b/packages/monitoring/src/__tests__/monitoring.test.ts @@ -2,6 +2,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { checkService, generateHealthReport, type ServiceTarget } from '../index.js'; +type FetchInput = Parameters[0]; +type MockFetchResult = Pick; + +function mockFetch(handler: (input: FetchInput) => Promise): void { + globalThis.fetch = vi.fn(handler) as unknown as typeof globalThis.fetch; +} + describe('monitoring', () => { const originalFetch = globalThis.fetch; @@ -14,11 +21,11 @@ describe('monitoring', () => { }); it('checkService returns healthy for 200', async () => { - globalThis.fetch = vi.fn(async () => ({ + mockFetch(async () => ({ ok: true, status: 200, json: async () => ({ status: 'ok' }), - })) as any; + })); const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' }); expect(res.status).toBe('healthy'); @@ -26,11 +33,11 @@ describe('monitoring', () => { }); it('checkService returns unhealthy for non-2xx', async () => { - globalThis.fetch = vi.fn(async () => ({ + mockFetch(async () => ({ ok: false, status: 503, json: async () => ({}), - })) as any; + })); const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' }); expect(res.status).toBe('unhealthy'); @@ -38,9 +45,9 @@ describe('monitoring', () => { }); it('checkService returns unreachable on fetch error', async () => { - globalThis.fetch = vi.fn(async () => { + mockFetch(async () => { throw new Error('network'); - }) as any; + }); const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' }); expect(res.status).toBe('unreachable'); @@ -48,9 +55,9 @@ describe('monitoring', () => { }); it('generateHealthReport sets overall=down when all unreachable', async () => { - globalThis.fetch = vi.fn(async () => { + mockFetch(async () => { throw new Error('network'); - }) as any; + }); const services: ServiceTarget[] = [ { name: 'a', url: 'http://a', path: '/health' }, @@ -63,11 +70,10 @@ describe('monitoring', () => { }); it('generateHealthReport sets overall=degraded when mixed', async () => { - const calls = vi.fn(async (url: string) => { - if (url.includes('good')) return { ok: true, status: 200, json: async () => ({}) }; + mockFetch(async url => { + if (String(url).includes('good')) return { ok: true, status: 200, json: async () => ({}) }; return { ok: false, status: 500, json: async () => ({}) }; }); - globalThis.fetch = calls as any; const services: ServiceTarget[] = [ { name: 'good', url: 'http://good', path: '/health' }, @@ -81,11 +87,11 @@ describe('monitoring', () => { }); it('generateHealthReport sets overall=healthy when all healthy', async () => { - globalThis.fetch = vi.fn(async () => ({ + mockFetch(async () => ({ ok: true, status: 200, json: async () => ({}), - })) as any; + })); const services: ServiceTarget[] = [ { name: 'a', url: 'http://a', path: '/health' },