|
| 1 | +/** |
| 2 | + * Unit tests for LanceDBVectorStore |
| 3 | + * Focus on testing the distance-to-similarity conversion bug fix |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +describe('LanceDB Distance to Similarity Conversion', () => { |
| 9 | + describe('Score Calculation', () => { |
| 10 | + /** |
| 11 | + * This is the core bug fix being tested: |
| 12 | + * LanceDB returns L2 distance, we need to convert to similarity score (0-1) |
| 13 | + */ |
| 14 | + |
| 15 | + it('should convert L2 distance to valid similarity score', () => { |
| 16 | + // Simulate the conversion we use: score = e^(-distance²) |
| 17 | + const calculateScore = (distance: number): number => { |
| 18 | + return Math.exp(-(distance * distance)); |
| 19 | + }; |
| 20 | + |
| 21 | + // Test cases from the bug: |
| 22 | + // Before fix: score = 1 - distance = 1 - 1.0 = 0 ❌ |
| 23 | + // After fix: score = e^(-distance²) ✅ |
| 24 | + |
| 25 | + // Distance ~1.0 (similar vectors in normalized space) |
| 26 | + const distance1 = 0.9999990463256836; // Real value from our testing |
| 27 | + const score1 = calculateScore(distance1); |
| 28 | + |
| 29 | + expect(score1).toBeGreaterThan(0); // Should NOT be 0 (the bug!) |
| 30 | + expect(score1).toBeLessThan(1); |
| 31 | + expect(score1).toBeCloseTo(0.37, 1); // e^(-1²) ≈ 0.37 |
| 32 | + }); |
| 33 | + |
| 34 | + it('should give high scores for low distances', () => { |
| 35 | + const calculateScore = (distance: number): number => { |
| 36 | + return Math.exp(-(distance * distance)); |
| 37 | + }; |
| 38 | + |
| 39 | + // Very similar vectors (distance ≈ 0) |
| 40 | + const veryClose = calculateScore(0.1); |
| 41 | + expect(veryClose).toBeGreaterThan(0.99); // e^(-0.01) ≈ 0.99 |
| 42 | + |
| 43 | + // Moderately similar (distance ≈ 0.5) |
| 44 | + const moderate = calculateScore(0.5); |
| 45 | + expect(moderate).toBeGreaterThan(0.7); // e^(-0.25) ≈ 0.78 |
| 46 | + |
| 47 | + // Less similar (distance ≈ 1.0) |
| 48 | + const less = calculateScore(1.0); |
| 49 | + expect(less).toBeGreaterThan(0.3); // e^(-1) ≈ 0.37 |
| 50 | + expect(less).toBeLessThan(0.4); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should give low scores for high distances', () => { |
| 54 | + const calculateScore = (distance: number): number => { |
| 55 | + return Math.exp(-(distance * distance)); |
| 56 | + }; |
| 57 | + |
| 58 | + // Dissimilar vectors (distance ≈ 2.0) |
| 59 | + const dissimilar = calculateScore(2.0); |
| 60 | + expect(dissimilar).toBeLessThan(0.02); // e^(-4) ≈ 0.018 |
| 61 | + |
| 62 | + // Very dissimilar (distance ≈ 3.0) |
| 63 | + const veryDissimilar = calculateScore(3.0); |
| 64 | + expect(veryDissimilar).toBeLessThan(0.001); // e^(-9) ≈ 0.00012 |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return scores in valid range (0-1)', () => { |
| 68 | + const calculateScore = (distance: number): number => { |
| 69 | + return Math.exp(-(distance * distance)); |
| 70 | + }; |
| 71 | + |
| 72 | + // Test a range of distances |
| 73 | + const distances = [0, 0.1, 0.5, 1.0, 1.5, 2.0, 3.0, 5.0]; |
| 74 | + |
| 75 | + for (const distance of distances) { |
| 76 | + const score = calculateScore(distance); |
| 77 | + expect(score).toBeGreaterThanOrEqual(0); |
| 78 | + expect(score).toBeLessThanOrEqual(1); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('should be monotonically decreasing', () => { |
| 83 | + const calculateScore = (distance: number): number => { |
| 84 | + return Math.exp(-(distance * distance)); |
| 85 | + }; |
| 86 | + |
| 87 | + // Scores should decrease as distance increases |
| 88 | + const distances = [0, 0.5, 1.0, 1.5, 2.0]; |
| 89 | + const scores = distances.map(calculateScore); |
| 90 | + |
| 91 | + for (let i = 0; i < scores.length - 1; i++) { |
| 92 | + expect(scores[i]).toBeGreaterThan(scores[i + 1]); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle edge cases', () => { |
| 97 | + const calculateScore = (distance: number): number => { |
| 98 | + return Math.exp(-(distance * distance)); |
| 99 | + }; |
| 100 | + |
| 101 | + // Distance = 0 (identical vectors) |
| 102 | + expect(calculateScore(0)).toBe(1); |
| 103 | + |
| 104 | + // Very large distance |
| 105 | + const huge = calculateScore(100); |
| 106 | + expect(huge).toBeCloseTo(0, 10); |
| 107 | + |
| 108 | + // Undefined/infinity handling |
| 109 | + const inf = calculateScore(Number.POSITIVE_INFINITY); |
| 110 | + expect(inf).toBe(0); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('Threshold Filtering', () => { |
| 115 | + it('should filter out results below threshold', () => { |
| 116 | + const results = [ |
| 117 | + { distance: 0.5, id: 'a' }, // score ≈ 0.78 |
| 118 | + { distance: 1.0, id: 'b' }, // score ≈ 0.37 |
| 119 | + { distance: 1.5, id: 'c' }, // score ≈ 0.11 |
| 120 | + { distance: 2.0, id: 'd' }, // score ≈ 0.02 |
| 121 | + ]; |
| 122 | + |
| 123 | + const calculateScore = (distance: number): number => { |
| 124 | + return Math.exp(-(distance * distance)); |
| 125 | + }; |
| 126 | + |
| 127 | + const threshold = 0.3; |
| 128 | + const filtered = results |
| 129 | + .map((r) => ({ ...r, score: calculateScore(r.distance) })) |
| 130 | + .filter((r) => r.score >= threshold); |
| 131 | + |
| 132 | + // Should keep scores >= 0.3 (distances <= ~1.0) |
| 133 | + expect(filtered.length).toBe(2); |
| 134 | + expect(filtered[0].id).toBe('a'); |
| 135 | + expect(filtered[1].id).toBe('b'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should keep all results with threshold 0', () => { |
| 139 | + const results = [ |
| 140 | + { distance: 1.0, id: 'a' }, |
| 141 | + { distance: 2.0, id: 'b' }, |
| 142 | + { distance: 3.0, id: 'c' }, |
| 143 | + ]; |
| 144 | + |
| 145 | + const calculateScore = (distance: number): number => { |
| 146 | + return Math.exp(-(distance * distance)); |
| 147 | + }; |
| 148 | + |
| 149 | + const threshold = 0.0; |
| 150 | + const filtered = results |
| 151 | + .map((r) => ({ ...r, score: calculateScore(r.distance) })) |
| 152 | + .filter((r) => r.score >= threshold); |
| 153 | + |
| 154 | + expect(filtered.length).toBe(3); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should filter out low scores with high threshold', () => { |
| 158 | + const results = [ |
| 159 | + { distance: 0.5, id: 'a' }, // score ≈ 0.78 |
| 160 | + { distance: 1.0, id: 'b' }, // score ≈ 0.37 |
| 161 | + { distance: 1.5, id: 'c' }, // score ≈ 0.11 |
| 162 | + ]; |
| 163 | + |
| 164 | + const calculateScore = (distance: number): number => { |
| 165 | + return Math.exp(-(distance * distance)); |
| 166 | + }; |
| 167 | + |
| 168 | + const threshold = 0.7; |
| 169 | + const filtered = results |
| 170 | + .map((r) => ({ ...r, score: calculateScore(r.distance) })) |
| 171 | + .filter((r) => r.score >= threshold); |
| 172 | + |
| 173 | + // Only the first result should pass |
| 174 | + expect(filtered.length).toBe(1); |
| 175 | + expect(filtered[0].id).toBe('a'); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments