Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: compare --fail-on-diff intermittently reds two identical pages, because brokenAssets is compared by count and a transient failed request on one side alone is indistinguishable from a regression (PIN-240)

Luigi Agosti94dd4921e624
diff --git a/docs/audit.md b/docs/audit.md
index d64e19eb..218ecde4 100644
--- a/docs/audit.md
+++ b/docs/audit.md
@@ -125,13 +125,22 @@ screenshot pairs, and diffs the data (uncaught JS errors, console error count,
broken assets, accessibility score, load time with a noise threshold). Pages
present on only one side are reported as `added`/`removed`.
-Uncaught JS errors (`jsErrors`) are compared by identity rather than by count:
-each exception's first message line, with the page's own `scheme://host`
-masked, so the same failure on two base URLs matches. An exception on one side
-only — or one swapped for another — is drift; the same exception on both sides
-is not, since this gate compares two deploys and does not judge absolute page
-health. The other fields, `consoleErrors` included, compare counts, so
-substituting one console message for another stays invisible.
+Uncaught JS errors (`jsErrors`) and broken assets (`brokenAssets`) are compared
+by identity rather than by count, both with the page's own `scheme://host`
+masked so the same failure on two base URLs matches: an exception's first
+message line, and an asset's `url` plus its HTTP status. A failure on one side
+only — or one swapped for another at the same count — is drift; the same
+failure on both sides is not, since this gate compares two deploys and does not
+judge absolute page health. The other fields, `consoleErrors` included, compare
+counts, so substituting one console message for another stays invisible.
+
+A broken asset with no HTTP status — a transport error such as a connection
+reset, or a request that never completed inside the collection window — is not
+compared. `--fail-on-diff` is a gate on the deployment, and which request a run
+happened to lose is a property of the run: counting those made two identical
+sites fail roughly one run in five. They are still reported in full by the
+audit itself and by `/network`, where a failed load is a real finding; the
+exclusion applies only to this comparison.
| Flag | Default | Meaning |
|---|---|---|
diff --git a/internal/audit/compare.go b/internal/audit/compare.go
index d0382e93..fc6bb5b1 100644
--- a/internal/audit/compare.go
+++ b/internal/audit/compare.go
@@ -244,10 +244,10 @@ func diffPageData(live, staging PageResult) []DataDrift {
record("consoleErrors", liveErrs, stagingErrs)
}
if liveJS, stagingJS := jsErrorIdentities(live), jsErrorIdentities(staging); !slices.Equal(liveJS, stagingJS) {
- record("jsErrors", jsErrorDriftValue(liveJS), jsErrorDriftValue(stagingJS))
+ record("jsErrors", identityDriftValue(liveJS), identityDriftValue(stagingJS))
}
- if lb, sb := len(live.Browser.BrokenAssets), len(staging.Browser.BrokenAssets); lb != sb {
- record("brokenAssets", lb, sb)
+ if liveAssets, stagingAssets := brokenAssetIdentities(live), brokenAssetIdentities(staging); !slices.Equal(liveAssets, stagingAssets) {
+ record("brokenAssets", identityDriftValue(liveAssets), identityDriftValue(stagingAssets))
}
if live.Browser.AccessibilityScore != staging.Browser.AccessibilityScore {
record("accessibilityScore", live.Browser.AccessibilityScore, staging.Browser.AccessibilityScore)
@@ -258,9 +258,18 @@ func diffPageData(live, staging PageResult) []DataDrift {
return drift
}
-// jsErrorOriginPlaceholder stands in for a page's own scheme://host inside an
-// exception message, so the same failure on two base URLs reads as one identity.
-const jsErrorOriginPlaceholder = "<origin>"
+// originPlaceholder stands in for a page's own scheme://host inside an identity, so the same
+// failure on two base URLs reads as one identity. One masking rule, shared by every identity
+// this file builds: a second copy would let the two comparisons drift apart on the one
+// property that makes them comparable at all.
+const originPlaceholder = "<origin>"
+
+func maskOrigin(value, origin string) string {
+ if origin == "" {
+ return value
+ }
+ return strings.ReplaceAll(value, origin, originPlaceholder)
+}
// jsErrorIdentities is the sorted multiset of uncaught exceptions on a page.
// Unlike the count-based fields, uncaught errors are compared by identity: a
@@ -280,10 +289,34 @@ func jsErrorIdentities(p PageResult) []string {
func jsErrorIdentity(message, origin string) string {
first, _, _ := strings.Cut(message, "\n")
- if origin != "" {
- first = strings.ReplaceAll(first, origin, jsErrorOriginPlaceholder)
+ return strings.TrimSpace(maskOrigin(first, origin))
+}
+
+// brokenAssetIdentities is the sorted multiset of assets that failed with an HTTP STATUS,
+// each as its url with the page's own origin masked. Comparing identities rather than counts
+// is what lets a 404 present on BOTH sides stop reading as clean and a swap — a different
+// asset broken on each side at the same count — stop reading as a wash.
+//
+// TRANSPORT ERRORS ARE EXCLUDED, and this is the only place they are: observe.IsBrokenAsset
+// counts a failed request as broken for /network and every other consumer, where a connection
+// reset genuinely IS a broken load and must stay visible. What that rule cannot survive is
+// being the input to a CI gate. A reset, a slow favicon landing outside the collection window
+// or a still-warming environment happens on ONE side only, so two identical pages reported a
+// brokenAssets drift about one run in five — and a gate that reds a clean deploy for no reason
+// is switched off, which gives back every regression it was there to catch. A failure carrying
+// an HTTP status is a property of the deployment and is compared; a failure without one is a
+// property of the run.
+func brokenAssetIdentities(p PageResult) []string {
+ origin := urlOrigin(p.URL)
+ identities := make([]string, 0, len(p.Browser.BrokenAssets))
+ for _, asset := range p.Browser.BrokenAssets {
+ if asset.Status < 400 {
+ continue
+ }
+ identities = append(identities, fmt.Sprintf("%s %d", maskOrigin(asset.URL, origin), asset.Status))
}
- return strings.TrimSpace(first)
+ sort.Strings(identities)
+ return identities
}
func urlOrigin(raw string) string {
@@ -294,9 +327,9 @@ func urlOrigin(raw string) string {
return u.Scheme + "://" + u.Host
}
-// jsErrorDriftValue names the exceptions behind the count, so a reader can tell
-// a substitution (1 vs 1) from an unchanged page.
-func jsErrorDriftValue(identities []string) string {
+// identityDriftValue names what is behind the count, so a reader can tell a substitution
+// (1 vs 1) from an unchanged page. Shared by every identity-compared field.
+func identityDriftValue(identities []string) string {
if len(identities) == 0 {
return "0"
}
diff --git a/internal/audit/compare_test.go b/internal/audit/compare_test.go
index 94f35d26..fe4bd199 100644
--- a/internal/audit/compare_test.go
+++ b/internal/audit/compare_test.go
@@ -89,7 +89,7 @@ func TestDiffPageDataFields(t *testing.T) {
})
b := page("http://y/p", func(p *PageResult) {
p.Browser.AccessibilityScore = 80
- p.Browser.BrokenAssets = []BrokenAsset{{URL: "x"}}
+ p.Browser.BrokenAssets = []BrokenAsset{{URL: "x", Status: 404}}
p.Browser.TimingMetrics.Load = 2000
})
drift := diffPageData(a, b)
@@ -205,11 +205,11 @@ func boolToInt(b bool) int {
func TestJSErrorDriftValueNamesTheExceptions(t *testing.T) {
staging := page("http://localhost:18602/index.html", jsError("Uncaught: TypeError: b is not a function"))
- value := jsErrorDriftValue(jsErrorIdentities(staging))
+ value := identityDriftValue(jsErrorIdentities(staging))
if !strings.Contains(value, "TypeError: b is not a function") || !strings.HasPrefix(value, "1:") {
t.Fatalf("drift value = %q, want the count and the exception text", value)
}
- if jsErrorDriftValue(jsErrorIdentities(page("http://localhost:18601/index.html"))) != "0" {
+ if identityDriftValue(jsErrorIdentities(page("http://localhost:18601/index.html"))) != "0" {
t.Fatalf("a page without exceptions should read as 0")
}
}
@@ -295,3 +295,177 @@ func TestComparePagesNoScreenshots(t *testing.T) {
t.Error("no screenshots and no drift should not be a diff")
}
}
+
+func brokenAsset(url string, status int, transportError string) func(*PageResult) {
+ return func(p *PageResult) {
+ p.Browser.BrokenAssets = append(p.Browser.BrokenAssets, BrokenAsset{URL: url, Status: status, Error: transportError})
+ }
+}
+
+// The reported symptom and the two properties identity comparison adds, in one table. The
+// live and staging pages are served from different ports, as they are in the real comparison,
+// so every row also exercises the origin mask.
+func TestDiffPageDataBrokenAssets(t *testing.T) {
+ live := "http://localhost:18601"
+ staging := "http://localhost:18602"
+
+ tests := []struct {
+ name string
+ live []func(*PageResult)
+ staging []func(*PageResult)
+ wantDrift bool
+ why string
+ }{
+ {
+ name: "a transport error on one side alone is not drift",
+ live: []func(*PageResult){brokenAsset(live+"/favicon.ico", 0, "net::ERR_CONNECTION_RESET")},
+ wantDrift: false,
+ why: "the card's headline symptom: two identical pages, one transient failed request, a red CI gate",
+ },
+ {
+ name: "a transport error on each side is not drift either",
+ live: []func(*PageResult){brokenAsset(live+"/favicon.ico", 0, "net::ERR_CONNECTION_RESET")},
+ staging: []func(*PageResult){brokenAsset(staging+"/analytics.js", 0, "net::ERR_ABORTED")},
+ wantDrift: false,
+ why: "which request the run happened to lose is not a property of the deployment",
+ },
+ {
+ name: "an HTTP status failure on one side alone is still drift",
+ staging: []func(*PageResult){brokenAsset(staging+"/logo.png", 404, "")},
+ wantDrift: true,
+ why: "the fix must not buy its green by ignoring broken assets generally",
+ },
+ {
+ name: "an HTTP status failure fixed on staging is drift",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, "")},
+ wantDrift: true,
+ },
+ {
+ name: "the same 404 on both sides is not drift",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, "")},
+ staging: []func(*PageResult){brokenAsset(staging+"/logo.png", 404, "")},
+ wantDrift: false,
+ why: "only the origin mask can see these as one identity",
+ },
+ {
+ name: "a different asset broken on each side at the same count is drift",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, "")},
+ staging: []func(*PageResult){brokenAsset(staging+"/hero.jpg", 404, "")},
+ wantDrift: true,
+ why: "the swap a count comparison reads as a wash",
+ },
+ {
+ name: "the same url failing with a different status is drift",
+ live: []func(*PageResult){brokenAsset(live+"/api/data", 404, "")},
+ staging: []func(*PageResult){brokenAsset(staging+"/api/data", 500, "")},
+ wantDrift: true,
+ why: "the status is half the identity",
+ },
+ {
+ name: "the same 404 twice is not one 404",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, "")},
+ staging: []func(*PageResult){brokenAsset(staging+"/logo.png", 404, ""), brokenAsset(staging+"/logo.png", 404, "")},
+ wantDrift: true,
+ why: "identity is a multiset, not a set",
+ },
+ {
+ name: "the same two 404s in a different order",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, ""), brokenAsset(live+"/hero.jpg", 404, "")},
+ staging: []func(*PageResult){
+ brokenAsset(staging+"/hero.jpg", 404, ""), brokenAsset(staging+"/logo.png", 404, ""),
+ },
+ wantDrift: false,
+ why: "collection order is not a property of the deployment",
+ },
+ {
+ name: "a transport error beside a matching 404 does not disturb the comparison",
+ live: []func(*PageResult){brokenAsset(live+"/logo.png", 404, "")},
+ staging: []func(*PageResult){brokenAsset(staging+"/logo.png", 404, ""), brokenAsset(staging+"/favicon.ico", 0, "net::ERR_ABORTED")},
+ wantDrift: false,
+ why: "the exclusion has to survive being mixed with a real failure, which is the realistic shape",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ drift := diffPageData(page(live+"/index.html", tc.live...), page(staging+"/index.html", tc.staging...))
+ var got *DataDrift
+ for i := range drift {
+ if drift[i].Field == "brokenAssets" {
+ got = &drift[i]
+ }
+ }
+ if tc.wantDrift && got == nil {
+ t.Fatalf("no brokenAssets drift in %+v; %s", drift, tc.why)
+ }
+ if !tc.wantDrift && got != nil {
+ t.Fatalf("unexpected brokenAssets drift %+v; %s", *got, tc.why)
+ }
+ if len(drift) != boolToInt(tc.wantDrift) {
+ t.Fatalf("broken assets disturbed the other drift fields: %+v", drift)
+ }
+ })
+ }
+}
+
+// The gate is the product here: with --fail-on-diff nobody reads the report, so the exit code
+// is what these two assertions are about. They run through ComparePages rather than
+// diffPageData so the whole path the CLI takes is covered.
+func TestTheGateIsNotRedByATransientFailureAndIsStillRedByA404(t *testing.T) {
+ const live, staging = "http://localhost:18601", "http://localhost:18602"
+
+ transient, err := ComparePages(live, staging,
+ AuditReport{Pages: []PageResult{page(live+"/index.html",
+ brokenAsset(live+"/favicon.ico", 0, "net::ERR_CONNECTION_RESET"))}},
+ AuditReport{Pages: []PageResult{page(staging + "/index.html")}})
+ if err != nil {
+ t.Fatalf("ComparePages: %v", err)
+ }
+ if transient.Report.HasDiffs {
+ t.Fatalf("two identical pages failed the gate over one transient request: %+v", transient.Report.Pages)
+ }
+
+ broken, err := ComparePages(live, staging,
+ AuditReport{Pages: []PageResult{page(live + "/index.html")}},
+ AuditReport{Pages: []PageResult{page(staging+"/index.html",
+ brokenAsset(staging+"/logo.png", 404, ""))}})
+ if err != nil {
+ t.Fatalf("ComparePages: %v", err)
+ }
+ if !broken.Report.HasDiffs {
+ t.Fatalf("a 404 introduced on staging must fail the gate: %+v", broken.Report.Pages)
+ }
+}
+
+// The drift value has to name the assets, for the same reason the jsErrors one does: a
+// reader seeing 1 vs 1 cannot tell a swap from an unchanged page.
+func TestBrokenAssetDriftValueNamesTheAssets(t *testing.T) {
+ staging := page("http://localhost:18602/index.html", brokenAsset("http://localhost:18602/logo.png", 404, ""))
+ value := identityDriftValue(brokenAssetIdentities(staging))
+ if !strings.Contains(value, "/logo.png 404") || !strings.HasPrefix(value, "1:") {
+ t.Fatalf("drift value = %q, want the count and the failing asset", value)
+ }
+ if strings.Contains(value, "localhost:18602") {
+ t.Fatalf("drift value = %q, want the page's own origin masked", value)
+ }
+ if identityDriftValue(brokenAssetIdentities(page("http://localhost:18601/index.html"))) != "0" {
+ t.Fatal("a page with no broken assets should read as 0")
+ }
+}
+
+// One masking rule, not two: the identity comparisons are only comparable across two base
+// URLs because they mask the same way, and a second copy is how they drift apart.
+func TestBothIdentitiesShareOneOriginMask(t *testing.T) {
+ const origin = "http://localhost:18602"
+ assets := brokenAssetIdentities(page(origin+"/index.html", brokenAsset(origin+"/logo.png", 404, "")))
+ errors := jsErrorIdentities(page(origin+"/index.html", jsError("Uncaught Error: boom at "+origin+"/logo.png:1:1")))
+
+ for _, identity := range append(append([]string{}, assets...), errors...) {
+ if strings.Contains(identity, origin) {
+ t.Fatalf("identity %q carries the page's own origin", identity)
+ }
+ if !strings.Contains(identity, originPlaceholder) {
+ t.Fatalf("identity %q did not mask the origin at all", identity)
+ }
+ }
+}