|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { BadRequestException } from '@nestjs/common'; |
| 3 | +import { EventValidationService } from './event-validation.service'; |
| 4 | +import { EventType } from '../entities/event.entity'; |
| 5 | + |
| 6 | +const VALID_UUID = '123e4567-e89b-12d3-a456-426614174000'; |
| 7 | + |
| 8 | +describe('EventValidationService', () => { |
| 9 | + let service: EventValidationService; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + const module: TestingModule = await Test.createTestingModule({ |
| 13 | + providers: [EventValidationService], |
| 14 | + }).compile(); |
| 15 | + |
| 16 | + service = module.get<EventValidationService>(EventValidationService); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('validateEvent', () => { |
| 20 | + it('fails when eventType is missing', () => { |
| 21 | + const result = service.validateEvent({}); |
| 22 | + expect(result).toEqual({ valid: false, errors: ['eventType is required'] }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('allows an event type with no registered schema', () => { |
| 26 | + const result = service.validateEvent({ eventType: EventType.LESSON_COMPLETE }); |
| 27 | + expect(result).toEqual({ valid: true, errors: [] }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('reports every missing required field', () => { |
| 31 | + const result = service.validateEvent({ eventType: EventType.SIGNUP }); |
| 32 | + expect(result.valid).toBe(false); |
| 33 | + expect(result.errors).toEqual( |
| 34 | + expect.arrayContaining([ |
| 35 | + 'Required field missing: userId', |
| 36 | + 'Required field missing: category', |
| 37 | + 'Required field missing: action', |
| 38 | + ]), |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('passes a well-formed event that satisfies its schema', () => { |
| 43 | + const result = service.validateEvent({ |
| 44 | + eventType: EventType.SIGNUP, |
| 45 | + userId: VALID_UUID, |
| 46 | + category: 'auth', |
| 47 | + action: 'signup', |
| 48 | + } as any); |
| 49 | + expect(result).toEqual({ valid: true, errors: [] }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('fails custom validation when userId is not a valid UUID', () => { |
| 53 | + const result = service.validateEvent({ |
| 54 | + eventType: EventType.LOGIN, |
| 55 | + userId: 'not-a-uuid', |
| 56 | + category: 'auth', |
| 57 | + action: 'login', |
| 58 | + } as any); |
| 59 | + expect(result.valid).toBe(false); |
| 60 | + expect(result.errors).toContain('Custom validation failed'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('enforces minValue constraints', () => { |
| 64 | + const result = service.validateEvent({ |
| 65 | + eventType: EventType.PURCHASE, |
| 66 | + userId: VALID_UUID, |
| 67 | + category: 'commerce', |
| 68 | + action: 'purchase', |
| 69 | + value: -5, |
| 70 | + properties: { courseId: VALID_UUID }, |
| 71 | + } as any); |
| 72 | + expect(result.valid).toBe(false); |
| 73 | + expect(result.errors).toContain('Value -5 is below minimum 0'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('enforces maxValue constraints', () => { |
| 77 | + service.registerSchema({ |
| 78 | + eventType: EventType.CUSTOM, |
| 79 | + requiredFields: ['category', 'action'], |
| 80 | + optionalFields: [], |
| 81 | + valueConstraints: { maxValue: 10 }, |
| 82 | + }); |
| 83 | + |
| 84 | + const result = service.validateEvent({ |
| 85 | + eventType: EventType.CUSTOM, |
| 86 | + category: 'c', |
| 87 | + action: 'a', |
| 88 | + value: 20, |
| 89 | + } as any); |
| 90 | + expect(result.valid).toBe(false); |
| 91 | + expect(result.errors).toContain('Value 20 exceeds maximum 10'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('enforces allowedValues constraints', () => { |
| 95 | + service.registerSchema({ |
| 96 | + eventType: EventType.CUSTOM, |
| 97 | + requiredFields: ['category', 'action'], |
| 98 | + optionalFields: [], |
| 99 | + valueConstraints: { allowedValues: [1, 2, 3] }, |
| 100 | + }); |
| 101 | + |
| 102 | + const result = service.validateEvent({ |
| 103 | + eventType: EventType.CUSTOM, |
| 104 | + category: 'c', |
| 105 | + action: 'a', |
| 106 | + value: 99, |
| 107 | + } as any); |
| 108 | + expect(result.valid).toBe(false); |
| 109 | + expect(result.errors).toContain('Value 99 is not in allowed values'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('accumulates multiple distinct validation errors', () => { |
| 113 | + const result = service.validateEvent({ |
| 114 | + eventType: EventType.PURCHASE, |
| 115 | + value: -1, |
| 116 | + } as any); |
| 117 | + expect(result.valid).toBe(false); |
| 118 | + expect(result.errors.length).toBeGreaterThan(1); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('validateEventOrThrow', () => { |
| 123 | + it('does not throw for a valid event', () => { |
| 124 | + expect(() => |
| 125 | + service.validateEventOrThrow({ |
| 126 | + eventType: EventType.CUSTOM, |
| 127 | + category: 'c', |
| 128 | + action: 'a', |
| 129 | + } as any), |
| 130 | + ).not.toThrow(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('throws BadRequestException with the collected errors for an invalid event', () => { |
| 134 | + expect(() => service.validateEventOrThrow({} as any)).toThrow(BadRequestException); |
| 135 | + expect(() => service.validateEventOrThrow({} as any)).toThrow(/eventType is required/); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('registerSchema / getSchema', () => { |
| 140 | + it('registers a new schema and makes it retrievable', () => { |
| 141 | + const schema = { |
| 142 | + eventType: EventType.WISHLIST_ADD, |
| 143 | + requiredFields: ['userId'], |
| 144 | + optionalFields: [], |
| 145 | + }; |
| 146 | + service.registerSchema(schema); |
| 147 | + |
| 148 | + expect(service.getSchema(EventType.WISHLIST_ADD)).toEqual(schema); |
| 149 | + }); |
| 150 | + |
| 151 | + it('overwrites a previously registered schema for the same event type', () => { |
| 152 | + const original = service.getSchema(EventType.SIGNUP); |
| 153 | + expect(original).toBeDefined(); |
| 154 | + |
| 155 | + const replacement = { |
| 156 | + eventType: EventType.SIGNUP, |
| 157 | + requiredFields: [], |
| 158 | + optionalFields: [], |
| 159 | + }; |
| 160 | + service.registerSchema(replacement); |
| 161 | + |
| 162 | + expect(service.getSchema(EventType.SIGNUP)).toEqual(replacement); |
| 163 | + }); |
| 164 | + |
| 165 | + it('returns undefined for an event type with no schema', () => { |
| 166 | + expect(service.getSchema(EventType.LESSON_COMPLETE)).toBeUndefined(); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments