|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + TestContextSchema, |
| 4 | + TestActionTypeSchema, |
| 5 | + TestActionSchema, |
| 6 | + TestAssertionTypeSchema, |
| 7 | + TestAssertionSchema, |
| 8 | + TestStepSchema, |
| 9 | + TestScenarioSchema, |
| 10 | + TestSuiteSchema, |
| 11 | +} from './testing.zod'; |
| 12 | + |
| 13 | +describe('TestContextSchema', () => { |
| 14 | + it('should accept a valid context record', () => { |
| 15 | + expect(() => TestContextSchema.parse({ userId: '123', debug: true })).not.toThrow(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should accept an empty record', () => { |
| 19 | + expect(() => TestContextSchema.parse({})).not.toThrow(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should reject non-object values', () => { |
| 23 | + expect(() => TestContextSchema.parse('invalid')).toThrow(); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('TestActionTypeSchema', () => { |
| 28 | + it('should accept all valid action types', () => { |
| 29 | + const types = ['create_record', 'update_record', 'delete_record', 'read_record', 'query_records', 'api_call', 'run_script', 'wait']; |
| 30 | + types.forEach(t => { |
| 31 | + expect(() => TestActionTypeSchema.parse(t)).not.toThrow(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should reject invalid action type', () => { |
| 36 | + expect(() => TestActionTypeSchema.parse('invalid_type')).toThrow(); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('TestActionSchema', () => { |
| 41 | + it('should accept minimal valid action', () => { |
| 42 | + const action = { type: 'create_record', target: 'account' }; |
| 43 | + const result = TestActionSchema.parse(action); |
| 44 | + expect(result.type).toBe('create_record'); |
| 45 | + expect(result.target).toBe('account'); |
| 46 | + expect(result.payload).toBeUndefined(); |
| 47 | + expect(result.user).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should accept full action with all optional fields', () => { |
| 51 | + const action = { |
| 52 | + type: 'api_call', |
| 53 | + target: '/api/v1/accounts', |
| 54 | + payload: { name: 'Test Account' }, |
| 55 | + user: 'admin', |
| 56 | + }; |
| 57 | + expect(() => TestActionSchema.parse(action)).not.toThrow(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should reject action without target', () => { |
| 61 | + expect(() => TestActionSchema.parse({ type: 'create_record' })).toThrow(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should reject action with invalid type', () => { |
| 65 | + expect(() => TestActionSchema.parse({ type: 'bad_type', target: 'x' })).toThrow(); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('TestAssertionTypeSchema', () => { |
| 70 | + it('should accept all valid assertion types', () => { |
| 71 | + const types = ['equals', 'not_equals', 'contains', 'not_contains', 'is_null', 'not_null', 'gt', 'gte', 'lt', 'lte', 'error']; |
| 72 | + types.forEach(t => { |
| 73 | + expect(() => TestAssertionTypeSchema.parse(t)).not.toThrow(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should reject invalid assertion type', () => { |
| 78 | + expect(() => TestAssertionTypeSchema.parse('unknown')).toThrow(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('TestAssertionSchema', () => { |
| 83 | + it('should accept a valid assertion', () => { |
| 84 | + const assertion = { field: 'body.status', operator: 'equals', expectedValue: 'active' }; |
| 85 | + const result = TestAssertionSchema.parse(assertion); |
| 86 | + expect(result.field).toBe('body.status'); |
| 87 | + expect(result.operator).toBe('equals'); |
| 88 | + expect(result.expectedValue).toBe('active'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should reject assertion without field', () => { |
| 92 | + expect(() => TestAssertionSchema.parse({ operator: 'equals', expectedValue: 1 })).toThrow(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should reject assertion with invalid operator', () => { |
| 96 | + expect(() => TestAssertionSchema.parse({ field: 'x', operator: 'invalid', expectedValue: 1 })).toThrow(); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('TestStepSchema', () => { |
| 101 | + const minimalStep = { |
| 102 | + name: 'Create account', |
| 103 | + action: { type: 'create_record', target: 'account' }, |
| 104 | + }; |
| 105 | + |
| 106 | + it('should accept minimal step', () => { |
| 107 | + const result = TestStepSchema.parse(minimalStep); |
| 108 | + expect(result.name).toBe('Create account'); |
| 109 | + expect(result.description).toBeUndefined(); |
| 110 | + expect(result.assertions).toBeUndefined(); |
| 111 | + expect(result.capture).toBeUndefined(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should accept step with all optional fields', () => { |
| 115 | + const step = { |
| 116 | + ...minimalStep, |
| 117 | + description: 'Creates a new account record', |
| 118 | + assertions: [{ field: 'body._id', operator: 'not_null', expectedValue: null }], |
| 119 | + capture: { newId: 'body._id' }, |
| 120 | + }; |
| 121 | + expect(() => TestStepSchema.parse(step)).not.toThrow(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should reject step without name', () => { |
| 125 | + expect(() => TestStepSchema.parse({ action: { type: 'read_record', target: 'x' } })).toThrow(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should reject step without action', () => { |
| 129 | + expect(() => TestStepSchema.parse({ name: 'step1' })).toThrow(); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('TestScenarioSchema', () => { |
| 134 | + const minimalScenario = { |
| 135 | + id: 'sc-001', |
| 136 | + name: 'Account CRUD Test', |
| 137 | + steps: [{ name: 'step1', action: { type: 'create_record', target: 'account' } }], |
| 138 | + }; |
| 139 | + |
| 140 | + it('should accept minimal scenario', () => { |
| 141 | + const result = TestScenarioSchema.parse(minimalScenario); |
| 142 | + expect(result.id).toBe('sc-001'); |
| 143 | + expect(result.name).toBe('Account CRUD Test'); |
| 144 | + expect(result.steps).toHaveLength(1); |
| 145 | + expect(result.description).toBeUndefined(); |
| 146 | + expect(result.tags).toBeUndefined(); |
| 147 | + expect(result.setup).toBeUndefined(); |
| 148 | + expect(result.teardown).toBeUndefined(); |
| 149 | + expect(result.requires).toBeUndefined(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should accept full scenario with all optional fields', () => { |
| 153 | + const scenario = { |
| 154 | + ...minimalScenario, |
| 155 | + description: 'Tests full CRUD lifecycle', |
| 156 | + tags: ['critical', 'regression'], |
| 157 | + setup: [{ name: 'setup-data', action: { type: 'run_script', target: 'seed' } }], |
| 158 | + teardown: [{ name: 'cleanup', action: { type: 'delete_record', target: 'account' } }], |
| 159 | + requires: { |
| 160 | + params: ['API_KEY'], |
| 161 | + plugins: ['crm'], |
| 162 | + }, |
| 163 | + }; |
| 164 | + expect(() => TestScenarioSchema.parse(scenario)).not.toThrow(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should reject scenario without steps', () => { |
| 168 | + expect(() => TestScenarioSchema.parse({ id: 'sc-001', name: 'Test' })).toThrow(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should reject scenario without id', () => { |
| 172 | + expect(() => TestScenarioSchema.parse({ name: 'Test', steps: [] })).toThrow(); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
| 176 | +describe('TestSuiteSchema', () => { |
| 177 | + it('should accept a valid test suite', () => { |
| 178 | + const suite = { |
| 179 | + name: 'CRM Test Suite', |
| 180 | + scenarios: [{ |
| 181 | + id: 'sc-001', |
| 182 | + name: 'Account Test', |
| 183 | + steps: [{ name: 'step1', action: { type: 'read_record', target: 'account' } }], |
| 184 | + }], |
| 185 | + }; |
| 186 | + const result = TestSuiteSchema.parse(suite); |
| 187 | + expect(result.name).toBe('CRM Test Suite'); |
| 188 | + expect(result.scenarios).toHaveLength(1); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should accept suite with empty scenarios', () => { |
| 192 | + expect(() => TestSuiteSchema.parse({ name: 'Empty Suite', scenarios: [] })).not.toThrow(); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should reject suite without name', () => { |
| 196 | + expect(() => TestSuiteSchema.parse({ scenarios: [] })).toThrow(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should reject suite without scenarios', () => { |
| 200 | + expect(() => TestSuiteSchema.parse({ name: 'Suite' })).toThrow(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments