|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + FeatureStrategy, |
| 4 | + FeatureFlagSchema, |
| 5 | + FeatureFlag, |
| 6 | + type FeatureFlag as FeatureFlagType, |
| 7 | +} from './feature.zod'; |
| 8 | + |
| 9 | +describe('FeatureStrategy', () => { |
| 10 | + it('should accept valid strategies', () => { |
| 11 | + expect(() => FeatureStrategy.parse('boolean')).not.toThrow(); |
| 12 | + expect(() => FeatureStrategy.parse('percentage')).not.toThrow(); |
| 13 | + expect(() => FeatureStrategy.parse('user_list')).not.toThrow(); |
| 14 | + expect(() => FeatureStrategy.parse('group')).not.toThrow(); |
| 15 | + expect(() => FeatureStrategy.parse('custom')).not.toThrow(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should reject invalid strategies', () => { |
| 19 | + expect(() => FeatureStrategy.parse('random')).toThrow(); |
| 20 | + expect(() => FeatureStrategy.parse('')).toThrow(); |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('FeatureFlagSchema', () => { |
| 25 | + const minimalFlag = { |
| 26 | + name: 'dark_mode', |
| 27 | + }; |
| 28 | + |
| 29 | + it('should accept minimal feature flag', () => { |
| 30 | + expect(() => FeatureFlagSchema.parse(minimalFlag)).not.toThrow(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should apply default values', () => { |
| 34 | + const parsed = FeatureFlagSchema.parse(minimalFlag); |
| 35 | + expect(parsed.enabled).toBe(false); |
| 36 | + expect(parsed.strategy).toBe('boolean'); |
| 37 | + expect(parsed.environment).toBe('all'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should accept feature flag with all fields', () => { |
| 41 | + const full: FeatureFlagType = { |
| 42 | + name: 'new_dashboard', |
| 43 | + label: 'New Dashboard', |
| 44 | + description: 'Enables the new dashboard UI', |
| 45 | + enabled: true, |
| 46 | + strategy: 'percentage', |
| 47 | + conditions: { |
| 48 | + percentage: 50, |
| 49 | + users: ['user_1', 'user_2'], |
| 50 | + groups: ['beta_testers'], |
| 51 | + expression: 'user.plan == "pro"', |
| 52 | + }, |
| 53 | + environment: 'staging', |
| 54 | + expiresAt: '2025-12-31T23:59:59Z', |
| 55 | + }; |
| 56 | + |
| 57 | + const parsed = FeatureFlagSchema.parse(full); |
| 58 | + expect(parsed.label).toBe('New Dashboard'); |
| 59 | + expect(parsed.conditions?.percentage).toBe(50); |
| 60 | + expect(parsed.conditions?.users).toHaveLength(2); |
| 61 | + expect(parsed.environment).toBe('staging'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should reject invalid name (not snake_case)', () => { |
| 65 | + expect(() => FeatureFlagSchema.parse({ name: 'DarkMode' })).toThrow(); |
| 66 | + expect(() => FeatureFlagSchema.parse({ name: 'dark-mode' })).toThrow(); |
| 67 | + expect(() => FeatureFlagSchema.parse({ name: '1invalid' })).toThrow(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should reject name that is too short', () => { |
| 71 | + expect(() => FeatureFlagSchema.parse({ name: 'a' })).toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should accept all environment values', () => { |
| 75 | + const envs = ['dev', 'staging', 'prod', 'all'] as const; |
| 76 | + envs.forEach(environment => { |
| 77 | + const parsed = FeatureFlagSchema.parse({ name: 'test_flag', environment }); |
| 78 | + expect(parsed.environment).toBe(environment); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should reject invalid environment', () => { |
| 83 | + expect(() => FeatureFlagSchema.parse({ |
| 84 | + name: 'test_flag', |
| 85 | + environment: 'local', |
| 86 | + })).toThrow(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should reject percentage out of range', () => { |
| 90 | + expect(() => FeatureFlagSchema.parse({ |
| 91 | + name: 'test_flag', |
| 92 | + conditions: { percentage: 101 }, |
| 93 | + })).toThrow(); |
| 94 | + |
| 95 | + expect(() => FeatureFlagSchema.parse({ |
| 96 | + name: 'test_flag', |
| 97 | + conditions: { percentage: -1 }, |
| 98 | + })).toThrow(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should accept percentage at boundaries', () => { |
| 102 | + const zero = FeatureFlagSchema.parse({ |
| 103 | + name: 'test_flag', |
| 104 | + conditions: { percentage: 0 }, |
| 105 | + }); |
| 106 | + expect(zero.conditions?.percentage).toBe(0); |
| 107 | + |
| 108 | + const hundred = FeatureFlagSchema.parse({ |
| 109 | + name: 'test_flag', |
| 110 | + conditions: { percentage: 100 }, |
| 111 | + }); |
| 112 | + expect(hundred.conditions?.percentage).toBe(100); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should validate expiresAt as ISO datetime', () => { |
| 116 | + expect(() => FeatureFlagSchema.parse({ |
| 117 | + name: 'test_flag', |
| 118 | + expiresAt: 'not-a-date', |
| 119 | + })).toThrow(); |
| 120 | + |
| 121 | + expect(() => FeatureFlagSchema.parse({ |
| 122 | + name: 'test_flag', |
| 123 | + expiresAt: '2025-06-15T10:00:00Z', |
| 124 | + })).not.toThrow(); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('FeatureFlag.create', () => { |
| 129 | + it('should return the config object as-is', () => { |
| 130 | + const config = { name: 'my_feature', enabled: true }; |
| 131 | + const result = FeatureFlag.create(config); |
| 132 | + expect(result).toEqual(config); |
| 133 | + }); |
| 134 | +}); |
0 commit comments