api-handlers.ts 4.0 KB

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