101 lines
3.1 KiB
JavaScript
101 lines
3.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { safeFetch, safeFetchText, getFetchMetrics } from '../apis/utils/fetch.mjs';
|
|
|
|
test('safeFetch reports HTML as degraded JSON response', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const source = 'unit-html-once';
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get: () => 'text/html' },
|
|
text: async () => '<html>not json</html>',
|
|
});
|
|
try {
|
|
const data = await safeFetch('https://example.test/json', { retries: 0, source });
|
|
assert.match(data.error, /Expected JSON/);
|
|
const bucket = getFetchMetrics().bySource[source];
|
|
assert.equal(bucket.requests, 1);
|
|
assert.equal(bucket.ok, 0);
|
|
assert.equal(bucket.failed, 1);
|
|
assert.equal(bucket.lastStatus, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('safeFetch records HTTP failure once with status and bytes', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const source = 'unit-http-failure-once';
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 503,
|
|
headers: { get: () => 'application/json' },
|
|
text: async () => 'service unavailable',
|
|
});
|
|
try {
|
|
const data = await safeFetch('https://example.test/fail', { retries: 0, source });
|
|
assert.match(data.error, /HTTP 503/);
|
|
const bucket = getFetchMetrics().bySource[source];
|
|
assert.equal(bucket.requests, 1);
|
|
assert.equal(bucket.ok, 0);
|
|
assert.equal(bucket.failed, 1);
|
|
assert.equal(bucket.lastStatus, 503);
|
|
assert.equal(bucket.bytes, 'service unavailable'.length);
|
|
assert.match(bucket.lastError, /HTTP 503/);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('safeFetch retry metrics count one record per attempt', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const source = 'unit-retry-attempts';
|
|
let calls = 0;
|
|
globalThis.fetch = async () => {
|
|
calls += 1;
|
|
if (calls === 1) {
|
|
return {
|
|
ok: false,
|
|
status: 502,
|
|
headers: { get: () => 'application/json' },
|
|
text: async () => 'bad gateway',
|
|
};
|
|
}
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get: () => 'application/json' },
|
|
text: async () => '{"ok":true}',
|
|
};
|
|
};
|
|
try {
|
|
const data = await safeFetch('https://example.test/retry', { retries: 1, source });
|
|
assert.equal(data.ok, true);
|
|
assert.equal(calls, 2);
|
|
const bucket = getFetchMetrics().bySource[source];
|
|
assert.equal(bucket.requests, 2);
|
|
assert.equal(bucket.ok, 1);
|
|
assert.equal(bucket.failed, 1);
|
|
assert.equal(bucket.lastStatus, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('safeFetchText returns text and byte count', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
text: async () => 'hello',
|
|
});
|
|
try {
|
|
const data = await safeFetchText('https://example.test/rss', { retries: 0, source: 'rss-unit' });
|
|
assert.equal(data.text, 'hello');
|
|
assert.equal(data.bytes, 5);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|