Tests: run the real draw code against a recording canvas

This commit is contained in:
Frederic Jung 2026-10-23 17:58:17 +02:00
parent 494d71793e
commit f49c60155b
No known key found for this signature in database
2 changed files with 24 additions and 1 deletions

View file

@ -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);

View file

@ -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, []);
});