test(telemetry): Phase 3 regression tests — UpdateClusterSchema, ClusterStatusEnum, extractClientIp (614→624 tests)

This commit is contained in:
saravanakumardb1 2026-02-17 11:24:59 -08:00
parent 788ee57935
commit 51e2ecdec8
2 changed files with 76 additions and 1 deletions

View File

@ -209,7 +209,7 @@ async function lookupGeo(ip: string): Promise<GeoResult> {
}
}
function extractClientIp(req: {
export function extractClientIp(req: {
headers: Record<string, string | string[] | undefined>;
ip?: string;
}): string {

View File

@ -20,7 +20,9 @@ import {
policyMatchesContext,
mergePolicies,
checkRateLimit,
extractClientIp,
} from './routes.js';
import { UpdateClusterSchema, ClusterStatusEnum } from './types.js';
// ─── Minimal valid event for reuse ──────────────────────────────────
@ -673,3 +675,76 @@ describe('checkRateLimit', () => {
expect(checkRateLimit(id1, 20)).toBe(false); // id1 exceeded
});
});
// ─── UpdateClusterSchema ─────────────────────────────────────────
describe('UpdateClusterSchema', () => {
it('accepts valid status values', () => {
for (const status of ['open', 'resolved', 'ignored']) {
const result = UpdateClusterSchema.safeParse({ status });
expect(result.success).toBe(true);
}
});
it('rejects invalid status', () => {
const result = UpdateClusterSchema.safeParse({ status: 'deleted' });
expect(result.success).toBe(false);
});
it('rejects missing status', () => {
const result = UpdateClusterSchema.safeParse({});
expect(result.success).toBe(false);
});
});
// ─── ClusterStatusEnum ───────────────────────────────────────────
describe('ClusterStatusEnum', () => {
it('defines exactly three values', () => {
expect(ClusterStatusEnum.options).toEqual(['open', 'resolved', 'ignored']);
});
});
// ─── extractClientIp ─────────────────────────────────────────────
describe('extractClientIp', () => {
it('extracts first IP from X-Forwarded-For string', () => {
expect(
extractClientIp({
headers: { 'x-forwarded-for': '1.2.3.4, 10.0.0.1, 172.16.0.1' },
})
).toBe('1.2.3.4');
});
it('extracts from X-Forwarded-For array', () => {
expect(
extractClientIp({
headers: { 'x-forwarded-for': ['5.6.7.8, 10.0.0.1'] },
})
).toBe('5.6.7.8');
});
it('falls back to req.ip when no X-Forwarded-For', () => {
expect(extractClientIp({ headers: {}, ip: '192.168.1.1' })).toBe('192.168.1.1');
});
it('returns empty string when no IP available', () => {
expect(extractClientIp({ headers: {} })).toBe('');
});
it('trims whitespace from X-Forwarded-For', () => {
expect(
extractClientIp({
headers: { 'x-forwarded-for': ' 9.8.7.6 , 10.0.0.1' },
})
).toBe('9.8.7.6');
});
it('handles single IP in X-Forwarded-For (no comma)', () => {
expect(
extractClientIp({
headers: { 'x-forwarded-for': '203.0.113.50' },
})
).toBe('203.0.113.50');
});
});