$(addMangaToCollection crashed when the backend returned 201/200 with no body — the shared client only short-circuited 204. Now any empty body returns undefined.) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
76 lines
3.0 KiB
TypeScript
76 lines
3.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
|
|
import { ApiError, request } from './client';
|
|
import { getManga } from './mangas';
|
|
|
|
describe('request error envelope parsing', () => {
|
|
let fetchSpy: MockInstance<typeof globalThis.fetch>;
|
|
|
|
beforeEach(() => {
|
|
fetchSpy = vi.spyOn(globalThis, 'fetch');
|
|
});
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('parses {error:{code,message}} into ApiError.code and message', async () => {
|
|
fetchSpy.mockResolvedValueOnce(
|
|
new Response(
|
|
JSON.stringify({ error: { code: 'invalid_input', message: 'title is required' } }),
|
|
{ status: 400, headers: { 'content-type': 'application/json' } }
|
|
)
|
|
);
|
|
await expect(getManga('x')).rejects.toMatchObject({
|
|
status: 400,
|
|
code: 'invalid_input',
|
|
message: 'title is required'
|
|
});
|
|
});
|
|
|
|
it('falls back to http_error code when body is HTML (e.g. upstream proxy)', async () => {
|
|
fetchSpy.mockResolvedValueOnce(
|
|
new Response('<html>upstream proxy bad</html>', {
|
|
status: 502,
|
|
headers: { 'content-type': 'text/html' }
|
|
})
|
|
);
|
|
const err = (await getManga('x').catch((e) => e)) as ApiError;
|
|
expect(err).toBeInstanceOf(ApiError);
|
|
expect(err.status).toBe(502);
|
|
expect(err.code).toBe('http_error');
|
|
expect(err.message).toContain('upstream proxy bad');
|
|
});
|
|
|
|
it('falls back to http_error code when body is empty', async () => {
|
|
fetchSpy.mockResolvedValueOnce(new Response('', { status: 500 }));
|
|
const err = (await getManga('x').catch((e) => e)) as ApiError;
|
|
expect(err).toBeInstanceOf(ApiError);
|
|
expect(err.status).toBe(500);
|
|
expect(err.code).toBe('http_error');
|
|
});
|
|
|
|
it('treats empty 200/201 bodies as undefined (no JSON.parse crash)', async () => {
|
|
// Regression: addMangaToCollection is typed `void` and the
|
|
// backend returns 201 (created) / 200 (already there) with
|
|
// no body. Without the empty-body short-circuit, `res.json()`
|
|
// would throw `JSON.parse: unexpected end of data`.
|
|
fetchSpy.mockResolvedValueOnce(new Response(null, { status: 201 }));
|
|
const created = await request<void>('/v1/whatever', { method: 'POST' });
|
|
expect(created).toBeUndefined();
|
|
|
|
fetchSpy.mockResolvedValueOnce(new Response(null, { status: 200 }));
|
|
const ok200 = await request<void>('/v1/whatever', { method: 'POST' });
|
|
expect(ok200).toBeUndefined();
|
|
});
|
|
|
|
it('falls back to http_error code when JSON has no error envelope', async () => {
|
|
fetchSpy.mockResolvedValueOnce(
|
|
new Response(JSON.stringify({ message: 'oops' }), {
|
|
status: 500,
|
|
headers: { 'content-type': 'application/json' }
|
|
})
|
|
);
|
|
const err = (await getManga('x').catch((e) => e)) as ApiError;
|
|
expect(err.code).toBe('http_error');
|
|
});
|
|
});
|