|
| 1 | +/** Pin the hexbin picker's output across a matrix of (viz, zoom, lat). |
| 2 | + * |
| 3 | + * Guards two things this project has landed and is likely to keep |
| 4 | + * iterating on: |
| 5 | + * |
| 6 | + * 1. **Hex mode**: `AUTO_RES_BY_ZOOM` calibration. Any edit to the |
| 7 | + * table shifts a specific (zoom → res) mapping, which shows up |
| 8 | + * immediately as a broken assertion here. |
| 9 | + * |
| 10 | + * 2. **Circle mode**: the "coarsest-fit" override — when target dot ≥ |
| 11 | + * 1.5px, drop to the coarsest res whose inscribed circle can still |
| 12 | + * fit the dot. Below the threshold, fall through to auto (preserves |
| 13 | + * rasterized-heatmap behavior at coarse zoom). |
| 14 | + * |
| 15 | + * Zoom sweep also enforces monotonicity — as zoom deepens, res never |
| 16 | + * goes coarser (equivalently, `res(z+ε) ≥ res(z)`). |
| 17 | + */ |
| 18 | +import { describe, it, expect } from "vitest" |
| 19 | +import { pickRes, circleRadiusPx, hexPxTargetFor } from "./picker" |
| 20 | + |
| 21 | +const NJ_LAT = 40.7 |
| 22 | + |
| 23 | +describe("picker: hex mode auto res per zoom", () => { |
| 24 | + // Anchor points from AUTO_RES_BY_ZOOM. Any drift → this test flags it |
| 25 | + // and the user gets to decide whether to update the golden or revert. |
| 26 | + const cases: Array<[number, number]> = [ |
| 27 | + // [zoom, expected_res] |
| 28 | + // Note: at z=7.0 exactly, the 1.0 min clamp on autoTarget pushes |
| 29 | + // the picker one res coarser than the AUTO_RES_BY_ZOOM entry (r8). |
| 30 | + // Auto anchor recovers by z=7.5 with mppx small enough to un-clamp. |
| 31 | + [7.0, 7], // statewide |
| 32 | + [8.5, 9], // county-overview |
| 33 | + [10.0, 10], // regional |
| 34 | + [10.94, 10], |
| 35 | + [12.0, 11], // urban |
| 36 | + [13.81, 11], |
| 37 | + [14.0, 12], |
| 38 | + [16.0, 12], |
| 39 | + [16.91, 12], |
| 40 | + [17.0, 13], // street-level |
| 41 | + [18.0, 14], |
| 42 | + [19.0, 15], // deep-zoom |
| 43 | + ] |
| 44 | + for (const [zoom, expected] of cases) { |
| 45 | + it(`z=${zoom} → r${expected}`, () => { |
| 46 | + expect(pickRes("hex", zoom, NJ_LAT)).toBe(expected) |
| 47 | + }) |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +describe("picker: circle mode coarsest-fit override", () => { |
| 52 | + // Below the rasterized threshold (target dot < 1.5px), circle mode |
| 53 | + // MUST fall through to hex-mode auto to preserve statewide density |
| 54 | + // paint. Above it, circle can go coarser than hex mode. |
| 55 | + it("z=8.5 (target < 1.5px): matches hex auto (r9, rasterized)", () => { |
| 56 | + expect(circleRadiusPx(8.5)).toBeLessThan(1.5) |
| 57 | + expect(pickRes("circle", 8.5, NJ_LAT)).toBe(pickRes("hex", 8.5, NJ_LAT)) |
| 58 | + }) |
| 59 | + |
| 60 | + // Anchor circle-mode picks at every zoom in a table so any drift |
| 61 | + // shows up loud. |
| 62 | + const cases: Array<[number, number, number]> = [ |
| 63 | + // [zoom, hex_res, circle_res] |
| 64 | + [8.5, 9, 9], // rasterized preserved (target < 1.5) |
| 65 | + [8.7, 9, 8], // one-step jump at threshold crossing (target ≈ 1.53) |
| 66 | + [9.0, 9, 8], |
| 67 | + [10.94, 10, 9], // Bergen — the vp that flagged the picker over-fetch |
| 68 | + [11.0, 10, 9], |
| 69 | + [12.0, 11, 9], |
| 70 | + [13.81, 11, 10], // mid-zoom (DTJC) |
| 71 | + [14.0, 12, 11], |
| 72 | + [16.91, 12, 12], // deep: auto wins (coarsestFit fits inside auto) |
| 73 | + [17.0, 13, 12], // circle stays r12; hex bumps to r13 |
| 74 | + [18.0, 14, 13], |
| 75 | + [19.0, 15, 13], // circle strictly coarser at very deep zoom |
| 76 | + [20.0, 15, 14], |
| 77 | + ] |
| 78 | + for (const [zoom, hex, circle] of cases) { |
| 79 | + it(`z=${zoom}: hex=r${hex}, circle=r${circle}`, () => { |
| 80 | + expect(pickRes("hex", zoom, NJ_LAT)).toBe(hex) |
| 81 | + expect(pickRes("circle", zoom, NJ_LAT)).toBe(circle) |
| 82 | + }) |
| 83 | + } |
| 84 | +}) |
| 85 | + |
| 86 | +describe("picker: hex mode monotone (never coarser as we zoom in)", () => { |
| 87 | + // Hex-mode tessellation should never drop res as zoom increases. |
| 88 | + // Circle mode is intentionally *not* monotone at the boundary |
| 89 | + // crossing (z≈8.5 target=1.49→1.55 wants slightly-coarser cells so |
| 90 | + // 1.5px dots fit inscribed instead of clamping to sub-px on r9). |
| 91 | + it("res non-decreasing across z=7 → 20 (hex)", () => { |
| 92 | + let prev = -1 |
| 93 | + for (let z = 7; z <= 20; z += 0.1) { |
| 94 | + const r = pickRes("hex", z, NJ_LAT) |
| 95 | + expect(r).toBeGreaterThanOrEqual(prev) |
| 96 | + prev = r |
| 97 | + } |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +describe("picker: circle mode piecewise monotone", () => { |
| 102 | + // Circle mode has ONE legit dropdown at the rasterized→discrete-dot |
| 103 | + // threshold crossing (z≈8.6 for the current curve). Everywhere else |
| 104 | + // it should stay non-decreasing as z increases. |
| 105 | + it("at most one res-drop across z=7 → 20", () => { |
| 106 | + let prev = -1 |
| 107 | + let drops = 0 |
| 108 | + for (let z = 7; z <= 20; z += 0.1) { |
| 109 | + const r = pickRes("circle", z, NJ_LAT) |
| 110 | + if (r < prev) drops++ |
| 111 | + prev = r |
| 112 | + } |
| 113 | + expect(drops).toBeLessThanOrEqual(1) |
| 114 | + }) |
| 115 | +}) |
| 116 | + |
| 117 | +describe("picker: circle mode never picks a res finer than hex mode", () => { |
| 118 | + // Corollary of the "Math.max with auto" guard — circle-mode's data |
| 119 | + // reduction should never accidentally fetch finer cells than hex. |
| 120 | + it("across z=7 → 20", () => { |
| 121 | + for (let z = 7; z <= 20; z += 0.25) { |
| 122 | + const hexRes = pickRes("hex", z, NJ_LAT) |
| 123 | + const circleRes = pickRes("circle", z, NJ_LAT) |
| 124 | + expect(circleRes).toBeLessThanOrEqual(hexRes) |
| 125 | + } |
| 126 | + }) |
| 127 | +}) |
| 128 | + |
| 129 | +describe("picker: circleRadiusPx curve", () => { |
| 130 | + // Anchor the growth curve at a couple of critical zooms. |
| 131 | + it("z=7 → 1.2px (min clamp)", () => { |
| 132 | + expect(circleRadiusPx(7)).toBeCloseTo(1.2, 2) |
| 133 | + }) |
| 134 | + it("z=17 → ~5px (hoverable street-level)", () => { |
| 135 | + expect(circleRadiusPx(17)).toBeGreaterThan(4.5) |
| 136 | + expect(circleRadiusPx(17)).toBeLessThan(5.5) |
| 137 | + }) |
| 138 | + it("z=20 → capped at 24 (max clamp)", () => { |
| 139 | + expect(circleRadiusPx(20)).toBeLessThanOrEqual(24) |
| 140 | + }) |
| 141 | +}) |
| 142 | + |
| 143 | +describe("picker: hexPxTargetFor stays reasonable", () => { |
| 144 | + // Regardless of viz mode / zoom, target should be within picker's |
| 145 | + // sane range (1..30). |
| 146 | + for (let z = 7; z <= 20; z += 1) { |
| 147 | + it(`z=${z}: 1 ≤ target ≤ 30 in both modes`, () => { |
| 148 | + for (const viz of ["hex", "circle"] as const) { |
| 149 | + const t = hexPxTargetFor(viz, z, NJ_LAT) |
| 150 | + expect(t).toBeGreaterThanOrEqual(1) |
| 151 | + expect(t).toBeLessThanOrEqual(30) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +}) |
0 commit comments