tobogganSe connecter

starfleet-ui

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

74 lignes · 2372 octetsBrut · Historique
1import { describe, expect, test } from 'bun:test';
2import {
3 MAX_RETRY_WAIT_MS,
4 fieldFromPath,
5 fixFor,
6 pageStatusFor,
7 retryDelayMs,
8 unexplainedMessage
9} from './errors';
10
11describe('retryDelayMs', () => {
12 test('retries contended conflicts quickly', () => {
13 expect(retryDelayMs(409, 'contended', null)).toBe(250);
14 });
15 test('does not retry other conflicts', () => {
16 expect(retryDelayMs(409, 'ship_docked', null)).toBeNull();
17 });
18 test('honours Retry-After on 429 and 503', () => {
19 expect(retryDelayMs(429, 'rate_limited', '2')).toBe(2000);
20 expect(retryDelayMs(503, 'busy', '0')).toBe(0);
21 });
22 test('defaults to one second when Retry-After is missing or junk', () => {
23 expect(retryDelayMs(429, 'rate_limited', null)).toBe(1000);
24 expect(retryDelayMs(503, 'busy', 'soon')).toBe(1000);
25 });
26 test('gives up when the wait is too long', () => {
27 expect(retryDelayMs(429, 'rate_limited', String(MAX_RETRY_WAIT_MS / 1000 + 1))).toBeNull();
28 });
29 test('never retries client errors', () => {
30 expect(retryDelayMs(422, 'invalid_body', '1')).toBeNull();
31 });
32});
33
34describe('fixFor', () => {
35 test('maps state refusals to the action that resolves them', () => {
36 expect(fixFor('ship_docked')).toBe('orbit');
37 expect(fixFor('ship_in_orbit')).toBe('dock');
38 expect(fixFor('not_enough_fuel')).toBe('refuel');
39 });
40 test('returns null for unknown codes', () => {
41 expect(fixFor('internal')).toBeNull();
42 });
43});
44
45describe('fieldFromPath', () => {
46 test('extracts a top-level field', () => {
47 expect(fieldFromPath('$.waypoint')).toBe('waypoint');
48 });
49 test('rejects nested or missing paths', () => {
50 expect(fieldFromPath('$.a.b')).toBeNull();
51 expect(fieldFromPath(undefined)).toBeNull();
52 });
53});
54
55describe('unexplainedMessage', () => {
56 test('names the status and its text', () => {
57 expect(unexplainedMessage(403, 'Forbidden')).toContain('403 Forbidden');
58 });
59 test('copes with an empty status text (HTTP/2)', () => {
60 expect(unexplainedMessage(502, '')).toContain('answered 502 without');
61 });
62});
63
64describe('pageStatusFor', () => {
65 test('passes client errors through', () => {
66 expect(pageStatusFor(401)).toBe(401);
67 expect(pageStatusFor(404)).toBe(404);
68 });
69 test('reports upstream failures as a bad gateway', () => {
70 expect(pageStatusFor(500)).toBe(502);
71 expect(pageStatusFor(503)).toBe(502);
72 expect(pageStatusFor(200)).toBe(502);
73 });
74});