-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecedence-scoring.test.ts
More file actions
806 lines (675 loc) · 24.4 KB
/
Copy pathprecedence-scoring.test.ts
File metadata and controls
806 lines (675 loc) · 24.4 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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/**
* Precedence Scoring Test Suite
* Tests the exact 5-component formula, boost multipliers,
* normalization, clamping, and edge cases
*
* Test Count: 65+
*/
import { describe, it, expect, beforeEach } from '@jest/globals';
import { applyPrecedenceMatrix, scoreRules } from '../functions/precedence-scoring';
function createRule(overrides: any = {}) {
return {
id: `rule-${Math.random().toString(36).substr(2, 9)}`,
category: 'structure',
condition: {
precedenceWeight: 50,
...overrides.condition
},
...overrides
};
}
describe('Precedence Scoring', () => {
// ============================================================================
// BASE PRECEDENCE WEIGHT
// ============================================================================
describe('Base Precedence Weight Application', () => {
it('uses rule precedenceWeight as base score', () => {
const rule = createRule({
condition: { precedenceWeight: 80 }
});
const context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
expect(score).toBeLessThanOrEqual(100);
});
it('applies default precedenceWeight = 50 when not specified', () => {
const rule = createRule({
condition: { precedenceWeight: undefined }
});
const context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
expect(score).toBeLessThanOrEqual(100);
});
it('higher base weights produce higher scores', () => {
const lowWeightRule = createRule({
id: 'low',
condition: { precedenceWeight: 30 }
});
const highWeightRule = createRule({
id: 'high',
condition: { precedenceWeight: 90 }
});
const context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const lowScore = applyPrecedenceMatrix(lowWeightRule, context);
const highScore = applyPrecedenceMatrix(highWeightRule, context);
expect(highScore).toBeGreaterThan(lowScore);
});
it('handles base precedenceWeight boundary values (0, 100)', () => {
// Weight = 0
let rule = createRule({ condition: { precedenceWeight: 0 } });
let context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
let score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
// Weight = 100
rule = createRule({ condition: { precedenceWeight: 100 } });
score = applyPrecedenceMatrix(rule, context);
expect(score).toBeLessThanOrEqual(100);
});
});
// ============================================================================
// WEIGHT NORMALIZATION (0-100 → 0.0-1.0)
// ============================================================================
describe('Weight Normalization: 0-100 to 0.0-1.0', () => {
it('normalizes SECURITY_WEIGHT from 0-100 to 0.0-1.0', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
// SECURITY_WEIGHT: 60 → 0.60
let context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 0
};
let score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
// SECURITY_WEIGHT: 100 → 1.0
context = {
SECURITY_WEIGHT: 100,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 0
};
score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
});
it('normalizes COMPLIANCE_WEIGHT from 0-100 to 0.0-1.0', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
// COMPLIANCE_WEIGHT: 50 → 0.50
let context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 0
};
let score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
// COMPLIANCE_WEIGHT: 100 → 1.0
context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 100,
THREAT_WEIGHT: 0
};
score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
});
it('normalizes THREAT_WEIGHT from 0-100 to 0.0-1.0', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
// THREAT_WEIGHT: 40 → 0.40
let context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 40
};
let score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
// THREAT_WEIGHT: 100 → 1.0
context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 100
};
score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
});
});
// ============================================================================
// FORMULA APPLICATION (5-COMPONENT)
// ============================================================================
describe('Formula Application: (SEC×0.35 + COMP×0.25 + THREAT×0.20) × BASE', () => {
it('applies exact formula with all weights at baseline', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {
SECURITY_WEIGHT: 100, // 1.0
COMPLIANCE_WEIGHT: 100, // 1.0
THREAT_WEIGHT: 100 // 1.0
};
const score = applyPrecedenceMatrix(rule, context);
// Expected: 100 × (1.0×0.35 + 1.0×0.25 + 1.0×0.20)
// = 100 × (0.35 + 0.25 + 0.20)
// = 100 × 0.80
// = 80 (before clamping)
expect(score).toBeCloseTo(80, 1);
});
it('applies exact formula with mixed weights', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {
SECURITY_WEIGHT: 100, // 1.0
COMPLIANCE_WEIGHT: 50, // 0.5
THREAT_WEIGHT: 80 // 0.8
};
const score = applyPrecedenceMatrix(rule, context);
// Expected: 100 × (1.0×0.35 + 0.5×0.25 + 0.8×0.20)
// = 100 × (0.35 + 0.125 + 0.16)
// = 100 × 0.635
// = 63.5
expect(score).toBeCloseTo(63.5, 0.5);
});
it('applies exact formula with low weights', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {
SECURITY_WEIGHT: 20, // 0.2
COMPLIANCE_WEIGHT: 10, // 0.1
THREAT_WEIGHT: 30 // 0.3
};
const score = applyPrecedenceMatrix(rule, context);
// Expected: 100 × (0.2×0.35 + 0.1×0.25 + 0.3×0.20)
// = 100 × (0.07 + 0.025 + 0.06)
// = 100 × 0.155
// = 15.5
expect(score).toBeCloseTo(15.5, 0.5);
});
it('applies exact formula with zero weights', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 0
};
const score = applyPrecedenceMatrix(rule, context);
// Expected: 100 × (0×0.35 + 0×0.25 + 0×0.20) = 0
expect(score).toBeLessThan(1); // Close to 0
});
it('uses default weights when not provided', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {}; // No weights specified
const score = applyPrecedenceMatrix(rule, context);
// Defaults: SECURITY=60, COMPLIANCE=50, THREAT=40
// Expected: 100 × (0.6×0.35 + 0.5×0.25 + 0.4×0.20)
// = 100 × (0.21 + 0.125 + 0.08)
// = 100 × 0.415
// = 41.5
expect(score).toBeCloseTo(41.5, 0.5);
});
});
// ============================================================================
// BOOST MULTIPLIERS: Compliance (1.5×) and Threat (1.3×)
// ============================================================================
describe('Compliance Boost: 1.5× when COMPLIANCE_FRAMEWORK is set', () => {
it('applies 1.5× boost to compliance rules with framework set', () => {
const rule = createRule({
id: 'compliance-rule',
category: 'compliance',
condition: { precedenceWeight: 80 }
});
// Without framework
let context = {
COMPLIANCE_FRAMEWORK: ['none'],
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const scoreWithout = applyPrecedenceMatrix(rule, context);
// With framework
context = {
COMPLIANCE_FRAMEWORK: ['SOC2'],
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const scoreWith = applyPrecedenceMatrix(rule, context);
expect(scoreWith).toBeGreaterThan(scoreWithout);
expect(scoreWith / scoreWithout).toBeCloseTo(1.5, 0.1);
});
it('applies 1.5× boost to multiple frameworks', () => {
const rule = createRule({
id: 'compliance-rule',
category: 'compliance',
condition: { precedenceWeight: 80 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2', 'ISO27001', 'GDPR'],
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(45); // Should be significantly boosted
});
it('does NOT apply boost to non-compliance rules even with framework', () => {
const rule = createRule({
id: 'structure-rule',
category: 'structure', // Not compliance
condition: { precedenceWeight: 80 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2'],
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// Structure rule should NOT get compliance boost
expect(score).toBeLessThan(80);
});
it('does NOT apply boost when COMPLIANCE_FRAMEWORK = ["none"]', () => {
const rule = createRule({
id: 'compliance-rule',
category: 'compliance',
condition: { precedenceWeight: 80 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['none'],
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// No boost for "none" framework
expect(score).toBeLessThan(80);
});
});
describe('Threat Boost: 1.3× when THREAT_LEVEL = critical', () => {
it('applies 1.3× boost to security rules when THREAT_LEVEL = critical', () => {
const rule = createRule({
id: 'security-rule',
category: 'security',
condition: { precedenceWeight: 70 }
});
// Without critical threat
let context = {
THREAT_LEVEL: 'low',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const scoreWithout = applyPrecedenceMatrix(rule, context);
// With critical threat
context = {
THREAT_LEVEL: 'critical',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const scoreWith = applyPrecedenceMatrix(rule, context);
expect(scoreWith).toBeGreaterThan(scoreWithout);
expect(scoreWith / scoreWithout).toBeCloseTo(1.3, 0.1);
});
it('does NOT apply boost to security rules at non-critical threat levels', () => {
const rule = createRule({
id: 'security-rule',
category: 'security',
condition: { precedenceWeight: 70 }
});
const levels = ['none', 'low', 'medium', 'high'];
for (const level of levels) {
const context = {
THREAT_LEVEL: level,
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// No 1.3× boost for non-critical
expect(score).toBeLessThan(100);
}
});
it('does NOT apply threat boost to non-security rules even at critical', () => {
const rule = createRule({
id: 'structure-rule',
category: 'structure', // Not security
condition: { precedenceWeight: 70 }
});
const context = {
THREAT_LEVEL: 'critical',
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// Structure rule should NOT get threat boost
expect(score).toBeLessThan(100);
});
});
// ============================================================================
// COMBINED BOOSTS
// ============================================================================
describe('Combined Boosts: Compliance + Threat on Single Rule', () => {
it('applies both boosts if rule is compliance AND threat is critical', () => {
const rule = createRule({
id: 'critical-compliance-rule',
category: 'compliance', // Gets compliance boost
condition: { precedenceWeight: 80 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2'], // Triggers 1.5× compliance boost
THREAT_LEVEL: 'critical', // Would trigger 1.3× IF rule was security
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// Only compliance boost applies (not threat, since rule is not security)
expect(score).toBeGreaterThan(45);
});
it('applies threat boost correctly for security rules', () => {
const rule = createRule({
id: 'critical-security-rule',
category: 'security', // Gets threat boost
condition: { precedenceWeight: 70 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2'], // Would NOT trigger for security rules
THREAT_LEVEL: 'critical', // Triggers 1.3× threat boost
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
// Threat boost applies (not compliance, since rule is not compliance)
expect(score).toBeGreaterThan(35);
});
});
// ============================================================================
// CLAMPING: [0, 100] Boundary Enforcement
// ============================================================================
describe('Clamping: Score Must Stay in [0, 100]', () => {
it('clamps excessive scores to 100', () => {
const rule = createRule({
id: 'excessive-weight',
category: 'compliance',
condition: { precedenceWeight: 200 } // Intentionally high
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2'], // 1.5× boost
SECURITY_WEIGHT: 100,
COMPLIANCE_WEIGHT: 100,
THREAT_WEIGHT: 100
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeLessThanOrEqual(100);
expect(score).toBeGreaterThanOrEqual(0);
});
it('clamps negative scores to 0', () => {
const rule = createRule({
condition: { precedenceWeight: -50 } // Intentionally negative
});
const context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 0
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
});
it('returns valid score (0-100) for all zero weights', () => {
const rule = createRule({ condition: { precedenceWeight: 50 } });
const context = {
SECURITY_WEIGHT: 0,
COMPLIANCE_WEIGHT: 0,
THREAT_WEIGHT: 0
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
expect(score).toBeLessThanOrEqual(100);
});
it('returns valid score (0-100) for all max weights', () => {
const rule = createRule({ condition: { precedenceWeight: 100 } });
const context = {
SECURITY_WEIGHT: 100,
COMPLIANCE_WEIGHT: 100,
THREAT_WEIGHT: 100
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
expect(score).toBeLessThanOrEqual(100);
});
it('maintains ordering across different boosts (no clamping artifacts)', () => {
const rule1 = createRule({
id: 'r1',
category: 'compliance',
condition: { precedenceWeight: 75 }
});
const rule2 = createRule({
id: 'r2',
category: 'structure',
condition: { precedenceWeight: 90 }
});
const context = {
COMPLIANCE_FRAMEWORK: ['SOC2'],
SECURITY_WEIGHT: 80,
COMPLIANCE_WEIGHT: 70,
THREAT_WEIGHT: 60
};
const score1 = applyPrecedenceMatrix(rule1, context);
const score2 = applyPrecedenceMatrix(rule2, context);
// Compliance boost should make rule1 higher
expect(score1).toBeGreaterThan(score2);
expect(score1).toBeLessThanOrEqual(100);
expect(score2).toBeLessThanOrEqual(100);
});
});
// ============================================================================
// SCORE INTERPRETATION BANDS
// ============================================================================
describe('Score Interpretation Bands', () => {
it('scores 90-100 indicate critical importance', () => {
const rule = createRule({
id: 'critical',
condition: { precedenceWeight: 95 }
});
const context = {
SECURITY_WEIGHT: 90,
COMPLIANCE_WEIGHT: 85,
THREAT_WEIGHT: 80
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(60);
});
it('scores 75-89 indicate high importance', () => {
const rule = createRule({
id: 'high',
condition: { precedenceWeight: 80 }
});
const context = {
SECURITY_WEIGHT: 70,
COMPLIANCE_WEIGHT: 65,
THREAT_WEIGHT: 60
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(40);
expect(score).toBeLessThanOrEqual(100);
});
it('scores 50-74 indicate medium importance', () => {
const rule = createRule({
id: 'medium',
condition: { precedenceWeight: 60 }
});
const context = {
SECURITY_WEIGHT: 50,
COMPLIANCE_WEIGHT: 40,
THREAT_WEIGHT: 30
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
expect(score).toBeLessThanOrEqual(100);
});
});
// ============================================================================
// EDGE CASES AND BOUNDARY CONDITIONS
// ============================================================================
describe('Edge Cases: Boundary Conditions', () => {
it('handles fractional weights correctly', () => {
const rule = createRule({ condition: { precedenceWeight: 75 } });
const context = {
SECURITY_WEIGHT: 66.5,
COMPLIANCE_WEIGHT: 33.3,
THREAT_WEIGHT: 25.7
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThanOrEqual(0);
expect(score).toBeLessThanOrEqual(100);
expect(typeof score).toBe('number');
});
it('handles rule with no condition.precedenceWeight (uses default)', () => {
const rule = createRule({
condition: {} // No precedenceWeight
});
const context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const score = applyPrecedenceMatrix(rule, context);
expect(score).toBeGreaterThan(0);
expect(score).toBeLessThanOrEqual(100);
});
it('returns finite number (no NaN or Infinity)', () => {
const rule = createRule({ condition: { precedenceWeight: 50 } });
const context = {
SECURITY_WEIGHT: 75,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 25
};
const score = applyPrecedenceMatrix(rule, context);
expect(Number.isFinite(score)).toBe(true);
expect(Number.isNaN(score)).toBe(false);
});
});
// ============================================================================
// COMPARATIVE SCORING: Determinism and Consistency
// ============================================================================
describe('Comparative Scoring: Determinism', () => {
it('produces consistent scores for identical inputs', () => {
const rule = createRule({
id: 'test-rule',
condition: { precedenceWeight: 75 }
});
const context = {
SECURITY_WEIGHT: 70,
COMPLIANCE_WEIGHT: 60,
THREAT_WEIGHT: 50
};
const score1 = applyPrecedenceMatrix(rule, context);
const score2 = applyPrecedenceMatrix(rule, context);
expect(score1).toBe(score2);
});
it('produces different scores for different weights', () => {
const rule = createRule({
id: 'test-rule',
condition: { precedenceWeight: 75 }
});
const context1 = {
SECURITY_WEIGHT: 50,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 50
};
const context2 = {
SECURITY_WEIGHT: 80,
COMPLIANCE_WEIGHT: 80,
THREAT_WEIGHT: 80
};
const score1 = applyPrecedenceMatrix(rule, context1);
const score2 = applyPrecedenceMatrix(rule, context2);
expect(score1).not.toBe(score2);
expect(score2).toBeGreaterThan(score1);
});
it('maintains score ordering across multiple rules', () => {
const rules = [
createRule({
id: 'rule1',
condition: { precedenceWeight: 50 }
}),
createRule({
id: 'rule2',
condition: { precedenceWeight: 75 }
}),
createRule({
id: 'rule3',
condition: { precedenceWeight: 90 }
})
];
const context = {
SECURITY_WEIGHT: 60,
COMPLIANCE_WEIGHT: 50,
THREAT_WEIGHT: 40
};
const scores = rules.map(r => applyPrecedenceMatrix(r, context));
expect(scores[1]).toBeGreaterThan(scores[0]);
expect(scores[2]).toBeGreaterThan(scores[1]);
});
});
});
// ============================================================================
// scoreRules — batch scoring via applyPrecedenceMatrix
// ============================================================================
describe('scoreRules (batch scoring)', () => {
const context = {
SECURITY_WEIGHT: 70,
COMPLIANCE_WEIGHT: 60,
THREAT_WEIGHT: 50
};
it('returns a ScoredRule for each input rule', () => {
const rules = [
createRule({ id: 'r1', condition: { precedenceWeight: 50 } }),
createRule({ id: 'r2', condition: { precedenceWeight: 80 } })
];
const scored = scoreRules(rules, context);
expect(scored).toHaveLength(2);
});
it('each scored rule has a numeric score property', () => {
const rules = [createRule({ id: 'r1', condition: { precedenceWeight: 60 } })];
const scored = scoreRules(rules, context);
expect(typeof scored[0].score).toBe('number');
expect(Number.isFinite(scored[0].score)).toBe(true);
});
it('preserves all original rule fields alongside score', () => {
const rule = createRule({ id: 'preserve-me', category: 'security', condition: { precedenceWeight: 75 } });
const scored = scoreRules([rule], context);
expect(scored[0].id).toBe('preserve-me');
expect(scored[0].category).toBe('security');
});
it('score matches applyPrecedenceMatrix for each rule individually', () => {
const rules = [
createRule({ id: 'r1', condition: { precedenceWeight: 50 } }),
createRule({ id: 'r2', condition: { precedenceWeight: 90 } })
];
const scored = scoreRules(rules, context);
rules.forEach((rule, i) => {
expect(scored[i].score).toBe(applyPrecedenceMatrix(rule, context));
});
});
it('returns empty array for empty input', () => {
expect(scoreRules([], context)).toEqual([]);
});
it('higher-weight rules receive higher scores', () => {
const rules = [
createRule({ id: 'low', condition: { precedenceWeight: 30 } }),
createRule({ id: 'high', condition: { precedenceWeight: 90 } })
];
const scored = scoreRules(rules, context);
expect(scored[1].score).toBeGreaterThan(scored[0].score);
});
});