| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import type { Page } from '@playwright/test'
- import {
- mockLoginResponse,
- mockAdminInfo,
- mockMachines,
- mockCameras,
- mockDashboardStats,
- wrapResponse
- } from '../../fixtures'
- /**
- * Mock login API responses
- * POST /admin/auth/login
- */
- export async function mockLoginAPI(page: Page) {
- await page.route('**/admin/auth/login', async (route) => {
- const request = route.request()
- if (request.method() !== 'POST') {
- await route.continue()
- return
- }
- const body = request.postDataJSON()
- if (body?.username === 'admin' && body?.password === '123456') {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockLoginResponse))
- })
- } else {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify({
- code: 401,
- message: '用户名或密码错误',
- data: null,
- timestamp: Date.now()
- })
- })
- }
- })
- }
- /**
- * Mock logout API responses
- * POST /admin/auth/logout
- */
- export async function mockLogoutAPI(page: Page) {
- await page.route('**/admin/auth/logout', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(null))
- })
- })
- }
- /**
- * Mock admin info API responses
- * GET /admin/auth/info
- */
- export async function mockAdminInfoAPI(page: Page) {
- await page.route('**/admin/auth/info', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockAdminInfo))
- })
- })
- }
- /**
- * Mock machines list API responses
- * GET /admin/machines/list
- */
- export async function mockMachinesListAPI(page: Page) {
- await page.route('**/admin/machines/list', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockMachines))
- })
- })
- }
- /**
- * Mock machine detail API responses
- * GET /admin/machines/detail
- */
- export async function mockMachineDetailAPI(page: Page) {
- await page.route('**/admin/machines/detail**', async (route) => {
- const url = new URL(route.request().url())
- const id = parseInt(url.searchParams.get('id') || '1')
- const machine = mockMachines.find((m) => m.id === id) || mockMachines[0]
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(machine))
- })
- })
- }
- /**
- * Mock cameras list API responses
- * GET /admin/cameras/list
- */
- export async function mockCamerasListAPI(page: Page) {
- await page.route('**/admin/cameras/list', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockCameras))
- })
- })
- }
- /**
- * Mock camera list API (controller)
- * GET /camera/list
- */
- export async function mockCameraListAPI(page: Page) {
- await page.route('**/camera/list', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockCameras))
- })
- })
- }
- /**
- * Mock dashboard stats API responses
- * GET /admin/stats/dashboard
- */
- export async function mockDashboardStatsAPI(page: Page) {
- await page.route('**/admin/stats/dashboard', async (route) => {
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify(wrapResponse(mockDashboardStats))
- })
- })
- }
- /**
- * Setup all common API mocks for authenticated pages
- */
- export async function setupAuthenticatedMocks(page: Page) {
- await Promise.all([
- mockAdminInfoAPI(page),
- mockLogoutAPI(page),
- mockMachinesListAPI(page),
- mockMachineDetailAPI(page),
- mockCamerasListAPI(page),
- mockCameraListAPI(page),
- mockDashboardStatsAPI(page)
- ])
- }
- /**
- * Setup all API mocks including login
- */
- export async function setupAllMocks(page: Page) {
- await mockLoginAPI(page)
- await setupAuthenticatedMocks(page)
- }
|