|
| 1 | +import { describe, expect, expectTypeOf, it } from 'vitest'; |
| 2 | +import backgroundRefresh, { BackgroundRefresh } from './background-refresh'; |
| 3 | + |
| 4 | +describe('BackgroundRefresh type compiler validation', () => { |
| 5 | + it('exposes singleton instance with correct type', () => { |
| 6 | + expectTypeOf(BackgroundRefresh.getInstance()).toEqualTypeOf<BackgroundRefresh>(); |
| 7 | + expectTypeOf(backgroundRefresh).toEqualTypeOf<BackgroundRefresh>(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('enforces public method input and return types', () => { |
| 11 | + expectTypeOf(backgroundRefresh.isStale).parameters.toEqualTypeOf< |
| 12 | + [lastSyncedAt: string | undefined] |
| 13 | + >(); |
| 14 | + expectTypeOf(backgroundRefresh.isStale).returns.toEqualTypeOf<boolean>(); |
| 15 | + |
| 16 | + expectTypeOf(backgroundRefresh.triggerRefresh).parameters.toEqualTypeOf<[username: string]>(); |
| 17 | + expectTypeOf(backgroundRefresh.triggerRefresh).returns.toEqualTypeOf<void>(); |
| 18 | + |
| 19 | + expectTypeOf(backgroundRefresh.isJobActive).parameters.toEqualTypeOf<[username: string]>(); |
| 20 | + expectTypeOf(backgroundRefresh.isJobActive).returns.toEqualTypeOf<boolean>(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('blocks invalid parameters during static type checking', () => { |
| 24 | + type IsStaleParams = Parameters<typeof backgroundRefresh.isStale>; |
| 25 | + type TriggerRefreshParams = Parameters<typeof backgroundRefresh.triggerRefresh>; |
| 26 | + type IsJobActiveParams = Parameters<typeof backgroundRefresh.isJobActive>; |
| 27 | + |
| 28 | + expectTypeOf<[number]>().not.toMatchTypeOf<IsStaleParams>(); |
| 29 | + expectTypeOf<[]>().not.toMatchTypeOf<TriggerRefreshParams>(); |
| 30 | + expectTypeOf<[null]>().not.toMatchTypeOf<IsJobActiveParams>(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('accepts optional timestamp values without compile errors', () => { |
| 34 | + expect(backgroundRefresh.isStale(undefined)).toBe(true); |
| 35 | + expect(backgroundRefresh.isStale(new Date().toISOString())).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('validates stale date constraints strictly', () => { |
| 39 | + const staleDate = new Date(Date.now() - 11 * 60 * 1000).toISOString(); |
| 40 | + const freshDate = new Date().toISOString(); |
| 41 | + |
| 42 | + expect(backgroundRefresh.isStale(staleDate)).toBe(true); |
| 43 | + expect(backgroundRefresh.isStale(freshDate)).toBe(false); |
| 44 | + expect(backgroundRefresh.isStale('invalid-date')).toBe(false); |
| 45 | + }); |
| 46 | +}); |
0 commit comments