unrip/test/operator-dashboard-api-client.test.mjs
philipp fd899a3788
Some checks failed
deploy / deploy (push) Failing after 35s
Fix dashboard auth JSON handling
Proof: npm test (212/212) and npm run operator-dashboard:build cover non-JSON auth failures and rebuilt the dashboard bundle.

Assumptions: browser auth failures may return plain text before a session cookie is established; API callers should receive JSON errors.

Still fake: dashboard quote outcomes still depend on inventory-delta attribution instead of venue-native terminal fill events.
2026-05-18 13:45:01 +02:00

69 lines
1.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { fetchJson } from '../src/operator-dashboard/static/lib/api.js';
test('dashboard fetch helper reports empty upstream 500 responses', async (t) => {
const originalFetch = globalThis.fetch;
t.after(() => {
globalThis.fetch = originalFetch;
});
globalThis.fetch = async () => new Response('', { status: 500 });
await assert.rejects(
fetchJson('/api/session', { timeoutMs: 0 }),
/HTTP 500/,
);
});
test('dashboard fetch helper reports plain-text auth failures without leaking JSON parse errors', async (t) => {
const originalFetch = globalThis.fetch;
t.after(() => {
globalThis.fetch = originalFetch;
});
globalThis.fetch = async () => new Response('authentication required\n', { status: 401 });
await assert.rejects(
fetchJson('/api/session', { timeoutMs: 0 }),
/HTTP 401: authentication required/,
);
});
test('dashboard fetch helper reports invalid successful JSON responses explicitly', async (t) => {
const originalFetch = globalThis.fetch;
t.after(() => {
globalThis.fetch = originalFetch;
});
globalThis.fetch = async () => new Response('<html></html>', {
status: 200,
headers: {
'content-type': 'application/json',
},
});
await assert.rejects(
fetchJson('/api/session', { timeoutMs: 0 }),
/Invalid JSON response/,
);
});
test('dashboard fetch helper times out stale port-forward requests', async (t) => {
const originalFetch = globalThis.fetch;
t.after(() => {
globalThis.fetch = originalFetch;
});
globalThis.fetch = async (_url, options = {}) => new Promise((_resolve, reject) => {
options.signal?.addEventListener('abort', () => {
reject(Object.assign(new Error('aborted'), { name: 'AbortError' }));
});
});
await assert.rejects(
fetchJson('/api/bootstrap', { timeoutMs: 1 }),
/Request timed out after 1ms/,
);
});