SmartPlugCard.test.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. * Tests for the SmartPlugCard component.
  3. *
  4. * These tests focus on critical regression scenarios:
  5. * - Toggle persistence for auto_on/auto_off settings
  6. * - Power control functionality
  7. * - Status display
  8. */
  9. import { describe, it, expect, vi, beforeEach } from 'vitest';
  10. import { screen, waitFor } from '@testing-library/react';
  11. import userEvent from '@testing-library/user-event';
  12. import { render } from '../utils';
  13. import { SmartPlugCard } from '../../components/SmartPlugCard';
  14. import type { SmartPlug } from '../../api/client';
  15. // Mock data
  16. const createMockPlug = (overrides: Partial<SmartPlug> = {}): SmartPlug => ({
  17. id: 1,
  18. name: 'Test Plug',
  19. plug_type: 'tasmota',
  20. ip_address: '192.168.1.100',
  21. ha_entity_id: null,
  22. ha_power_entity: null,
  23. ha_energy_today_entity: null,
  24. ha_energy_total_entity: null,
  25. // MQTT fields (legacy)
  26. mqtt_topic: null,
  27. mqtt_multiplier: 1.0,
  28. // MQTT power fields
  29. mqtt_power_topic: null,
  30. mqtt_power_path: null,
  31. mqtt_power_multiplier: 1.0,
  32. // MQTT energy fields
  33. mqtt_energy_topic: null,
  34. mqtt_energy_path: null,
  35. mqtt_energy_multiplier: 1.0,
  36. // MQTT state fields
  37. mqtt_state_topic: null,
  38. mqtt_state_path: null,
  39. mqtt_state_on_value: null,
  40. printer_id: 1,
  41. controls_printer_power: true,
  42. enabled: true,
  43. auto_on: true,
  44. auto_off: true,
  45. auto_off_persistent: false,
  46. off_delay_mode: 'time',
  47. off_delay_minutes: 5,
  48. off_temp_threshold: 70,
  49. username: null,
  50. password: null,
  51. power_alert_enabled: false,
  52. power_alert_high: null,
  53. power_alert_low: null,
  54. power_alert_last_triggered: null,
  55. schedule_enabled: false,
  56. schedule_on_time: null,
  57. schedule_off_time: null,
  58. last_state: 'ON',
  59. last_checked: null,
  60. auto_off_executed: false,
  61. show_in_switchbar: false,
  62. created_at: '2024-01-01T00:00:00Z',
  63. updated_at: '2024-01-01T00:00:00Z',
  64. ...overrides,
  65. });
  66. describe('SmartPlugCard', () => {
  67. const mockOnEdit = vi.fn();
  68. beforeEach(() => {
  69. vi.clearAllMocks();
  70. });
  71. describe('rendering', () => {
  72. it('renders plug name', () => {
  73. const plug = createMockPlug({ name: 'My Test Plug' });
  74. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  75. expect(screen.getByText('My Test Plug')).toBeInTheDocument();
  76. });
  77. it('renders plug IP address', () => {
  78. const plug = createMockPlug({ ip_address: '192.168.1.200' });
  79. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  80. expect(screen.getByText('192.168.1.200')).toBeInTheDocument();
  81. });
  82. it('shows power ON/OFF buttons', () => {
  83. const plug = createMockPlug();
  84. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  85. // Look for power control buttons
  86. const buttons = screen.getAllByRole('button');
  87. expect(buttons.length).toBeGreaterThan(0);
  88. });
  89. });
  90. describe('automation settings', () => {
  91. it('shows automation settings section when expanded', async () => {
  92. const user = userEvent.setup();
  93. const plug = createMockPlug();
  94. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  95. // Find and click the settings toggle
  96. const settingsToggle = screen.getByText('Automation Settings');
  97. await user.click(settingsToggle);
  98. // Should show Auto On and Auto Off labels
  99. await waitFor(() => {
  100. expect(screen.getByText('Auto On')).toBeInTheDocument();
  101. expect(screen.getByText('Auto Off')).toBeInTheDocument();
  102. });
  103. });
  104. it('displays auto_off toggle in correct state when enabled', async () => {
  105. const user = userEvent.setup();
  106. const plug = createMockPlug({ auto_off: true });
  107. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  108. // Expand settings
  109. await user.click(screen.getByText('Automation Settings'));
  110. await waitFor(() => {
  111. // The toggle should reflect auto_off = true
  112. const autoOffText = screen.getByText('Auto Off');
  113. expect(autoOffText).toBeInTheDocument();
  114. });
  115. });
  116. it('displays auto_off toggle in correct state when disabled', async () => {
  117. const user = userEvent.setup();
  118. const plug = createMockPlug({ auto_off: false });
  119. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  120. // Expand settings
  121. await user.click(screen.getByText('Automation Settings'));
  122. await waitFor(() => {
  123. const autoOffText = screen.getByText('Auto Off');
  124. expect(autoOffText).toBeInTheDocument();
  125. });
  126. });
  127. it('shows delay mode options when auto_off is enabled', async () => {
  128. const user = userEvent.setup();
  129. const plug = createMockPlug({ auto_off: true, off_delay_mode: 'time' });
  130. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  131. // Expand settings
  132. await user.click(screen.getByText('Automation Settings'));
  133. await waitFor(() => {
  134. // Should show delay mode buttons
  135. expect(screen.getByText('Time')).toBeInTheDocument();
  136. expect(screen.getByText('Temp')).toBeInTheDocument();
  137. });
  138. });
  139. it('does not show delay mode options when auto_off is disabled', async () => {
  140. const user = userEvent.setup();
  141. const plug = createMockPlug({ auto_off: false });
  142. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  143. // Expand settings
  144. await user.click(screen.getByText('Automation Settings'));
  145. await waitFor(() => {
  146. // Delay mode options should not be visible
  147. expect(screen.queryByText('Turn Off Delay Mode')).not.toBeInTheDocument();
  148. });
  149. });
  150. });
  151. describe('schedule display', () => {
  152. it('shows schedule badge when scheduling is enabled', () => {
  153. const plug = createMockPlug({
  154. schedule_enabled: true,
  155. schedule_on_time: '08:00',
  156. schedule_off_time: '22:00',
  157. });
  158. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  159. expect(screen.getByText(/08:00.*22:00/)).toBeInTheDocument();
  160. });
  161. it('does not show schedule badge when scheduling is disabled', () => {
  162. const plug = createMockPlug({
  163. schedule_enabled: false,
  164. schedule_on_time: '08:00',
  165. schedule_off_time: '22:00',
  166. });
  167. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  168. // Schedule times should not be visible
  169. expect(screen.queryByText(/08:00.*22:00/)).not.toBeInTheDocument();
  170. });
  171. });
  172. describe('power control', () => {
  173. it('shows confirmation modal before power off', async () => {
  174. const user = userEvent.setup();
  175. const plug = createMockPlug({ last_state: 'ON' });
  176. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  177. // Find and click the Off button
  178. const offButton = screen.getByRole('button', { name: /off/i });
  179. await user.click(offButton);
  180. // Confirmation modal should appear with the dialog title
  181. await waitFor(() => {
  182. expect(screen.getByText('Turn Off Smart Plug')).toBeInTheDocument();
  183. });
  184. });
  185. });
  186. describe('edit functionality', () => {
  187. it('calls onEdit when edit button is clicked', async () => {
  188. const user = userEvent.setup();
  189. const plug = createMockPlug();
  190. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  191. // Expand settings first
  192. await user.click(screen.getByText('Automation Settings'));
  193. // Find and click edit button
  194. await waitFor(async () => {
  195. const editButtons = screen.getAllByRole('button');
  196. const editButton = editButtons.find(
  197. (btn) =>
  198. btn.textContent?.includes('Edit') ||
  199. btn.querySelector('[class*="pencil"]')
  200. );
  201. if (editButton) {
  202. await user.click(editButton);
  203. }
  204. });
  205. // onEdit should have been called (may not be called if edit button not found)
  206. // This test verifies the interaction pattern
  207. });
  208. });
  209. describe('persistent auto-off', () => {
  210. it('shows Keep Enabled toggle when auto_off is enabled', async () => {
  211. const user = userEvent.setup();
  212. const plug = createMockPlug({ auto_off: true, auto_off_persistent: false });
  213. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  214. await user.click(screen.getByText('Automation Settings'));
  215. await waitFor(() => {
  216. expect(screen.getByText('Keep Enabled')).toBeInTheDocument();
  217. expect(screen.getByText('Stay enabled between prints instead of one-shot')).toBeInTheDocument();
  218. });
  219. });
  220. it('does not show Keep Enabled toggle when auto_off is disabled', async () => {
  221. const user = userEvent.setup();
  222. const plug = createMockPlug({ auto_off: false });
  223. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  224. await user.click(screen.getByText('Automation Settings'));
  225. await waitFor(() => {
  226. expect(screen.queryByText('Keep Enabled')).not.toBeInTheDocument();
  227. });
  228. });
  229. it('shows Keep Enabled toggle for HA plugs with auto_off enabled', async () => {
  230. const user = userEvent.setup();
  231. const plug = createMockPlug({
  232. plug_type: 'homeassistant',
  233. ip_address: null,
  234. ha_entity_id: 'switch.bentobox_filter',
  235. auto_off: true,
  236. auto_off_persistent: true,
  237. });
  238. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  239. await user.click(screen.getByText('Automation Settings'));
  240. await waitFor(() => {
  241. expect(screen.getByText('Keep Enabled')).toBeInTheDocument();
  242. });
  243. });
  244. });
  245. describe('powers the printer toggle (#2629)', () => {
  246. it('shows the toggle when a printer is linked', async () => {
  247. const user = userEvent.setup();
  248. const plug = createMockPlug({ printer_id: 1, controls_printer_power: false });
  249. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  250. await user.click(screen.getByText('Automation Settings'));
  251. await waitFor(() => {
  252. expect(screen.getByText('Powers the printer')).toBeInTheDocument();
  253. });
  254. });
  255. it('hides the toggle when no printer is linked', async () => {
  256. const user = userEvent.setup();
  257. const plug = createMockPlug({ printer_id: null });
  258. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  259. await user.click(screen.getByText('Automation Settings'));
  260. await waitFor(() => {
  261. expect(screen.getByText('Auto On')).toBeInTheDocument();
  262. });
  263. expect(screen.queryByText('Powers the printer')).not.toBeInTheDocument();
  264. });
  265. });
  266. describe('disabled state', () => {
  267. it('renders plug even when disabled', () => {
  268. const plug = createMockPlug({ enabled: false });
  269. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  270. // Plug should still render with its name
  271. expect(screen.getByText('Test Plug')).toBeInTheDocument();
  272. });
  273. });
  274. describe('Home Assistant plugs', () => {
  275. it('renders HA plug with entity_id instead of IP', () => {
  276. const plug = createMockPlug({
  277. plug_type: 'homeassistant',
  278. ip_address: null,
  279. ha_entity_id: 'switch.printer_plug',
  280. });
  281. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  282. // Should show entity_id, not IP
  283. expect(screen.getByText('switch.printer_plug')).toBeInTheDocument();
  284. expect(screen.queryByText('192.168.1.100')).not.toBeInTheDocument();
  285. });
  286. it('renders HA plug name correctly', () => {
  287. const plug = createMockPlug({
  288. name: 'HA Printer Plug',
  289. plug_type: 'homeassistant',
  290. ip_address: null,
  291. ha_entity_id: 'switch.printer_plug',
  292. });
  293. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  294. expect(screen.getByText('HA Printer Plug')).toBeInTheDocument();
  295. });
  296. it('shows power controls for HA plug', () => {
  297. const plug = createMockPlug({
  298. plug_type: 'homeassistant',
  299. ip_address: null,
  300. ha_entity_id: 'switch.printer_plug',
  301. });
  302. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  303. // Power control buttons should still be present
  304. const buttons = screen.getAllByRole('button');
  305. expect(buttons.length).toBeGreaterThan(0);
  306. });
  307. });
  308. describe('MQTT plugs', () => {
  309. it('renders MQTT plug with topic instead of IP', () => {
  310. const plug = createMockPlug({
  311. plug_type: 'mqtt',
  312. ip_address: null,
  313. mqtt_topic: 'zigbee2mqtt/shelly-power',
  314. mqtt_power_path: 'power_l1',
  315. });
  316. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  317. // Should show topic, not IP
  318. expect(screen.getByText('zigbee2mqtt/shelly-power')).toBeInTheDocument();
  319. expect(screen.queryByText('192.168.1.100')).not.toBeInTheDocument();
  320. });
  321. it('renders MQTT plug name correctly', () => {
  322. const plug = createMockPlug({
  323. name: 'MQTT Energy Monitor',
  324. plug_type: 'mqtt',
  325. ip_address: null,
  326. mqtt_topic: 'sensors/power',
  327. mqtt_power_path: 'power',
  328. });
  329. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  330. expect(screen.getByText('MQTT Energy Monitor')).toBeInTheDocument();
  331. });
  332. it('shows Monitor Only badge for MQTT plug', () => {
  333. const plug = createMockPlug({
  334. plug_type: 'mqtt',
  335. ip_address: null,
  336. mqtt_topic: 'test/topic',
  337. mqtt_power_path: 'power',
  338. });
  339. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  340. expect(screen.getByText('Monitor Only')).toBeInTheDocument();
  341. });
  342. it('does not show power control buttons for MQTT plug', () => {
  343. const plug = createMockPlug({
  344. plug_type: 'mqtt',
  345. ip_address: null,
  346. mqtt_topic: 'test/topic',
  347. mqtt_power_path: 'power',
  348. });
  349. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  350. // On/Off buttons should not be present for monitor-only plugs
  351. expect(screen.queryByRole('button', { name: /^on$/i })).not.toBeInTheDocument();
  352. expect(screen.queryByRole('button', { name: /^off$/i })).not.toBeInTheDocument();
  353. });
  354. it('shows Settings instead of Automation Settings for MQTT plug', async () => {
  355. const plug = createMockPlug({
  356. plug_type: 'mqtt',
  357. ip_address: null,
  358. mqtt_topic: 'test/topic',
  359. mqtt_power_path: 'power',
  360. });
  361. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  362. // Should show "Settings" not "Automation Settings"
  363. expect(screen.getByText('Settings')).toBeInTheDocument();
  364. expect(screen.queryByText('Automation Settings')).not.toBeInTheDocument();
  365. });
  366. });
  367. });