StreamOverlayBuilder.test.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('switches printer', async () => {
  45. const user = userEvent.setup();
  46. render(<StreamOverlayBuilder />);
  47. await waitFor(() => expect(screen.getByLabelText('Printer')).toBeInTheDocument());
  48. await user.selectOptions(screen.getByLabelText('Printer'), '2');
  49. await waitFor(() => expect(shownUrl()).toContain('/overlay/2'));
  50. });
  51. it('adds a temperature field the URL did not have', async () => {
  52. const user = userEvent.setup();
  53. render(<StreamOverlayBuilder />);
  54. await waitFor(() => expect(screen.getByLabelText('Nozzle')).toBeInTheDocument());
  55. await user.click(screen.getByLabelText('Nozzle'));
  56. await waitFor(() =>
  57. expect(shownUrl()).toContain('show=filename%2Cstatus%2Cprogress%2Clayers%2Ceta%2Cnozzle'),
  58. );
  59. });
  60. it('emits fields in the overlay order, not the order they were clicked', async () => {
  61. const user = userEvent.setup();
  62. render(<StreamOverlayBuilder />);
  63. // "Printer name" is first in the overlay's own top-to-bottom order, so
  64. // ticking it last must still put it at the front. Otherwise the same
  65. // selection would produce a different URL depending on click order, and a
  66. // scene file would stop being comparable to the one next to it.
  67. await waitFor(() => expect(screen.getByLabelText('Printer name')).toBeInTheDocument());
  68. await user.click(screen.getByLabelText('Printer name'));
  69. await waitFor(() => expect(shownUrl()).toContain('show=printer%2Cfilename'));
  70. });
  71. it('drops a field when its box is cleared', async () => {
  72. const user = userEvent.setup();
  73. render(<StreamOverlayBuilder />);
  74. await waitFor(() => expect(screen.getByLabelText('Layer count')).toBeInTheDocument());
  75. await user.click(screen.getByLabelText('Layer count'));
  76. await waitFor(() => expect(shownUrl()).not.toContain('layers'));
  77. expect(shownUrl()).toContain('progress');
  78. });
  79. it('emits camera=false when the camera feed is switched off', async () => {
  80. const user = userEvent.setup();
  81. render(<StreamOverlayBuilder />);
  82. await waitFor(() => expect(screen.getByLabelText('Camera feed')).toBeInTheDocument());
  83. await user.click(screen.getByLabelText('Camera feed'));
  84. await waitFor(() => expect(shownUrl()).toContain('camera=false'));
  85. });
  86. it('emits size and fps only when they differ from the defaults', async () => {
  87. const user = userEvent.setup();
  88. render(<StreamOverlayBuilder />);
  89. await waitFor(() => expect(screen.getByLabelText('Text size')).toBeInTheDocument());
  90. await user.selectOptions(screen.getByLabelText('Text size'), 'large');
  91. await waitFor(() => expect(shownUrl()).toContain('size=large'));
  92. await user.selectOptions(screen.getByLabelText('Text size'), 'medium');
  93. await waitFor(() => expect(shownUrl()).not.toContain('size='));
  94. });
  95. it('appends a token and warns that the URL is now a key', async () => {
  96. const user = userEvent.setup();
  97. render(<StreamOverlayBuilder />);
  98. await waitFor(() => expect(screen.getByLabelText(/token/i)).toBeInTheDocument());
  99. expect(screen.queryByText(/This URL contains a token/)).not.toBeInTheDocument();
  100. await user.type(screen.getByLabelText(/token/i), 'bblt_abc');
  101. await waitFor(() => expect(shownUrl()).toContain('token=bblt_abc'));
  102. expect(screen.getByText(/This URL contains a token/)).toBeInTheDocument();
  103. });
  104. it('opens no camera stream until the preview is asked for', async () => {
  105. const user = userEvent.setup();
  106. render(<StreamOverlayBuilder />);
  107. await waitFor(() => expect(screen.getByText('Show preview')).toBeInTheDocument());
  108. // An always-on preview would hold a subscriber on the printer's single
  109. // camera connection for as long as the settings tab stays open.
  110. expect(document.querySelector('iframe')).toBeNull();
  111. await user.click(screen.getByText('Show preview'));
  112. await waitFor(() => expect(document.querySelector('iframe')).not.toBeNull());
  113. expect(document.querySelector('iframe')?.getAttribute('src')).toContain('/overlay/1');
  114. });
  115. it('still builds a URL when the printer list cannot be loaded', async () => {
  116. server.use(http.get('/api/v1/printers', () => HttpResponse.json({ detail: 'nope' }, { status: 500 })));
  117. render(<StreamOverlayBuilder />);
  118. // Falls back to printer 1 rather than rendering /overlay/null — the number
  119. // is the one thing the user can fix by hand in the URL.
  120. await waitFor(() => expect(shownUrl()).toContain('/overlay/1'));
  121. });
  122. });