-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport.test.ts
More file actions
55 lines (46 loc) · 1.95 KB
/
Copy pathtransport.test.ts
File metadata and controls
55 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
buildServerEndpointCandidates,
fetchLocalServer,
fetchServerJson,
normalizeServerUrl,
readServerError,
} from '../transport';
describe('background transport helpers', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('normalizes the server URL and preserves a trailing slash', () => {
expect(normalizeServerUrl('http://localhost:8765').toString()).toBe('http://localhost:8765/');
expect(normalizeServerUrl('http://localhost:8765/api').toString()).toBe('http://localhost:8765/api/');
});
it('builds localhost fallback candidates', () => {
expect(buildServerEndpointCandidates('http://localhost:8765', '/health')).toEqual([
'http://localhost:8765/health',
'http://127.0.0.1:8765/health',
]);
});
it('retries the alternate loopback hostname when the first candidate fails', async () => {
const fetchMock = vi.spyOn(globalThis, 'fetch')
.mockRejectedValueOnce(new TypeError('network down'))
.mockResolvedValueOnce(new Response(JSON.stringify({ status: 'ok' }), { status: 200 }));
const response = await fetchLocalServer('http://localhost:8765', '/health', {});
expect(response.status).toBe(200);
expect(fetchMock).toHaveBeenCalledTimes(2);
});
it('fetches JSON using the resolved server URL from settings', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(JSON.stringify({ status: 'ok' }), { status: 200 }),
);
const payload = await fetchServerJson<{ status: string }>(
'/health',
{},
{ getSettings: async () => ({ localServerUrl: 'http://localhost:8765' }) },
);
expect(payload.status).toBe('ok');
});
it('extracts server detail from JSON error responses', async () => {
const response = new Response(JSON.stringify({ detail: 'bad request' }), { status: 400 });
await expect(readServerError(response)).resolves.toBe('bad request');
});
});