|
| 1 | +import { applyDeterministicVariation } from '../../src/common/number'; |
| 2 | + |
| 3 | +describe('applyDeterministicVariation', () => { |
| 4 | + const baseValue = 1000; |
| 5 | + const maxVariationPercent = 7.6; |
| 6 | + |
| 7 | + it('should return deterministic results for the same seed', () => { |
| 8 | + const seed = 'test-opportunity-id-123'; |
| 9 | + |
| 10 | + const result1 = applyDeterministicVariation({ |
| 11 | + value: baseValue, |
| 12 | + seed, |
| 13 | + maxVariationPercent, |
| 14 | + }); |
| 15 | + const result2 = applyDeterministicVariation({ |
| 16 | + value: baseValue, |
| 17 | + seed, |
| 18 | + maxVariationPercent, |
| 19 | + }); |
| 20 | + |
| 21 | + expect(result1).toBe(result2); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return different results for different seeds', () => { |
| 25 | + const result1 = applyDeterministicVariation({ |
| 26 | + value: baseValue, |
| 27 | + seed: 'completely-different-seed-alpha', |
| 28 | + maxVariationPercent, |
| 29 | + }); |
| 30 | + const result2 = applyDeterministicVariation({ |
| 31 | + value: baseValue, |
| 32 | + seed: 'another-unique-identifier-beta', |
| 33 | + maxVariationPercent, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result1).not.toBe(result2); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return results within the expected range', () => { |
| 40 | + const seeds = [ |
| 41 | + 'abc', |
| 42 | + 'xyz', |
| 43 | + '123', |
| 44 | + 'test', |
| 45 | + 'opportunity-1', |
| 46 | + 'opportunity-2', |
| 47 | + 'some-uuid-here', |
| 48 | + 'another-id', |
| 49 | + 'short', |
| 50 | + 'a-very-long-seed-string-to-test-with', |
| 51 | + ]; |
| 52 | + |
| 53 | + const minExpected = Math.round(baseValue * (1 - maxVariationPercent / 100)); |
| 54 | + const maxExpected = Math.round(baseValue * (1 + maxVariationPercent / 100)); |
| 55 | + |
| 56 | + seeds.forEach((seed) => { |
| 57 | + const result = applyDeterministicVariation({ |
| 58 | + value: baseValue, |
| 59 | + seed, |
| 60 | + maxVariationPercent, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toBeGreaterThanOrEqual(minExpected); |
| 64 | + expect(result).toBeLessThanOrEqual(maxExpected); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should produce both positive and negative variations across different seeds', () => { |
| 69 | + // Use a large set of seeds to statistically ensure we get both positive and negative variations |
| 70 | + const seeds = Array.from({ length: 100 }, (_, i) => `seed-${i}`); |
| 71 | + |
| 72 | + const results = seeds.map((seed) => |
| 73 | + applyDeterministicVariation({ |
| 74 | + value: baseValue, |
| 75 | + seed, |
| 76 | + maxVariationPercent, |
| 77 | + }), |
| 78 | + ); |
| 79 | + |
| 80 | + const hasPositiveVariation = results.some((r) => r > baseValue); |
| 81 | + const hasNegativeVariation = results.some((r) => r < baseValue); |
| 82 | + |
| 83 | + expect(hasPositiveVariation).toBe(true); |
| 84 | + expect(hasNegativeVariation).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return an integer (rounded result)', () => { |
| 88 | + const result = applyDeterministicVariation({ |
| 89 | + value: 1000, |
| 90 | + seed: 'test-seed', |
| 91 | + maxVariationPercent: 7.6, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(Number.isInteger(result)).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should scale variation based on maxVariationPercent', () => { |
| 98 | + const seed = 'consistent-seed'; |
| 99 | + |
| 100 | + const result = applyDeterministicVariation({ |
| 101 | + value: baseValue, |
| 102 | + seed, |
| 103 | + maxVariationPercent: 15, |
| 104 | + }); |
| 105 | + |
| 106 | + // The deviation from base should be proportionally larger with higher maxVariationPercent |
| 107 | + const deviation = Math.abs(result - baseValue); |
| 108 | + |
| 109 | + expect(deviation).toBe(34); // Expected deviation for 15% max variation |
| 110 | + }); |
| 111 | + |
| 112 | + it('should handle zero value', () => { |
| 113 | + const result = applyDeterministicVariation({ |
| 114 | + value: 0, |
| 115 | + seed: 'any-seed', |
| 116 | + maxVariationPercent: 7.6, |
| 117 | + }); |
| 118 | + |
| 119 | + expect(result).toBe(0); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return original value when seed is empty', () => { |
| 123 | + const result = applyDeterministicVariation({ |
| 124 | + value: baseValue, |
| 125 | + seed: '', |
| 126 | + maxVariationPercent: 7.6, |
| 127 | + }); |
| 128 | + |
| 129 | + // Empty seed should return original value without variation |
| 130 | + expect(result).toBe(baseValue); |
| 131 | + }); |
| 132 | +}); |
0 commit comments