project-transition-state.test.ts81 lines · main
| 1 | import { beforeEach, describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { |
| 4 | clearPersistedTransitionStartTime, |
| 5 | getPersistedTransitionStartTime, |
| 6 | getRemainingTransitionTimeMs, |
| 7 | hoursToMilliseconds, |
| 8 | minutesToMilliseconds, |
| 9 | } from './project-transition-state' |
| 10 | |
| 11 | describe('project-transition-state', () => { |
| 12 | beforeEach(() => { |
| 13 | window.localStorage.clear() |
| 14 | }) |
| 15 | |
| 16 | describe('getPersistedTransitionStartTime', () => { |
| 17 | it('stores the current time when no start time exists', () => { |
| 18 | const startTime = getPersistedTransitionStartTime('transition-key', 1_000) |
| 19 | |
| 20 | expect(startTime).toBe(1_000) |
| 21 | expect(window.localStorage.getItem('transition-key')).toBe('1000') |
| 22 | }) |
| 23 | |
| 24 | it('reuses an existing stored start time', () => { |
| 25 | window.localStorage.setItem('transition-key', '2_000'.replace('_', '')) |
| 26 | |
| 27 | const startTime = getPersistedTransitionStartTime('transition-key', 3_000) |
| 28 | |
| 29 | expect(startTime).toBe(2_000) |
| 30 | expect(window.localStorage.getItem('transition-key')).toBe('2000') |
| 31 | }) |
| 32 | |
| 33 | it('replaces malformed stored values with the current time', () => { |
| 34 | window.localStorage.setItem('transition-key', 'not-a-number') |
| 35 | |
| 36 | const startTime = getPersistedTransitionStartTime('transition-key', 3_000) |
| 37 | |
| 38 | expect(startTime).toBe(3_000) |
| 39 | expect(window.localStorage.getItem('transition-key')).toBe('3000') |
| 40 | }) |
| 41 | |
| 42 | it('replaces stale stored values when they exceed the allowed max age', () => { |
| 43 | window.localStorage.setItem('transition-key', '1_000'.replace('_', '')) |
| 44 | |
| 45 | const startTime = getPersistedTransitionStartTime( |
| 46 | 'transition-key', |
| 47 | hoursToMilliseconds(30), |
| 48 | hoursToMilliseconds(24) |
| 49 | ) |
| 50 | |
| 51 | expect(startTime).toBe(hoursToMilliseconds(30)) |
| 52 | expect(window.localStorage.getItem('transition-key')).toBe(String(hoursToMilliseconds(30))) |
| 53 | }) |
| 54 | }) |
| 55 | |
| 56 | it('removes a persisted transition start time', () => { |
| 57 | window.localStorage.setItem('transition-key', '1000') |
| 58 | |
| 59 | clearPersistedTransitionStartTime('transition-key') |
| 60 | |
| 61 | expect(window.localStorage.getItem('transition-key')).toBeNull() |
| 62 | }) |
| 63 | |
| 64 | it('returns the remaining threshold time and clamps at zero', () => { |
| 65 | expect( |
| 66 | getRemainingTransitionTimeMs({ |
| 67 | startTimeMs: 1_000, |
| 68 | thresholdMs: minutesToMilliseconds(10), |
| 69 | now: 4_000, |
| 70 | }) |
| 71 | ).toBe(minutesToMilliseconds(10) - 3_000) |
| 72 | |
| 73 | expect( |
| 74 | getRemainingTransitionTimeMs({ |
| 75 | startTimeMs: 1_000, |
| 76 | thresholdMs: minutesToMilliseconds(10), |
| 77 | now: minutesToMilliseconds(10) + 5_000, |
| 78 | }) |
| 79 | ).toBe(0) |
| 80 | }) |
| 81 | }) |