Skip to content

Commit 09b6024

Browse files
committed
test(browser): wait for the app's tab list to catch up with the new tab
The address of a tab a page opened reaches the app through events that wait while the tab is set up, so the app may show it a moment after the bot read the page. The second macOS run showed the tab without it yet.
1 parent 67eaa69 commit 09b6024

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

‎crates/botloftd/tests/browser.rs‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,29 @@ async fn new_tabs_and_dialogs_reach_the_bot_and_the_owner_watches() {
182182
.await
183183
.expect("new tab");
184184
assert!(tab.contains("Thanks, Tab"), "{tab}");
185-
let open = b.app.call("browser.list", Value::Null).await.expect("list");
186-
// The new tab is the active one, after the tab that opened it.
187-
let tabs = open[0]["tabs"].as_array().expect("tabs");
185+
// The new tab is the active one, after the tab that opened it. The app
186+
// may hear of its address a moment after the bot read the page: the
187+
// events that bring it wait while the new tab is set up.
188+
let deadline = Instant::now() + Duration::from_secs(5);
189+
let tabs = loop {
190+
let open = b.app.call("browser.list", Value::Null).await.expect("list");
191+
let tabs = open[0]["tabs"].as_array().expect("tabs").clone();
192+
let shown = tabs.len() == 2
193+
&& tabs[1]["url"]
194+
.as_str()
195+
.is_some_and(|url| url.contains("/done"));
196+
if shown || Instant::now() > deadline {
197+
break tabs;
198+
}
199+
tokio::time::sleep(Duration::from_millis(50)).await;
200+
};
188201
assert_eq!(tabs.len(), 2, "{tabs:?}");
189202
assert_eq!(tabs[0]["active"], false);
190203
assert_eq!(tabs[1]["active"], true);
191-
assert!(tabs[1]["url"].as_str().expect("url").contains("/done"));
204+
assert!(
205+
tabs[1]["url"].as_str().expect("url").contains("/done"),
206+
"{tabs:?}"
207+
);
192208

193209
let picture = b.mcp.tool_result("browser_screenshot", json!({})).await;
194210
assert_eq!(picture["content"][0]["type"], "image");

0 commit comments

Comments
 (0)