-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtraining.test.ts
More file actions
265 lines (234 loc) · 7.56 KB
/
Copy pathtraining.test.ts
File metadata and controls
265 lines (234 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { describe, it, expect } from 'vitest';
import {
TrainingCategorySchema,
TrainingCompletionStatusSchema,
TrainingCourseSchema,
TrainingRecordSchema,
TrainingPlanSchema,
type TrainingCourse,
type TrainingRecord,
} from './training.zod';
describe('TrainingCategorySchema', () => {
it('should accept all valid categories', () => {
const validCategories = [
'security_awareness', 'data_protection', 'incident_response',
'access_control', 'phishing_awareness', 'compliance',
'secure_development', 'physical_security', 'business_continuity', 'other',
];
validCategories.forEach((category) => {
expect(() => TrainingCategorySchema.parse(category)).not.toThrow();
});
});
it('should reject invalid category', () => {
expect(() => TrainingCategorySchema.parse('yoga')).toThrow();
});
});
describe('TrainingCompletionStatusSchema', () => {
it('should accept all valid statuses', () => {
const statuses = ['not_started', 'in_progress', 'completed', 'failed', 'expired'];
statuses.forEach((status) => {
expect(() => TrainingCompletionStatusSchema.parse(status)).not.toThrow();
});
});
it('should reject invalid status', () => {
expect(() => TrainingCompletionStatusSchema.parse('skipped')).toThrow();
});
});
describe('TrainingCourseSchema', () => {
it('should accept valid course with defaults', () => {
const course = TrainingCourseSchema.parse({
id: 'COURSE-SEC-001',
title: 'Information Security Fundamentals',
description: 'Annual security awareness training for all employees',
category: 'security_awareness',
durationMinutes: 60,
targetRoles: ['all_employees'],
});
expect(course.mandatory).toBe(false);
expect(course.passingScore).toBeUndefined();
expect(course.validityDays).toBeUndefined();
});
it('should accept full course configuration', () => {
const course: TrainingCourse = {
id: 'COURSE-SEC-002',
title: 'Phishing Awareness Training',
description: 'Recognize and report phishing attempts',
category: 'phishing_awareness',
durationMinutes: 30,
mandatory: true,
targetRoles: ['all_employees', 'contractors'],
validityDays: 365,
passingScore: 80,
version: '2.0',
};
expect(() => TrainingCourseSchema.parse(course)).not.toThrow();
});
it('should accept all category types', () => {
const categories = [
'security_awareness', 'data_protection', 'incident_response',
'access_control', 'phishing_awareness', 'compliance',
'secure_development', 'physical_security', 'business_continuity', 'other',
];
categories.forEach((category) => {
expect(() => TrainingCourseSchema.parse({
id: `COURSE-${category}`,
title: `${category} Training`,
description: `Training for ${category}`,
category,
durationMinutes: 30,
targetRoles: ['all'],
})).not.toThrow();
});
});
it('should reject invalid duration', () => {
expect(() => TrainingCourseSchema.parse({
id: 'COURSE-001',
title: 'Test',
description: 'Test',
category: 'other',
durationMinutes: 0,
targetRoles: ['all'],
})).toThrow();
});
it('should reject passing score out of range', () => {
expect(() => TrainingCourseSchema.parse({
id: 'COURSE-001',
title: 'Test',
description: 'Test',
category: 'other',
durationMinutes: 30,
targetRoles: ['all'],
passingScore: 101,
})).toThrow();
expect(() => TrainingCourseSchema.parse({
id: 'COURSE-001',
title: 'Test',
description: 'Test',
category: 'other',
durationMinutes: 30,
targetRoles: ['all'],
passingScore: -1,
})).toThrow();
});
it('should reject missing required fields', () => {
expect(() => TrainingCourseSchema.parse({})).toThrow();
expect(() => TrainingCourseSchema.parse({ id: 'COURSE-001' })).toThrow();
});
});
describe('TrainingRecordSchema', () => {
it('should accept valid completed training record', () => {
const record: TrainingRecord = {
courseId: 'COURSE-SEC-001',
userId: 'user_123',
status: 'completed',
assignedAt: 1704067200000,
completedAt: 1704153600000,
score: 95,
expiresAt: 1735689600000,
};
expect(() => TrainingRecordSchema.parse(record)).not.toThrow();
});
it('should accept minimal not-started record', () => {
const record = {
courseId: 'COURSE-SEC-002',
userId: 'user_456',
status: 'not_started',
assignedAt: Date.now(),
};
expect(() => TrainingRecordSchema.parse(record)).not.toThrow();
});
it('should accept failed record', () => {
const record = {
courseId: 'COURSE-SEC-003',
userId: 'user_789',
status: 'failed',
assignedAt: 1704067200000,
completedAt: 1704153600000,
score: 45,
notes: 'Did not meet passing score of 80%',
};
expect(() => TrainingRecordSchema.parse(record)).not.toThrow();
});
it('should reject score out of range', () => {
expect(() => TrainingRecordSchema.parse({
courseId: 'COURSE-001',
userId: 'user_123',
status: 'completed',
assignedAt: Date.now(),
score: 150,
})).toThrow();
});
it('should reject missing required fields', () => {
expect(() => TrainingRecordSchema.parse({})).toThrow();
expect(() => TrainingRecordSchema.parse({ courseId: 'COURSE-001' })).toThrow();
});
});
describe('TrainingPlanSchema', () => {
it('should accept plan with defaults', () => {
const plan = TrainingPlanSchema.parse({
courses: [
{
id: 'COURSE-SEC-001',
title: 'Security Awareness',
description: 'Annual security training',
category: 'security_awareness',
durationMinutes: 60,
targetRoles: ['all_employees'],
},
],
});
expect(plan.enabled).toBe(true);
expect(plan.recertificationIntervalDays).toBe(365);
expect(plan.trackCompletion).toBe(true);
expect(plan.gracePeriodDays).toBe(30);
expect(plan.sendReminders).toBe(true);
expect(plan.reminderDaysBefore).toBe(14);
});
it('should accept full plan configuration', () => {
const plan = TrainingPlanSchema.parse({
enabled: true,
courses: [
{
id: 'COURSE-SEC-001',
title: 'Security Fundamentals',
description: 'Core security training',
category: 'security_awareness',
durationMinutes: 60,
mandatory: true,
targetRoles: ['all_employees'],
validityDays: 365,
passingScore: 80,
},
{
id: 'COURSE-SEC-002',
title: 'Secure Development',
description: 'Secure coding practices',
category: 'secure_development',
durationMinutes: 120,
mandatory: true,
targetRoles: ['developers', 'devops'],
validityDays: 365,
passingScore: 85,
},
],
recertificationIntervalDays: 180,
trackCompletion: true,
gracePeriodDays: 14,
sendReminders: true,
reminderDaysBefore: 30,
});
expect(plan.courses).toHaveLength(2);
expect(plan.recertificationIntervalDays).toBe(180);
expect(plan.gracePeriodDays).toBe(14);
expect(plan.reminderDaysBefore).toBe(30);
});
it('should accept plan with empty courses', () => {
const plan = TrainingPlanSchema.parse({
courses: [],
});
expect(plan.courses).toHaveLength(0);
});
it('should reject missing courses', () => {
expect(() => TrainingPlanSchema.parse({})).toThrow();
});
});