SmartPlugCard.test.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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('names a REST plug by its URL for an editor and by its type for a reader', () => {
  78. const { unmount } = render(
  79. <SmartPlugCard
  80. plug={createMockPlug({ plug_type: 'rest', ip_address: null, rest_on_url: 'http://plug.example/on' })}
  81. onEdit={mockOnEdit}
  82. />,
  83. );
  84. expect(screen.getByText('http://plug.example/on')).toBeInTheDocument();
  85. unmount();
  86. render(
  87. <SmartPlugCard
  88. plug={createMockPlug({ plug_type: 'rest', ip_address: null, rest_on_url: null, rest_off_url: null })}
  89. onEdit={mockOnEdit}
  90. />,
  91. );
  92. expect(screen.getByText('REST')).toBeInTheDocument();
  93. });
  94. it('renders plug IP address', () => {
  95. const plug = createMockPlug({ ip_address: '192.168.1.200' });
  96. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  97. expect(screen.getByText('192.168.1.200')).toBeInTheDocument();
  98. });
  99. it('shows power ON/OFF buttons', () => {
  100. const plug = createMockPlug();
  101. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  102. // Look for power control buttons
  103. const buttons = screen.getAllByRole('button');
  104. expect(buttons.length).toBeGreaterThan(0);
  105. });
  106. });
  107. describe('automation settings', () => {
  108. it('shows automation settings section when expanded', async () => {
  109. const user = userEvent.setup();
  110. const plug = createMockPlug();
  111. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  112. // Find and click the settings toggle
  113. const settingsToggle = screen.getByText('Automation Settings');
  114. await user.click(settingsToggle);
  115. // Should show Auto On and Auto Off labels
  116. await waitFor(() => {
  117. expect(screen.getByText('Auto On')).toBeInTheDocument();
  118. expect(screen.getByText('Auto Off')).toBeInTheDocument();
  119. });
  120. });
  121. it('displays auto_off toggle in correct state when enabled', async () => {
  122. const user = userEvent.setup();
  123. const plug = createMockPlug({ auto_off: true });
  124. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  125. // Expand settings
  126. await user.click(screen.getByText('Automation Settings'));
  127. await waitFor(() => {
  128. // The toggle should reflect auto_off = true
  129. const autoOffText = screen.getByText('Auto Off');
  130. expect(autoOffText).toBeInTheDocument();
  131. });
  132. });
  133. it('displays auto_off toggle in correct state when disabled', async () => {
  134. const user = userEvent.setup();
  135. const plug = createMockPlug({ auto_off: false });
  136. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  137. // Expand settings
  138. await user.click(screen.getByText('Automation Settings'));
  139. await waitFor(() => {
  140. const autoOffText = screen.getByText('Auto Off');
  141. expect(autoOffText).toBeInTheDocument();
  142. });
  143. });
  144. it('shows delay mode options when auto_off is enabled', async () => {
  145. const user = userEvent.setup();
  146. const plug = createMockPlug({ auto_off: true, off_delay_mode: 'time' });
  147. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  148. // Expand settings
  149. await user.click(screen.getByText('Automation Settings'));
  150. await waitFor(() => {
  151. // Should show delay mode buttons
  152. expect(screen.getByText('Time')).toBeInTheDocument();
  153. expect(screen.getByText('Temp')).toBeInTheDocument();
  154. });
  155. });
  156. it('does not show delay mode options when auto_off is disabled', async () => {
  157. const user = userEvent.setup();
  158. const plug = createMockPlug({ auto_off: false });
  159. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  160. // Expand settings
  161. await user.click(screen.getByText('Automation Settings'));
  162. await waitFor(() => {
  163. // Delay mode options should not be visible
  164. expect(screen.queryByText('Turn Off Delay Mode')).not.toBeInTheDocument();
  165. });
  166. });
  167. });
  168. describe('schedule display', () => {
  169. it('shows schedule badge when scheduling is enabled', () => {
  170. const plug = createMockPlug({
  171. schedule_enabled: true,
  172. schedule_on_time: '08:00',
  173. schedule_off_time: '22:00',
  174. });
  175. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  176. expect(screen.getByText(/08:00.*22:00/)).toBeInTheDocument();
  177. });
  178. it('does not show schedule badge when scheduling is disabled', () => {
  179. const plug = createMockPlug({
  180. schedule_enabled: false,
  181. schedule_on_time: '08:00',
  182. schedule_off_time: '22:00',
  183. });
  184. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  185. // Schedule times should not be visible
  186. expect(screen.queryByText(/08:00.*22:00/)).not.toBeInTheDocument();
  187. });
  188. });
  189. describe('power control', () => {
  190. it('shows confirmation modal before power off', async () => {
  191. const user = userEvent.setup();
  192. const plug = createMockPlug({ last_state: 'ON' });
  193. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  194. // Find and click the Off button
  195. const offButton = screen.getByRole('button', { name: /off/i });
  196. await user.click(offButton);
  197. // Confirmation modal should appear with the dialog title
  198. await waitFor(() => {
  199. expect(screen.getByText('Turn Off Smart Plug')).toBeInTheDocument();
  200. });
  201. });
  202. });
  203. describe('edit functionality', () => {
  204. it('calls onEdit when edit button is clicked', async () => {
  205. const user = userEvent.setup();
  206. const plug = createMockPlug();
  207. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  208. // Expand settings first
  209. await user.click(screen.getByText('Automation Settings'));
  210. // Find and click edit button
  211. await waitFor(async () => {
  212. const editButtons = screen.getAllByRole('button');
  213. const editButton = editButtons.find(
  214. (btn) =>
  215. btn.textContent?.includes('Edit') ||
  216. btn.querySelector('[class*="pencil"]')
  217. );
  218. if (editButton) {
  219. await user.click(editButton);
  220. }
  221. });
  222. // onEdit should have been called (may not be called if edit button not found)
  223. // This test verifies the interaction pattern
  224. });
  225. });
  226. describe('persistent auto-off', () => {
  227. it('shows Keep Enabled toggle when auto_off is enabled', async () => {
  228. const user = userEvent.setup();
  229. const plug = createMockPlug({ auto_off: true, auto_off_persistent: false });
  230. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  231. await user.click(screen.getByText('Automation Settings'));
  232. await waitFor(() => {
  233. expect(screen.getByText('Keep Enabled')).toBeInTheDocument();
  234. expect(screen.getByText('Stay enabled between prints instead of one-shot')).toBeInTheDocument();
  235. });
  236. });
  237. it('does not show Keep Enabled toggle when auto_off is disabled', async () => {
  238. const user = userEvent.setup();
  239. const plug = createMockPlug({ auto_off: false });
  240. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  241. await user.click(screen.getByText('Automation Settings'));
  242. await waitFor(() => {
  243. expect(screen.queryByText('Keep Enabled')).not.toBeInTheDocument();
  244. });
  245. });
  246. it('shows Keep Enabled toggle for HA plugs with auto_off enabled', async () => {
  247. const user = userEvent.setup();
  248. const plug = createMockPlug({
  249. plug_type: 'homeassistant',
  250. ip_address: null,
  251. ha_entity_id: 'switch.bentobox_filter',
  252. auto_off: true,
  253. auto_off_persistent: true,
  254. });
  255. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  256. await user.click(screen.getByText('Automation Settings'));
  257. await waitFor(() => {
  258. expect(screen.getByText('Keep Enabled')).toBeInTheDocument();
  259. });
  260. });
  261. });
  262. describe('powers the printer toggle (#2629)', () => {
  263. it('shows the toggle when a printer is linked', async () => {
  264. const user = userEvent.setup();
  265. const plug = createMockPlug({ printer_id: 1, controls_printer_power: false });
  266. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  267. await user.click(screen.getByText('Automation Settings'));
  268. await waitFor(() => {
  269. expect(screen.getByText('Powers the printer')).toBeInTheDocument();
  270. });
  271. });
  272. it('hides the toggle when no printer is linked', async () => {
  273. const user = userEvent.setup();
  274. const plug = createMockPlug({ printer_id: null });
  275. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  276. await user.click(screen.getByText('Automation Settings'));
  277. await waitFor(() => {
  278. expect(screen.getByText('Auto On')).toBeInTheDocument();
  279. });
  280. expect(screen.queryByText('Powers the printer')).not.toBeInTheDocument();
  281. });
  282. });
  283. describe('disabled state', () => {
  284. it('renders plug even when disabled', () => {
  285. const plug = createMockPlug({ enabled: false });
  286. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  287. // Plug should still render with its name
  288. expect(screen.getByText('Test Plug')).toBeInTheDocument();
  289. });
  290. });
  291. describe('Home Assistant plugs', () => {
  292. it('renders HA plug with entity_id instead of IP', () => {
  293. const plug = createMockPlug({
  294. plug_type: 'homeassistant',
  295. ip_address: null,
  296. ha_entity_id: 'switch.printer_plug',
  297. });
  298. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  299. // Should show entity_id, not IP
  300. expect(screen.getByText('switch.printer_plug')).toBeInTheDocument();
  301. expect(screen.queryByText('192.168.1.100')).not.toBeInTheDocument();
  302. });
  303. it('renders HA plug name correctly', () => {
  304. const plug = createMockPlug({
  305. name: 'HA Printer Plug',
  306. plug_type: 'homeassistant',
  307. ip_address: null,
  308. ha_entity_id: 'switch.printer_plug',
  309. });
  310. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  311. expect(screen.getByText('HA Printer Plug')).toBeInTheDocument();
  312. });
  313. it('shows power controls for HA plug', () => {
  314. const plug = createMockPlug({
  315. plug_type: 'homeassistant',
  316. ip_address: null,
  317. ha_entity_id: 'switch.printer_plug',
  318. });
  319. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  320. // Power control buttons should still be present
  321. const buttons = screen.getAllByRole('button');
  322. expect(buttons.length).toBeGreaterThan(0);
  323. });
  324. });
  325. describe('MQTT plugs', () => {
  326. it('renders MQTT plug with topic instead of IP', () => {
  327. const plug = createMockPlug({
  328. plug_type: 'mqtt',
  329. ip_address: null,
  330. mqtt_topic: 'zigbee2mqtt/shelly-power',
  331. mqtt_power_path: 'power_l1',
  332. });
  333. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  334. // Should show topic, not IP
  335. expect(screen.getByText('zigbee2mqtt/shelly-power')).toBeInTheDocument();
  336. expect(screen.queryByText('192.168.1.100')).not.toBeInTheDocument();
  337. });
  338. it('renders MQTT plug name correctly', () => {
  339. const plug = createMockPlug({
  340. name: 'MQTT Energy Monitor',
  341. plug_type: 'mqtt',
  342. ip_address: null,
  343. mqtt_topic: 'sensors/power',
  344. mqtt_power_path: 'power',
  345. });
  346. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  347. expect(screen.getByText('MQTT Energy Monitor')).toBeInTheDocument();
  348. });
  349. it('shows Monitor Only badge for MQTT plug', () => {
  350. const plug = createMockPlug({
  351. plug_type: 'mqtt',
  352. ip_address: null,
  353. mqtt_topic: 'test/topic',
  354. mqtt_power_path: 'power',
  355. });
  356. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  357. expect(screen.getByText('Monitor Only')).toBeInTheDocument();
  358. });
  359. it('does not show power control buttons for MQTT plug', () => {
  360. const plug = createMockPlug({
  361. plug_type: 'mqtt',
  362. ip_address: null,
  363. mqtt_topic: 'test/topic',
  364. mqtt_power_path: 'power',
  365. });
  366. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  367. // On/Off buttons should not be present for monitor-only plugs
  368. expect(screen.queryByRole('button', { name: /^on$/i })).not.toBeInTheDocument();
  369. expect(screen.queryByRole('button', { name: /^off$/i })).not.toBeInTheDocument();
  370. });
  371. it('shows Settings instead of Automation Settings for MQTT plug', async () => {
  372. const plug = createMockPlug({
  373. plug_type: 'mqtt',
  374. ip_address: null,
  375. mqtt_topic: 'test/topic',
  376. mqtt_power_path: 'power',
  377. });
  378. render(<SmartPlugCard plug={plug} onEdit={mockOnEdit} />);
  379. // Should show "Settings" not "Automation Settings"
  380. expect(screen.getByText('Settings')).toBeInTheDocument();
  381. expect(screen.queryByText('Automation Settings')).not.toBeInTheDocument();
  382. });
  383. });
  384. });