Files
textbee/web/e2e/mock-api.ts
isra el c92740c401 test: add vitest + rtl + msw + playwright harness with mocked api
Establish the testing safety net for the web dashboard before refactoring.

- Vitest + React Testing Library + jsdom for unit/component tests
- MSW node server mocks the API; unhandled requests throw so tests can
  never reach a real backend
- Playwright e2e boots the real Next app but intercepts every /api/v1 call
  with shared fixtures, and mints a NextAuth session cookie to run authed
- Shared fixtures in test/fixtures.ts back both layers
- Seed tests: Button unit tests, MSW sanity checks, and a dashboard e2e
  happy path plus an auth-guard redirect

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 01:01:05 +03:00

46 lines
1.7 KiB
TypeScript

import type { Page, Route } from '@playwright/test'
import {
mockApiKeys,
mockBillingPlans,
mockDevices,
mockMessages,
mockStats,
mockSubscription,
mockUser,
mockWebhookNotifications,
mockWebhooks,
} from '../test/fixtures'
const json = (route: Route, body: unknown, status = 200) =>
route.fulfill({
status,
contentType: 'application/json',
body: JSON.stringify(body),
})
// Intercept EVERY call to the backend (`/api/v1/**`) and serve fixtures. Because
// the app is pointed at a backend host that nothing listens on, this guarantees
// no request ever reaches a real backend during e2e.
export async function mockApi(page: Page) {
await page.route('**/api/v1/**', (route) => {
const path = new URL(route.request().url()).pathname.replace('/api/v1', '')
if (path === '/auth/who-am-i') return json(route, { data: mockUser })
if (path === '/billing/current-subscription')
return json(route, mockSubscription)
if (path === '/billing/plans') return json(route, { data: mockBillingPlans })
if (path === '/gateway/devices') return json(route, { data: mockDevices })
if (path === '/gateway/stats') return json(route, { data: mockStats })
if (path === '/webhooks') return json(route, { data: mockWebhooks })
if (path === '/webhooks/notifications')
return json(route, mockWebhookNotifications)
if (path === '/auth/api-keys') return json(route, { data: mockApiKeys })
if (/\/gateway\/devices\/[^/]+\/(messages|get-received-sms)/.test(path))
return json(route, mockMessages)
// Any unmapped backend call still gets a benign mocked response so the test
// cannot fall through to a real backend.
return json(route, { data: [] })
})
}