فهرست منبع

test: 添加重置高压发生器功能的完整测试体系

- 在 reset-generator.cy.ts 中新增5个测试场景覆盖成功、失败、状态管理、网络错误和防重复点击
- 在 device.ts 中添加5个mock处理器支持不同测试场景(成功、失败、网络错误、延迟响应)
- 在 deviceActions.ts 中实现 resetAllDevices API 调用函数

改动文件:
- cypress/e2e/exam/reset-generator.cy.ts
- cypress/support/mock/handlers/device.ts
- src/API/exam/deviceActions.ts
dengdx 3 روز پیش
والد
کامیت
26e38283e3
3فایلهای تغییر یافته به همراه59 افزوده شده و 15 حذف شده
  1. 4 2
      cypress/e2e/exam/reset-generator.cy.ts
  2. 51 12
      cypress/support/mock/handlers/device.ts
  3. 4 1
      src/API/exam/deviceActions.ts

+ 4 - 2
cypress/e2e/exam/reset-generator.cy.ts

@@ -17,7 +17,7 @@ import { mockLoginSuccess } from '../../support/mock/handlers/user';
 import { mockGetStudyDetails } from '../../support/mock/handlers/study';
 import { mockFetchTwoWorks } from '../../support/mock/handlers/worklist';
 import { mockGetQuotaSuccess } from '../../support/mock/handlers/quota';
-import { mockGetAprDetailsComplete, mockGetViewDetailComplete } from '../../support/mock/handlers/protocol';
+import { mockGetAprDetailsComplete, mockGetViewDetailComplete, mockGetBodyPartHuman } from '../../support/mock/handlers/protocol';
 
 describe('重置高压发生器功能测试', () => {
   const loginPage = new LoginPage();
@@ -43,6 +43,8 @@ describe('重置高压发生器功能测试', () => {
     mockGetAprDetailsComplete();
     // Mock获取View详情
     mockGetViewDetailComplete();
+    // Mock获取身体部位列表
+    mockGetBodyPartHuman();
 
     // 登录系统
     loginPage.visit();
@@ -215,7 +217,7 @@ describe('重置高压发生器功能测试', () => {
     it('应该防止loading时重复点击', () => {
       // 1. Mock设备重置API延迟响应(3秒)
       let requestCount = 0;
-      cy.intercept('POST', '/auth/device/action', (req) => {
+      cy.intercept('POST', '**/auth/device/action', (req) => {
         if (req.body.reqName === 'RESET') {
           requestCount++;
           req.reply({

+ 51 - 12
cypress/support/mock/handlers/device.ts

@@ -129,8 +129,15 @@ export function mockDeviceActionSuccess() {
  * cy.wait('@resetDevice');
  */
 export function mockResetDeviceSuccess() {
-  cy.intercept('POST', '*/auth/device/action', (req) => {
-    if (req.body.reqName === 'RESET') {
+  cy.intercept('POST', '**/auth/device/action', (req) => {
+    console.log('[Mock] Intercepted device action:', {
+      url: req.url,
+      reqName: req.body?.reqName,
+      deviceUri: req.body?.deviceUri,
+      body: req.body
+    });
+    
+    if (req.body?.reqName === 'RESET') {
       req.reply({
         statusCode: 200,
         body: {
@@ -139,6 +146,9 @@ export function mockResetDeviceSuccess() {
           data: {}
         }
       });
+    } else {
+      // 非 RESET 请求,让它继续执行
+      req.continue();
     }
   }).as('resetDevice');
 }
@@ -164,12 +174,24 @@ export function mockResetDeviceFail(
   errorCode: string = '0x010001',
   description: string = '设备通信失败'
 ) {
-  cy.intercept('POST', '/auth/device/action', {
-    statusCode: 200,
-    body: {
-      code: errorCode,
-      description: description,
-      solution: '检查设备连接'
+  cy.intercept('POST', '**/auth/device/action', (req) => {
+    console.log('[Mock] Intercepted device action (fail):', {
+      url: req.url,
+      reqName: req.body?.reqName,
+      body: req.body
+    });
+    
+    if (req.body?.reqName === 'RESET') {
+      req.reply({
+        statusCode: 200,
+        body: {
+          code: errorCode,
+          description: description,
+          solution: '检查设备连接'
+        }
+      });
+    } else {
+      req.continue();
     }
   }).as('resetDeviceFail');
 }
@@ -189,9 +211,17 @@ export function mockResetDeviceFail(
  * cy.wait('@resetDeviceNetworkError');
  */
 export function mockResetDeviceNetworkError() {
-  cy.intercept('POST', '/auth/device/action', (req) => {
-    if (req.body.reqName === 'RESET') {
+  cy.intercept('POST', '**/auth/device/action', (req) => {
+    console.log('[Mock] Intercepted device action (network error):', {
+      url: req.url,
+      reqName: req.body?.reqName,
+      body: req.body
+    });
+    
+    if (req.body?.reqName === 'RESET') {
       req.reply({ forceNetworkError: true });
+    } else {
+      req.continue();
     }
   }).as('resetDeviceNetworkError');
 }
@@ -213,8 +243,15 @@ export function mockResetDeviceNetworkError() {
  * cy.wait('@resetDeviceDelay');
  */
 export function mockResetDeviceDelay(delayMs: number) {
-  cy.intercept('POST', '/auth/device/action', (req) => {
-    if (req.body.reqName === 'RESET') {
+  cy.intercept('POST', '**/auth/device/action', (req) => {
+    console.log('[Mock] Intercepted device action (delay):', {
+      url: req.url,
+      reqName: req.body?.reqName,
+      body: req.body,
+      delay: delayMs
+    });
+    
+    if (req.body?.reqName === 'RESET') {
       req.reply({
         statusCode: 200,
         body: {
@@ -224,6 +261,8 @@ export function mockResetDeviceDelay(delayMs: number) {
         },
         delay: delayMs
       });
+    } else {
+      req.continue();
     }
   }).as('resetDeviceDelay');
 }

+ 4 - 1
src/API/exam/deviceActions.ts

@@ -23,7 +23,10 @@ const resetAllDevices = async () => {
 
   try {
     console.log(`[deviceActions][resetAllDevices][重置高压发生器] ${JSON.stringify(resetGenerator)}`)
-    await axiosInstance.post('/auth/device/action', resetGenerator);
+    const response = await axiosInstance.post('/auth/device/action', resetGenerator);
+    if(response.data.code!='0x000000'){
+      throw new Error('返回码不是0x000000');
+    }
   } catch (error) {
     console.error(`[重置所有设备出错 ] `, error);
     throw error;