import { describe, expect, it } from 'vitest'; import { parseApiErrorMessage } from './jsonClient'; describe('parseApiErrorMessage', () => { it('prefers api error detail messages for business failures', () => { expect( parseApiErrorMessage( JSON.stringify({ ok: false, data: null, error: { code: 'BAD_REQUEST', message: '请求参数不合法', details: { message: 'big_fish 发布校验未通过:还缺少 16 个基础动作', provider: 'spacetimedb', }, }, meta: { apiVersion: '2026-06-16', }, }), 'Fallback failure', ), ).toBe('big_fish 发布校验未通过:还缺少 16 个基础动作'); }); it('prefers nested api error messages', () => { expect( parseApiErrorMessage( JSON.stringify({ error: { message: 'Detailed failure', }, }), 'Fallback failure', ), ).toBe('Detailed failure'); }); it('falls back to the raw response text when the payload is not json', () => { expect(parseApiErrorMessage('Plain text failure', 'Fallback failure')).toBe( 'Plain text failure', ); }); it('uses the fallback when the response body is empty', () => { expect(parseApiErrorMessage('', 'Fallback failure')).toBe( 'Fallback failure', ); }); });