import { test as base, expect } from '@playwright/test';

export { expect };
export type { Page } from '@playwright/test';

/**
 * Shared Playwright `test` for the E2E suite.
 *
 * Adds an auto-use fallback for the `/api` surface: any request a spec
 * forgot to mock is fulfilled here with a fast 503 instead of falling
 * through to the SvelteKit dev proxy. That proxy targets a backend that
 * isn't running under E2E (`BACKEND_URL` in `.env`), so an unmocked call
 * otherwise incurs a proxy round-trip + Vite error logging — which, under
 * 8 parallel workers at cold start, piles up and flakes the first tests.
 *
 * Because this route is registered during fixture setup (before the test
 * body runs), any `page.route(...)` a spec registers later takes
 * precedence — only genuinely-unmocked calls land here. The 503 also
 * surfaces mock gaps loudly (a clear warning + a fast, attributable
 * failure) instead of a 30s "element never appeared" timeout.
 */
export const test = base.extend<{ apiFallback: void }>({
    apiFallback: [
        async ({ page }, use) => {
            // Scope to `/api/v1/**` (the real backend surface). A broader
            // `**/api/**` would also swallow Vite's own dev module requests
            // under `/src/lib/api/*.ts` and break the app bundle.
            await page.route('**/api/v1/**', (route) => {
                const { pathname } = new URL(route.request().url());
                console.warn(`[e2e] unmocked API call: ${route.request().method()} ${pathname}`);
                return route.fulfill({
                    status: 503,
                    contentType: 'application/json',
                    body: JSON.stringify({
                        error: { code: 'e2e_unmocked', message: `unmocked in test: ${pathname}` }
                    })
                });
            });
            await use();
        },
        { auto: true }
    ]
});
