|
| 1 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { GeneratorClient } from './GeneratorClient'; |
| 3 | +import type { |
| 4 | + GeneratorState, |
| 5 | + Technology, |
| 6 | + Social, |
| 7 | + TechCategory, |
| 8 | + SocialCategory, |
| 9 | + IconType, |
| 10 | +} from './types'; |
| 11 | +import type { |
| 12 | + GitHubUser, |
| 13 | + GitHubRepo, |
| 14 | + GitHubSocialAccount, |
| 15 | + ImportedData, |
| 16 | +} from './utils/githubMapper'; |
| 17 | +import { mapGitHubData } from './utils/githubMapper'; |
| 18 | + |
| 19 | +describe('GeneratorClient TypeScript Compiler Validation', () => { |
| 20 | + // 1. Use type-testing assertions (expectTypeOf) to enforce field property configurations. |
| 21 | + it('enforces field property configurations of GeneratorState', () => { |
| 22 | + expectTypeOf<GeneratorState['name']>().toEqualTypeOf<string>(); |
| 23 | + expectTypeOf<GeneratorState['description']>().toEqualTypeOf<string>(); |
| 24 | + expectTypeOf<GeneratorState['selectedTechs']>().toEqualTypeOf<string[]>(); |
| 25 | + expectTypeOf<GeneratorState['selectedSocials']>().toEqualTypeOf<string[]>(); |
| 26 | + expectTypeOf<GeneratorState['socialLinks']>().toEqualTypeOf<Record<string, string>>(); |
| 27 | + expectTypeOf<GeneratorState['githubUsername']>().toEqualTypeOf<string>(); |
| 28 | + expectTypeOf<GeneratorState['showCommitPulse']>().toEqualTypeOf<boolean>(); |
| 29 | + expectTypeOf<GeneratorState['commitPulseAccent']>().toEqualTypeOf<string>(); |
| 30 | + expectTypeOf<GeneratorState['showRepoSpotlight']>().toEqualTypeOf<boolean>(); |
| 31 | + expectTypeOf<GeneratorState['spotlightRepo']>().toEqualTypeOf<string>(); |
| 32 | + expectTypeOf<GeneratorState['showSnakeGraph']>().toEqualTypeOf<boolean>(); |
| 33 | + expectTypeOf<GeneratorState['showPacmanGraph']>().toEqualTypeOf<boolean>(); |
| 34 | + expectTypeOf<GeneratorState['graphPlacement']>().toEqualTypeOf<'top' | 'middle' | 'bottom'>(); |
| 35 | + }); |
| 36 | + |
| 37 | + // 2. Assert that invalid prop parameters are blocked during static type checking. |
| 38 | + it('blocks invalid prop parameters during static type checking', () => { |
| 39 | + // GeneratorClient has no props |
| 40 | + expectTypeOf(GeneratorClient).toBeFunction(); |
| 41 | + |
| 42 | + void ({ |
| 43 | + name: 'John Doe', |
| 44 | + description: 'Bio details', |
| 45 | + selectedTechs: [], |
| 46 | + selectedSocials: [], |
| 47 | + socialLinks: {}, |
| 48 | + githubUsername: 'john', |
| 49 | + showCommitPulse: true, |
| 50 | + commitPulseAccent: '#ff0000', |
| 51 | + showRepoSpotlight: false, |
| 52 | + spotlightRepo: '', |
| 53 | + showSnakeGraph: false, |
| 54 | + showPacmanGraph: false, |
| 55 | + // @ts-expect-error graphPlacement must be 'top' | 'middle' | 'bottom' |
| 56 | + graphPlacement: 'left', |
| 57 | + } satisfies GeneratorState); |
| 58 | + |
| 59 | + void ({ |
| 60 | + name: 'John Doe', |
| 61 | + description: 'Bio details', |
| 62 | + // @ts-expect-error selectedTechs must be array of strings |
| 63 | + selectedTechs: 'react', |
| 64 | + selectedSocials: [], |
| 65 | + socialLinks: {}, |
| 66 | + githubUsername: 'john', |
| 67 | + showCommitPulse: true, |
| 68 | + commitPulseAccent: '#ff0000', |
| 69 | + showRepoSpotlight: false, |
| 70 | + spotlightRepo: '', |
| 71 | + showSnakeGraph: false, |
| 72 | + showPacmanGraph: false, |
| 73 | + graphPlacement: 'top', |
| 74 | + } satisfies GeneratorState); |
| 75 | + }); |
| 76 | + |
| 77 | + // 3. Verify custom types accept optional/nullable values without compile errors. |
| 78 | + it('verifies custom GitHub data types accept optional/nullable values without compile errors', () => { |
| 79 | + const user: GitHubUser = { |
| 80 | + name: null, |
| 81 | + bio: null, |
| 82 | + blog: null, |
| 83 | + twitter_username: null, |
| 84 | + email: null, |
| 85 | + }; |
| 86 | + |
| 87 | + expectTypeOf(user.name).toEqualTypeOf<string | null>(); |
| 88 | + expectTypeOf(user.bio).toEqualTypeOf<string | null>(); |
| 89 | + }); |
| 90 | + |
| 91 | + // 4. Verify field configurations and types for Technology and Social interfaces. |
| 92 | + it('verifies Technology and Social interfaces and categories', () => { |
| 93 | + expectTypeOf<Technology['category']>().toEqualTypeOf<TechCategory>(); |
| 94 | + expectTypeOf<Social['category']>().toEqualTypeOf<SocialCategory>(); |
| 95 | + expectTypeOf<Technology['type']>().toEqualTypeOf<IconType>(); |
| 96 | + expectTypeOf<Social['type']>().toEqualTypeOf<IconType>(); |
| 97 | + }); |
| 98 | + |
| 99 | + // 5. Verify schema validation constraints return strict validation reports. |
| 100 | + it('verifies parameter constraints and return shapes on mapGitHubData', () => { |
| 101 | + expectTypeOf(mapGitHubData).parameter(0).toEqualTypeOf<GitHubUser>(); |
| 102 | + expectTypeOf(mapGitHubData).parameter(1).toEqualTypeOf<GitHubRepo[]>(); |
| 103 | + expectTypeOf(mapGitHubData).parameter(2).toEqualTypeOf<GitHubSocialAccount[]>(); |
| 104 | + expectTypeOf(mapGitHubData).returns.toEqualTypeOf<ImportedData>(); |
| 105 | + |
| 106 | + // @ts-expect-error mapGitHubData parameter 0 must satisfy GitHubUser structure |
| 107 | + void mapGitHubData({ name: 123 }, [], []); |
| 108 | + }); |
| 109 | +}); |
0 commit comments