Lightpanda: the headless browser designed for AI and automation
dev: explicit capture expected browser divergences
Karl Seguinf5ba302bde4e
diff --git a/src/browser/tests/intersection_observer/attributes.html b/src/browser/tests/intersection_observer/attributes.htmlindex 0f3011bf9..7114e3825 100644--- a/src/browser/tests/intersection_observer/attributes.html+++ b/src/browser/tests/intersection_observer/attributes.html@@ -1,4 +1,5 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><div id="root"></div>@@ -38,8 +39,6 @@testing.expectEqual("10px 10px 10px 10px", margin("10PX"));testing.expectEqual("10px 10px 10px 10px", margin("1e1px"));testing.expectEqual("3px 3px 3px 3px", margin("+3px"));- // Serialized as given (Chrome truncates px values to integers, "1px").- testing.expectEqual("1.5px 1.5px 1.5px 1.5px", margin("1.50px"));testing.expectEqual("0px 0px 0px 0px", margin(""));testing.expectEqual("5.5% 5.5% 5.5% 5.5%", margin("5.5%"));testing.expectEqual("10px 20px 10px 20px", margin("10px/**/20px"));@@ -61,8 +60,8 @@};const thresholds = (threshold) => outcome({ threshold }, (o) => o.thresholds.join());testing.expectEqual("0", thresholds([]));- // Kept as doubles (Chrome stores floats: 0.10000000149011612).- testing.expectEqual("0.1,0.3,0.3", thresholds([0.3, 0.1, 0.3]));+ // Sorted, duplicates kept. Values exact as floats: Chrome stores floats.+ testing.expectEqual("0.25,0.75,0.75", thresholds([0.75, 0.25, 0.75]));testing.expectEqual("0", thresholds(null));testing.expectEqual("RangeError", thresholds(-0.1));testing.expectEqual("RangeError", thresholds([0.5, 1.5]));@@ -73,10 +72,6 @@testing.expectEqual("50 false", delay({ delay: 50 }));testing.expectEqual("1 false", delay({ delay: 1.7 }));testing.expectEqual("150 true", delay({ trackVisibility: true, delay: 150 }));- // Tracking visibility raises the delay to 100ms (Chrome throws a- // NotSupportedError instead, as an earlier draft did).- testing.expectEqual("100 true", delay({ trackVisibility: true }));- testing.expectEqual("100 true", delay({ trackVisibility: true, delay: 20 }));testing.expectEqual("TypeError", outcome({ root: document.createTextNode("x") }, (o) => o.root));// The options' conversion errors (a root of the wrong type, a non-finite@@ -89,3 +84,29 @@testing.expectEqual("0px 0px 0px 0px", margin({ rootMargin: "-0px" }));}</script>++<script id="diverges">+{+ const outcome = (options, read) => {+ try {+ return read(new IntersectionObserver(() => {}, options));+ } catch (err) {+ return err.name;+ }+ };++ testing.diverges("Chrome rounds px margins down to whole pixels", () => {+ testing.expectEqual("1.5px 1.5px 1.5px 1.5px", outcome({ rootMargin: "1.50px" }, (o) => o.rootMargin));+ });+ testing.diverges("Chrome stores thresholds as floats (0.10000000149011612)", () => {+ testing.expectEqual("0.1,0.3", outcome({ threshold: [0.3, 0.1] }, (o) => o.thresholds.join()));+ });++ // Tracking visibility raises the delay to 100ms.+ const delay = (options) => outcome(options, (o) => `${o.delay} ${o.trackVisibility}`);+ testing.diverges("Chrome throws NotSupportedError for trackVisibility with a delay under 100, as an earlier draft did", () => {+ testing.expectEqual("100 true", delay({ trackVisibility: true }));+ testing.expectEqual("100 true", delay({ trackVisibility: true, delay: 20 }));+ });+}+</script>diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.jsindex 5b787af73..cebeafbf9 100644--- a/src/browser/tests/testing.js+++ b/src/browser/tests/testing.js@@ -8,6 +8,9 @@let async_capture = null;let current_script_id = null;+ // set inside a testing.diverges() block when not in Lightpanda+ let diverging = null;+// can only have 1 async test per <script>, this tracks itlet async_seen = new Set();@@ -64,9 +67,17 @@function expectEqual(expected, actual, opts) {if (_equal(expected, actual)) {+ if (diverging) {+ _divergeMatched(_displayValue(actual), opts);+ return;+ }_registerObservation('ok', opts);return;}+ if (diverging) {+ _diverged(`expected: ${_displayValue(expected)}, got: ${_displayValue(actual)}`, opts);+ return;+ }failed = true;_registerObservation('fail', opts);let err = `expected: ${_displayValue(expected)}, got: ${_displayValue(actual)}\n script_id: ${_currentScriptId()}`;@@ -78,6 +89,10 @@}function fail(reason) {+ if (diverging) {+ _diverged(reason);+ return;+ }failed = true;console.error(reason);throw new Error('testing.fail()');@@ -86,6 +101,10 @@function expectError(expected, fn) {withError((err) => {if (!err.toString().includes(expected)) {+ if (diverging) {+ _diverged(`expected error to contain: ${expected}, was: ${err.toString()}`);+ return;+ }console.error(`Expecte error to contains: ${expected}, was: ${err.toString()}`);expectEqual(true, false);} else {@@ -222,6 +241,57 @@return true;}+ function diverges(reason, fn) {+ if (!reason) {+ throw new Error('testing.diverges() needs a reason');+ }+ if (IS_TEST_RUNNER) {+ return _expectSync(fn());+ }+ if (diverging) {+ throw new Error('testing.diverges() blocks cannot be nested');+ }++ diverging = {reason: reason, count: 0, matched: 0};+ const state = diverging;+ let result = null;+ try {+ result = fn();+ } catch (err) {+ _diverged(`threw ${err}`);+ } finally {+ diverging = null;+ }+ _expectSync(result);+ if (state.count === 0 && state.matched === 0) {+ failed = true;+ _registerObservation('fail');+ console.error(`testing.diverges() block made no checks: ${reason}\n script_id: ${_currentScriptId()}`);+ }+ }++ function _expectSync(result) {+ if (result instanceof Promise) {+ throw new Error('testing.diverges() callback must be synchronous');+ }+ }++ // An expected difference counts as a passing observation, so a script made+ // only of diverges() blocks isn't reported as having no assertions.+ function _diverged(detail, opts) {+ diverging.count += 1;+ _registerObservation('ok', opts);+ console.info(`diverges (${diverging.reason}): ${detail}\n script_id: ${_currentScriptId()}`);+ }++ // Doesn't throw: fn's exceptions count as differences.+ function _divergeMatched(detail, opts) {+ diverging.matched += 1;+ failed = true;+ _registerObservation('fail', opts);+ console.error(`testing.diverges() check passed, move it out of the block (${diverging.reason}): ${detail}\n script_id: ${_currentScriptId()}`);+ }+// Brackets messages the browser itself logs (not console.* calls, so they// can't be silenced) for an expected error case. Folded into a collapsed// group so a clean run in Chrome/Firefox shows nothing unexpected.@@ -255,6 +325,7 @@window.testing = {fail: fail,async: async,+ diverges: diverges,assertOk: assertOk,expectTrue: expectTrue,expectFalse: expectFalse,