getSpoolmanFillLevel.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Tests for getSpoolmanFillLevel helper function.
  3. * This function is defined in PrintersPage.tsx but tested here for isolation.
  4. * We replicate the logic to test it independently.
  5. */
  6. import { describe, it, expect } from 'vitest';
  7. // Replicate the function from PrintersPage.tsx for testing
  8. interface LinkedSpoolInfo {
  9. id: number;
  10. remaining_weight: number | null;
  11. filament_weight: number | null;
  12. }
  13. function getSpoolmanFillLevel(
  14. linkedSpool: LinkedSpoolInfo | undefined
  15. ): number | null {
  16. if (!linkedSpool?.remaining_weight || !linkedSpool?.filament_weight
  17. || linkedSpool.filament_weight <= 0) return null;
  18. return Math.min(100, Math.round(
  19. (linkedSpool.remaining_weight / linkedSpool.filament_weight) * 100
  20. ));
  21. }
  22. describe('getSpoolmanFillLevel', () => {
  23. it('returns null for undefined spool', () => {
  24. expect(getSpoolmanFillLevel(undefined)).toBeNull();
  25. });
  26. it('returns null when remaining_weight is null', () => {
  27. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: null, filament_weight: 1000 })).toBeNull();
  28. });
  29. it('returns null when filament_weight is null', () => {
  30. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 500, filament_weight: null })).toBeNull();
  31. });
  32. it('returns null when remaining_weight is 0', () => {
  33. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 0, filament_weight: 1000 })).toBeNull();
  34. });
  35. it('returns null when filament_weight is 0', () => {
  36. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 500, filament_weight: 0 })).toBeNull();
  37. });
  38. it('returns null when filament_weight is negative', () => {
  39. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 500, filament_weight: -100 })).toBeNull();
  40. });
  41. it('calculates correct percentage for half-full spool', () => {
  42. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 500, filament_weight: 1000 })).toBe(50);
  43. });
  44. it('calculates correct percentage for full spool', () => {
  45. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 1000, filament_weight: 1000 })).toBe(100);
  46. });
  47. it('calculates correct percentage for nearly empty spool', () => {
  48. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 50, filament_weight: 1000 })).toBe(5);
  49. });
  50. it('caps at 100% when remaining exceeds filament weight', () => {
  51. // This can happen if user manually sets remaining_weight higher
  52. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 1200, filament_weight: 1000 })).toBe(100);
  53. });
  54. it('rounds to nearest integer', () => {
  55. // 333/1000 = 33.3% -> 33%
  56. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 333, filament_weight: 1000 })).toBe(33);
  57. // 666/1000 = 66.6% -> 67%
  58. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 666, filament_weight: 1000 })).toBe(67);
  59. });
  60. it('handles small weights correctly', () => {
  61. expect(getSpoolmanFillLevel({ id: 1, remaining_weight: 1, filament_weight: 100 })).toBe(1);
  62. });
  63. });