From f49c60155b4a9038643e4c98f5fb59668d48b73a Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Fri, 23 Oct 2026 17:58:17 +0200 Subject: [PATCH] Tests: run the real draw code against a recording canvas --- tests/harness.js | 16 +++++++++++++++- tests/modes.test.js | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/harness.js b/tests/harness.js index 94b29ff..c1526d5 100644 --- a/tests/harness.js +++ b/tests/harness.js @@ -21,7 +21,21 @@ function load(opts = {}) { beforeParse(window) { window.__TUBES_TEST__ = true; for (const [k, v] of Object.entries(opts.storage || {})) window.localStorage.setItem(k, v); - window.Path2D = class { moveTo() {} quadraticCurveTo() {} closePath() {} }; + window.Path2D = class { moveTo() {} lineTo() {} quadraticCurveTo() {} bezierCurveTo() {} arc() {} rect() {} closePath() {} }; + // A recording 2D context, so draw code really runs under test (jsdom has no canvas). + const fake = () => { const calls = { n: 0 }; + const grad = { addColorStop() {} }; + return new Proxy({ calls }, { get(t, k) { + if (k in t) return t[k]; + if (k === 'measureText') return (s) => ({ width: String(s).length * 7 }); + if (k === 'createLinearGradient' || k === 'createRadialGradient' || k === 'createPattern') return () => grad; + if (k === 'getImageData') return () => ({ data: new Uint8ClampedArray(4) }); + if (k === 'canvas') return null; + return (...a) => { calls.n++; }; + }, set(t, k, v) { t[k] = v; return true; } }); }; + window.HTMLCanvasElement.prototype.getContext = function () { return this.__ctx || (this.__ctx = fake()); }; + window.HTMLCanvasElement.prototype.toDataURL = () => 'data:image/png;base64,'; + window.HTMLCanvasElement.prototype.toBlob = (cb) => cb(null); }, }); const ev = (src) => dom.window.eval(src); diff --git a/tests/modes.test.js b/tests/modes.test.js index 28e034e..b916e0c 100644 --- a/tests/modes.test.js +++ b/tests/modes.test.js @@ -132,3 +132,12 @@ test('half speed and deeper zoom', () => { assert.equal(ev("cam.z"), 6); assert.deepEqual(errors, []); }); + +test('the map draws at every zoom, with and without animations, without throwing', () => { + const { ev, errors } = load(); + ev("ACT.start({});S.cash=1e9;S.lvl=4;ACT.launchBB({city:'fra'});for(const c of ['ams','lon','nyc'])ACT.buildPop({city:c});UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:1});closeModal();compute()"); + for (const z of [0.35, 0.8, 1.4, 3, 6]) ev(`cam.z=${z};draw(performance.now());draw(0)`); + ev("ACT.animSet({v:'off'});frame(performance.now()+16)"); + assert.ok(ev("ctx&&ctx.calls.n") > 100, 'the canvas was actually drawn on'); + assert.deepEqual(errors, []); +});