| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257 |
- /**
- * Tests for the PrintersPage component.
- */
- import { describe, it, expect, beforeEach } from 'vitest';
- import { screen, waitFor, fireEvent } from '@testing-library/react';
- import userEvent from '@testing-library/user-event';
- import { render } from '../utils';
- import { PrintersPage } from '../../pages/PrintersPage';
- import { http, HttpResponse } from 'msw';
- import { server } from '../mocks/server';
- const mockPrinters = [
- {
- id: 1,
- name: 'X1 Carbon',
- ip_address: '192.168.1.100',
- serial_number: '00M09A350100001',
- access_code: '12345678',
- model: 'X1C',
- enabled: true,
- nozzle_diameter: 0.4,
- nozzle_type: 'hardened_steel',
- location: 'Workshop',
- auto_archive: true,
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
- },
- {
- id: 2,
- name: 'P1S Backup',
- ip_address: '192.168.1.101',
- serial_number: '00W00A123456789',
- access_code: '87654321',
- model: 'P1S',
- enabled: false,
- nozzle_diameter: 0.4,
- nozzle_type: 'stainless_steel',
- location: null,
- auto_archive: true,
- created_at: '2024-01-02T00:00:00Z',
- updated_at: '2024-01-02T00:00:00Z',
- },
- ];
- const mockPrinterStatus = {
- connected: true,
- state: 'IDLE',
- awaiting_plate_clear: false,
- progress: 0,
- layer_num: 0,
- total_layers: 0,
- temperatures: {
- nozzle: 25,
- bed: 25,
- chamber: 25,
- },
- remaining_time: 0,
- filename: null,
- wifi_signal: -50,
- vt_tray: [],
- };
- const selectToolbarDropdownOption = async (triggerName: RegExp, optionName: RegExp) => {
- const user = userEvent.setup();
- await user.click(screen.getByRole('button', { name: triggerName }));
- await user.click(await screen.findByRole('button', { name: optionName }));
- };
- describe('PrintersPage', () => {
- beforeEach(() => {
- localStorage.removeItem('printerCardSize');
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json(mockPrinters);
- }),
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json(mockPrinterStatus);
- }),
- http.post('/api/v1/printers/:id/clear-plate', () => {
- return HttpResponse.json({ success: true, message: 'Plate cleared' });
- }),
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- auto_archive: true,
- save_thumbnails: true,
- capture_finish_photo: true,
- default_filament_cost: 25.0,
- currency: 'USD',
- ams_humidity_good: 40,
- ams_humidity_fair: 60,
- ams_temp_good: 30,
- ams_temp_fair: 35,
- require_plate_clear: true,
- });
- }),
- // PrintersPage now reads UI rendering fields from the public ui-preferences
- // endpoint instead of /settings (#1293) — admin pages still hit /settings.
- http.get('/api/v1/settings/ui-preferences', () => {
- return HttpResponse.json({
- ams_humidity_good: 40,
- ams_humidity_fair: 60,
- ams_temp_good: 30,
- ams_temp_fair: 35,
- require_plate_clear: true,
- });
- }),
- http.get('/api/v1/queue/', () => {
- return HttpResponse.json([]);
- })
- );
- });
- describe('rendering', () => {
- it('renders the page title', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('Printers')).toBeInTheDocument();
- });
- });
- it('shows printer cards', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- });
- it('shows printer models', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1C')).toBeInTheDocument();
- expect(screen.getByText('P1S')).toBeInTheDocument();
- });
- });
- it('shows printer status', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- // Status should be shown - may vary based on state
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- });
- });
- describe('printer info', () => {
- it('shows IP address in printer info modal', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // IP address is shown in the PrinterInfoModal (accessed via 3-dot menu),
- // not directly on the card. Verify the printer data loaded correctly.
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- it('shows location when set', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- // Printers should render - location display may vary
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- });
- });
- describe('temperature display', () => {
- it('shows nozzle temperature', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- // Temperatures are shown in the UI
- expect(screen.getAllByText(/25/)).toBeTruthy();
- });
- });
- });
- describe('empty state', () => {
- it('shows empty state when no printers', async () => {
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json([]);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText(/no printers/i)).toBeInTheDocument();
- });
- });
- });
- describe('printer actions', () => {
- it('has action buttons', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // There should be some interactive elements for printer actions
- const buttons = screen.getAllByRole('button');
- expect(buttons.length).toBeGreaterThan(0);
- });
- it('shows plate clear status and action on finished printers when not cleared', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
- });
- expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
- });
- it('shows plate clear status and action on failed printers when not cleared', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'FAILED', awaiting_plate_clear: true });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
- });
- expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
- });
- it('keeps the clear action available when an idle printer is still awaiting acknowledgment', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'IDLE', awaiting_plate_clear: true });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
- });
- expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
- });
- it('updates the plate clear status after using the printer card action', async () => {
- let awaitingPlateClear = true;
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json([mockPrinters[0]]);
- }),
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
- }),
- http.post('/api/v1/printers/:id/clear-plate', () => {
- awaitingPlateClear = false;
- return HttpResponse.json({ success: true, message: 'Plate cleared' });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
- });
- fireEvent.click(screen.getAllByRole('button', { name: 'Mark plate as cleared' })[0]);
- await waitFor(() => {
- expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
- });
- expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
- });
- it('shows an icon-only plate clear action in small card view', async () => {
- let awaitingPlateClear = true;
- server.use(
- http.get('/api/v1/printers/', () => {
- return HttpResponse.json([mockPrinters[0]]);
- }),
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
- }),
- http.post('/api/v1/printers/:id/clear-plate', () => {
- awaitingPlateClear = false;
- return HttpResponse.json({ success: true, message: 'Plate cleared' });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- fireEvent.click(screen.getByRole('button', { name: 'S' }));
- await waitFor(() => {
- expect(screen.queryByText('Mark plate as cleared')).not.toBeInTheDocument();
- });
- const clearButton = screen.getByRole('button', { name: 'Mark plate as cleared' });
- fireEvent.click(clearButton);
- await waitFor(() => {
- expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
- });
- });
- it('shows plate clear status but no action while idle', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
- });
- expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
- });
- it('shows plate in use status while printing and hides the clear action', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING', awaiting_plate_clear: false });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Plate in Use').length).toBeGreaterThan(0);
- });
- expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
- });
- it('hides plate status and action when plate-clear confirmation is disabled', async () => {
- server.use(
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- auto_archive: true,
- save_thumbnails: true,
- capture_finish_photo: true,
- default_filament_cost: 25.0,
- currency: 'USD',
- ams_humidity_good: 40,
- ams_humidity_fair: 60,
- ams_temp_good: 30,
- ams_temp_fair: 35,
- require_plate_clear: false,
- });
- }),
- http.get('/api/v1/settings/ui-preferences', () => {
- return HttpResponse.json({
- ams_humidity_good: 40,
- ams_humidity_fair: 60,
- ams_temp_good: 30,
- ams_temp_fair: 35,
- require_plate_clear: false,
- });
- }),
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
- expect(screen.queryByText('Plate Clear')).not.toBeInTheDocument();
- expect(screen.queryByText('Plate in Use')).not.toBeInTheDocument();
- expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
- });
- });
- describe('disabled printer', () => {
- it('shows disabled state for disabled printers', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- // Disabled printers have visual indication
- const disabledPrinter = screen.getByText('P1S Backup').closest('div');
- expect(disabledPrinter).toBeInTheDocument();
- });
- });
- describe('nozzle rack card', () => {
- const h2cStatus = {
- ...mockPrinterStatus,
- nozzle_rack: [
- { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: 'SN-L', filament_color: '', filament_id: '', filament_type: '' },
- { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 0, max_temp: 300, serial_number: 'SN-R', filament_color: '', filament_id: '', filament_type: '' },
- { id: 16, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 10, stat: 0, max_temp: 300, serial_number: 'SN-16', filament_color: '', filament_id: '', filament_type: '' },
- { id: 17, nozzle_type: 'HH01', nozzle_diameter: '0.6', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-17', filament_color: '', filament_id: '', filament_type: '' },
- { id: 18, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 2, stat: 0, max_temp: 300, serial_number: 'SN-18', filament_color: '', filament_id: '', filament_type: '' },
- { id: 19, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
- { id: 20, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
- { id: 21, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
- ],
- };
- it('shows nozzle rack when H2C rack slots present', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json(h2cStatus);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
- });
- });
- it('shows 6 rack slot elements for H2C', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json(h2cStatus);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
- });
- // Rack shows diameters for occupied slots and dashes for empty ones
- const dashes = screen.getAllByText('—');
- expect(dashes.length).toBeGreaterThanOrEqual(3); // 3 empty rack positions (IDs 19,20,21)
- });
- it('keeps empty slot anchored to physical position when its nozzle is mounted (#943)', async () => {
- // H2C with rack slot 16 picked up into the hotend — firmware omits ID 16
- // entirely from nozzle.info. Each rack diameter is unique so we can assert
- // the ordering by tooltip lookup.
- const h2cSlot16Mounted = {
- ...mockPrinterStatus,
- nozzle_rack: [
- { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: 'SN-L', filament_color: '', filament_id: '', filament_type: '' },
- { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 0, max_temp: 300, serial_number: 'SN-R', filament_color: '', filament_id: '', filament_type: '' },
- // ID 16 missing — currently in hotend
- { id: 17, nozzle_type: 'HS', nozzle_diameter: '0.2', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-17', filament_color: '', filament_id: '', filament_type: '' },
- { id: 18, nozzle_type: 'HS', nozzle_diameter: '0.6', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-18', filament_color: '', filament_id: '', filament_type: '' },
- { id: 19, nozzle_type: 'HS', nozzle_diameter: '0.8', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-19', filament_color: '', filament_id: '', filament_type: '' },
- { id: 20, nozzle_type: 'HH01', nozzle_diameter: '1.0', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-20', filament_color: '', filament_id: '', filament_type: '' },
- { id: 21, nozzle_type: 'HH01', nozzle_diameter: '1.2', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-21', filament_color: '', filament_id: '', filament_type: '' },
- ],
- };
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json(h2cSlot16Mounted);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
- });
- // Slot 1 (leftmost, ID 16) should be the empty dash; slots 2..6 should
- // hold the 5 remaining nozzles in order 17, 18, 19, 20, 21.
- const rackLabel = screen.getAllByText('Nozzle Rack')[0];
- const rackCard = rackLabel.parentElement!;
- const slotRow = rackCard.querySelectorAll('div.flex')[0];
- const slotTexts = Array.from(slotRow.querySelectorAll('span')).map(s => s.textContent);
- expect(slotTexts).toEqual(['—', '0.2', '0.6', '0.8', '1.0', '1.2']);
- });
- it('hides nozzle rack when only L/R nozzles present (H2D)', async () => {
- const h2dStatus = {
- ...mockPrinterStatus,
- nozzle_rack: [
- { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
- { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
- ],
- };
- server.use(
- http.get('/api/v1/printers/:id/status', () => {
- return HttpResponse.json(h2dStatus);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- expect(screen.queryByText('Nozzle Rack')).not.toBeInTheDocument();
- });
- });
- describe('firmware version badge', () => {
- const firmwareUpToDate = {
- printer_id: 1,
- current_version: '01.09.00.00',
- latest_version: '01.09.00.00',
- update_available: false,
- download_url: null,
- release_notes: 'Bug fixes and improvements.',
- };
- const firmwareUpdateAvailable = {
- printer_id: 1,
- current_version: '01.08.00.00',
- latest_version: '01.09.00.00',
- update_available: true,
- download_url: 'https://example.com/firmware.bin',
- release_notes: 'New features added.',
- };
- it('shows green badge when firmware is up to date', async () => {
- server.use(
- http.get('/api/v1/firmware/updates/:id', () => {
- return HttpResponse.json(firmwareUpToDate);
- }),
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- check_printer_firmware: true,
- auto_archive: true,
- save_thumbnails: true,
- });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('01.09.00.00').length).toBeGreaterThan(0);
- });
- const badge = screen.getAllByText('01.09.00.00')[0].closest('button');
- expect(badge).toBeInTheDocument();
- expect(badge?.className).toContain('text-status-ok');
- });
- it('shows orange badge when firmware update is available', async () => {
- server.use(
- http.get('/api/v1/firmware/updates/:id', () => {
- return HttpResponse.json(firmwareUpdateAvailable);
- }),
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- check_printer_firmware: true,
- auto_archive: true,
- save_thumbnails: true,
- });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getAllByText('01.08.00.00').length).toBeGreaterThan(0);
- });
- const badge = screen.getAllByText('01.08.00.00')[0].closest('button');
- expect(badge).toBeInTheDocument();
- expect(badge?.className).toContain('text-orange-400');
- });
- it('hides badge when firmware check is disabled', async () => {
- server.use(
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- check_printer_firmware: false,
- auto_archive: true,
- save_thumbnails: true,
- });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Version should not appear when firmware check is disabled
- expect(screen.queryByText('01.09.00.00')).not.toBeInTheDocument();
- expect(screen.queryByText('01.08.00.00')).not.toBeInTheDocument();
- });
- it('hides badge when API has no firmware data for the model', async () => {
- const firmwareNoData = {
- printer_id: 1,
- current_version: '01.01.03.00',
- latest_version: null,
- update_available: false,
- download_url: null,
- release_notes: null,
- };
- server.use(
- http.get('/api/v1/firmware/updates/:id', () => {
- return HttpResponse.json(firmwareNoData);
- }),
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- check_printer_firmware: true,
- auto_archive: true,
- save_thumbnails: true,
- });
- })
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Badge should not appear when API returns no latest_version
- expect(screen.queryByText('01.01.03.00')).not.toBeInTheDocument();
- });
- });
- describe('bulk selection', () => {
- it('shows select button in toolbar', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // The Select button should be in the toolbar (title attribute)
- const selectButton = screen.getByTitle('Select');
- expect(selectButton).toBeInTheDocument();
- });
- it('shows selection toolbar after clicking select button', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Click the Select button to enter selection mode
- fireEvent.click(screen.getByTitle('Select'));
- // The floating toolbar should appear with Select All
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- });
- it('shows selection count when printers are selected', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Enter selection mode
- fireEvent.click(screen.getByTitle('Select'));
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- // Click Select All to select both printers
- fireEvent.click(screen.getByText('Select All'));
- // Should show "2 selected"
- await waitFor(() => {
- expect(screen.getByText('2 selected')).toBeInTheDocument();
- });
- });
- it('shows select by state dropdown', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Enter selection mode
- fireEvent.click(screen.getByTitle('Select'));
- await waitFor(() => {
- expect(screen.getByText('Select by State')).toBeInTheDocument();
- });
- });
- it('exits selection mode on close button', async () => {
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- });
- // Enter selection mode
- fireEvent.click(screen.getByTitle('Select'));
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- // Click the Select button again to exit (it toggles)
- fireEvent.click(screen.getByTitle('Select'));
- // Floating toolbar should disappear
- await waitFor(() => {
- expect(screen.queryByText('Select All')).not.toBeInTheDocument();
- });
- });
- });
- describe('search and filter', () => {
- beforeEach(() => {
- server.use(
- http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
- http.get('/api/v1/queue/', () => HttpResponse.json([]))
- );
- });
- it('filters by name (case-insensitive)', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'x1 carbon' } });
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
- });
- });
- it('trims leading and trailing whitespace from search', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- // " X1 Carbon " with surrounding spaces must still match
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: ' X1 Carbon ' } });
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
- });
- });
- it('filters by model', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'P1S' } });
- await waitFor(() => {
- expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- });
- it('filters by serial number', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: '00M09A' } });
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
- });
- });
- it('shows empty state when no printers match search', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'ZZZ_NO_MATCH' } });
- await waitFor(() => {
- expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
- });
- });
- it('clear button resets search and shows all printers', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1 Carbon' } });
- await waitFor(() => expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument());
- // Click the accessible clear button
- fireEvent.click(screen.getByRole('button', { name: 'Clear' }));
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- });
- it('filters by status (offline) via dropdown', async () => {
- // Override: printer 1 online, printer 2 offline
- server.use(
- http.get('/api/v1/printers/:id/status', ({ params }) => {
- if (Number(params.id) === 2) {
- return HttpResponse.json({ ...mockPrinterStatus, connected: false });
- }
- return HttpResponse.json(mockPrinterStatus);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- await selectToolbarDropdownOption(/all statuses/i, /^offline$/i);
- await waitFor(() => {
- expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- });
- it('shows empty state when status filter matches nothing', async () => {
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- // Both printers are IDLE; filtering by "printing" should yield no results
- await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
- await waitFor(() => {
- expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
- });
- });
- it('combines search and status filter', async () => {
- // Printer 1 = RUNNING (printing), printer 2 = IDLE
- server.use(
- http.get('/api/v1/printers/:id/status', ({ params }) => {
- if (Number(params.id) === 1) {
- return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING' });
- }
- return HttpResponse.json(mockPrinterStatus);
- })
- );
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- // Filter to only "printing" printers
- await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
- // Then also search for a term that only matches printer 1
- fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1' } });
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
- });
- });
- it('filters by location via dropdown', async () => {
- // Override: give printer 2 its own location so the dropdown has two options
- // and we can verify the filter picks the right one. Printer 1 stays at 'Workshop'.
- server.use(
- http.get('/api/v1/printers/', () =>
- HttpResponse.json([
- mockPrinters[0],
- { ...mockPrinters[1], location: 'Office' },
- ])
- )
- );
- render(<PrintersPage />);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- await selectToolbarDropdownOption(/all locations/i, /^workshop$/i);
- await waitFor(() => {
- expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
- expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
- });
- await selectToolbarDropdownOption(/^workshop$/i, /^office$/i);
- await waitFor(() => {
- expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
- expect(screen.getByText('P1S Backup')).toBeInTheDocument();
- });
- });
- it('hides location filter when no printers have a location', async () => {
- // Both printers have null location — dropdown should not render at all
- server.use(
- http.get('/api/v1/printers/', () =>
- HttpResponse.json([
- { ...mockPrinters[0], location: null },
- { ...mockPrinters[1], location: null },
- ])
- )
- );
- render(<PrintersPage />);
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- // Status filter is still there, but the location filter should be absent.
- expect(screen.getByRole('button', { name: /all statuses/i })).toBeInTheDocument();
- expect(screen.queryByRole('button', { name: /all locations/i })).not.toBeInTheDocument();
- });
- });
- describe('Spoolman loading guard', () => {
- it('does not show Assign Spool button while Spoolman queries are loading', async () => {
- // Spoolman enabled but inventory and slot-assignment queries never resolve
- server.use(
- http.get('/api/v1/spoolman/status', () =>
- HttpResponse.json({ enabled: true, connected: true })
- ),
- http.get('/api/v1/spoolman/inventory/spools', () =>
- new Promise(() => {}) // never resolves
- ),
- http.get('/api/v1/spoolman/inventory/slot-assignments/all', () =>
- new Promise(() => {}) // never resolves
- )
- );
- render(<PrintersPage />);
- // Wait for the page to render (printers should be visible)
- await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
- // While Spoolman queries are still loading, the "Assign Spool" button must
- // not appear (inventory prop is undefined → {inventory && ...} guard fires)
- expect(screen.queryByText('Assign Spool')).not.toBeInTheDocument();
- });
- });
- });
- /**
- * Phase 13 P13-1 (PrintersPage EmptySlotHoverCard onAssignSpool gate removal)
- *
- * Pre-Phase-13 each of the three EmptySlotHoverCard call-sites in PrintersPage
- * gated `onAssignSpool` on `spoolmanEnabled ? (...) : undefined`, so empty
- * slots in local-Inventory mode never showed an Assign action. Maintainer
- * Foto 7 confirmed users expect the button regardless of mode.
- *
- * To assert wiring without going through hover-card animations, we mock the
- * EmptySlotHoverCard component at module level and capture every props
- * payload. The same mock is active in both modes; tests differ only in the
- * spoolman-settings mock. The mock module covers BOTH FilamentHoverCard exports
- * so tests outside this `describe` aren't affected (we re-export the real
- * FilamentHoverCard).
- */
- const phase13EmptySlotProps: Array<Record<string, unknown>> = [];
- const phase14HoverCardProps: Array<Record<string, unknown>> = [];
- vi.mock('../../components/FilamentHoverCard', async (importOriginal) => {
- const actual = await importOriginal<typeof import('../../components/FilamentHoverCard')>();
- return {
- ...actual,
- EmptySlotHoverCard: (props: Record<string, unknown>) => {
- phase13EmptySlotProps.push({ ...props });
- return null;
- },
- FilamentHoverCard: (props: Record<string, unknown>) => {
- phase14HoverCardProps.push({ ...props });
- return null;
- },
- };
- });
- describe('PrintersPage Phase 13 — EmptySlotHoverCard onAssignSpool wiring', () => {
- beforeEach(() => {
- phase13EmptySlotProps.length = 0;
- localStorage.removeItem('printerCardSize');
- server.use(
- http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
- // Status response includes an empty AMS slot so EmptySlotHoverCard renders.
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
- ...mockPrinterStatus,
- ams: [{
- id: 0,
- tray: [{ id: 0, tray_type: '' }],
- }],
- })),
- http.get('/api/v1/settings/', () => HttpResponse.json({
- auto_archive: true, save_thumbnails: true, capture_finish_photo: true,
- default_filament_cost: 25.0, currency: 'USD',
- ams_humidity_good: 40, ams_humidity_fair: 60,
- ams_temp_good: 30, ams_temp_fair: 35,
- })),
- http.get('/api/v1/queue/', () => HttpResponse.json([])),
- );
- });
- it('P13-1 (local mode): EmptySlotHoverCard receives onAssignSpool callback', async () => {
- server.use(
- http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
- spoolman_enabled: 'false', spoolman_url: '',
- })),
- );
- render(<PrintersPage />);
- // Wait for printer status to load and at least one EmptySlotHoverCard
- // to mount with an onAssignSpool callback. Pre-Phase-13 this would have
- // been undefined in local mode (the gate filtered it out).
- await waitFor(() => {
- const withCallback = phase13EmptySlotProps.filter(p => typeof p.onAssignSpool === 'function');
- expect(withCallback.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- });
- it('P13-1 (spoolman mode): EmptySlotHoverCard still receives onAssignSpool callback', async () => {
- server.use(
- http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
- spoolman_enabled: 'true', spoolman_url: 'http://x:7912',
- })),
- http.get('/api/v1/spoolman/spools/inventory*', () => HttpResponse.json([])),
- http.get('/api/v1/spoolman/inventory/spools', () => HttpResponse.json([])),
- http.get('/api/v1/spoolman/inventory/slot-assignments/all', () => HttpResponse.json([])),
- );
- render(<PrintersPage />);
- await waitFor(() => {
- const withCallback = phase13EmptySlotProps.filter(p => typeof p.onAssignSpool === 'function');
- expect(withCallback.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- });
- });
- /**
- * Phase 14 — Local-Branch BL-detection symmetry.
- *
- * The Spoolman branch of every IIFE in PrintersPage already passes
- * isAssigned: !!slotAssignment || isBambuLabSpool(tray)
- * onUnassignSpool: (spoolmanSpool && !isBambuLabSpool(tray)) ? ... : undefined
- *
- * The local branch was missing both. As a result a BL-RFID-tagged slot in
- * local-Inventory mode showed an "Assign Spool" button (because no manual
- * SpoolAssignment exists), and a manually-assigned BL-RFID slot showed
- * "Unassign" — which would be overwritten on the next RFID re-read.
- *
- * The same FilamentHoverCard mock from the Phase 13 block above captures
- * inventory props on every render so we can inspect them after setup.
- */
- describe('PrintersPage Phase 14 — Local-Branch BL-detection symmetry', () => {
- beforeEach(() => {
- phase14HoverCardProps.length = 0;
- localStorage.removeItem('printerCardSize');
- server.use(
- http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
- http.get('/api/v1/settings/', () => HttpResponse.json({
- auto_archive: true, save_thumbnails: true, capture_finish_photo: true,
- default_filament_cost: 25.0, currency: 'USD',
- ams_humidity_good: 40, ams_humidity_fair: 60,
- ams_temp_good: 30, ams_temp_fair: 35,
- })),
- http.get('/api/v1/queue/', () => HttpResponse.json([])),
- http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
- spoolman_enabled: 'false', spoolman_url: '',
- })),
- );
- });
- it('P14-1a (local + BL-RFID + no assignment): inventory.isAssigned=true', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
- ...mockPrinterStatus,
- ams: [{
- id: 0,
- tray: [{
- id: 0,
- tray_type: 'PLA',
- tray_uuid: '11223344556677880011223344556677',
- tag_uid: '0000000000000000',
- tray_color: 'FF0000FF',
- tray_sub_brands: 'Bambu PLA Basic',
- }],
- }],
- })),
- http.get('/api/v1/inventory/assignments', () => HttpResponse.json([])),
- );
- render(<PrintersPage />);
- await waitFor(() => {
- const matches = phase14HoverCardProps.filter(
- p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
- );
- expect(matches.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- });
- it('P14-1b (local + non-BL + no assignment): inventory.isAssigned is falsy', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
- ...mockPrinterStatus,
- ams: [{
- id: 0,
- tray: [{
- id: 0,
- tray_type: 'PLA',
- tray_uuid: '00000000000000000000000000000000',
- tag_uid: '0000000000000000',
- tray_color: 'FF0000FF',
- tray_sub_brands: 'Generic PLA',
- }],
- }],
- })),
- http.get('/api/v1/inventory/assignments', () => HttpResponse.json([])),
- );
- render(<PrintersPage />);
- // Wait for FilamentHoverCard to render at least once.
- await waitFor(() => {
- expect(phase14HoverCardProps.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- // No render should ever set isAssigned=true for this slot.
- const truthyMatches = phase14HoverCardProps.filter(
- p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
- );
- expect(truthyMatches.length).toBe(0);
- });
- it('P14-1c (local + manual assignment): inventory.isAssigned=true', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
- ...mockPrinterStatus,
- ams: [{
- id: 0,
- tray: [{
- id: 0,
- tray_type: 'PLA',
- tray_uuid: '00000000000000000000000000000000',
- tag_uid: '0000000000000000',
- tray_color: 'FF0000FF',
- tray_sub_brands: 'Generic PLA',
- }],
- }],
- })),
- http.get('/api/v1/inventory/assignments', () => HttpResponse.json([
- {
- id: 1,
- spool_id: 42,
- printer_id: 1,
- ams_id: 0,
- tray_id: 0,
- printer_name: 'X1 Carbon',
- ams_label: null,
- spool: {
- id: 42,
- material: 'PLA',
- brand: 'Generic',
- color_name: 'Red',
- label_weight: 1000,
- weight_used: 0,
- rgba: 'FF0000FF',
- },
- },
- ])),
- );
- render(<PrintersPage />);
- await waitFor(() => {
- const matches = phase14HoverCardProps.filter(
- p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
- );
- expect(matches.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- });
- it('P14-2 (local + BL-RFID + manual assignment): onUnassignSpool=undefined', async () => {
- server.use(
- http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
- ...mockPrinterStatus,
- ams: [{
- id: 0,
- tray: [{
- id: 0,
- tray_type: 'PLA',
- tray_uuid: '11223344556677880011223344556677',
- tag_uid: '0000000000000000',
- tray_color: 'FF0000FF',
- tray_sub_brands: 'Bambu PLA Basic',
- }],
- }],
- })),
- http.get('/api/v1/inventory/assignments', () => HttpResponse.json([
- {
- id: 1,
- spool_id: 42,
- printer_id: 1,
- ams_id: 0,
- tray_id: 0,
- printer_name: 'X1 Carbon',
- ams_label: null,
- spool: {
- id: 42,
- material: 'PLA',
- brand: 'Bambu Lab',
- color_name: 'Red',
- label_weight: 1000,
- weight_used: 0,
- rgba: 'FF0000FF',
- },
- },
- ])),
- );
- render(<PrintersPage />);
- // Wait for FilamentHoverCard renders to settle.
- await waitFor(() => {
- expect(phase14HoverCardProps.length).toBeGreaterThan(0);
- }, { timeout: 3000 });
- // For BL-detected slots in local mode, onUnassignSpool must always be
- // undefined — even when a manual assignment exists. Otherwise the user
- // could unassign a BL-RFID slot that the printer would re-assign on the
- // next re-read, surprising them with phantom ghost-assignments.
- const definedUnassign = phase14HoverCardProps.filter(
- p => typeof (p.inventory as { onUnassignSpool?: () => void } | undefined)?.onUnassignSpool === 'function'
- );
- expect(definedUnassign.length).toBe(0);
- });
- });
|