Skip to content

Commit 65c6ce8

Browse files
Add type compiler validation tests for TrackUserProtection (JhaSourav07#3001)
The TrackUserProtection class had no type-level tests verifying its return types, method signatures, or validation schema constraints. This made it easy to accidentally change the shape of return values or allow invalid reason strings during refactors. Add five type-level tests using expectTypeOf to pin down: - The return type shape of verifyAndDeduplicate - Method parameter and return types - The singleton getInstance return type - That invalid string literals are blocked for the reason field - That schema validation returns a strict, stable report type Co-authored-by: atul-upadhyay-7 <atul-upadhyay-7@users.noreply.github.com>
1 parent f983222 commit 65c6ce8

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expectTypeOf, it } from 'vitest';
2+
import { TrackUserProtection } from './track-user-protection';
3+
4+
type VerifyResult = Awaited<ReturnType<TrackUserProtection['verifyAndDeduplicate']>>;
5+
type ValidReason = NonNullable<VerifyResult['reason']>;
6+
7+
describe('TypeScript Compiler Validation & Schema Constraints Stability', () => {
8+
it('enforces correct return type shape for verifyAndDeduplicate', () => {
9+
expectTypeOf<VerifyResult>().toHaveProperty('allowed').toBeBoolean();
10+
expectTypeOf<VerifyResult>().toHaveProperty('reason').toEqualTypeOf<ValidReason | undefined>();
11+
expectTypeOf<VerifyResult>().toHaveProperty('remainingMs').toEqualTypeOf<number | undefined>();
12+
});
13+
14+
it('validates method signatures accept correct parameter types and return types', () => {
15+
type Instance = TrackUserProtection;
16+
expectTypeOf<Instance['validateFormat']>().parameters.toEqualTypeOf<[username: string]>();
17+
expectTypeOf<Instance['validateFormat']>().returns.toBeBoolean();
18+
expectTypeOf<Instance['isWriteAllowed']>().parameters.toEqualTypeOf<[username: string]>();
19+
expectTypeOf<Instance['isWriteAllowed']>().returns.toBeBoolean();
20+
expectTypeOf<Instance['recordWrite']>().returns.toBeVoid();
21+
});
22+
23+
it('validates singleton instance pattern returns correct type', () => {
24+
expectTypeOf<
25+
ReturnType<(typeof TrackUserProtection)['getInstance']>
26+
>().toEqualTypeOf<TrackUserProtection>();
27+
});
28+
29+
it('blocks invalid string literals from being assigned to reason field', () => {
30+
expectTypeOf<'INVALID'>().not.toMatchTypeOf<ValidReason>();
31+
expectTypeOf<'INVALID_FORMAT'>().toMatchTypeOf<ValidReason>();
32+
expectTypeOf<'COOLDOWN_ACTIVE'>().toMatchTypeOf<ValidReason>();
33+
expectTypeOf<'USER_NOT_FOUND'>().toMatchTypeOf<ValidReason>();
34+
});
35+
36+
it('verifies schema validation constraints return strict validation reports', () => {
37+
expectTypeOf<TrackUserProtection['verifyAndDeduplicate']>().parameters.toEqualTypeOf<
38+
[username: string]
39+
>();
40+
expectTypeOf<TrackUserProtection['verifyAndDeduplicate']>().returns.toEqualTypeOf<
41+
Promise<VerifyResult>
42+
>();
43+
});
44+
});

0 commit comments

Comments
 (0)