Files
codex/codex-rs/tui/src/app_server_session/cloud_project_tests.rs
T
Benjamin Carlsson e9a2a9e115 Support saved environments when starting Cloud threads (#61956)
## What changed

- Add `EnvironmentSelection` to start Cloud threads with either a saved environment ID or a project checkout in the default environment.
- Expose saved environment discovery through the authenticated Cloud request handle, rejecting results if the account changes.
- Thread environment selection through the app-server client and TUI session startup, and defer executor readiness for either explicit environment choice.

## Testing

Extend prewarm request tests to cover saved environment IDs and deferred readiness. Extend TUI session tests to verify saved environment routing without local working-directory or workspace-root overrides.

GitOrigin-RevId: 626cec9926dad8f1a89b1a3ee9e4470850ae6a76
2026-11-17 03:17:05 +00:00

180 lines
6.3 KiB
Rust

#![cfg(test)]
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn github_origins_are_normalized_without_credentials_or_query_strings() {
for origin in [
"git@github.com:Org/Repo.git",
"ssh://git@github.com/Org/Repo.git",
"https://user:password@github.com/Org/Repo.git?token=secret#fragment",
] {
let project = github_project(origin).unwrap();
assert_eq!(
(project.name, project.origin_urls),
(
"repo".to_owned(),
vec!["https://github.com/org/repo".to_owned()]
),
);
}
}
#[tokio::test]
async fn cloud_starts_use_each_requests_host_project_without_caching() {
use super::super::AppServerSession;
use super::super::ThreadParamsMode;
use crate::legacy_core::config::ConfigBuilder;
use crate::local_settings::LocalSettings;
use codex_app_server_client::AppServerClient;
use codex_app_server_client::CloudClient;
use codex_app_server_client::CloudOperation;
use codex_app_server_client::EnvironmentSelection;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::JSONRPCErrorError;
let home = tempfile::tempdir().unwrap();
let config = ConfigBuilder::default()
.codex_home(home.path().to_path_buf())
.build()
.await
.unwrap();
let local_settings = LocalSettings::from(&config);
let first = tempfile::tempdir().unwrap();
let second = tempfile::tempdir().unwrap();
let misleading = tempfile::tempdir().unwrap();
for (directory, origin) in [
(first.path(), "git@github.com:org/first.git"),
(second.path(), "ssh://git@ssh.github.com:443/Org/Second.git"),
(
misleading.path(),
"https://example.com?target=@github.com/org/wrong",
),
] {
for args in [
vec!["init", "--quiet"],
vec!["remote", "add", "origin", origin],
] {
assert!(
tokio::process::Command::new("git")
.args(args)
.current_dir(directory)
.status()
.await
.unwrap()
.success()
);
}
}
let nested = first.path().join("src");
std::fs::create_dir(&nested).unwrap();
let projectless = tempfile::tempdir().unwrap();
let (client, mut commands, _events) = CloudClient::channel(/*capacity*/ 1);
let mut session = AppServerSession::new(
crate::backend_id::BackendId::new(),
AppServerClient::Cloud(client),
ThreadParamsMode::Cloud,
);
let worker = tokio::spawn(async move {
for expected in [
Some("first"),
Some("second"),
Some("saved"),
None,
None,
None,
None,
] {
let command = commands.recv().await.unwrap();
match (expected, command.operation) {
(
Some(expected),
CloudOperation::StartThreadWithEnvironment {
params,
environment: EnvironmentSelection::Project(project),
..
},
) => {
assert_eq!(project.name, expected);
assert_eq!(project.cwd, Some(format!("/workspace/org/{expected}")));
assert_eq!(params.cwd, None);
assert_eq!(params.runtime_workspace_roots, None);
assert_eq!(
project.origin_urls,
vec![format!("https://github.com/org/{expected}")]
);
}
(
Some("saved"),
CloudOperation::StartThreadWithEnvironment {
params,
environment: EnvironmentSelection::Saved(id),
..
},
) => {
assert_eq!(id, "ws~asenvcfg_saved");
assert_eq!(params.cwd, None);
assert_eq!(params.runtime_workspace_roots, None);
}
(None, CloudOperation::Request(request)) => {
assert!(matches!(*request, ClientRequest::ThreadStart { .. }))
}
_ => panic!("incorrect project routing"),
}
command
.response
.send(Ok(Err(JSONRPCErrorError {
code: -32000,
message: "checkout failed".into(),
data: None,
})))
.unwrap();
}
});
use super::super::CloudThreadStart::Project;
use super::super::CloudThreadStart::Saved;
for (mode, environment) in [
(ThreadParamsMode::Cloud, Project(Some(nested.as_path()))),
(ThreadParamsMode::Cloud, Project(Some(second.path()))),
(ThreadParamsMode::Cloud, Saved("ws~asenvcfg_saved".into())),
(ThreadParamsMode::Cloud, Project(Some(misleading.path()))),
(ThreadParamsMode::Cloud, Project(Some(projectless.path()))),
(ThreadParamsMode::Cloud, Project(None)),
(ThreadParamsMode::Remote, Project(Some(first.path()))),
] {
session.thread_params_mode = mode;
let result = session
.start_thread_with_environment(
&local_settings,
&config,
/*session_start_source*/ None,
/*remote_cwd_override*/ None,
/*selected_profile*/ None,
environment,
)
.await;
assert!(result.unwrap_err().to_string().contains("checkout failed"));
}
worker.await.unwrap();
}
#[test]
fn unsupported_or_non_repository_origins_leave_the_thread_blank() {
for origin in [
"/local/repository",
"github.com/org/repo",
"https://git.example.com/org/repo",
"http://ssh.github.com:443/org/repo.git",
"ssh://git@ssh.github.com:8443/org/repo.git",
"https://github.com/org",
"https://github.com/org/repo/tree/main",
"https://github.com/org/repo%2fother",
] {
assert!(
github_project(origin).is_none(),
"unexpected origin: {origin}"
);
}
}