Repository navigation
feat(bus): identity fields in SessionOptions (contract 2.20) #112
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
+566
−18
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
147 changes: 147 additions & 0 deletions
147
crates/tinycomputer-browser/src/sessions/sessions_tests/identity_tests.rs
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,147 @@ | ||
| //! Tests for the identity a session asks its browser to present: the launch | ||
| //! command carries it, and a real browser shows it to the page (gated on the | ||
| //! `agent-browser` feature and `TINYCOMPUTER_LIVE_BROWSER=1`, since CI has no | ||
| //! browser to launch). | ||
|
|
||
| use tinycomputer_bus::browser::{Screen, SessionOptions, UserAgentBrand, UserAgentMetadata}; | ||
|
|
||
| use crate::fake::Fake; | ||
| use crate::sessions::Browser; | ||
|
|
||
| use super::scratch; | ||
|
|
||
| fn tokyo() -> SessionOptions { | ||
| SessionOptions { | ||
| timezone: Some("Asia/Tokyo".to_owned()), | ||
| locale: Some("ja-JP".to_owned()), | ||
| accept_language: Some("ja-JP,ja".to_owned()), | ||
| user_agent: Some( | ||
| "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like \ | ||
| Gecko) Chrome/140.0.0.0 Safari/537.36" | ||
| .to_owned(), | ||
| ), | ||
| user_agent_metadata: Some(UserAgentMetadata { | ||
| brands: Some(vec![UserAgentBrand { | ||
| brand: "Chromium".to_owned(), | ||
| version: "140".to_owned(), | ||
| }]), | ||
| full_version_list: None, | ||
| full_version: Some("140.0.0.0".to_owned()), | ||
| platform: "macOS".to_owned(), | ||
| platform_version: "14.5.0".to_owned(), | ||
| architecture: "arm".to_owned(), | ||
| model: String::new(), | ||
| mobile: false, | ||
| bitness: Some("64".to_owned()), | ||
| wow64: None, | ||
| }), | ||
| screen: Some(Screen { | ||
| width: 1920, | ||
| height: 1080, | ||
| device_scale_factor: 2.0, | ||
| }), | ||
| init_scripts: vec!["window.fromInitScript = 'ran first';".to_owned()], | ||
| stealth: true, | ||
| ..SessionOptions::default() | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn opening_a_session_hands_the_engine_every_identity_field_in_its_launch() { | ||
| let fake = Fake::new(); | ||
| let browser = Browser::with_scratch(std::sync::Arc::new(fake.clone()), scratch("identity")); | ||
| browser.open_session(tokyo()).await.unwrap(); | ||
|
|
||
| let launch = fake.last("launch"); | ||
| assert_eq!(launch["timezone"], "Asia/Tokyo"); | ||
| assert_eq!(launch["locale"], "ja-JP"); | ||
| assert_eq!(launch["acceptLanguage"], "ja-JP,ja"); | ||
| assert_eq!(launch["userAgentMetadata"]["platform"], "macOS"); | ||
| assert_eq!(launch["screen"]["width"], 1920); | ||
| assert_eq!( | ||
| launch["initScriptSources"][0], | ||
| "window.fromInitScript = 'ran first';" | ||
| ); | ||
| assert_eq!(launch["stealth"], true); | ||
| } | ||
|
|
||
| /// A page on `localhost`, served to the first few requests. | ||
| #[cfg(feature = "agent-browser")] | ||
| fn serve_a_page() -> String { | ||
| use std::io::{Read, Write}; | ||
|
|
||
| let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); | ||
| let port = listener.local_addr().unwrap().port(); | ||
| std::thread::spawn(move || { | ||
| for stream in listener.incoming().take(8) { | ||
| let Ok(mut stream) = stream else { continue }; | ||
| let mut request = [0_u8; 2048]; | ||
| let _ = stream.read(&mut request); | ||
| let body = "<!doctype html><title>identity</title><p>identity</p>"; | ||
| let _ = write!( | ||
| stream, | ||
| "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", | ||
| body.len() | ||
| ); | ||
| } | ||
| }); | ||
| format!("http://localhost:{port}/") | ||
| } | ||
|
|
||
| #[cfg(feature = "agent-browser")] | ||
| #[tokio::test] | ||
| async fn live_a_page_sees_the_identity_its_session_was_opened_with() { | ||
| use std::sync::Arc; | ||
|
|
||
| use serde_json::json; | ||
|
|
||
| if std::env::var("TINYCOMPUTER_LIVE_BROWSER").as_deref() != Ok("1") { | ||
| return; | ||
| } | ||
| let browser = Browser::new(Arc::new(crate::AgentBrowser)); | ||
| let options = SessionOptions { | ||
| executable: std::env::var("TINYCOMPUTER_BROWSER_EXECUTABLE").ok(), | ||
| ..tokyo() | ||
| }; | ||
| let info = browser | ||
| .open_session(options) | ||
| .await | ||
| .expect("a browser launches when live runs are asked for"); | ||
| // Init scripts run in a document loaded after launch, not the blank tab, | ||
| // and client hints exist only in a secure context, which a page on | ||
| // localhost is and a `data:` page is not. | ||
| let url = serve_a_page(); | ||
| browser | ||
| .command(&info.id, json!({"action": "navigate", "url": url})) | ||
| .await | ||
| .expect("the page loads"); | ||
| let read = |expression: &'static str| { | ||
| let browser = &browser; | ||
| let id = &info.id; | ||
| async move { | ||
| browser | ||
| .command(id, json!({"action": "evaluate", "script": expression})) | ||
| .await | ||
| .expect("the expression runs")["result"] | ||
| .clone() | ||
| } | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| read("Intl.DateTimeFormat().resolvedOptions().timeZone").await, | ||
| "Asia/Tokyo" | ||
| ); | ||
| assert_eq!(read("navigator.language").await, "ja-JP"); | ||
| assert_eq!(read("navigator.languages.join(',')").await, "ja-JP,ja"); | ||
| assert_eq!(read("navigator.userAgentData.platform").await, "macOS"); | ||
| assert_eq!( | ||
| read("navigator.userAgentData.brands.some(b => b.brand === 'Chromium' && b.version === '140')") | ||
| .await, | ||
| true | ||
| ); | ||
| assert_eq!(read("screen.width").await, 1920); | ||
| assert_eq!(read("screen.height").await, 1080); | ||
| assert_eq!(read("window.devicePixelRatio").await, 2); | ||
| assert_eq!(read("window.fromInitScript").await, "ran first"); | ||
| browser.close_session(&info.id).await.unwrap(); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Do not apply a launch-only screen setting to attached browsers.
If
endpointandscreenare both set,Browser::open_sessionstill sends this viewport command. The attached browser therefore receivesscreen.device_scale_factor, althoughSessionOptions.screenand the contract documentation say screen settings apply only to launched browsers. Keep the viewport’s own scale factor whenendpointis set. (raw.githubusercontent.com)🤖 Prompt for AI Agents
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Report the effective scale factor in
SessionInfo.viewport.If a launched session has a 1× viewport and a 2× screen, this command applies 2×.
Browser::open_sessionstill copies the 1×options.viewportintoSessionInfo.viewport, whichOpenSessionandListSessionsreport as the rendered viewport. Set the reported scale factor to the value sent in the viewport command. (raw.githubusercontent.com)🤖 Prompt for AI Agents