|
| 1 | +import { describe, it, expect, expectTypeOf } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
| 3 | + |
| 4 | +// Test suite: verify TypeScript compiler stability and runtime schema boundaries |
| 5 | +describe('Contributors loading type & schema compiler checks (Variation 10)', () => { |
| 6 | + // Baseline expected contributor shape (matches ContributorsClient internal shape) |
| 7 | + type ExpectedContributor = { |
| 8 | + id: number; |
| 9 | + login: string; |
| 10 | + avatar_url: string; |
| 11 | + contributions: number; |
| 12 | + html_url: string; |
| 13 | + }; |
| 14 | + |
| 15 | + it('Case 1: Enforce field property configuration types match expected baseline structures exactly', () => { |
| 16 | + // A matching shape should be exactly equal to the expected contributor type |
| 17 | + type Matching = { |
| 18 | + id: number; |
| 19 | + login: string; |
| 20 | + avatar_url: string; |
| 21 | + contributions: number; |
| 22 | + html_url: string; |
| 23 | + }; |
| 24 | + |
| 25 | + expectTypeOf<Matching>().toEqualTypeOf<ExpectedContributor>(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('Case 2: Assert that invalid parameter shapes are structurally incompatible', () => { |
| 29 | + // Wrong types / missing required fields should not match ExpectedContributor |
| 30 | + type InvalidContributor = { |
| 31 | + id: string; // wrong type |
| 32 | + login?: string; // optional where required |
| 33 | + }; |
| 34 | + |
| 35 | + expectTypeOf<InvalidContributor>().not.toMatchTypeOf<ExpectedContributor>(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('Case 3: Verify custom configurations accept optional parameters without compiler warnings', () => { |
| 39 | + // Define an expected loading config with optional parameters |
| 40 | + type LoadingConfig = { |
| 41 | + limit?: number; |
| 42 | + showAvatars?: boolean; |
| 43 | + filters?: { org?: string } | undefined; |
| 44 | + }; |
| 45 | + |
| 46 | + // A custom consumer may provide only a subset of optional parameters |
| 47 | + const customConfig = { limit: 6 } satisfies LoadingConfig; |
| 48 | + |
| 49 | + // runtime assertion to keep TS happy and ensure the optional property works |
| 50 | + expect(customConfig.limit).toBe(6); |
| 51 | + |
| 52 | + // compile-time: ensure the custom minimal shape is assignable to LoadingConfig |
| 53 | + type CustomMinimal = typeof customConfig; |
| 54 | + expectTypeOf<CustomMinimal>().toMatchTypeOf<LoadingConfig>(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Case 4: Runtime validation schemas flag structural boundary anomalies with strict error reports', () => { |
| 58 | + const contributorSchema = z |
| 59 | + .object({ |
| 60 | + id: z.number(), |
| 61 | + login: z.string(), |
| 62 | + avatar_url: z.string(), |
| 63 | + contributions: z.number(), |
| 64 | + html_url: z.string(), |
| 65 | + }) |
| 66 | + .strict(); |
| 67 | + |
| 68 | + const badPayload = { |
| 69 | + id: 1, |
| 70 | + login: 'alice', |
| 71 | + avatar_url: 'https://example.com/a.png', |
| 72 | + contributions: 42, |
| 73 | + html_url: 'https://github.com/alice', |
| 74 | + unexpected_extra: 'surprise', |
| 75 | + }; |
| 76 | + |
| 77 | + try { |
| 78 | + contributorSchema.parse(badPayload); |
| 79 | + // If parse does not throw, force a failure |
| 80 | + expect(false).toBe(true); |
| 81 | + } catch (err) { |
| 82 | + // ZodError -> inspect issues for unrecognized keys |
| 83 | + const zErr = err as z.ZodError; |
| 84 | + const hasUnrecognized = zErr.issues.some((i) => i.code === z.ZodIssueCode.unrecognized_keys); |
| 85 | + expect(hasUnrecognized).toBe(true); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('Case 5: Ensure type parameters resolve down to stable readonly or structured object contracts', () => { |
| 90 | + type ReadonlyContributor = Readonly<ExpectedContributor>; |
| 91 | + |
| 92 | + // Expected readonly contract |
| 93 | + type ExplicitReadonly = { |
| 94 | + readonly id: number; |
| 95 | + readonly login: string; |
| 96 | + readonly avatar_url: string; |
| 97 | + readonly contributions: number; |
| 98 | + readonly html_url: string; |
| 99 | + }; |
| 100 | + |
| 101 | + expectTypeOf<ReadonlyContributor>().toEqualTypeOf<ExplicitReadonly>(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments