|
| 1 | +import type { Err } from 'neverthrow'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import * as datetime from '../datetime.js'; |
| 5 | +import { safeFetch, waitForServer } from '../http.js'; |
| 6 | + |
| 7 | +const fetch = vi.hoisted(() => vi.fn()); |
| 8 | +const networkError = new Error('Network Error'); |
| 9 | + |
| 10 | +vi.stubGlobal('fetch', fetch); |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + vi.resetAllMocks(); |
| 14 | +}); |
| 15 | + |
| 16 | +describe('safeFetch', () => { |
| 17 | + it('should return an Ok result when the request succeeds', async () => { |
| 18 | + const mockResponse = { ok: true, status: 200, statusText: 'OK' }; |
| 19 | + fetch.mockResolvedValueOnce(mockResponse); |
| 20 | + const result = await safeFetch('http://localhost:6789', { method: 'GET' }); |
| 21 | + expect(result.isOk()).toBe(true); |
| 22 | + if (result.isOk()) { |
| 23 | + expect(result.value).toBe(mockResponse); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return an Err result with HTTP_ERROR when the response is not ok', async () => { |
| 28 | + const mockResponse = { ok: false, status: 404, statusText: 'Not Found' }; |
| 29 | + fetch.mockResolvedValueOnce(mockResponse); |
| 30 | + const result = await safeFetch('http://localhost:6789', { method: 'GET' }); |
| 31 | + expect(result.isErr()).toBe(true); |
| 32 | + if (result.isErr()) { |
| 33 | + expect(result.error).toMatchObject({ |
| 34 | + details: { |
| 35 | + kind: 'HTTP_ERROR', |
| 36 | + status: 404, |
| 37 | + statusText: 'Not Found', |
| 38 | + url: 'http://localhost:6789' |
| 39 | + }, |
| 40 | + name: 'FetchException' |
| 41 | + }); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return an Err result with NETWORK_ERROR when fetch throws', async () => { |
| 46 | + fetch.mockRejectedValueOnce(networkError); |
| 47 | + const result = await safeFetch('http://localhost:6789', { method: 'GET' }); |
| 48 | + expect(result.isErr()).toBe(true); |
| 49 | + if (result.isErr()) { |
| 50 | + expect(result.error).toMatchObject({ |
| 51 | + cause: networkError, |
| 52 | + details: { |
| 53 | + kind: 'NETWORK_ERROR', |
| 54 | + url: 'http://localhost:6789' |
| 55 | + }, |
| 56 | + name: 'FetchException' |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('should pass through the RequestInit options to fetch', async () => { |
| 62 | + const mockResponse = { ok: true, status: 200, statusText: 'OK' }; |
| 63 | + fetch.mockResolvedValueOnce(mockResponse); |
| 64 | + const init = { |
| 65 | + body: JSON.stringify({ test: 'data' }), |
| 66 | + headers: { 'Content-Type': 'application/json' }, |
| 67 | + method: 'POST' |
| 68 | + }; |
| 69 | + await safeFetch('http://localhost:6789', init); |
| 70 | + expect(fetch).toHaveBeenCalledWith('http://localhost:6789', init); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('waitForServer', () => { |
| 75 | + it('should return an Ok result when server becomes available', async () => { |
| 76 | + fetch.mockRejectedValueOnce(networkError); |
| 77 | + fetch.mockRejectedValueOnce(networkError); |
| 78 | + fetch.mockResolvedValueOnce({ ok: true }); |
| 79 | + const time = Date.now(); |
| 80 | + const result = await waitForServer('http://localhost:6789', { interval: 0.1, timeout: 0.3 }); |
| 81 | + expect(result.isOk()).toBe(true); |
| 82 | + expect(Date.now() - time).toBeGreaterThanOrEqual(200); |
| 83 | + expect(Date.now() - time).toBeLessThan(300); |
| 84 | + expect(fetch).toHaveBeenCalledTimes(3); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return an Err result after the timeout if the server never becomes available', async () => { |
| 88 | + fetch.mockRejectedValue(networkError); |
| 89 | + const result = (await waitForServer('http://localhost:6789', { interval: 0.1, timeout: 0.3 })) as Err<void, Error>; |
| 90 | + expect(result.error.message).toBe("Failed to connect to 'http://localhost:6789' after timeout of 0.3s"); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should use a default timeout of 10 seconds and interval of 1 second', async () => { |
| 94 | + fetch.mockRejectedValue(networkError); |
| 95 | + vi.spyOn(datetime, 'sleep').mockResolvedValue(); |
| 96 | + const result = (await waitForServer('http://localhost:6789')) as Err<void, Error>; |
| 97 | + expect(result.error.message).toBe("Failed to connect to 'http://localhost:6789' after timeout of 10s"); |
| 98 | + expect(fetch).toHaveBeenCalledTimes(10); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return an Err result if the HTTP request succeeds, but is not a 200-level response', async () => { |
| 102 | + fetch.mockResolvedValueOnce({ ok: false, status: 500 }); |
| 103 | + const result = (await waitForServer('http://localhost:6789', { interval: 0.1, timeout: 0.3 })) as Err<void, Error>; |
| 104 | + expect(result.error.message).toBe("Request to 'http://localhost:6789' returned status code 500"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments