-
Notifications
You must be signed in to change notification settings - Fork 0
ci: report visual baseline drift without failing advisory checks #1791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e2d26e
ci: keep visual baseline drift advisory
BigSimmo cad1681
Merge branch 'main' into codex/visual-baseline-advisory-pr
BigSimmo 6bc5771
fix(ci): classify visual drift and harden formulation Clear→Draft
cursoragent ba78101
docs: record PR #1791 babysit unblock in review ledger
cursoragent 802c238
Merge branch 'main' into codex/visual-baseline-advisory-pr
cursoragent 4ad2014
Merge origin/main (incl. #1798) into codex/visual-baseline-advisory-pr
cursoragent cbb74f0
Merge origin/main after #1797 into codex/visual-baseline-advisory-pr
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Decide whether a failed `test:e2e:visual` run was pixel drift (advisory) or an | ||
| * infrastructure / non-comparison failure that must stay red. | ||
| * | ||
| * Exit 0 → advisory drift only (or no failures found in an existing report). | ||
| * Exit 1 → missing report, missing baselines, runtime/assertion failures, or mixed. | ||
| */ | ||
| import { existsSync, readFileSync, readdirSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { pathToFileURL } from "node:url"; | ||
|
|
||
| const DEFAULT_JUNIT = "test-results/playwright-junit.xml"; | ||
| const DEFAULT_RESULTS = "test-results/playwright-results.json"; | ||
|
|
||
| const decode = (value) => | ||
| value | ||
| .replaceAll(""", '"') | ||
| .replaceAll("'", "'") | ||
| .replaceAll("<", "<") | ||
| .replaceAll(">", ">") | ||
| .replaceAll("&", "&"); | ||
|
|
||
| const attribute = (attributes, name) => { | ||
| const match = attributes.match(new RegExp(`\\b${name}="([^"]*)"`)); | ||
| return match ? decode(match[1]) : ""; | ||
| }; | ||
|
|
||
| /** Failure/error bodies from a Playwright JUnit report. */ | ||
| export function failureBodiesFromJunit(xml) { | ||
| return [...xml.matchAll(/<testcase\b([^>]*)>([\s\S]*?)<\/testcase>/g)].flatMap((match) => { | ||
| const title = attribute(match[1], "name"); | ||
| const classname = attribute(match[1], "classname"); | ||
| return [...match[2].matchAll(/<(?:failure|error)\b([^>]*)>([\s\S]*?)<\/(?:failure|error)>/g)].map((failure) => ({ | ||
| title, | ||
| classname, | ||
| message: attribute(failure[1], "message"), | ||
| body: decode(failure[2]).trim(), | ||
| })); | ||
| }); | ||
| } | ||
|
|
||
| function collectErrorMessages(node, out = []) { | ||
| if (!node || typeof node !== "object") return out; | ||
| if (Array.isArray(node)) { | ||
| for (const item of node) collectErrorMessages(item, out); | ||
| return out; | ||
| } | ||
| if (node.error?.message) out.push(String(node.error.message)); | ||
| if (Array.isArray(node.errors)) { | ||
| for (const error of node.errors) { | ||
| if (error?.message) out.push(String(error.message)); | ||
| } | ||
| } | ||
| for (const value of Object.values(node)) collectErrorMessages(value, out); | ||
| return out; | ||
| } | ||
|
|
||
| export function failureMessagesFromResults(resultsJson) { | ||
| return collectErrorMessages(resultsJson); | ||
| } | ||
|
|
||
| /** Pixel-drift only: toHaveScreenshot mismatch with an existing baseline. */ | ||
| export function isPixelDriftFailure(text) { | ||
| const haystack = String(text ?? ""); | ||
| if (!haystack) return false; | ||
| if (/snapshot doesn't exist/i.test(haystack)) return false; | ||
| if (/AWAITING_BASELINE/i.test(haystack)) return false; | ||
| return /toHaveScreenshot/i.test(haystack) || /Screenshot comparison failed/i.test(haystack); | ||
| } | ||
|
|
||
| function listDiffPngs(root = "test-results") { | ||
| if (!existsSync(root)) return []; | ||
| const found = []; | ||
| const walk = (dir) => { | ||
| for (const entry of readdirSync(dir, { withFileTypes: true })) { | ||
| const full = join(dir, entry.name); | ||
| if (entry.isDirectory()) walk(full); | ||
| else if (entry.isFile() && /-diff\.png$/i.test(entry.name)) found.push(full); | ||
| } | ||
| }; | ||
| walk(root); | ||
| return found; | ||
| } | ||
|
|
||
| export function classifyVisualBaselineOutcome({ | ||
| junitPath = DEFAULT_JUNIT, | ||
| resultsPath = DEFAULT_RESULTS, | ||
| testResultsDir = "test-results", | ||
| } = {}) { | ||
| const hasJunit = existsSync(junitPath); | ||
| const hasResults = existsSync(resultsPath); | ||
| if (!hasJunit && !hasResults) { | ||
| return { | ||
| kind: "infrastructure", | ||
| reason: "No Playwright JUnit or JSON report after visual comparison failure.", | ||
| }; | ||
| } | ||
|
|
||
| const messages = []; | ||
| if (hasJunit) { | ||
| for (const failure of failureBodiesFromJunit(readFileSync(junitPath, "utf8"))) { | ||
| messages.push([failure.message, failure.body, failure.title].filter(Boolean).join("\n")); | ||
| } | ||
| } | ||
| if (hasResults) { | ||
| messages.push(...failureMessagesFromResults(JSON.parse(readFileSync(resultsPath, "utf8")))); | ||
| } | ||
|
|
||
| const unique = [...new Set(messages.map((message) => message.trim()).filter(Boolean))]; | ||
| if (unique.length === 0) { | ||
| const diffs = listDiffPngs(testResultsDir); | ||
| if (diffs.length > 0) { | ||
| return { | ||
| kind: "pixel-drift", | ||
| reason: `Found ${diffs.length} screenshot diff artifact(s) without parsed failure text.`, | ||
| }; | ||
| } | ||
| return { | ||
| kind: "infrastructure", | ||
| reason: "Visual comparison failed but the report contains no failed testcases.", | ||
| }; | ||
| } | ||
|
|
||
| const nonDrift = unique.filter((message) => !isPixelDriftFailure(message)); | ||
| if (nonDrift.length > 0) { | ||
| return { | ||
| kind: "non-drift", | ||
| reason: `Non-comparison failure(s) present (${nonDrift.length}/${unique.length}).`, | ||
| samples: nonDrift.slice(0, 3), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| kind: "pixel-drift", | ||
| reason: `All ${unique.length} failure(s) are toHaveScreenshot pixel mismatches.`, | ||
| }; | ||
| } | ||
|
|
||
| function main() { | ||
| const outcome = classifyVisualBaselineOutcome(); | ||
| if (outcome.kind === "pixel-drift") { | ||
| console.log(`Visual baseline outcome: pixel-drift — ${outcome.reason}`); | ||
| process.exitCode = 0; | ||
| return; | ||
| } | ||
| console.error(`Visual baseline outcome: ${outcome.kind} — ${outcome.reason}`); | ||
| for (const sample of outcome.samples ?? []) { | ||
| console.error(`- ${sample.split("\n")[0].slice(0, 200)}`); | ||
| } | ||
| process.exitCode = 1; | ||
| } | ||
|
|
||
| if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.