import { beforeEach, describe, expect, it } from 'vitest'; import { screen } from '@testing-library/react'; import { http, HttpResponse } from 'msw'; import { renderApp, mockGuest } from '@/test/utils'; import { server } from '@/mocks/server'; import { API_BASE } from '@/features/auth/mocks/fixtures'; import { _resetSetupStatusCache } from '@/router'; beforeEach(() => { _resetSetupStatusCache(); }); const STATUS = `${API_BASE}/api/v1/Setup/status`; const AVAILABILITY = `${API_BASE}/api/v1/Availability/status`; describe('a setup status that cannot be read never sends anybody to the setup page', () => { it.each([ ['429 (rate limited)', () => HttpResponse.json({ status: 429, title: 'Too Many Requests' }, { status: 429 })], ['500', () => HttpResponse.json({ status: 500 }, { status: 500 })], ['503 (service unavailable)', () => HttpResponse.json({ status: 503, title: 'Service Unavailable' }, { status: 503 })], ['404', () => new HttpResponse(null, { status: 404 })], ['an empty 200', () => new HttpResponse(null, { status: 200 })], ['a network failure', () => HttpResponse.error()], ])('%s: the login page is shown, not the setup page', async (_label, respond) => { mockGuest(); server.use(http.get(STATUS, respond)); renderApp('/dashboard'); expect(await screen.findByTestId('login-form-submit-button', {}, { timeout: 10000 })).toBeInTheDocument(); expect(screen.queryByTestId('setup-form')).not.toBeInTheDocument(); }); it('an unreadable status does not move a visitor who is already on the setup page either', async () => { mockGuest(); server.use(http.get(STATUS, () => HttpResponse.json({ status: 503 }, { status: 503 }))); renderApp('/setup'); // Nothing can be said about the system, so the router leaves the page where it is. expect(await screen.findByTestId('setup-form', {}, { timeout: 10000 })).toBeInTheDocument(); }); it('a readable "not initialized" still leads to the setup page (the real first-run case)', async () => { mockGuest(); server.use(http.get(STATUS, () => HttpResponse.json({ initialized: false }))); renderApp('/dashboard'); expect(await screen.findByTestId('setup-form', {}, { timeout: 10000 })).toBeInTheDocument(); }); }); describe('the login page of a CMS that is switched off', () => { function availability(status: 'Available' | 'Maintenance' | 'NotAvailable', message = '') { server.use( http.get(AVAILABILITY, () => HttpResponse.json({ status, checkedAt: '2026-11-09T00:00:00Z', message, isMasterControlled: false }), ), ); } it('says so, with the message, and still offers the login form (administrators pass)', async () => { mockGuest(); availability('NotAvailable', 'Uitgeschakeld door de master'); renderApp('/login'); const notice = await screen.findByTestId('login-cms-off', {}, { timeout: 10000 }); expect(notice).toHaveTextContent(/disabled/i); expect(notice).toHaveTextContent('Uitgeschakeld door de master'); expect(notice).toHaveTextContent(/only administrators/i); expect(screen.getByTestId('login-form-submit-button')).toBeInTheDocument(); }); it('names maintenance as maintenance', async () => { mockGuest(); availability('Maintenance'); renderApp('/login'); expect(await screen.findByTestId('login-cms-off', {}, { timeout: 10000 })).toHaveTextContent(/maintenance/i); }); it('shows nothing extra while the CMS is available', async () => { mockGuest(); availability('Available'); renderApp('/login'); await screen.findByTestId('login-form-submit-button', {}, { timeout: 10000 }); expect(screen.queryByTestId('login-cms-off')).not.toBeInTheDocument(); }); });