-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-intermediate-conditional.test.ts
More file actions
666 lines (592 loc) · 21.8 KB
/
Copy pathe2e-intermediate-conditional.test.ts
File metadata and controls
666 lines (592 loc) · 21.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**
* E2E Tests: Intermediate Conditional Path
* Tests the intermediate user flow: Tier 1 → conditional Tier 2 unlock → Tier 3 defaults → moderate detail
* Coverage: All Tier 2 unlock conditions tested, context variables mapped correctly = 15 tests
*/
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import fs from 'fs';
import path from 'path';
import os from 'os';
import {
loadAllRules,
filterRulesByContext,
scoreRules
} from '../recommendation-engine';
import { resolveAllConflicts } from '../conflict-resolver';
import {
AuditRule,
PrecedenceContext,
UserAnswers,
Tier1Answers,
Tier2Answers
} from '../types/audit';
describe('E2E: Intermediate Conditional Path', () => {
let tempDir: string;
let mockRules: AuditRule[];
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'audit-e2e-'));
mockRules = [
{
id: 'rule-compliance-soc2-detail',
description: 'SOC2 compliance detailed requirements',
category: 'compliance',
condition: {
contextVars: { COMPLIANCE_FRAMEWORK: 'SOC2' },
precedenceWeight: 90
},
action: {
type: 'audit',
recommendation: 'Implement SOC2 Type II audit trail',
enforcementLevel: 'hard-mandatory'
},
rationale: 'SOC2 requires detailed compliance controls'
},
{
id: 'rule-codeowners-multi-team',
description: 'CODEOWNERS for multi-team governance',
category: 'structure',
condition: {
contextVars: { TEAM_SCALE: ['small', 'multi-team', 'enterprise'] },
precedenceWeight: 75
},
action: {
type: 'scaffold',
recommendation: 'Create and maintain CODEOWNERS file',
enforcementLevel: 'soft-mandatory'
},
rationale: 'Multi-team projects need explicit code ownership'
},
{
id: 'rule-agentic-detail',
description: 'Agentic system detailed patterns',
category: 'structure',
condition: {
contextVars: { AI_PATTERN: 'agentic' },
precedenceWeight: 70
},
action: {
type: 'scaffold',
recommendation: 'Set up agent orchestration with memory management',
enforcementLevel: 'soft-mandatory'
},
rationale: 'Agentic systems require sophisticated memory/tool management'
},
{
id: 'rule-ci-production-full',
description: 'Full CI/CD for production',
category: 'tooling',
condition: {
contextVars: { PROFILE_STAGE: 'production', CI_MATURITY: 'full' },
precedenceWeight: 85
},
action: {
type: 'scaffold',
recommendation: 'Implement full CI/CD with approval gates',
enforcementLevel: 'hard-mandatory'
},
rationale: 'Production systems need gated deployments'
},
{
id: 'rule-observability-metrics',
description: 'Production observability with metrics',
category: 'tooling',
condition: {
contextVars: { PROFILE_STAGE: 'production', OBSERVABILITY_LEVEL: ['metrics', 'metrics+alerts'] },
precedenceWeight: 80
},
action: {
type: 'scaffold',
recommendation: 'Set up Prometheus metrics and alerting',
enforcementLevel: 'soft-mandatory'
},
rationale: 'Production systems need monitoring and alerts'
},
{
id: 'rule-gdpr-data-handling',
description: 'GDPR data handling procedures',
category: 'compliance',
condition: {
contextVars: { COMPLIANCE_FRAMEWORK: 'GDPR' },
precedenceWeight: 88
},
action: {
type: 'audit',
recommendation: 'Implement GDPR data processing agreement',
enforcementLevel: 'hard-mandatory'
},
rationale: 'GDPR compliance requires explicit data handling'
},
{
id: 'rule-rag-advanced',
description: 'Advanced RAG system patterns',
category: 'structure',
condition: {
contextVars: { AI_PATTERN: 'RAG' },
precedenceWeight: 65
},
action: {
type: 'scaffold',
recommendation: 'Set up vector store with advanced retrieval',
enforcementLevel: 'soft-mandatory'
},
rationale: 'RAG systems need sophisticated retrieval patterns'
},
{
id: 'rule-iso27001-security',
description: 'ISO27001 security controls',
category: 'compliance',
condition: {
contextVars: { COMPLIANCE_FRAMEWORK: 'ISO27001' },
precedenceWeight: 87
},
action: {
type: 'hardening',
recommendation: 'Implement ISO27001 security controls',
enforcementLevel: 'hard-mandatory'
},
rationale: 'ISO27001 requires systematic security management'
},
{
id: 'rule-solo-minimal',
description: 'Solo developer minimal setup',
category: 'structure',
condition: {
contextVars: { TEAM_SCALE: 'solo' },
precedenceWeight: 20
},
action: {
type: 'scaffold',
recommendation: 'Basic project structure only',
enforcementLevel: 'advisory'
},
rationale: 'Solo projects can skip team governance'
}
];
});
afterEach(() => {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
// Test 1: T2_UNLOCK_COMPLIANCE triggered by non-none compliance
it('intermediate-t2-unlock-compliance: compliance != none triggers T2-Q1', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'small',
ai_involvement: 'none',
compliance: 'SOC2' // Triggers unlock
};
// Intermediate user sees T2-Q1 unlocked
const tier2: Tier2Answers = {
complianceDetails: 'SOC2' // User answers T2-Q1
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 75,
COMPLIANCE_WEIGHT: 85,
THREAT_WEIGHT: 60
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-compliance-soc2-detail');
// Verify hard-mandatory enforcement
const soc2Rule = filtered.find(r => r.id === 'rule-compliance-soc2-detail');
expect(soc2Rule?.action.enforcementLevel).toBe('hard-mandatory');
});
// Test 2: T2_UNLOCK_COMPLIANCE NOT triggered by 'none'
it('intermediate-t2-no-unlock-compliance: compliance = none does NOT trigger T2-Q1', () => {
const tier1: Tier1Answers = {
stage: 'MVP',
team_scope: 'solo',
ai_involvement: 'none',
compliance: 'none' // Does NOT trigger unlock
};
// Tier 2 should not have compliance question
// No T2-Q1 presented to user
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'low',
SECURITY_WEIGHT: 40,
COMPLIANCE_WEIGHT: 20,
THREAT_WEIGHT: 30
};
const filtered = filterRulesByContext(mockRules, context);
// Compliance rules should not apply
expect(
filtered.filter(r => r.category === 'compliance').some(r =>
r.condition.contextVars.COMPLIANCE_FRAMEWORK !== undefined &&
r.condition.contextVars.COMPLIANCE_FRAMEWORK !== 'none'
)
).toBe(false);
});
// Test 3: T2_UNLOCK_TEAM_SCALE triggered by small team
it('intermediate-t2-unlock-team-scale-small: team = small triggers governance T2-Q2', () => {
const tier1: Tier1Answers = {
stage: 'beta',
team_scope: 'small', // Triggers unlock
ai_involvement: 'none',
compliance: 'none'
};
const tier2: Tier2Answers = {
teamSize: 'yes' // User answers T2-Q2: needs CODEOWNERS
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-codeowners-multi-team');
});
// Test 4: T2_UNLOCK_TEAM_SCALE NOT triggered by solo
it('intermediate-t2-no-unlock-team-scale-solo: team = solo does NOT trigger governance', () => {
const tier1: Tier1Answers = {
stage: 'beta',
team_scope: 'solo', // Does NOT trigger unlock
ai_involvement: 'none',
compliance: 'none'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 50,
COMPLIANCE_WEIGHT: 30,
THREAT_WEIGHT: 40
};
const filtered = filterRulesByContext(mockRules, context);
// CODEOWNERS rule should not apply to solo
expect(filtered.map(r => r.id)).not.toContain('rule-codeowners-multi-team');
});
// Test 5: T2_UNLOCK_AI_PATTERN triggered by agentic
it('intermediate-t2-unlock-ai-pattern-agentic: ai_pattern = agentic triggers T2-Q3', () => {
const tier1: Tier1Answers = {
stage: 'beta',
team_scope: 'small',
ai_involvement: 'agentic', // Triggers unlock
compliance: 'none'
};
const tier2: Tier2Answers = {
aiPatternDetail: 'agentic complex' // User refines AI pattern
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-agentic-detail');
});
// Test 6: T2_UNLOCK_AI_PATTERN NOT triggered by 'none'
it('intermediate-t2-no-unlock-ai-pattern-none: ai_pattern = none does NOT trigger T2-Q3', () => {
const tier1: Tier1Answers = {
stage: 'beta',
team_scope: 'small',
ai_involvement: 'none', // Does NOT trigger unlock
compliance: 'none'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const filtered = filterRulesByContext(mockRules, context);
// AI-specific rules should not apply when AI_PATTERN = none
expect(
filtered.filter(r =>
['rule-agentic-detail', 'rule-rag-advanced'].includes(r.id)
).length
).toBe(0);
});
// Test 7: T2_UNLOCK_PRODUCTION_OBSERVABILITY triggered by production
it('intermediate-t2-unlock-production: stage = production triggers T2-Q4 and T2-Q5', () => {
const tier1: Tier1Answers = {
stage: 'production', // Triggers unlock
team_scope: 'small',
ai_involvement: 'none',
compliance: 'none'
};
const tier2: Tier2Answers = {
ciMaturity: 'full',
observabilityLevel: 'metrics' // User answers T2-Q4 and T2-Q5
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 80,
COMPLIANCE_WEIGHT: 60,
THREAT_WEIGHT: 70,
CI_MATURITY: tier2.ciMaturity as any,
OBSERVABILITY_LEVEL: tier2.observabilityLevel as any
};
const filtered = filterRulesByContext(mockRules, context);
// Both CI and observability rules should apply
expect(filtered.map(r => r.id)).toContain('rule-ci-production-full');
expect(filtered.map(r => r.id)).toContain('rule-observability-metrics');
});
// Test 8: T2_UNLOCK_PRODUCTION NOT triggered by PoC
it('intermediate-t2-no-unlock-production: stage = PoC does NOT trigger T2-Q4/Q5', () => {
const tier1: Tier1Answers = {
stage: 'PoC', // Does NOT trigger unlock
team_scope: 'small',
ai_involvement: 'none',
compliance: 'none'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 50,
COMPLIANCE_WEIGHT: 30,
THREAT_WEIGHT: 40
// No CI_MATURITY or OBSERVABILITY_LEVEL (defaults applied silently)
};
const filtered = filterRulesByContext(mockRules, context);
// Production-specific rules should not apply
expect(filtered.map(r => r.id)).not.toContain('rule-ci-production-full');
});
// Test 9: Multi-condition filtering in intermediate path
it('intermediate-multi-condition: GDPR + production applies both compliance and ci rules', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'multi-team',
ai_involvement: 'none',
compliance: 'GDPR'
};
const tier2: Tier2Answers = {
complianceDetails: 'GDPR',
ciMaturity: 'full',
observabilityLevel: 'metrics'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 85,
COMPLIANCE_WEIGHT: 90,
THREAT_WEIGHT: 75,
CI_MATURITY: tier2.ciMaturity as any,
OBSERVABILITY_LEVEL: tier2.observabilityLevel as any
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-gdpr-data-handling');
expect(filtered.map(r => r.id)).toContain('rule-codeowners-multi-team');
expect(filtered.map(r => r.id)).toContain('rule-ci-production-full');
});
// Test 10: Context variables correctly map from Tier 1 + Tier 2
it('intermediate-context-mapping-tier1-tier2: Tier 2 overrides/refines Tier 1', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'multi-team',
ai_involvement: 'RAG',
compliance: 'SOC2'
};
const tier2: Tier2Answers = {
complianceDetails: 'ISO27001', // User refines compliance choice
aiPatternDetail: 'RAG advanced',
ciMaturity: 'full',
observabilityLevel: 'APM'
};
// Context should include Tier 2 refinements
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 85,
COMPLIANCE_WEIGHT: 90,
THREAT_WEIGHT: 75,
CI_MATURITY: tier2.ciMaturity as any,
OBSERVABILITY_LEVEL: tier2.observabilityLevel as any
};
// Tier 2 override should be applied
expect(context.COMPLIANCE_FRAMEWORK).toContain('ISO27001');
expect(context.CI_MATURITY).toBe('full');
expect(context.OBSERVABILITY_LEVEL).toBe('APM');
});
// Test 11: ISO27001 compliance unlocks governance and security
it('intermediate-iso27001-comprehensive: ISO27001 affects multiple rule categories', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'small',
ai_involvement: 'none',
compliance: 'ISO27001'
};
const tier2: Tier2Answers = {
complianceDetails: 'ISO27001'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 85,
COMPLIANCE_WEIGHT: 90,
THREAT_WEIGHT: 70
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-iso27001-security');
// ISO27001 is hard-mandatory
const iso27001Rule = filtered.find(r => r.id === 'rule-iso27001-security');
expect(iso27001Rule?.action.enforcementLevel).toBe('hard-mandatory');
});
// Test 12: Tier 3 defaults apply invisibly in intermediate path
it('intermediate-tier3-defaults-invisible: Tier 3 defaults applied without user seeing Tier 3', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'small',
ai_involvement: 'none',
compliance: 'none'
};
const tier2: Tier2Answers = {
ciMaturity: 'full',
observabilityLevel: 'metrics'
};
// Tier 3 not shown, but defaults applied
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium', // Tier 3 default
SECURITY_WEIGHT: 60, // Tier 3 default
COMPLIANCE_WEIGHT: 50, // Tier 3 default
THREAT_WEIGHT: 40, // Tier 3 default
CI_MATURITY: tier2.ciMaturity as any,
OBSERVABILITY_LEVEL: tier2.observabilityLevel as any,
TEST_MATURITY: 'unit', // Tier 3 default
DOC_EXPECTATION: 'minimal-informal', // Tier 3 default
REUSE_INTENT: 'project-scoped' // Tier 3 default
};
// Verify defaults populated
expect(context.THREAT_LEVEL).toBe('medium');
expect(context.SECURITY_WEIGHT).toBe(60);
expect(context.TEST_MATURITY).toBe('unit');
});
// Test 13: RAG unlock conditions and rules
it('intermediate-rag-unlock: RAG ai_pattern triggers detailed rules', () => {
const tier1: Tier1Answers = {
stage: 'beta',
team_scope: 'small',
ai_involvement: 'RAG',
compliance: 'none'
};
const tier2: Tier2Answers = {
aiPatternDetail: 'RAG advanced'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier1.compliance],
THREAT_LEVEL: 'medium',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const filtered = filterRulesByContext(mockRules, context);
expect(filtered.map(r => r.id)).toContain('rule-rag-advanced');
});
// Test 14: All three Tier 2 unlocks triggered simultaneously
it('intermediate-all-tier2-unlocks: production SOC2 multi-team agentic triggers all T2 questions', () => {
const tier1: Tier1Answers = {
stage: 'production', // Triggers T2_UNLOCK_PRODUCTION_OBSERVABILITY
team_scope: 'multi-team', // Triggers T2_UNLOCK_TEAM_SCALE
ai_involvement: 'agentic', // Triggers T2_UNLOCK_AI_PATTERN
compliance: 'SOC2' // Triggers T2_UNLOCK_COMPLIANCE
};
const tier2: Tier2Answers = {
complianceDetails: 'SOC2',
teamSize: 'yes',
ciMaturity: 'full',
observabilityLevel: 'APM',
aiPatternDetail: 'agentic complex'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'critical',
SECURITY_WEIGHT: 95,
COMPLIANCE_WEIGHT: 95,
THREAT_WEIGHT: 90,
CI_MATURITY: tier2.ciMaturity as any,
OBSERVABILITY_LEVEL: tier2.observabilityLevel as any
};
const filtered = filterRulesByContext(mockRules, context);
// All applicable rule categories should be present
const categories = new Set(filtered.map(r => r.category));
expect(categories.has('compliance')).toBe(true);
expect(categories.has('structure')).toBe(true);
expect(categories.has('tooling')).toBe(true);
// Verify hard-mandatory rules exist
expect(filtered.some(r => r.action.enforcementLevel === 'hard-mandatory')).toBe(true);
});
// Test 15: Precedence scoring with Tier 2 context
it('intermediate-precedence-scoring: Tier 2 answers affect rule scoring', () => {
const tier1: Tier1Answers = {
stage: 'production',
team_scope: 'small',
ai_involvement: 'none',
compliance: 'SOC2'
};
const tier2: Tier2Answers = {
complianceDetails: 'SOC2',
ciMaturity: 'full'
};
const context: PrecedenceContext = {
PROFILE_STAGE: tier1.stage as any,
TEAM_SCALE: tier1.team_scope as any,
AI_PATTERN: tier1.ai_involvement as any,
COMPLIANCE_FRAMEWORK: [tier2.complianceDetails || tier1.compliance],
THREAT_LEVEL: 'high',
SECURITY_WEIGHT: 85,
COMPLIANCE_WEIGHT: 90,
THREAT_WEIGHT: 75,
CI_MATURITY: tier2.ciMaturity as any
};
const filtered = filterRulesByContext(mockRules, context);
const scored = scoreRules(filtered, context);
// SOC2 rule should score highest (compliance framework set + hard-mandatory)
const soc2Rule = scored.find(r => r.id === 'rule-compliance-soc2-detail');
const ciRule = scored.find(r => r.id === 'rule-ci-production-full');
expect(soc2Rule?.score).toBeDefined();
expect(ciRule?.score).toBeDefined();
if (soc2Rule && ciRule) {
expect(soc2Rule.score).toBeGreaterThanOrEqual(ciRule.score);
}
});
});