src/transport/telemetry.test.ts
v0.3.0 · 2 KB
import { describe, expect, it } from 'vitest';
import { createInfoClient } from './info-client.js';
import * as transport from './index.js';
import { createRequestCounter } from './telemetry.js';
describe('createRequestCounter', () => {
it('aggregates by label, sorts by weight and resets on drain', () => {
const c = createRequestCounter();
c.record('mon:clearinghouseState', 2);
c.record('mon:clearinghouseState', 2);
c.record('mon:meta', 20);
c.record('trade:meta', 20);
c.record('mon:meta', 20);
c.record('odd', Number.NaN);
expect(c.snapshot()).toEqual([
{ label: 'mon:meta', requests: 2, weight: 40 },
{ label: 'trade:meta', requests: 1, weight: 20 },
{ label: 'mon:clearinghouseState', requests: 2, weight: 4 },
{ label: 'odd', requests: 1, weight: 0 },
]);
expect(c.drain()).toHaveLength(4);
expect(c.snapshot()).toEqual([]);
});
it('can be fed from the info client, counting retries as real requests', async () => {
const c = createRequestCounter();
let n = 0;
const info = createInfoClient({
limiter: null,
retry: { baseDelayMs: 1, jitter: 0 },
fetch: async () => (++n === 1 ? new Response('x', { status: 429 }) : new Response('{}', { status: 200 })),
onAttempt: (e) => c.record(e.label ?? e.type, e.weight + e.extraWeight),
});
await info({ type: 'meta' }, { label: 'hl:meta' });
expect(c.snapshot()).toEqual([{ label: 'hl:meta', requests: 2, weight: 40 }]);
});
});
describe('public index', () => {
it('exposes the main entry points', () => {
for (const name of [
'createWeightLimiter',
'getSharedWeightLimiter',
'createInfoClient',
'weightOf',
'INFO_WEIGHTS',
'HlHttpError',
'isRateLimitError',
'isTransientError',
'withRetry',
'backoffDelayMs',
'limitExchange',
'getAddressBudget',
'computeAddressBudget',
'createCachedLoader',
'createRequestCounter',
'API_URL',
]) {
expect(transport).toHaveProperty(name);
}
});
});