handlers.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * MSW request handlers for mocking API responses in tests.
  3. */
  4. import { http, HttpResponse } from 'msw';
  5. // Sample data
  6. const mockSmartPlugs = [
  7. {
  8. id: 1,
  9. name: 'Test Plug',
  10. ip_address: '192.168.1.100',
  11. printer_id: 1,
  12. enabled: true,
  13. auto_on: true,
  14. auto_off: true,
  15. off_delay_mode: 'time',
  16. off_delay_minutes: 5,
  17. off_temp_threshold: 70,
  18. username: null,
  19. password: null,
  20. power_alert_enabled: false,
  21. power_alert_high: null,
  22. power_alert_low: null,
  23. power_alert_last_triggered: null,
  24. schedule_enabled: false,
  25. schedule_on_time: null,
  26. schedule_off_time: null,
  27. last_state: 'ON',
  28. last_checked: null,
  29. auto_off_executed: false,
  30. auto_off_pending: false,
  31. auto_off_pending_since: null,
  32. created_at: '2024-01-01T00:00:00Z',
  33. updated_at: '2024-01-01T00:00:00Z',
  34. },
  35. ];
  36. const mockNotificationProviders = [
  37. {
  38. id: 1,
  39. name: 'Test Webhook',
  40. provider_type: 'webhook',
  41. enabled: true,
  42. config: { webhook_url: 'http://test.local/webhook' },
  43. on_print_start: true,
  44. on_print_complete: true,
  45. on_print_failed: true,
  46. on_print_stopped: false,
  47. on_print_progress: false,
  48. on_printer_offline: false,
  49. on_printer_error: false,
  50. on_filament_low: false,
  51. on_maintenance_due: false,
  52. on_ams_humidity_high: false,
  53. on_ams_temperature_high: false,
  54. quiet_hours_enabled: false,
  55. quiet_hours_start: null,
  56. quiet_hours_end: null,
  57. daily_digest_enabled: false,
  58. daily_digest_time: null,
  59. printer_id: null,
  60. last_success: null,
  61. last_error: null,
  62. last_error_at: null,
  63. created_at: '2024-01-01T00:00:00Z',
  64. updated_at: '2024-01-01T00:00:00Z',
  65. },
  66. ];
  67. const mockPrinters = [
  68. {
  69. id: 1,
  70. name: 'Test Printer',
  71. serial_number: '00M09A000000000',
  72. ip_address: '192.168.1.200',
  73. is_active: true,
  74. model: 'X1C',
  75. nozzle_count: 1,
  76. auto_archive: true,
  77. location: null,
  78. created_at: '2024-01-01T00:00:00Z',
  79. updated_at: '2024-01-01T00:00:00Z',
  80. },
  81. ];
  82. export const handlers = [
  83. // ========================================================================
  84. // Smart Plugs
  85. // ========================================================================
  86. http.get('/api/v1/smart-plugs/', () => {
  87. return HttpResponse.json(mockSmartPlugs);
  88. }),
  89. http.get('/api/v1/smart-plugs/:id', ({ params }) => {
  90. const plug = mockSmartPlugs.find((p) => p.id === Number(params.id));
  91. if (!plug) {
  92. return new HttpResponse(null, { status: 404 });
  93. }
  94. return HttpResponse.json(plug);
  95. }),
  96. http.post('/api/v1/smart-plugs/', async ({ request }) => {
  97. const body = (await request.json()) as Record<string, unknown>;
  98. const { id: _id, ...baseData } = mockSmartPlugs[0];
  99. const newPlug = {
  100. id: mockSmartPlugs.length + 1,
  101. ...baseData,
  102. ...body,
  103. };
  104. return HttpResponse.json(newPlug);
  105. }),
  106. http.patch('/api/v1/smart-plugs/:id', async ({ params, request }) => {
  107. const body = (await request.json()) as Record<string, unknown>;
  108. const plug = mockSmartPlugs.find((p) => p.id === Number(params.id));
  109. if (!plug) {
  110. return new HttpResponse(null, { status: 404 });
  111. }
  112. return HttpResponse.json({ ...plug, ...body });
  113. }),
  114. http.delete('/api/v1/smart-plugs/:id', ({ params }) => {
  115. const index = mockSmartPlugs.findIndex((p) => p.id === Number(params.id));
  116. if (index === -1) {
  117. return new HttpResponse(null, { status: 404 });
  118. }
  119. return HttpResponse.json({ success: true });
  120. }),
  121. http.get('/api/v1/smart-plugs/:id/status', () => {
  122. return HttpResponse.json({
  123. state: 'ON',
  124. reachable: true,
  125. device_name: 'Test Plug',
  126. energy: {
  127. power: 150.5,
  128. voltage: 120.0,
  129. current: 1.25,
  130. today: 2.5,
  131. total: 100.0,
  132. },
  133. });
  134. }),
  135. http.post('/api/v1/smart-plugs/:id/control', async ({ request }) => {
  136. const body = (await request.json()) as { action: string };
  137. return HttpResponse.json({
  138. success: true,
  139. action: body.action,
  140. });
  141. }),
  142. // ========================================================================
  143. // Notification Providers
  144. // ========================================================================
  145. http.get('/api/v1/notifications/', () => {
  146. return HttpResponse.json(mockNotificationProviders);
  147. }),
  148. http.get('/api/v1/notifications/:id', ({ params }) => {
  149. const provider = mockNotificationProviders.find(
  150. (p) => p.id === Number(params.id)
  151. );
  152. if (!provider) {
  153. return new HttpResponse(null, { status: 404 });
  154. }
  155. return HttpResponse.json(provider);
  156. }),
  157. http.post('/api/v1/notifications/', async ({ request }) => {
  158. const body = (await request.json()) as Record<string, unknown>;
  159. const { id: _id, ...baseData } = mockNotificationProviders[0];
  160. const newProvider = {
  161. id: mockNotificationProviders.length + 1,
  162. ...baseData,
  163. ...body,
  164. };
  165. return HttpResponse.json(newProvider);
  166. }),
  167. http.patch('/api/v1/notifications/:id', async ({ params, request }) => {
  168. const body = (await request.json()) as Record<string, unknown>;
  169. const provider = mockNotificationProviders.find(
  170. (p) => p.id === Number(params.id)
  171. );
  172. if (!provider) {
  173. return new HttpResponse(null, { status: 404 });
  174. }
  175. return HttpResponse.json({ ...provider, ...body });
  176. }),
  177. http.delete('/api/v1/notifications/:id', ({ params }) => {
  178. const index = mockNotificationProviders.findIndex(
  179. (p) => p.id === Number(params.id)
  180. );
  181. if (index === -1) {
  182. return new HttpResponse(null, { status: 404 });
  183. }
  184. return HttpResponse.json({ success: true });
  185. }),
  186. http.post('/api/v1/notifications/:id/test', () => {
  187. return HttpResponse.json({
  188. success: true,
  189. message: 'Test notification sent',
  190. });
  191. }),
  192. // ========================================================================
  193. // Printers
  194. // ========================================================================
  195. http.get('/api/v1/printers/', () => {
  196. return HttpResponse.json(mockPrinters);
  197. }),
  198. http.get('/api/v1/printers/:id', ({ params }) => {
  199. const printer = mockPrinters.find((p) => p.id === Number(params.id));
  200. if (!printer) {
  201. return new HttpResponse(null, { status: 404 });
  202. }
  203. return HttpResponse.json(printer);
  204. }),
  205. http.get('/api/v1/printers/:id/status', ({ params }) => {
  206. return HttpResponse.json({
  207. id: Number(params.id),
  208. name: 'Test Printer',
  209. connected: true,
  210. state: 'IDLE',
  211. progress: 0,
  212. layer_num: 0,
  213. total_layers: 0,
  214. temperatures: {
  215. nozzle: 25,
  216. bed: 25,
  217. chamber: 25,
  218. },
  219. remaining_time: 0,
  220. filename: null,
  221. });
  222. }),
  223. // ========================================================================
  224. // Settings
  225. // ========================================================================
  226. http.get('/api/v1/settings/', () => {
  227. return HttpResponse.json({
  228. auto_archive: true,
  229. save_thumbnails: true,
  230. capture_finish_photo: true,
  231. default_filament_cost: 25.0,
  232. currency: 'USD',
  233. ams_humidity_good: 40,
  234. ams_humidity_fair: 60,
  235. ams_temp_good: 30,
  236. ams_temp_fair: 35,
  237. });
  238. }),
  239. http.patch('/api/v1/settings/', async ({ request }) => {
  240. const body = (await request.json()) as Record<string, unknown>;
  241. return HttpResponse.json(body);
  242. }),
  243. // ========================================================================
  244. // Version / Health
  245. // ========================================================================
  246. http.get('/api/v1/version', () => {
  247. return HttpResponse.json({
  248. version: '0.1.5',
  249. build: 'test',
  250. });
  251. }),
  252. http.get('/health', () => {
  253. return HttpResponse.json({ status: 'healthy' });
  254. }),
  255. ];