SmartPlugCard.test.tsx 15 KB

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