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.
This commit is contained in:
parent
46900728d3
commit
afe7d88869
@ -2,6 +2,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|||||||
|
|
||||||
import { checkService, generateHealthReport, type ServiceTarget } from '../index.js';
|
import { checkService, generateHealthReport, type ServiceTarget } from '../index.js';
|
||||||
|
|
||||||
|
type FetchInput = Parameters<typeof globalThis.fetch>[0];
|
||||||
|
type MockFetchResult = Pick<Response, 'ok' | 'status' | 'json'>;
|
||||||
|
|
||||||
|
function mockFetch(handler: (input: FetchInput) => Promise<MockFetchResult>): void {
|
||||||
|
globalThis.fetch = vi.fn(handler) as unknown as typeof globalThis.fetch;
|
||||||
|
}
|
||||||
|
|
||||||
describe('monitoring', () => {
|
describe('monitoring', () => {
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
|
|
||||||
@ -14,11 +21,11 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('checkService returns healthy for 200', async () => {
|
it('checkService returns healthy for 200', async () => {
|
||||||
globalThis.fetch = vi.fn(async () => ({
|
mockFetch(async () => ({
|
||||||
ok: true,
|
ok: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
json: async () => ({ status: 'ok' }),
|
json: async () => ({ status: 'ok' }),
|
||||||
})) as any;
|
}));
|
||||||
|
|
||||||
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
||||||
expect(res.status).toBe('healthy');
|
expect(res.status).toBe('healthy');
|
||||||
@ -26,11 +33,11 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('checkService returns unhealthy for non-2xx', async () => {
|
it('checkService returns unhealthy for non-2xx', async () => {
|
||||||
globalThis.fetch = vi.fn(async () => ({
|
mockFetch(async () => ({
|
||||||
ok: false,
|
ok: false,
|
||||||
status: 503,
|
status: 503,
|
||||||
json: async () => ({}),
|
json: async () => ({}),
|
||||||
})) as any;
|
}));
|
||||||
|
|
||||||
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
||||||
expect(res.status).toBe('unhealthy');
|
expect(res.status).toBe('unhealthy');
|
||||||
@ -38,9 +45,9 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('checkService returns unreachable on fetch error', async () => {
|
it('checkService returns unreachable on fetch error', async () => {
|
||||||
globalThis.fetch = vi.fn(async () => {
|
mockFetch(async () => {
|
||||||
throw new Error('network');
|
throw new Error('network');
|
||||||
}) as any;
|
});
|
||||||
|
|
||||||
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
const res = await checkService({ name: 'svc', url: 'http://x', path: '/health' });
|
||||||
expect(res.status).toBe('unreachable');
|
expect(res.status).toBe('unreachable');
|
||||||
@ -48,9 +55,9 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('generateHealthReport sets overall=down when all unreachable', async () => {
|
it('generateHealthReport sets overall=down when all unreachable', async () => {
|
||||||
globalThis.fetch = vi.fn(async () => {
|
mockFetch(async () => {
|
||||||
throw new Error('network');
|
throw new Error('network');
|
||||||
}) as any;
|
});
|
||||||
|
|
||||||
const services: ServiceTarget[] = [
|
const services: ServiceTarget[] = [
|
||||||
{ name: 'a', url: 'http://a', path: '/health' },
|
{ name: 'a', url: 'http://a', path: '/health' },
|
||||||
@ -63,11 +70,10 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('generateHealthReport sets overall=degraded when mixed', async () => {
|
it('generateHealthReport sets overall=degraded when mixed', async () => {
|
||||||
const calls = vi.fn(async (url: string) => {
|
mockFetch(async url => {
|
||||||
if (url.includes('good')) return { ok: true, status: 200, json: async () => ({}) };
|
if (String(url).includes('good')) return { ok: true, status: 200, json: async () => ({}) };
|
||||||
return { ok: false, status: 500, json: async () => ({}) };
|
return { ok: false, status: 500, json: async () => ({}) };
|
||||||
});
|
});
|
||||||
globalThis.fetch = calls as any;
|
|
||||||
|
|
||||||
const services: ServiceTarget[] = [
|
const services: ServiceTarget[] = [
|
||||||
{ name: 'good', url: 'http://good', path: '/health' },
|
{ name: 'good', url: 'http://good', path: '/health' },
|
||||||
@ -81,11 +87,11 @@ describe('monitoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('generateHealthReport sets overall=healthy when all healthy', async () => {
|
it('generateHealthReport sets overall=healthy when all healthy', async () => {
|
||||||
globalThis.fetch = vi.fn(async () => ({
|
mockFetch(async () => ({
|
||||||
ok: true,
|
ok: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
json: async () => ({}),
|
json: async () => ({}),
|
||||||
})) as any;
|
}));
|
||||||
|
|
||||||
const services: ServiceTarget[] = [
|
const services: ServiceTarget[] = [
|
||||||
{ name: 'a', url: 'http://a', path: '/health' },
|
{ name: 'a', url: 'http://a', path: '/health' },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user