mirror of
https://github.com/vernu/textbee.git
synced 2026-08-01 01:48:17 -04:00
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>
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import type { BrowserContext } from '@playwright/test'
|
|
import { encode } from 'next-auth/jwt'
|
|
import { TEST_AUTH_SECRET } from '../playwright.config'
|
|
import { mockUser, TEST_ACCESS_TOKEN } from '../test/fixtures'
|
|
|
|
// NextAuth v4 default session cookie name over http (non-secure).
|
|
const SESSION_COOKIE = 'next-auth.session-token'
|
|
|
|
// Mint a valid NextAuth JWT session cookie so middleware's getToken() accepts
|
|
// the request and the app treats us as authenticated, without ever running the
|
|
// real login flow or contacting a backend.
|
|
export async function authenticate(context: BrowserContext) {
|
|
const token = await encode({
|
|
token: {
|
|
sub: mockUser.id,
|
|
id: mockUser.id,
|
|
name: mockUser.name,
|
|
email: mockUser.email,
|
|
phone: mockUser.phone,
|
|
role: mockUser.role,
|
|
accessToken: TEST_ACCESS_TOKEN,
|
|
},
|
|
secret: TEST_AUTH_SECRET,
|
|
})
|
|
|
|
await context.addCookies([
|
|
{
|
|
name: SESSION_COOKIE,
|
|
value: token,
|
|
domain: 'localhost',
|
|
path: '/',
|
|
httpOnly: true,
|
|
sameSite: 'Lax',
|
|
expires: Math.floor(Date.now() / 1000) + 60 * 60,
|
|
},
|
|
])
|
|
}
|