api-handlers.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import type { Page } from '@playwright/test'
  2. import { mockLoginResponse, mockAdminInfo, mockMachines, mockCameras, mockDashboardStats, wrapResponse } from '../../fixtures'
  3. /**
  4. * Mock login API responses
  5. * POST /admin/auth/login
  6. */
  7. export async function mockLoginAPI(page: Page) {
  8. await page.route('**/admin/auth/login', async (route) => {
  9. const request = route.request()
  10. if (request.method() !== 'POST') {
  11. await route.continue()
  12. return
  13. }
  14. const body = request.postDataJSON()
  15. if (body?.username === 'admin' && body?.password === 'admin123') {
  16. await route.fulfill({
  17. status: 200,
  18. contentType: 'application/json',
  19. body: JSON.stringify(wrapResponse(mockLoginResponse))
  20. })
  21. } else {
  22. await route.fulfill({
  23. status: 200,
  24. contentType: 'application/json',
  25. body: JSON.stringify({
  26. code: 401,
  27. message: '用户名或密码错误',
  28. data: null,
  29. timestamp: Date.now()
  30. })
  31. })
  32. }
  33. })
  34. }
  35. /**
  36. * Mock logout API responses
  37. * POST /admin/auth/logout
  38. */
  39. export async function mockLogoutAPI(page: Page) {
  40. await page.route('**/admin/auth/logout', async (route) => {
  41. await route.fulfill({
  42. status: 200,
  43. contentType: 'application/json',
  44. body: JSON.stringify(wrapResponse(null))
  45. })
  46. })
  47. }
  48. /**
  49. * Mock admin info API responses
  50. * GET /admin/auth/info
  51. */
  52. export async function mockAdminInfoAPI(page: Page) {
  53. await page.route('**/admin/auth/info', async (route) => {
  54. await route.fulfill({
  55. status: 200,
  56. contentType: 'application/json',
  57. body: JSON.stringify(wrapResponse(mockAdminInfo))
  58. })
  59. })
  60. }
  61. /**
  62. * Mock machines list API responses
  63. * GET /admin/machines/list
  64. */
  65. export async function mockMachinesListAPI(page: Page) {
  66. await page.route('**/admin/machines/list', async (route) => {
  67. await route.fulfill({
  68. status: 200,
  69. contentType: 'application/json',
  70. body: JSON.stringify(wrapResponse(mockMachines))
  71. })
  72. })
  73. }
  74. /**
  75. * Mock machine detail API responses
  76. * GET /admin/machines/detail
  77. */
  78. export async function mockMachineDetailAPI(page: Page) {
  79. await page.route('**/admin/machines/detail**', async (route) => {
  80. const url = new URL(route.request().url())
  81. const id = parseInt(url.searchParams.get('id') || '1')
  82. const machine = mockMachines.find((m) => m.id === id) || mockMachines[0]
  83. await route.fulfill({
  84. status: 200,
  85. contentType: 'application/json',
  86. body: JSON.stringify(wrapResponse(machine))
  87. })
  88. })
  89. }
  90. /**
  91. * Mock cameras list API responses
  92. * GET /admin/cameras/list
  93. */
  94. export async function mockCamerasListAPI(page: Page) {
  95. await page.route('**/admin/cameras/list', async (route) => {
  96. await route.fulfill({
  97. status: 200,
  98. contentType: 'application/json',
  99. body: JSON.stringify(wrapResponse(mockCameras))
  100. })
  101. })
  102. }
  103. /**
  104. * Mock camera list API (controller)
  105. * GET /camera/list
  106. */
  107. export async function mockCameraListAPI(page: Page) {
  108. await page.route('**/camera/list', async (route) => {
  109. await route.fulfill({
  110. status: 200,
  111. contentType: 'application/json',
  112. body: JSON.stringify(wrapResponse(mockCameras))
  113. })
  114. })
  115. }
  116. /**
  117. * Mock dashboard stats API responses
  118. * GET /admin/stats/dashboard
  119. */
  120. export async function mockDashboardStatsAPI(page: Page) {
  121. await page.route('**/admin/stats/dashboard', async (route) => {
  122. await route.fulfill({
  123. status: 200,
  124. contentType: 'application/json',
  125. body: JSON.stringify(wrapResponse(mockDashboardStats))
  126. })
  127. })
  128. }
  129. /**
  130. * Setup all common API mocks for authenticated pages
  131. */
  132. export async function setupAuthenticatedMocks(page: Page) {
  133. await Promise.all([
  134. mockAdminInfoAPI(page),
  135. mockLogoutAPI(page),
  136. mockMachinesListAPI(page),
  137. mockMachineDetailAPI(page),
  138. mockCamerasListAPI(page),
  139. mockCameraListAPI(page),
  140. mockDashboardStatsAPI(page)
  141. ])
  142. }
  143. /**
  144. * Setup all API mocks including login
  145. */
  146. export async function setupAllMocks(page: Page) {
  147. await mockLoginAPI(page)
  148. await setupAuthenticatedMocks(page)
  149. }