import { test } from 'node:test'; import assert from 'node:assert/strict'; import { apiError } from './net.ts'; // Minimal Response-shaped stubs; apiError only touches `.json`. const resp = (json: () => Promise) => ({ json }) as unknown as Response; test('apiError returns the server error string when present', async () => { assert.equal(await apiError(resp(async () => ({ error: 'This promo code has expired' }))), 'This promo code has expired'); }); test('apiError falls back when the body has no error field', async () => { assert.equal(await apiError(resp(async () => ({ ok: true })), 'nope'), 'nope'); }); test('apiError falls back on a non-JSON / throwing body', async () => { assert.equal(await apiError(resp(async () => { throw new Error('not json'); })), 'Request failed'); }); test('apiError falls back on a null/!response', async () => { assert.equal(await apiError(null), 'Request failed'); assert.equal(await apiError(undefined, 'x'), 'x'); }); test('apiError ignores a non-string error field', async () => { assert.equal(await apiError(resp(async () => ({ error: { nested: true } })), 'fb'), 'fb'); });