|
| 1 | +//! Exercise the Desktop registration used by startup, not only the shared handler. |
| 2 | +use super::*; |
| 3 | +use axum::{extract::Json, http::HeaderMap, routing::get, Router}; |
| 4 | +use openbitfun_core::agentic::tools::{ |
| 5 | + framework::{Tool, ToolResult, ToolUseContext}, |
| 6 | + implementations::PageManageTool, |
| 7 | +}; |
| 8 | +use serde_json::{json, Value}; |
| 9 | +use std::sync::atomic::AtomicBool; |
| 10 | + |
| 11 | +#[tokio::test] |
| 12 | +async fn desktop_page_manage_registration_reaches_the_executing_account() { |
| 13 | + // Registrations are process-wide OnceLocks. Isolate the production registry |
| 14 | + // and account state from other Desktop tests and from the developer's login. |
| 15 | + if std::env::var_os("OPENBITFUN_PAGE_TOOL_TEST_CHILD").is_none() { |
| 16 | + let root = tempfile::tempdir().unwrap(); |
| 17 | + let mut command = openbitfun_services_core::process_manager::create_tokio_command( |
| 18 | + std::env::current_exe().unwrap(), |
| 19 | + ); |
| 20 | + command.args([ |
| 21 | + "api::remote_connect_api::page_tool_tests::desktop_page_manage_registration_reaches_the_executing_account", |
| 22 | + "--exact", "--nocapture", |
| 23 | + ]) |
| 24 | + .env("OPENBITFUN_PAGE_TOOL_TEST_CHILD", "1") |
| 25 | + .env("OPENBITFUN_HOME", root.path()) |
| 26 | + .env("NO_PROXY", "127.0.0.1,localhost") |
| 27 | + .env("no_proxy", "127.0.0.1,localhost") |
| 28 | + .kill_on_drop(true); |
| 29 | + let output = tokio::time::timeout(std::time::Duration::from_secs(30), command.output()) |
| 30 | + .await |
| 31 | + .unwrap() |
| 32 | + .unwrap(); |
| 33 | + assert!( |
| 34 | + output.status.success(), |
| 35 | + "{}\n{}", |
| 36 | + String::from_utf8_lossy(&output.stdout), |
| 37 | + String::from_utf8_lossy(&output.stderr) |
| 38 | + ); |
| 39 | + assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed; 0 failed")); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 44 | + let relay_url = format!("http://{}", listener.local_addr().unwrap()); |
| 45 | + let page = json!({ |
| 46 | + "slug":"demo", "generation":"generation-a", "title":"Desktop Page", "visibility":"private", |
| 47 | + "file_count":1, "total_bytes":5, "created_at":1, "updated_at":2, |
| 48 | + "url_path":"/p/demo", "deployed_version_id":"v1", |
| 49 | + }); |
| 50 | + let change_account = Arc::new(AtomicBool::new(false)); |
| 51 | + let mutations = Arc::new(AtomicUsize::new(0)); |
| 52 | + let app = Router::new() |
| 53 | + .route( |
| 54 | + "/api/pages", |
| 55 | + get({ |
| 56 | + let page = page.clone(); |
| 57 | + let change_account = change_account.clone(); |
| 58 | + move |headers: HeaderMap| { |
| 59 | + let page = page.clone(); |
| 60 | + let change_account = change_account.clone(); |
| 61 | + async move { |
| 62 | + assert_eq!(headers["authorization"], "Bearer desktop-pages-token"); |
| 63 | + if change_account.swap(false, Ordering::SeqCst) { |
| 64 | + ACCOUNT_CONTEXT_GENERATION.fetch_add(1, Ordering::AcqRel); |
| 65 | + } |
| 66 | + Json(json!([page])) |
| 67 | + } |
| 68 | + } |
| 69 | + }), |
| 70 | + ) |
| 71 | + .route( |
| 72 | + "/api/pages/demo", |
| 73 | + axum::routing::patch({ |
| 74 | + let mutations = mutations.clone(); |
| 75 | + move |headers: HeaderMap, Json(request): Json<Value>| { |
| 76 | + let page = page.clone(); |
| 77 | + let mutations = mutations.clone(); |
| 78 | + async move { |
| 79 | + assert_eq!(headers["authorization"], "Bearer desktop-pages-token"); |
| 80 | + assert_eq!( |
| 81 | + request, |
| 82 | + json!({"expected_generation":"generation-a", "visibility":"public"}) |
| 83 | + ); |
| 84 | + mutations.fetch_add(1, Ordering::SeqCst); |
| 85 | + let mut page = page; |
| 86 | + page["visibility"] = json!("public"); |
| 87 | + Json(page) |
| 88 | + } |
| 89 | + } |
| 90 | + }), |
| 91 | + ); |
| 92 | + let server = tokio::spawn(async move { |
| 93 | + axum::serve(listener, app).await.unwrap(); |
| 94 | + }); |
| 95 | + *get_account_context().write().await = Some(AccountContextState { |
| 96 | + session: AccountSession::new("desktop-pages-token".into(), "desktop-user".into(), [0; 32]), |
| 97 | + relay_url: relay_url.clone(), |
| 98 | + }); |
| 99 | + sync_account_login_capability(true); |
| 100 | + register_page_tool_hosts(); |
| 101 | + let tool = PageManageTool::new(); |
| 102 | + let context = ToolUseContext { |
| 103 | + tool_call_id: None, |
| 104 | + agent_type: None, |
| 105 | + session_id: None, |
| 106 | + dialog_turn_id: None, |
| 107 | + workspace: None, |
| 108 | + loaded_deferred_tool_specs: Vec::new(), |
| 109 | + primary_model_facts: Default::default(), |
| 110 | + custom_data: HashMap::new(), |
| 111 | + computer_use_host: None, |
| 112 | + runtime_tool_restrictions: Default::default(), |
| 113 | + runtime_handles: Default::default(), |
| 114 | + }; |
| 115 | + assert!(tool.is_available_in_context(Some(&context)).await); |
| 116 | + let results = tool |
| 117 | + .call_impl(&json!({"action":"list"}), &context) |
| 118 | + .await |
| 119 | + .unwrap(); |
| 120 | + let ToolResult::Result { data, .. } = &results[0] else { |
| 121 | + panic!("expected a Page list") |
| 122 | + }; |
| 123 | + assert_eq!(data["pages"][0]["name"], "Desktop Page"); |
| 124 | + assert_eq!(data["pages"][0]["url"], format!("{relay_url}/p/demo")); |
| 125 | + assert_eq!(data["pages"][0]["visibility"], "private"); |
| 126 | + assert_eq!(data["pages"][0]["updated_at"], 2); |
| 127 | + |
| 128 | + let permissions = json!({"action":"permissions", "slug":"demo", "visibility":"public"}); |
| 129 | + tool.call_impl(&permissions, &context).await.unwrap(); |
| 130 | + assert_eq!(mutations.load(Ordering::SeqCst), 1); |
| 131 | + change_account.store(true, Ordering::SeqCst); |
| 132 | + assert!(tool |
| 133 | + .call_impl(&permissions, &context) |
| 134 | + .await |
| 135 | + .unwrap_err() |
| 136 | + .to_string() |
| 137 | + .contains("account context changed")); |
| 138 | + assert_eq!( |
| 139 | + mutations.load(Ordering::SeqCst), |
| 140 | + 1, |
| 141 | + "a stale account snapshot cannot continue to mutation" |
| 142 | + ); |
| 143 | + |
| 144 | + sync_account_login_capability(false); |
| 145 | + assert!(!tool.is_available_in_context(Some(&context)).await); |
| 146 | + assert!(tool |
| 147 | + .call_impl(&json!({"action":"list"}), &context) |
| 148 | + .await |
| 149 | + .unwrap_err() |
| 150 | + .to_string() |
| 151 | + .contains("logged-in")); |
| 152 | + server.abort(); |
| 153 | +} |
0 commit comments