|
@@ -0,0 +1,162 @@
|
|
|
|
|
+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 === 'admin123') {
|
|
|
|
|
+ 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)
|
|
|
|
|
+}
|