device.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Device Mock Handlers
  3. * 设备相关的 mock 处理器
  4. */
  5. /**
  6. * 打开设备 - 成功场景
  7. *
  8. * @description 打开指定设备
  9. * @method POST
  10. * @url /api/v1/auth/device/open
  11. * @access 需要认证
  12. *
  13. * @param {Object} requestBody - 请求体
  14. * @param {string} requestBody.deviceUri - 设备URI
  15. *
  16. * @returns {Object} 成功响应
  17. *
  18. * @example
  19. * mockOpenDeviceSuccess();
  20. * cy.wait('@openDeviceSuccess');
  21. *
  22. * @see docs/DR.md - 章节28
  23. */
  24. export function mockOpenDeviceSuccess() {
  25. cy.intercept('POST', '/api/v1/auth/device/open', {
  26. statusCode: 200,
  27. body: {
  28. code: "0x000000",
  29. description: "Success",
  30. solution: "",
  31. data: {
  32. "@type": "type.googleapis.com/google.protobuf.Empty",
  33. value: {}
  34. }
  35. }
  36. }).as('openDeviceSuccess');
  37. }
  38. /**
  39. * 执行设备Get操作 - 成功场景
  40. *
  41. * @description 执行设备的Get操作,获取设备状态或参数
  42. * @method POST
  43. * @url /api/v1/auth/device/get
  44. * @access 需要认证
  45. *
  46. * @param {Object} requestBody - 请求体
  47. * @param {string} requestBody.deviceUri - 设备URI
  48. * @param {string} requestBody.reqName - 请求名称
  49. *
  50. * @returns {Object} data - 设备状态数据(JSON字符串)
  51. *
  52. * @example
  53. * mockDeviceGetSuccess();
  54. * cy.wait('@deviceGetSuccess');
  55. *
  56. * @see docs/DR.md - 章节29
  57. */
  58. export function mockDeviceGetSuccess() {
  59. cy.intercept('POST', '/api/v1/auth/device/get', {
  60. statusCode: 200,
  61. body: {
  62. code: "0x000000",
  63. description: "Success",
  64. solution: "",
  65. data: JSON.stringify({
  66. IsDemo: { Value: "1" },
  67. GENERATORSTATUS: { Value: "4" },
  68. KV: { Value: "70.0" },
  69. MA: { Value: "125.0" }
  70. })
  71. }
  72. }).as('deviceGetSuccess');
  73. }
  74. /**
  75. * 执行设备Action操作 - 成功场景
  76. *
  77. * @description 执行设备的Action操作,控制设备行为
  78. * @method POST
  79. * @url /api/v1/auth/device/action
  80. * @access 需要认证
  81. *
  82. * @param {Object} requestBody - 请求体
  83. * @param {string} requestBody.deviceUri - 设备URI
  84. * @param {string} requestBody.reqName - 请求名称
  85. * @param {string} requestBody.reqParam - 请求参数
  86. *
  87. * @returns {Object} 成功响应
  88. *
  89. * @example
  90. * mockDeviceActionSuccess();
  91. * cy.wait('@deviceActionSuccess');
  92. *
  93. * @see docs/DR.md - 章节30
  94. */
  95. export function mockDeviceActionSuccess() {
  96. cy.intercept('POST', '/api/v1/auth/device/action', {
  97. statusCode: 200,
  98. body: {
  99. code: "0x000000",
  100. description: "Success",
  101. solution: "",
  102. data: {
  103. "@type": "type.googleapis.com/google.protobuf.Empty",
  104. value: {}
  105. }
  106. }
  107. }).as('deviceActionSuccess');
  108. }
  109. /**
  110. * 重置高压发生器 - 成功场景
  111. *
  112. * @description 重置高压发生器参数到初始状态
  113. * @method POST
  114. * @url /auth/device/action
  115. * @access 需要认证
  116. *
  117. * @param {Object} requestBody - 请求体
  118. * @param {string} requestBody.deviceUri - 设备URI (DIOS/DEVICE/Generator)
  119. * @param {string} requestBody.reqName - 请求名称 (RESET)
  120. *
  121. * @returns {Object} 成功响应
  122. *
  123. * @example
  124. * mockResetDeviceSuccess();
  125. * cy.wait('@resetDevice');
  126. */
  127. export function mockResetDeviceSuccess() {
  128. cy.intercept('POST', '**/auth/device/action', (req) => {
  129. console.log('[Mock] Intercepted device action:', {
  130. url: req.url,
  131. reqName: req.body?.reqName,
  132. deviceUri: req.body?.deviceUri,
  133. body: req.body
  134. });
  135. if (req.body?.reqName === 'RESET') {
  136. req.reply({
  137. statusCode: 200,
  138. body: {
  139. code: '0x000000',
  140. description: 'Success',
  141. data: {}
  142. }
  143. });
  144. } else {
  145. // 非 RESET 请求,让它继续执行
  146. req.continue();
  147. }
  148. }).as('resetDevice');
  149. }
  150. /**
  151. * 重置高压发生器 - 失败场景
  152. *
  153. * @description 模拟设备重置失败
  154. * @method POST
  155. * @url /auth/device/action
  156. * @access 需要认证
  157. *
  158. * @param {string} errorCode - 错误码,默认为'0x010001'
  159. * @param {string} description - 错误描述,默认为'设备通信失败'
  160. *
  161. * @returns {Object} 失败响应
  162. *
  163. * @example
  164. * mockResetDeviceFail();
  165. * cy.wait('@resetDeviceFail');
  166. */
  167. export function mockResetDeviceFail(
  168. errorCode: string = '0x010001',
  169. description: string = '设备通信失败'
  170. ) {
  171. cy.intercept('POST', '**/auth/device/action', (req) => {
  172. console.log('[Mock] Intercepted device action (fail):', {
  173. url: req.url,
  174. reqName: req.body?.reqName,
  175. body: req.body
  176. });
  177. if (req.body?.reqName === 'RESET') {
  178. req.reply({
  179. statusCode: 200,
  180. body: {
  181. code: errorCode,
  182. description: description,
  183. solution: '检查设备连接'
  184. }
  185. });
  186. } else {
  187. req.continue();
  188. }
  189. }).as('resetDeviceFail');
  190. }
  191. /**
  192. * 重置高压发生器 - 网络错误
  193. *
  194. * @description 模拟网络通信错误
  195. * @method POST
  196. * @url /auth/device/action
  197. * @access 需要认证
  198. *
  199. * @returns {Object} 网络错误
  200. *
  201. * @example
  202. * mockResetDeviceNetworkError();
  203. * cy.wait('@resetDeviceNetworkError');
  204. */
  205. export function mockResetDeviceNetworkError() {
  206. cy.intercept('POST', '**/auth/device/action', (req) => {
  207. console.log('[Mock] Intercepted device action (network error):', {
  208. url: req.url,
  209. reqName: req.body?.reqName,
  210. body: req.body
  211. });
  212. if (req.body?.reqName === 'RESET') {
  213. req.reply({ forceNetworkError: true });
  214. } else {
  215. req.continue();
  216. }
  217. }).as('resetDeviceNetworkError');
  218. }
  219. /**
  220. * 重置高压发生器 - 延迟响应
  221. *
  222. * @description 模拟设备重置延迟响应(用于测试loading状态)
  223. * @method POST
  224. * @url /auth/device/action
  225. * @access 需要认证
  226. *
  227. * @param {number} delayMs - 延迟毫秒数
  228. *
  229. * @returns {Object} 延迟后的成功响应
  230. *
  231. * @example
  232. * mockResetDeviceDelay(2000); // 延迟2秒
  233. * cy.wait('@resetDeviceDelay');
  234. */
  235. export function mockResetDeviceDelay(delayMs: number) {
  236. cy.intercept('POST', '**/auth/device/action', (req) => {
  237. console.log('[Mock] Intercepted device action (delay):', {
  238. url: req.url,
  239. reqName: req.body?.reqName,
  240. body: req.body,
  241. delay: delayMs
  242. });
  243. if (req.body?.reqName === 'RESET') {
  244. req.reply({
  245. statusCode: 200,
  246. body: {
  247. code: '0x000000',
  248. description: 'Success',
  249. data: {}
  250. },
  251. delay: delayMs
  252. });
  253. } else {
  254. req.continue();
  255. }
  256. }).as('resetDeviceDelay');
  257. }