| 1 |
import { test } from 'node:test'; |
| 2 |
import assert from 'node:assert/strict'; |
| 3 |
import { apiError } from './net.ts'; |
| 4 |
|
| 5 |
// Minimal Response-shaped stubs; apiError only touches `.json`. |
| 6 |
const resp = (json: () => Promise<unknown>) => ({ json }) as unknown as Response; |
| 7 |
|
| 8 |
test('apiError returns the server error string when present', async () => { |
| 9 |
assert.equal(await apiError(resp(async () => ({ error: 'This promo code has expired' }))), 'This promo code has expired'); |
| 10 |
}); |
| 11 |
|
| 12 |
test('apiError falls back when the body has no error field', async () => { |
| 13 |
assert.equal(await apiError(resp(async () => ({ ok: true })), 'nope'), 'nope'); |
| 14 |
}); |
| 15 |
|
| 16 |
test('apiError falls back on a non-JSON / throwing body', async () => { |
| 17 |
assert.equal(await apiError(resp(async () => { throw new Error('not json'); })), 'Request failed'); |
| 18 |
}); |
| 19 |
|
| 20 |
test('apiError falls back on a null/!response', async () => { |
| 21 |
assert.equal(await apiError(null), 'Request failed'); |
| 22 |
assert.equal(await apiError(undefined, 'x'), 'x'); |
| 23 |
}); |
| 24 |
|
| 25 |
test('apiError ignores a non-string error field', async () => { |
| 26 |
assert.equal(await apiError(resp(async () => ({ error: { nested: true } })), 'fb'), 'fb'); |
| 27 |
}); |
| 28 |
|