StreamOverlayBuilder.test.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Tests for the streaming-overlay URL builder (#1422).
  3. *
  4. * The builder's whole output is a URL, so that is what these assert: the field
  5. * order, what is omitted at its default, and that the preview does not open a
  6. * camera stream until it is asked to.
  7. */
  8. import { describe, it, expect, beforeEach } from 'vitest';
  9. import { screen, waitFor } from '@testing-library/react';
  10. import userEvent from '@testing-library/user-event';
  11. import { http, HttpResponse } from 'msw';
  12. import { render } from '../utils';
  13. import { server } from '../mocks/server';
  14. import { StreamOverlayBuilder } from '../../components/StreamOverlayBuilder';
  15. const printers = [
  16. { id: 1, name: 'X1 Carbon', ip_address: '192.168.1.100', serial_number: '00M09A350100001', model: 'X1C' },
  17. { id: 2, name: 'P1S', ip_address: '192.168.1.101', serial_number: '01P00A000000002', model: 'P1S' },
  18. ];
  19. // The URL is rendered inside a <code>, so read it back the way a user would.
  20. function shownUrl(): string {
  21. const code = document.querySelector('code');
  22. return code?.textContent ?? '';
  23. }
  24. describe('StreamOverlayBuilder', () => {
  25. beforeEach(() => {
  26. server.use(http.get('/api/v1/printers', () => HttpResponse.json(printers)));
  27. });
  28. it('starts on the first printer with the overlay defaults', async () => {
  29. render(<StreamOverlayBuilder />);
  30. await waitFor(() => {
  31. expect(shownUrl()).toContain('/overlay/1');
  32. });
  33. // The same set parseConfig() defaults to, so the builder's starting point
  34. // and a bare /overlay/1 render the same overlay. Emitted in the overlay's
  35. // own top-to-bottom field order rather than parseConfig's listing order —
  36. // ?show= is read with includes(), so order is free to be the stable one.
  37. expect(shownUrl()).toContain('show=filename%2Cstatus%2Cprogress%2Clayers%2Ceta');
  38. // Defaults are omitted rather than spelled out — a shorter URL to paste.
  39. expect(shownUrl()).not.toContain('size=');
  40. expect(shownUrl()).not.toContain('fps=');
  41. expect(shownUrl()).not.toContain('camera=');
  42. expect(shownUrl()).not.toContain('token=');
  43. });
  44. it('keeps the model opt-in and updates the URL and preview when toggled', async () => {
  45. const user = userEvent.setup();
  46. render(<StreamOverlayBuilder />);
  47. const model = await screen.findByLabelText('Printer model');
  48. expect(model).not.toBeChecked();
  49. const originalUrl = shownUrl();
  50. await user.click(model);
  51. expect(new URL(shownUrl()).searchParams.get('show')).toBe('model,filename,status,progress,layers,eta');
  52. await user.click(screen.getByLabelText('Printer name'));
  53. expect(new URL(shownUrl()).searchParams.get('show')).toBe('printer,model,filename,status,progress,layers,eta');
  54. await user.click(screen.getByRole('button', { name: 'Show preview' }));
  55. expect(screen.getByTitle('Overlay preview')).toHaveAttribute('src', shownUrl());
  56. await user.click(model);
  57. await user.click(screen.getByLabelText('Printer name'));
  58. expect(shownUrl()).toBe(originalUrl);
  59. expect(screen.getByTitle('Overlay preview')).toHaveAttribute('src', originalUrl);
  60. });
  61. it('switches printer', async () => {
  62. const user = userEvent.setup();
  63. render(<StreamOverlayBuilder />);
  64. await waitFor(() => expect(screen.getByLabelText('Printer')).toBeInTheDocument());
  65. await user.selectOptions(screen.getByLabelText('Printer'), '2');
  66. await waitFor(() => expect(shownUrl()).toContain('/overlay/2'));
  67. });
  68. it('adds a temperature field the URL did not have', async () => {
  69. const user = userEvent.setup();
  70. render(<StreamOverlayBuilder />);
  71. await waitFor(() => expect(screen.getByLabelText('Nozzle')).toBeInTheDocument());
  72. await user.click(screen.getByLabelText('Nozzle'));
  73. await waitFor(() =>
  74. expect(shownUrl()).toContain('show=filename%2Cstatus%2Cprogress%2Clayers%2Ceta%2Cnozzle'),
  75. );
  76. });
  77. it('emits fields in the overlay order, not the order they were clicked', async () => {
  78. const user = userEvent.setup();
  79. render(<StreamOverlayBuilder />);
  80. // "Printer name" is first in the overlay's own top-to-bottom order, so
  81. // ticking it last must still put it at the front. Otherwise the same
  82. // selection would produce a different URL depending on click order, and a
  83. // scene file would stop being comparable to the one next to it.
  84. await waitFor(() => expect(screen.getByLabelText('Printer name')).toBeInTheDocument());
  85. await user.click(screen.getByLabelText('Printer name'));
  86. await waitFor(() => expect(shownUrl()).toContain('show=printer%2Cfilename'));
  87. });
  88. it('drops a field when its box is cleared', async () => {
  89. const user = userEvent.setup();
  90. render(<StreamOverlayBuilder />);
  91. await waitFor(() => expect(screen.getByLabelText('Layer count')).toBeInTheDocument());
  92. await user.click(screen.getByLabelText('Layer count'));
  93. await waitFor(() => expect(shownUrl()).not.toContain('layers'));
  94. expect(shownUrl()).toContain('progress');
  95. });
  96. it('emits camera=false when the camera feed is switched off', async () => {
  97. const user = userEvent.setup();
  98. render(<StreamOverlayBuilder />);
  99. await waitFor(() => expect(screen.getByLabelText('Camera feed')).toBeInTheDocument());
  100. await user.click(screen.getByLabelText('Camera feed'));
  101. await waitFor(() => expect(shownUrl()).toContain('camera=false'));
  102. });
  103. it('emits size and fps only when they differ from the defaults', async () => {
  104. const user = userEvent.setup();
  105. render(<StreamOverlayBuilder />);
  106. await waitFor(() => expect(screen.getByLabelText('Text size')).toBeInTheDocument());
  107. await user.selectOptions(screen.getByLabelText('Text size'), 'large');
  108. await waitFor(() => expect(shownUrl()).toContain('size=large'));
  109. await user.selectOptions(screen.getByLabelText('Text size'), 'medium');
  110. await waitFor(() => expect(shownUrl()).not.toContain('size='));
  111. });
  112. it('appends a token and warns that the URL is now a key', async () => {
  113. const user = userEvent.setup();
  114. render(<StreamOverlayBuilder />);
  115. await waitFor(() => expect(screen.getByLabelText(/token/i)).toBeInTheDocument());
  116. expect(screen.queryByText(/This URL contains a token/)).not.toBeInTheDocument();
  117. await user.type(screen.getByLabelText(/token/i), 'bblt_abc');
  118. await waitFor(() => expect(shownUrl()).toContain('token=bblt_abc'));
  119. expect(screen.getByText(/This URL contains a token/)).toBeInTheDocument();
  120. });
  121. it('opens no camera stream until the preview is asked for', async () => {
  122. const user = userEvent.setup();
  123. render(<StreamOverlayBuilder />);
  124. await waitFor(() => expect(screen.getByText('Show preview')).toBeInTheDocument());
  125. // An always-on preview would hold a subscriber on the printer's single
  126. // camera connection for as long as the settings tab stays open.
  127. expect(document.querySelector('iframe')).toBeNull();
  128. await user.click(screen.getByText('Show preview'));
  129. await waitFor(() => expect(document.querySelector('iframe')).not.toBeNull());
  130. expect(document.querySelector('iframe')?.getAttribute('src')).toContain('/overlay/1');
  131. });
  132. it('still builds a URL when the printer list cannot be loaded', async () => {
  133. server.use(http.get('/api/v1/printers', () => HttpResponse.json({ detail: 'nope' }, { status: 500 })));
  134. render(<StreamOverlayBuilder />);
  135. // Falls back to printer 1 rather than rendering /overlay/null — the number
  136. // is the one thing the user can fix by hand in the URL.
  137. await waitFor(() => expect(shownUrl()).toContain('/overlay/1'));
  138. });
  139. });