|
| 1 | +// Tests the ?scaminmerge A/B toggle on <chart-canvas>: in merged mode the client |
| 2 | +// asks the engine for its zoom-expression SCAMIN gate (scaminMerge=1, NOT |
| 3 | +// scaminFilterGate) and DISABLES the whole client SCAMIN injection path |
| 4 | +// (_scaminUpdate / _scaminApplySettled / _scaminForceWhenReady early-return, so no |
| 5 | +// setFilter / source reload fires on zoom). Normal mode is unchanged. |
| 6 | +// Run: node --test web/src/chart-canvas/chart-canvas.scaminmerge.test.mjs |
| 7 | +// |
| 8 | +// <chart-canvas> extends HTMLElement and calls customElements.define at load, so we |
| 9 | +// shim the two custom-element globals before importing — the module is otherwise |
| 10 | +// node-safe (maplibre is a lazy dynamic import). We drive the pure methods via |
| 11 | +// prototype.call(stub) so no DOM/map is constructed. |
| 12 | +import test from "node:test"; |
| 13 | +import assert from "node:assert/strict"; |
| 14 | + |
| 15 | +globalThis.HTMLElement = globalThis.HTMLElement || class {}; |
| 16 | +globalThis.customElements = globalThis.customElements || { define() {} }; |
| 17 | + |
| 18 | +const { ChartCanvas } = await import("./chart-canvas.mjs"); |
| 19 | +const proto = ChartCanvas.prototype; |
| 20 | + |
| 21 | +// Minimal `this` for _marinerQuery: it reads _mariner (an object of settings, all |
| 22 | +// omitted here so every key is skipped), _active, _engineSet, the three SCAMIN |
| 23 | +// flags, and _featureSizeScale(). |
| 24 | +function marinerStub(over) { |
| 25 | + return Object.assign({ |
| 26 | + _mariner: {}, |
| 27 | + _active: "day", |
| 28 | + _engineSet: "tile57", |
| 29 | + _ignoreScamin: false, |
| 30 | + _scaminMerged: false, |
| 31 | + _scaminGate: true, |
| 32 | + _featureSizeScale: () => 1, |
| 33 | + }, over); |
| 34 | +} |
| 35 | + |
| 36 | +test("_marinerQuery: merged mode sends scaminMerge=1 and NOT scaminFilterGate", () => { |
| 37 | + const q = new URLSearchParams(proto._marinerQuery.call(marinerStub({ _scaminMerged: true }))); |
| 38 | + assert.equal(q.get("scaminMerge"), "1"); |
| 39 | + assert.equal(q.get("scaminFilterGate"), null); |
| 40 | + assert.equal(q.get("set"), "tile57"); |
| 41 | +}); |
| 42 | + |
| 43 | +test("_marinerQuery: normal (non-merged) mode sends scaminFilterGate=1 and NOT scaminMerge", () => { |
| 44 | + const q = new URLSearchParams(proto._marinerQuery.call(marinerStub({ _scaminMerged: false, _scaminGate: true }))); |
| 45 | + assert.equal(q.get("scaminFilterGate"), "1"); |
| 46 | + assert.equal(q.get("scaminMerge"), null); |
| 47 | +}); |
| 48 | + |
| 49 | +test("_scaminForceWhenReady: merged mode is a no-op (never calls _scaminUpdate)", () => { |
| 50 | + let updates = 0; |
| 51 | + const stub = { |
| 52 | + _scaminMerged: true, |
| 53 | + _map: { isStyleLoaded: () => true, once: () => { throw new Error("must not defer"); } }, |
| 54 | + _scaminUpdate: () => { updates++; }, |
| 55 | + }; |
| 56 | + proto._scaminForceWhenReady.call(stub); |
| 57 | + assert.equal(updates, 0, "merged mode must not inject a cutoff"); |
| 58 | +}); |
| 59 | + |
| 60 | +test("_scaminForceWhenReady: normal mode DOES inject the cutoff (calls _scaminUpdate(true))", () => { |
| 61 | + const args = []; |
| 62 | + const stub = { |
| 63 | + _scaminMerged: false, |
| 64 | + _map: { isStyleLoaded: () => true }, |
| 65 | + _scaminLayersCache: {}, _chartLayerIdsCache: {}, |
| 66 | + _scaminUpdate: (force) => { args.push(force); }, |
| 67 | + }; |
| 68 | + proto._scaminForceWhenReady.call(stub); |
| 69 | + assert.deepEqual(args, [true], "normal mode re-injects the live cutoff"); |
| 70 | +}); |
| 71 | + |
| 72 | +test("_scaminUpdate: merged mode early-returns before any setFilter (even with all other guards open)", () => { |
| 73 | + let setFilters = 0; |
| 74 | + // All the NON-merged guards are deliberately satisfied, so _scaminMerged is the |
| 75 | + // ONLY thing that can stop the injection loop. |
| 76 | + const stub = { |
| 77 | + _scaminMerged: true, |
| 78 | + _scaminGate: true, |
| 79 | + _engineMode: true, |
| 80 | + _map: { |
| 81 | + isStyleLoaded: () => true, |
| 82 | + getZoom: () => 13, getCenter: () => ({ lat: 38.9 }), |
| 83 | + getLayer: () => ({}), getFilter: () => ["all"], |
| 84 | + setFilter: () => { setFilters++; }, |
| 85 | + }, |
| 86 | + _pxPitch: undefined, |
| 87 | + _engineScaminValues: [30000, 12000], |
| 88 | + _scaminGatedLayers: () => { throw new Error("must not scan gated layers in merged mode"); }, |
| 89 | + }; |
| 90 | + proto._scaminUpdate.call(stub, true); |
| 91 | + assert.equal(setFilters, 0, "merged mode must issue zero setFilter (the zoom-expression self-gates)"); |
| 92 | +}); |
| 93 | + |
| 94 | +test("_scaminApplySettled: merged mode schedules no settle apply", async () => { |
| 95 | + let scheduled = false; |
| 96 | + const realSetTimeout = globalThis.setTimeout; |
| 97 | + globalThis.setTimeout = (fn, d) => { scheduled = true; return realSetTimeout(fn, d); }; |
| 98 | + try { |
| 99 | + proto._scaminApplySettled.call({ _scaminMerged: true, _scaminApplyT: 0 }, 120); |
| 100 | + assert.equal(scheduled, false, "merged mode must not arm the settle timer"); |
| 101 | + } finally { |
| 102 | + globalThis.setTimeout = realSetTimeout; |
| 103 | + } |
| 104 | +}); |
0 commit comments