Skip to content

Commit 09d3eeb

Browse files
authored
test(background-refresh): add type compiler validation coverage (JhaSourav07#3300)
## Description Fixes JhaSourav07#2922 Added TypeScript compiler validation and schema constraint stability tests for the background refresh service. ### Changes Made * Created `services/github/background-refresh.type-compiler.test.ts` * Added type-level assertions using `expectTypeOf` * Verified public method parameter and return types * Added checks to ensure invalid parameter types are rejected by TypeScript * Verified optional values are accepted without type errors * Added runtime validation tests for stale and fresh timestamps These tests help prevent regressions in the BackgroundRefresh API and ensure type safety remains stable during future updates. ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally. * [x] I have run `npm run format` and resolved formatting issues. * [x] My commits follow the Conventional Commits format. * [ ] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that I have only one commit to merge in this PR. * [x] The changes do not affect SVG output. * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents daed6d4 + b3592bf commit 09d3eeb

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)