|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { ZodError } from 'zod'; |
| 3 | +import type { ConfigPatterns } from './models.js'; |
| 4 | +import { configPatternsSchema, interpolatedSlugSchema } from './schemas.js'; |
| 5 | + |
| 6 | +describe('interpolatedSlugSchema', () => { |
| 7 | + it('should accept a valid slug', () => { |
| 8 | + expect(interpolatedSlugSchema.parse('valid-slug')).toBe('valid-slug'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should accept a slug with {projectName} interpolation', () => { |
| 12 | + expect(interpolatedSlugSchema.parse('{projectName}-slug')).toBe( |
| 13 | + '{projectName}-slug', |
| 14 | + ); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should reject an invalid slug that cannot be fixed by interpolation', () => { |
| 18 | + expect(() => interpolatedSlugSchema.parse('Invalid Slug!')).toThrow( |
| 19 | + ZodError, |
| 20 | + ); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should reject a non-string value', () => { |
| 24 | + expect(() => interpolatedSlugSchema.parse(123)).toThrow(ZodError); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe('configPatternsSchema', () => { |
| 29 | + it('should accept valid persist and upload configs', () => { |
| 30 | + const configPatterns: Required<ConfigPatterns> = { |
| 31 | + persist: { |
| 32 | + outputDir: '.code-pushup/{projectName}', |
| 33 | + filename: 'report', |
| 34 | + format: ['json', 'md'], |
| 35 | + skipReports: false, |
| 36 | + }, |
| 37 | + upload: { |
| 38 | + server: 'https://api.code-pushup.example.com/graphql', |
| 39 | + apiKey: 'cp_...', |
| 40 | + organization: 'example', |
| 41 | + project: '{projectName}', |
| 42 | + }, |
| 43 | + }; |
| 44 | + expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should accept persist config without upload', () => { |
| 48 | + const configPatterns: ConfigPatterns = { |
| 49 | + persist: { |
| 50 | + outputDir: '.code-pushup/{projectName}', |
| 51 | + filename: 'report', |
| 52 | + format: ['json', 'md'], |
| 53 | + skipReports: false, |
| 54 | + }, |
| 55 | + }; |
| 56 | + expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns); |
| 57 | + }); |
| 58 | + |
| 59 | + it('fills in default persist values if missing', () => { |
| 60 | + expect( |
| 61 | + configPatternsSchema.parse({ |
| 62 | + persist: { |
| 63 | + filename: '{projectName}-report', |
| 64 | + }, |
| 65 | + }), |
| 66 | + ).toEqual<ConfigPatterns>({ |
| 67 | + persist: { |
| 68 | + outputDir: '.code-pushup', |
| 69 | + filename: '{projectName}-report', |
| 70 | + format: ['json', 'md'], |
| 71 | + skipReports: false, |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should reject if persist is missing', () => { |
| 77 | + expect(() => configPatternsSchema.parse({})).toThrow(ZodError); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should reject if persist has invalid values', () => { |
| 81 | + expect(() => |
| 82 | + configPatternsSchema.parse({ |
| 83 | + persist: { |
| 84 | + format: 'json', // should be array |
| 85 | + }, |
| 86 | + }), |
| 87 | + ).toThrow(ZodError); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should reject if upload is missing required fields', () => { |
| 91 | + expect(() => |
| 92 | + configPatternsSchema.parse({ |
| 93 | + persist: { |
| 94 | + outputDir: '.code-pushup/{projectName}', |
| 95 | + filename: 'report', |
| 96 | + format: ['json', 'md'], |
| 97 | + skipReports: false, |
| 98 | + }, |
| 99 | + upload: { |
| 100 | + server: 'https://api.code-pushup.example.com/graphql', |
| 101 | + organization: 'example', |
| 102 | + project: '{projectName}', |
| 103 | + // missing apiKey |
| 104 | + }, |
| 105 | + }), |
| 106 | + ).toThrow(ZodError); |
| 107 | + }); |
| 108 | +}); |
0 commit comments