PrintersPageFormatPrintName.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Unit tests for the formatPrintName helper on PrintersPage.
  3. *
  4. * Regression coverage for the #881 follow-up: when the printer card has an
  5. * archive-linked plate label (resolved from the backend's current_archive_id
  6. * + the archive's is_multi_plate plate list), the label must take precedence
  7. * over the gcode_file regex fallback, including for plate 1.
  8. */
  9. import { describe, it, expect } from 'vitest';
  10. import { formatPrintName } from '../../utils/printName';
  11. // Minimal translator stub: returns the fallback with the plate number interpolated
  12. // the same way i18next would. Keeps these tests independent of the i18n setup.
  13. const t = (_key: string, fallback: string, opts?: Record<string, unknown>) =>
  14. fallback.replace('{{number}}', String(opts?.number ?? ''));
  15. describe('formatPrintName', () => {
  16. it('returns the name unchanged when neither plate source is available', () => {
  17. expect(formatPrintName('Benchy', null, t)).toBe('Benchy');
  18. });
  19. it('appends gcode-file plate number only when > 1 (single-plate noise guard)', () => {
  20. // Plate 1 from gcode_file alone is ambiguous (could be a single-plate 3MF)
  21. // so the legacy fallback path keeps it silent.
  22. expect(formatPrintName('Benchy', '/Metadata/plate_1.gcode', t)).toBe('Benchy');
  23. expect(formatPrintName('Benchy', '/Metadata/plate_2.gcode', t)).toBe('Benchy — Plate 2');
  24. });
  25. it('uses plateLabel verbatim when provided, overriding the gcode_file fallback', () => {
  26. // plateLabel comes from the archive lookup and is already disambiguated
  27. // (only set when is_multi_plate === true). It must show even for plate 1.
  28. expect(formatPrintName('Benchy', '/Metadata/plate_1.gcode', t, 'Plate 1')).toBe('Benchy — Plate 1');
  29. expect(formatPrintName('Benchy', '/Metadata/plate_2.gcode', t, 'Small Parts')).toBe('Benchy — Small Parts');
  30. });
  31. it('returns empty string when name is missing, regardless of plate info', () => {
  32. expect(formatPrintName(null, '/Metadata/plate_2.gcode', t)).toBe('');
  33. expect(formatPrintName(null, null, t, 'Plate 3')).toBe('');
  34. });
  35. it('treats null/empty plateLabel as absent and falls through to gcode_file parsing', () => {
  36. expect(formatPrintName('Benchy', '/Metadata/plate_2.gcode', t, null)).toBe('Benchy — Plate 2');
  37. expect(formatPrintName('Benchy', '/Metadata/plate_2.gcode', t, '')).toBe('Benchy — Plate 2');
  38. });
  39. });