Repository navigation
Detector: corpus round 11 deferred edge cases #1155
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
+645
−80
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54dec42
Detector: corpus round 11 deferred edge cases (#1076)
pbakaus 9e6e6e7
Merge origin/main into fix/detector-round11-edge-cases
pbakaus 2a196ef
Merge origin/main into fix/detector-round11-edge-cases
pbakaus 04af943
Read 1px text at its drawn size in every reader (#1076 item 1)
pbakaus 9678fe2
Wordmark: read the host name with punctuation dropped, as the title p…
pbakaus 910a57d
Merge origin/main into fix/detector-round11-edge-cases
pbakaus cd20e24
Wordmark: match title parts against the host without its top-level label
pbakaus 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| //! Corpus round 11's deferred edge cases (#1076), over three fixtures in | ||
| //! `tests/fixtures/antipatterns/`, rendered by an installed browser. Skips | ||
| //! cleanly when there is none. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::io::{Read, Write}; | ||
| use std::net::{TcpListener, TcpStream}; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use impeccable_browser::BrowserEngine; | ||
| use impeccable_detect::engines::{ScanOptions, UrlEngine}; | ||
|
|
||
| fn fixtures_dir() -> PathBuf { | ||
| Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/antipatterns") | ||
| } | ||
|
|
||
| fn serve() -> u16 { | ||
| let listener = TcpListener::bind("127.0.0.1:0").expect("bind"); | ||
| let port = listener.local_addr().unwrap().port(); | ||
| std::thread::spawn(move || { | ||
| for stream in listener.incoming().flatten() { | ||
| std::thread::spawn(move || handle(stream)); | ||
| } | ||
| }); | ||
| port | ||
| } | ||
|
|
||
| fn handle(mut stream: TcpStream) { | ||
| let mut buf = [0u8; 8192]; | ||
| let n = stream.read(&mut buf).unwrap_or(0); | ||
| let request = String::from_utf8_lossy(&buf[..n]); | ||
| let rel = request | ||
| .lines() | ||
| .next() | ||
| .and_then(|l| l.split_whitespace().nth(1)) | ||
| .unwrap_or("/") | ||
| .trim_start_matches('/') | ||
| .to_string(); | ||
| let (status, body) = match std::fs::read(fixtures_dir().join(&rel)) { | ||
| Ok(body) if !rel.contains("..") => ("200 OK", body), | ||
| _ => ("404 Not Found", b"missing".to_vec()), | ||
| }; | ||
| let head = format!( | ||
| "HTTP/1.0 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", | ||
| body.len() | ||
| ); | ||
| let _ = stream.write_all(head.as_bytes()); | ||
| let _ = stream.write_all(&body); | ||
| let _ = stream.flush(); | ||
| } | ||
|
|
||
| fn engine() -> Option<BrowserEngine> { | ||
| let env: HashMap<String, String> = std::env::vars().collect(); | ||
| if impeccable_browser::discovery::find_browser(&env).is_err() { | ||
| eprintln!("skip: no installed browser found"); | ||
| return None; | ||
| } | ||
| Some(BrowserEngine::new(env)) | ||
| } | ||
|
|
||
| fn scan(engine: &BrowserEngine, port: u16, fixture: &str) -> Vec<impeccable_core::findings::Finding> { | ||
| let url = format!("http://127.0.0.1:{port}/{fixture}"); | ||
| engine.detect_url(&url, &ScanOptions::default()).expect("scan") | ||
| } | ||
|
|
||
| fn selector(f: &impeccable_core::findings::Finding) -> &str { | ||
| f.extras.get("selector").and_then(|v| v.as_str()).unwrap_or("") | ||
| } | ||
|
|
||
| /// A glow or halo inside an `@media` block no 1280px viewport matches, on | ||
| /// an element the page shows, hides nothing: the cyan candidates report. | ||
| #[test] | ||
| fn pattern_candidates_in_inactive_at_rules_are_passed_over() { | ||
| let Some(engine) = engine() else { return }; | ||
| let port = serve(); | ||
| let findings = scan(&engine, port, "pattern-walk-inactive-at-rules.html"); | ||
| let rows: Vec<(&str, &str, &str)> = | ||
| findings.iter().map(|f| (f.antipattern.as_str(), selector(f), f.snippet.as_str())).collect(); | ||
| eprintln!("{rows:#?}"); | ||
| for rule in ["dark-glow", "radial-halo", "codex-grid-background"] { | ||
| assert!(rows.iter().any(|(r, ..)| *r == rule), "{rule} reports: {rows:#?}"); | ||
| } | ||
| assert!(!rows.iter().any(|(_, s, d)| s.contains("pass-") || d.contains("#ff00ff")), "{rows:#?}"); | ||
| } | ||
|
|
||
| /// Code in a full-width page shell at `overflow: auto` that runs past the | ||
| /// screen edge is cut there; the same line in a 560px scroller is not. | ||
| #[test] | ||
| fn code_in_a_scrolling_page_shell_is_read() { | ||
| let Some(engine) = engine() else { return }; | ||
| let port = serve(); | ||
| let findings = scan(&engine, port, "text-overflow-code-page-shell.html"); | ||
| let rows: Vec<&str> = findings.iter().filter(|f| f.antipattern == "text-overflow").map(selector).collect(); | ||
| eprintln!("{rows:#?}"); | ||
| assert!(rows.iter().any(|s| s.contains("flag-code-in-a-scrolling-shell")), "{rows:#?}"); | ||
| assert!(!rows.iter().any(|s| s.contains("pass-")), "{rows:#?}"); | ||
| } | ||
|
|
||
| /// The page's own part of the title, set huge and faint, is a heading to | ||
| /// read; the site's `og:site_name` set the same way is a wordmark. | ||
| #[test] | ||
| fn only_the_part_that_names_the_site_is_a_wordmark() { | ||
| let Some(engine) = engine() else { return }; | ||
| let port = serve(); | ||
| let findings = scan(&engine, port, "low-contrast-title-wordmark.html"); | ||
| let low: Vec<(&str, &str)> = findings | ||
| .iter() | ||
| .filter(|f| f.antipattern == "low-contrast") | ||
| .map(|f| (selector(f), f.severity.as_str())) | ||
| .collect(); | ||
| eprintln!("{low:#?}"); | ||
| let severity = |id: &str| low.iter().find(|(s, _)| s.contains(id)).map(|(_, sev)| *sev); | ||
| assert_eq!(severity("flag-faint-page-name"), Some("warning"), "{low:#?}"); | ||
| assert_eq!(severity("advisory-faint-site-name"), Some("advisory"), "{low:#?}"); | ||
| } |
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.