import { expect, test, type Page } from '@playwright/test'; const HEALTH_OK = { status: 'ok', version: '0.1.0' }; const API_ARTICLE = { id: 100, title: 'Live API Article', content: 'This story was served by the (mocked) newzy backend API. '.repeat(20), author_id: 7, category_id: 1, created_at: '2025-06-01T08:00:00Z', published: true, author: { id: 7, name: 'Ada Lovelace', email: 'ada@example.com', bio: null, }, category: { id: 1, name: 'Technology', slug: 'technology' }, }; /** Mock the health endpoint so tests do not depend on a running backend. */ async function mockHealth(page: Page): Promise { await page.route('**/api/v1/health', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(HEALTH_OK), }), ); } /** * API interactions: verifies the app talks to the backend through the * typed client (base URL /api/v1, JSON accept header) and reflects the * responses in the UI. */ test.describe('api interactions', () => { test('requests the backend health endpoint on load and reports it online', async ({ page, }) => { await mockHealth(page); const healthRequest = page.waitForRequest( (request) => request.url().includes('/api/v1/health') && request.method() === 'GET', ); await page.goto('/'); const request = await healthRequest; expect(request.headers()['accept']).toBe('application/json'); await expect(page.getByTestId('api-status')).toHaveText(/api:\s*online/i); }); test('renders articles returned by the API', async ({ page }) => { await mockHealth(page); await page.route('**/api/v1/articles', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([API_ARTICLE]), }), ); await page.goto('/'); // The API article replaces the sample feed. const cards = page.getByTestId('article-card'); await expect(cards).toHaveCount(1); await expect(cards.first()).toContainText('Live API Article'); await expect(cards.first()).toContainText('Ada Lovelace'); await expect(cards.first()).toContainText('Technology'); // Sample-data fallback note is hidden when real data is present. await expect(page.getByTestId('sample-data-note')).toHaveCount(0); }); test('falls back to sample data and reports the API as offline', async ({ page }) => { // Block every API call to simulate the backend being down. await page.route('**/api/v1/**', (route) => route.abort()); await page.goto('/'); await expect(page.getByTestId('sample-data-note')).toBeVisible(); await expect(page.getByTestId('article-card').first()).toBeVisible(); await expect(page.getByTestId('api-status')).toHaveText(/api:\s*offline/i); }); test('survives API error responses (non-2xx envelope)', async ({ page }) => { await mockHealth(page); await page.route('**/api/v1/articles', (route) => route.fulfill({ status: 404, contentType: 'application/json', body: JSON.stringify({ detail: 'Not Found', code: 'not_found' }), }), ); const pageErrors: string[] = []; page.on('pageerror', (error) => pageErrors.push(error.message)); await page.goto('/'); // 404 from the articles endpoint -> graceful sample-data fallback. await expect(page.getByTestId('sample-data-note')).toBeVisible(); await expect(page.getByTestId('article-card').first()).toBeVisible(); expect(pageErrors).toEqual([]); }); });