archiveAmsMapping.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Tests for `resolveArchiveSlicerAmsMapping` (#2700).
  3. *
  4. * This is the gate between an archive's saved slicer AMS pick and the print
  5. * modal offering it. The saved tray IDs are *global tray IDs*, which only mean
  6. * something against the AMS layout of the one printer they were resolved
  7. * against — tray 5 on printer A can hold a completely different spool than
  8. * tray 5 on printer B. Everything here exists to make sure the mapping is only
  9. * ever offered for its own printer.
  10. */
  11. import { describe, it, expect } from 'vitest';
  12. import { resolveArchiveSlicerAmsMapping } from '../../components/PrintModal/archiveAmsMapping';
  13. const SAVED = { slicer_ams_mapping: { mapping: [4, -1, 12, -1], printer_id: 7 } };
  14. describe('resolveArchiveSlicerAmsMapping', () => {
  15. it('returns the mapping when the selected printer is the one it was saved for', () => {
  16. expect(resolveArchiveSlicerAmsMapping(SAVED, 7)).toEqual([4, -1, 12, -1]);
  17. });
  18. it('refuses the mapping on a different printer', () => {
  19. // The whole point of storing printer_id. Tray 4 on printer 9 is not the
  20. // spool the slicer picked on printer 7.
  21. expect(resolveArchiveSlicerAmsMapping(SAVED, 9)).toBeUndefined();
  22. });
  23. it('refuses the mapping when no printer is selected yet', () => {
  24. // Guards the `undefined === undefined` reading as a match: with no printer
  25. // chosen there is nothing to compare against, so nothing may be offered.
  26. expect(resolveArchiveSlicerAmsMapping(SAVED, null)).toBeUndefined();
  27. expect(resolveArchiveSlicerAmsMapping(SAVED, undefined)).toBeUndefined();
  28. });
  29. it('returns undefined for archives with no extra_data at all', () => {
  30. expect(resolveArchiveSlicerAmsMapping(null, 7)).toBeUndefined();
  31. expect(resolveArchiveSlicerAmsMapping(undefined, 7)).toBeUndefined();
  32. expect(resolveArchiveSlicerAmsMapping({}, 7)).toBeUndefined();
  33. });
  34. it('ignores unrelated extra_data keys', () => {
  35. // The common case: archives carry plenty of metadata but no saved mapping.
  36. expect(resolveArchiveSlicerAmsMapping({ printable_objects: { '1': 'part' } }, 7)).toBeUndefined();
  37. });
  38. it('rejects a stored value that is missing its printer_id', () => {
  39. // A mapping saved without knowing which printer it came from can't be
  40. // safely reused on any printer, including the one it actually came from —
  41. // there'd be no way to tell.
  42. expect(resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: { mapping: [4, 12] } }, 7)).toBeUndefined();
  43. });
  44. it('rejects malformed stored values instead of throwing', () => {
  45. // extra_data is free-form JSON off the wire; a bad shape must degrade to
  46. // "no saved mapping", never crash the modal.
  47. expect(resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: 'nope' }, 7)).toBeUndefined();
  48. expect(resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: null }, 7)).toBeUndefined();
  49. expect(
  50. resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: { mapping: 'nope', printer_id: 7 } }, 7),
  51. ).toBeUndefined();
  52. expect(
  53. resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: { mapping: [], printer_id: 7 } }, 7),
  54. ).toBeUndefined();
  55. });
  56. it('does not coerce printer ids', () => {
  57. // A string "7" from a hand-edited record is not printer 7.
  58. expect(
  59. resolveArchiveSlicerAmsMapping({ slicer_ams_mapping: { mapping: [4], printer_id: '7' } }, 7),
  60. ).toBeUndefined();
  61. });
  62. });