File size: 3,820 Bytes
4545bb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import { fireEvent, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { ErrorAlert } from '../components/common';
import { renderUi } from '../test/utils';
import { ApiError, errorMessage } from './client';
import { explainError, supportDetails } from './errors';
const err = (code: string, detail = 'x', status = 409, body: unknown = {}) => new ApiError(status, code, detail, body, 'rid-42');
describe('plain-language errors', () => {
it('maps each known code to a message and what to do', () => {
const pod = explainError(err('POD_CHECK_FAILED', 'Acme UAT: the sign-in was refused'));
expect(pod.message).toBe('The test environment did not accept its sign-in check. Acme UAT: the sign-in was refused.');
expect(pod.action).toEqual({ label: 'Check sign-in details', to: '/pods' });
expect(explainError(err('NOTHING_APPROVED', '', 422)).message).toMatch(/No scenario is approved/);
expect(explainError(err('KILL_SWITCH', 'kill switch engaged', 423)).message).toMatch(/emergency stop is on/);
expect(explainError(err('BROWSER_RUNS_OFF')).hint).toMatch(/quick simulation/);
expect(explainError(err('AREA_NOT_ON_FUSION', 'On an Oracle Fusion pod, only these business areas have tests so far: AP')).message).toMatch(/^On an Oracle Fusion pod/);
expect(explainError(err('RATE_LIMITED', 'too many', 429)).hint).toMatch(/Wait a minute/);
expect(explainError(err('INTERNAL', 'KeyError', 500)).message).toBe('Something went wrong on the server.');
expect(explainError(err('WHATEVER', 'teapot', 503)).message).toBe('Something went wrong on the server.');
});
it('VALIDATION lists the field messages from errors[]', () => {
const v = explainError(err('VALIDATION', 'request validation failed', 422, { errors: [{ loc: ['body', 'base_url'], msg: 'Value error, must start with https://' }, { loc: ['body', 'name'], msg: 'String should have at least 2 characters' }] }));
expect(v.message).toBe('2 fields need a change.');
expect(v.fields).toEqual([{ field: 'Address', message: 'must start with https://' }, { field: 'Name', message: 'Should have at least 2 characters' }]);
});
it('never puts the code or the request id in the message', () => {
const m = errorMessage(err('KILL_SWITCH', 'kill switch engaged', 423));
expect(m).not.toMatch(/KILL_SWITCH|rid-42/);
expect(errorMessage(new TypeError('Failed to fetch'))).toMatch(/cannot reach its server/);
});
it('the alert shows the words and the action; the request id is only in the support details', async () => {
const write = vi.fn(async () => undefined);
Object.assign(navigator, { clipboard: { writeText: write } });
renderUi(<ErrorAlert error={err('POD_CHECK_FAILED', 'sign-in refused')} />);
const alert = screen.getByRole('alert');
expect(alert).toHaveTextContent('The test environment did not accept its sign-in check.');
expect(alert).not.toHaveTextContent('rid-42');
expect(alert).not.toHaveTextContent('POD_CHECK_FAILED');
expect(screen.getByRole('link', { name: 'Check sign-in details' })).toHaveAttribute('href', '/pods');
fireEvent.click(screen.getByRole('button', { name: 'Copy details for support' }));
expect(write).toHaveBeenCalledWith(expect.stringContaining('Request id: rid-42'));
expect(supportDetails(explainError(err('INTERNAL', 'KeyError', 500)), '/runs')).toMatch(/Error: INTERNAL \(HTTP 500\)\nRequest id: rid-42/);
});
it('VALIDATION in the alert: one line per field', () => {
renderUi(<ErrorAlert error={err('VALIDATION', 'request validation failed', 422, { errors: [{ loc: ['body', 'name'], msg: 'too short' }] })} />);
expect(screen.getByTestId('error-fields')).toHaveTextContent('Name: too short');
});
});
|