|
| 1 | +import { mockServices } from '@backstage/backend-test-utils'; |
| 2 | + |
| 3 | +import { getCacheTtl } from './config'; |
| 4 | + |
| 5 | +describe('getCacheTtl', () => { |
| 6 | + it('should return default TTL when cache.ttl is not configured', () => { |
| 7 | + const mockConfig = mockServices.rootConfig({ |
| 8 | + data: {}, |
| 9 | + }); |
| 10 | + |
| 11 | + const result = getCacheTtl(mockConfig); |
| 12 | + |
| 13 | + expect(result).toBe(3_600_000); // 1 hour in milliseconds |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return default TTL when readme config section exists but no ttl key', () => { |
| 17 | + const mockConfig = mockServices.rootConfig({ |
| 18 | + data: { |
| 19 | + readme: {}, |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + const result = getCacheTtl(mockConfig); |
| 24 | + |
| 25 | + expect(result).toBe(3_600_000); // 1 hour in milliseconds |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return configured TTL in seconds', () => { |
| 29 | + const mockConfig = mockServices.rootConfig({ |
| 30 | + data: { |
| 31 | + readme: { |
| 32 | + cacheTtl: { seconds: 1800 }, // 30 minutes |
| 33 | + }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + const result = getCacheTtl(mockConfig); |
| 38 | + |
| 39 | + expect(result).toBe(1_800_000); // 30 minutes in milliseconds |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return configured TTL in minutes', () => { |
| 43 | + const mockConfig = mockServices.rootConfig({ |
| 44 | + data: { |
| 45 | + readme: { |
| 46 | + cacheTtl: { minutes: 10 }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const result = getCacheTtl(mockConfig); |
| 52 | + |
| 53 | + expect(result).toBe(600_000); // 10 minutes in milliseconds |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return configured TTL in hours', () => { |
| 57 | + const mockConfig = mockServices.rootConfig({ |
| 58 | + data: { |
| 59 | + readme: { |
| 60 | + cacheTtl: { hours: 2 }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const result = getCacheTtl(mockConfig); |
| 66 | + |
| 67 | + expect(result).toBe(7_200_000); // 2 hours in milliseconds |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return configured TTL as a string value (interpreted as milliseconds)', () => { |
| 71 | + const mockConfig = mockServices.rootConfig({ |
| 72 | + data: { |
| 73 | + readme: { |
| 74 | + cacheTtl: '4w', |
| 75 | + }, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const result = getCacheTtl(mockConfig); |
| 80 | + |
| 81 | + expect(result).toBe(2419200000); // 4 weeks in milliseconds |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle complex duration configuration', () => { |
| 85 | + const mockConfig = mockServices.rootConfig({ |
| 86 | + data: { |
| 87 | + readme: { |
| 88 | + cacheTtl: { |
| 89 | + hours: 1, |
| 90 | + minutes: 30, |
| 91 | + seconds: 45, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + const result = getCacheTtl(mockConfig); |
| 98 | + |
| 99 | + expect(result).toBe(5_445_000); // 1h 30m 45s in milliseconds |
| 100 | + }); |
| 101 | +}); |
0 commit comments