api-handlers.ts 4.9 KB

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