SpoolBuddyInventorySearch.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Regression test for #1738 — SpoolBuddy's inventory search must match by
  3. * numeric spool ID, just like Bambuddy's main InventoryPage. Both pages now
  4. * share `filterSpoolsByQuery` so behaviour stays in lockstep; this test fails
  5. * loudly if SpoolBuddyInventoryPage ever re-inlines its filter and drops
  6. * fields.
  7. */
  8. import { describe, it, expect } from 'vitest';
  9. import type { InventorySpool } from '../../api/client';
  10. import { filterSpoolsByQuery } from '../../utils/inventorySearch';
  11. function makeSpool(overrides: Partial<InventorySpool> & { id: number }): InventorySpool {
  12. return {
  13. material: 'PLA',
  14. subtype: 'Basic',
  15. brand: 'Bambu Lab',
  16. color_name: 'White',
  17. rgba: 'FFFFFFFF',
  18. label_weight: 1000,
  19. core_weight: 250,
  20. core_weight_catalog_id: null,
  21. weight_used: 0,
  22. weight_locked: false,
  23. slicer_filament: null,
  24. slicer_filament_name: null,
  25. nozzle_temp_min: null,
  26. nozzle_temp_max: null,
  27. note: null,
  28. added_full: null,
  29. last_used: null,
  30. encode_time: null,
  31. tag_uid: null,
  32. tray_uuid: null,
  33. data_origin: null,
  34. tag_type: null,
  35. archived_at: null,
  36. created_at: '2025-01-01T00:00:00Z',
  37. updated_at: '2025-01-01T00:00:00Z',
  38. k_profiles: [],
  39. cost_per_kg: null,
  40. last_scale_weight: null,
  41. last_weighed_at: null,
  42. storage_location: null,
  43. ...overrides,
  44. };
  45. }
  46. describe('SpoolBuddyInventoryPage search filter (#1738)', () => {
  47. it('matches an exact spool ID', () => {
  48. const spools = [
  49. makeSpool({ id: 1336 }),
  50. makeSpool({ id: 1337 }),
  51. makeSpool({ id: 42 }),
  52. ];
  53. const result = filterSpoolsByQuery(spools, '1336');
  54. expect(result.map((s) => s.id)).toEqual([1336]);
  55. });
  56. it('matches a partial spool ID', () => {
  57. const spools = [
  58. makeSpool({ id: 100 }),
  59. makeSpool({ id: 200 }),
  60. makeSpool({ id: 1001 }),
  61. ];
  62. const result = filterSpoolsByQuery(spools, '00');
  63. expect(result.map((s) => s.id).sort((a, b) => a - b)).toEqual([100, 200, 1001]);
  64. });
  65. it('still matches by the existing fields SpoolBuddy supported pre-fix', () => {
  66. const spools = [
  67. makeSpool({ id: 1, material: 'PLA', brand: 'Bambu Lab', color_name: 'Red' }),
  68. makeSpool({ id: 2, material: 'PETG', brand: 'Polymaker', color_name: 'Blue' }),
  69. ];
  70. expect(filterSpoolsByQuery(spools, 'polymaker').map((s) => s.id)).toEqual([2]);
  71. expect(filterSpoolsByQuery(spools, 'PLA').map((s) => s.id)).toEqual([1]);
  72. expect(filterSpoolsByQuery(spools, 'red').map((s) => s.id)).toEqual([1]);
  73. });
  74. it('also matches by storage_location and slicer_filament_name (parity gain)', () => {
  75. const spools = [
  76. makeSpool({ id: 1, storage_location: 'IKEA Regal' }),
  77. makeSpool({ id: 2, slicer_filament_name: 'Generic PLA Matte' }),
  78. ];
  79. expect(filterSpoolsByQuery(spools, 'IKEA').map((s) => s.id)).toEqual([1]);
  80. expect(filterSpoolsByQuery(spools, 'Matte').map((s) => s.id)).toEqual([2]);
  81. });
  82. });