|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { computeSegments, bridgeSmallGaps } from "./sparse-data-utils"; |
| 3 | + |
| 4 | +const identity = (d: number | null) => d; |
| 5 | +const cloneWith = (_d: number | null, v: number): number | null => v; |
| 6 | +// 1:1 pixel mapping so gap width = index difference |
| 7 | +const xPixel = (i: number) => i; |
| 8 | + |
| 9 | +describe("computeSegments", () => { |
| 10 | + it("finds contiguous non-null segments", () => { |
| 11 | + const data = [1, null, null, 2, 3, null, 4]; |
| 12 | + expect(computeSegments(data, identity)).toEqual([ |
| 13 | + { startIndex: 0, endIndex: 0 }, |
| 14 | + { startIndex: 3, endIndex: 4 }, |
| 15 | + { startIndex: 6, endIndex: 6 }, |
| 16 | + ]); |
| 17 | + }); |
| 18 | + |
| 19 | + it("returns empty for all-null data", () => { |
| 20 | + expect(computeSegments([null, null], identity)).toEqual([]); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns single segment for all-non-null data", () => { |
| 24 | + expect(computeSegments([1, 2, 3], identity)).toEqual([ |
| 25 | + { startIndex: 0, endIndex: 2 }, |
| 26 | + ]); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("bridgeSmallGaps", () => { |
| 31 | + it("bridges small gaps when connectNulls is true", () => { |
| 32 | + // Gap of 2 indices (< default 36px threshold with 1:1 pixel mapping) |
| 33 | + const data: (number | null)[] = [10, null, 20]; |
| 34 | + const result = bridgeSmallGaps(data, identity, cloneWith, xPixel, true); |
| 35 | + expect(result.values[1]).toBe(15); // linearly interpolated |
| 36 | + expect(result.bridgedSegments).toEqual([{ startIndex: 0, endIndex: 2 }]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("does not bridge when connectNulls is false", () => { |
| 40 | + const data: (number | null)[] = [10, null, 20]; |
| 41 | + const result = bridgeSmallGaps(data, identity, cloneWith, xPixel, false); |
| 42 | + expect(result.values[1]).toBeNull(); |
| 43 | + expect(result.bridgedSegments).toEqual([ |
| 44 | + { startIndex: 0, endIndex: 0 }, |
| 45 | + { startIndex: 2, endIndex: 2 }, |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("does not bridge gaps wider than maxGapPx", () => { |
| 50 | + // With 1:1 pixel mapping and maxGapPx=2, a gap of 3 indices won't bridge |
| 51 | + const data: (number | null)[] = [10, null, null, 20]; |
| 52 | + const result = bridgeSmallGaps(data, identity, cloneWith, xPixel, true, 2); |
| 53 | + expect(result.values[1]).toBeNull(); |
| 54 | + expect(result.values[2]).toBeNull(); |
| 55 | + expect(result.bridgedSegments).toEqual([ |
| 56 | + { startIndex: 0, endIndex: 0 }, |
| 57 | + { startIndex: 3, endIndex: 3 }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("singleton detection with connectNulls on", () => { |
| 62 | + it("singleton surrounded by wide gaps remains a singleton in bridgedSegments", () => { |
| 63 | + // Gap too wide to bridge (> maxGapPx=2): singleton at index 5 stays isolated |
| 64 | + const data: (number | null)[] = [ |
| 65 | + 10, |
| 66 | + null, |
| 67 | + null, |
| 68 | + null, |
| 69 | + null, |
| 70 | + 50, |
| 71 | + null, |
| 72 | + null, |
| 73 | + null, |
| 74 | + null, |
| 75 | + 100, |
| 76 | + ]; |
| 77 | + const result = bridgeSmallGaps( |
| 78 | + data, |
| 79 | + identity, |
| 80 | + cloneWith, |
| 81 | + xPixel, |
| 82 | + true, |
| 83 | + 2, |
| 84 | + ); |
| 85 | + |
| 86 | + // The singleton at index 5 should still appear as a singleton segment |
| 87 | + const singletons = result.bridgedSegments |
| 88 | + .filter((s) => s.startIndex === s.endIndex) |
| 89 | + .map((s) => s.startIndex); |
| 90 | + |
| 91 | + expect(singletons).toContain(0); |
| 92 | + expect(singletons).toContain(5); |
| 93 | + expect(singletons).toContain(10); |
| 94 | + }); |
| 95 | + |
| 96 | + it("singleton gets merged when gap is small enough to bridge", () => { |
| 97 | + // Gap of 2 (within maxGapPx=36): singleton should get bridged into neighbors |
| 98 | + const data: (number | null)[] = [10, null, 20, null, 30]; |
| 99 | + const result = bridgeSmallGaps(data, identity, cloneWith, xPixel, true); |
| 100 | + |
| 101 | + // All gaps bridged: single continuous segment |
| 102 | + expect(result.bridgedSegments).toEqual([{ startIndex: 0, endIndex: 4 }]); |
| 103 | + |
| 104 | + const singletons = result.bridgedSegments |
| 105 | + .filter((s) => s.startIndex === s.endIndex) |
| 106 | + .map((s) => s.startIndex); |
| 107 | + expect(singletons).toEqual([]); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments