дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
Порівняти коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
ce4c723280 | ||
|
|
88201ce450 | ||
|
|
b3abc8edd1 |
No files matched your search
@@ -51,6 +51,7 @@ uuid.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
ide-ssh = { workspace = true, features = ["testing"] }
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "io-util", "test-util"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
|
||||
@@ -91,6 +91,7 @@ mod tests {
|
||||
let host = Host::new(HostConfig {
|
||||
config_dir: base.join("config"),
|
||||
data_dir: base.join("data"),
|
||||
local_data_dir: base.join("data"),
|
||||
languages: Arc::new(ide_language::LanguageRegistry::load(None).unwrap()),
|
||||
syntax: Arc::default(),
|
||||
sink_for: Box::new(|_| Arc::new(|_| {})),
|
||||
|
||||
@@ -222,6 +222,7 @@ pub fn restricted_command_allowed(cmd: &str) -> bool {
|
||||
| "index_status"
|
||||
| "jvm_file_presentations"
|
||||
| "deploy_coverage"
|
||||
| "deploy_test_connection"
|
||||
| "find_files"
|
||||
| "find_symbols"
|
||||
| "mark_recent"
|
||||
@@ -243,6 +244,7 @@ pub fn restricted_command_allowed(cmd: &str) -> bool {
|
||||
| "remote_current"
|
||||
| "remote_disconnect"
|
||||
| "remote_connect"
|
||||
| "remote_bootstrap_list"
|
||||
| "remote_list"
|
||||
| "remote_open"
|
||||
| "remote_of"
|
||||
|
||||
@@ -56,6 +56,8 @@ async fn application_commands_work_in_restricted_projects_and_without_a_project(
|
||||
"db_console_path",
|
||||
"db_export_file",
|
||||
"vcs_write_patch",
|
||||
"remote_bootstrap_list",
|
||||
"deploy_test_connection",
|
||||
] {
|
||||
assert!(ide_host::trust::restricted_command_allowed(command), "{command}");
|
||||
for window in ["main", "welcome"] {
|
||||
@@ -75,6 +77,15 @@ async fn application_commands_work_in_restricted_projects_and_without_a_project(
|
||||
"vcs_git_version",
|
||||
"docker_compose_op",
|
||||
"db_execute",
|
||||
"remote_delete",
|
||||
"remote_mkdir",
|
||||
"remote_rename",
|
||||
"remote_import_local",
|
||||
"remote_bootstrap_exec",
|
||||
"deploy_upload",
|
||||
"deploy_download",
|
||||
"deploy_delete_remote",
|
||||
"deploy_sync",
|
||||
"unknown_command",
|
||||
] {
|
||||
assert!(!ide_host::trust::restricted_command_allowed(command), "{command}");
|
||||
@@ -109,6 +120,55 @@ async fn application_commands_work_in_restricted_projects_and_without_a_project(
|
||||
host.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn welcome_window_browses_selected_ssh_host_without_executing_commands() {
|
||||
use ide_core::remote_settings::{SshAuth, SshConfiguration};
|
||||
use ide_ssh::{FsConnection, SshFiles, SshManager};
|
||||
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let server_root = tmp.path().join("server");
|
||||
std::fs::create_dir_all(server_root.join("projects/demo")).unwrap();
|
||||
std::fs::write(server_root.join("projects/README.md"), "remote file").unwrap();
|
||||
let port = ide_ssh::testing::sshd(server_root, Arc::new(|cmd| panic!("unexpected SSH exec: {cmd}"))).await;
|
||||
let ssh = Arc::new(SshManager::with_files(
|
||||
tmp.path().join("secrets.json"),
|
||||
SshFiles { known_hosts: tmp.path().join("known_hosts"), config: None },
|
||||
));
|
||||
ssh.set_configs(vec![SshConfiguration {
|
||||
id: "selected-host".into(),
|
||||
host: "127.0.0.1".into(),
|
||||
port: Some(port),
|
||||
user: "dev".into(),
|
||||
auth: SshAuth::Password,
|
||||
..Default::default()
|
||||
}]);
|
||||
ssh.set_secret("selected-host", Some("secret".into()), false);
|
||||
assert!(ssh.session("selected-host").await.is_err());
|
||||
ssh.trust_host("selected-host").await.unwrap();
|
||||
|
||||
let mut host = host(&tmp.path().join("app"), false);
|
||||
let state = Arc::get_mut(&mut host).unwrap();
|
||||
state.ssh = ssh;
|
||||
state.remote = Arc::new(ide_remote::RemoteService::new(state.ssh.clone(), tmp.path().join("cache")));
|
||||
assert!(host.project("welcome").is_err());
|
||||
host.authorize_command("welcome", "remote_bootstrap_list").unwrap();
|
||||
let connection = FsConnection::Sftp { ssh: "selected-host".into() };
|
||||
let entries = host.remote.list(&connection, "/projects").await.unwrap();
|
||||
assert!(entries.iter().any(|e| e.path == "/projects/demo" && e.is_dir));
|
||||
assert!(entries.iter().any(|e| e.name == "README.md" && !e.is_dir));
|
||||
assert!(host.remote.list(&connection, "/projects/demo").await.unwrap().is_empty());
|
||||
host.authorize_command("welcome", "deploy_test_connection").unwrap();
|
||||
host.remote.test_connection(&connection, "/projects").await.unwrap();
|
||||
let local = tmp.path().join("local-deployment");
|
||||
std::fs::create_dir(&local).unwrap();
|
||||
ide_host::ops::deploy::deploy_test_connection(&host, FsConnection::Local, local.to_str().unwrap().into())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read_dir(local).unwrap().count(), 0, "write probe is cleaned up");
|
||||
assert!(host.project("welcome").is_err());
|
||||
host.shutdown().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persists_user_decisions_locations_revocation_and_one_time_migration() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -2,6 +2,29 @@
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Platform adapters supply the system's tab preference and active project state.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TabbingPreference {
|
||||
Always,
|
||||
InFullScreen,
|
||||
Manually,
|
||||
}
|
||||
|
||||
pub fn should_group_new_project(
|
||||
preference: TabbingPreference,
|
||||
fullscreen: bool,
|
||||
active_project: bool,
|
||||
light_edit: bool,
|
||||
) -> bool {
|
||||
active_project
|
||||
&& !light_edit
|
||||
&& match preference {
|
||||
TabbingPreference::Always => true,
|
||||
TabbingPreference::InFullScreen => fullscreen,
|
||||
TabbingPreference::Manually => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct WindowGroup {
|
||||
pub members: Vec<String>,
|
||||
@@ -39,6 +62,13 @@ impl WindowGroups {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adds a window to the anchor's group without merging unrelated groups.
|
||||
pub fn join(&mut self, anchor: &str, member: &str) -> IdeResult<()> {
|
||||
let mut members = self.group(anchor).map(|g| g.members.clone()).unwrap_or_else(|| vec![anchor.to_owned()]);
|
||||
members.push(member.to_owned());
|
||||
self.merge(members, anchor)
|
||||
}
|
||||
|
||||
pub fn select(&mut self, caller: &str, target: &str) -> IdeResult<WindowGroup> {
|
||||
let group = self
|
||||
.groups
|
||||
@@ -94,6 +124,45 @@ mod tests {
|
||||
values.iter().map(|s| (*s).into()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_project_grouping_follows_system_preference_and_excludes_light_edit() {
|
||||
for preference in [TabbingPreference::Always, TabbingPreference::InFullScreen, TabbingPreference::Manually] {
|
||||
for fullscreen in [false, true] {
|
||||
for active_project in [false, true] {
|
||||
for light_edit in [false, true] {
|
||||
let expected = matches!(
|
||||
(preference, fullscreen, active_project, light_edit),
|
||||
(TabbingPreference::Always, _, true, false)
|
||||
| (TabbingPreference::InFullScreen, true, true, false)
|
||||
);
|
||||
assert_eq!(
|
||||
should_group_new_project(preference, fullscreen, active_project, light_edit),
|
||||
expected,
|
||||
"{preference:?}, fullscreen={fullscreen}, active={active_project}, light={light_edit}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn joining_new_windows_preserves_the_active_group_and_other_groups() {
|
||||
let mut groups = WindowGroups::default();
|
||||
groups.merge(members(&["other-a", "other-b"]), "other-b").unwrap();
|
||||
groups.join("a", "b").unwrap();
|
||||
assert_eq!(groups.group("a").unwrap().members, members(&["a", "b"]));
|
||||
let before = groups.select("a", "b").unwrap();
|
||||
assert_eq!(before.selected, "a");
|
||||
groups.join("b", "c").unwrap();
|
||||
let before = groups.select("b", "c").unwrap();
|
||||
assert_eq!(before.selected, "b");
|
||||
assert_eq!(groups.group("c").unwrap().members, members(&["a", "b", "c"]));
|
||||
assert_eq!(groups.group("c").unwrap().selected, "c");
|
||||
assert_eq!(groups.group("other-a").unwrap().members, members(&["other-a", "other-b"]));
|
||||
assert_eq!(groups.group("other-a").unwrap().selected, "other-b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selecting_and_detaching_preserve_the_other_sessions() {
|
||||
let mut groups = WindowGroups::default();
|
||||
|
||||
@@ -11,6 +11,8 @@ mod events;
|
||||
#[cfg(any(target_os = "linux", test))]
|
||||
mod linux_window_state;
|
||||
mod logging;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos;
|
||||
mod menu;
|
||||
mod remote_dev;
|
||||
|
||||
@@ -172,7 +174,10 @@ pub fn run() {
|
||||
tauri::WindowEvent::Focused(false) => application_focus::changed(window.app_handle(), false),
|
||||
tauri::WindowEvent::Destroyed => {
|
||||
#[cfg(target_os = "macos")]
|
||||
titlebar::forget(window);
|
||||
{
|
||||
titlebar::forget(window);
|
||||
macos::forget_fullscreen(window.label());
|
||||
}
|
||||
window_groups::destroyed(window.app_handle(), window.label());
|
||||
if let Some(settings) = window.app_handle().get_webview_window(&windows::settings_label(window.label())) {
|
||||
let _ = settings.destroy();
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
//! macOS window tab preferences, applied to application-drawn project tabs.
|
||||
use crate::windows::{Launch, WindowsState};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use ide_workspace::window_groups::{TabbingPreference, should_group_new_project};
|
||||
use objc2::{MainThreadMarker, rc::Retained, runtime::ProtocolObject};
|
||||
use objc2_app_kit::{NSWindow, NSWindowUserTabbingPreference};
|
||||
use objc2_foundation::{NSNotificationCenter, NSObjectProtocol};
|
||||
use tauri::{AppHandle, Manager, WebviewWindow};
|
||||
|
||||
fn tabbing_preference(app: &AppHandle) -> IdeResult<TabbingPreference> {
|
||||
fn read(mtm: MainThreadMarker) -> TabbingPreference {
|
||||
match NSWindow::userTabbingPreference(mtm) {
|
||||
NSWindowUserTabbingPreference::Always => TabbingPreference::Always,
|
||||
NSWindowUserTabbingPreference::InFullScreen => TabbingPreference::InFullScreen,
|
||||
_ => TabbingPreference::Manually,
|
||||
}
|
||||
}
|
||||
if let Some(mtm) = MainThreadMarker::new() {
|
||||
return Ok(read(mtm));
|
||||
}
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
app.run_on_main_thread(move || {
|
||||
let _ = tx.send(read(MainThreadMarker::new().expect("AppKit main thread")));
|
||||
})
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
rx.recv().map_err(|e| IdeError::internal(e.to_string()))
|
||||
}
|
||||
|
||||
/// Capture before building the new window, which takes focus itself.
|
||||
pub fn new_project_tab_target(app: &AppHandle, launch: &Launch) -> IdeResult<Option<WebviewWindow>> {
|
||||
let windows = app.webview_windows();
|
||||
let active = windows
|
||||
.values()
|
||||
.find(|w| w.is_focused().unwrap_or(false))
|
||||
.cloned()
|
||||
.or_else(|| app.state::<WindowsState>().last_focused().and_then(|label| windows.get(&label).cloned()));
|
||||
let active = active.and_then(|w| {
|
||||
let label = crate::windows::settings_parent(w.label()).unwrap_or(w.label());
|
||||
windows.get(label).cloned()
|
||||
});
|
||||
let active = active.filter(|w| {
|
||||
let host = app.state::<crate::state::AppState>();
|
||||
host.project(w.label()).is_ok_and(|p| !p.light) || host.remote_dev.get(w.label()).is_some()
|
||||
});
|
||||
let fullscreen = active.as_ref().is_some_and(|w| w.is_fullscreen().unwrap_or(false));
|
||||
let light_edit = launch.project.is_none() && launch.remote.is_none();
|
||||
Ok(should_group_new_project(tabbing_preference(app)?, fullscreen, active.is_some(), light_edit)
|
||||
.then_some(active)
|
||||
.flatten())
|
||||
}
|
||||
|
||||
struct FullscreenObserver(Retained<ProtocolObject<dyn NSObjectProtocol>>);
|
||||
|
||||
impl Drop for FullscreenObserver {
|
||||
fn drop(&mut self) {
|
||||
let observer: &ProtocolObject<dyn NSObjectProtocol> = &self.0;
|
||||
unsafe { NSNotificationCenter::defaultCenter().removeObserver(observer.as_ref()) };
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static FULLSCREEN_TRANSFERS: std::cell::RefCell<std::collections::HashMap<String, FullscreenObserver>> = std::cell::RefCell::default();
|
||||
}
|
||||
|
||||
pub fn forget_fullscreen(label: &str) {
|
||||
FULLSCREEN_TRANSFERS.with_borrow_mut(|observers| observers.remove(label));
|
||||
}
|
||||
|
||||
/// AppKit cannot enter another window's fullscreen Space until the old one has exited.
|
||||
pub fn transfer_fullscreen(from: &WebviewWindow, to: &WebviewWindow) -> tauri::Result<()> {
|
||||
let source = from.label().to_owned();
|
||||
let to = to.clone();
|
||||
from.with_webview(move |webview| unsafe {
|
||||
let native = &*webview.ns_window().cast::<NSWindow>();
|
||||
forget_fullscreen(&source);
|
||||
let label = source.clone();
|
||||
let block = block2::RcBlock::new(move |_| {
|
||||
forget_fullscreen(&label);
|
||||
let app = to.app_handle();
|
||||
let selected = app.state::<WindowsState>().groups.lock().group(to.label()).map(|g| g.selected.clone());
|
||||
if let Some(selected) = selected {
|
||||
// Exiting fullscreen can show the old window again; reapply group visibility.
|
||||
let _ = crate::window_groups::select(app, &selected, &selected);
|
||||
if selected == to.label() {
|
||||
let _ = to.set_fullscreen(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
let observer = NSNotificationCenter::defaultCenter().addObserverForName_object_queue_usingBlock(
|
||||
Some(objc2_app_kit::NSWindowDidExitFullScreenNotification),
|
||||
Some(native),
|
||||
None,
|
||||
&block,
|
||||
);
|
||||
FULLSCREEN_TRANSFERS.with_borrow_mut(|observers| observers.insert(source, FullscreenObserver(observer)));
|
||||
native.toggleFullScreen(None);
|
||||
})
|
||||
}
|
||||
@@ -51,8 +51,13 @@ fn copy_frame(from: &WebviewWindow, to: &WebviewWindow) -> IdeResult<()> {
|
||||
let err = |e: tauri::Error| IdeError::internal(e.to_string());
|
||||
let fullscreen = from.is_fullscreen().map_err(err)?;
|
||||
if fullscreen {
|
||||
from.set_fullscreen(false).map_err(err)?;
|
||||
to.set_fullscreen(true).map_err(err)?;
|
||||
#[cfg(target_os = "macos")]
|
||||
crate::macos::transfer_fullscreen(from, to).map_err(err)?;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
from.set_fullscreen(false).map_err(err)?;
|
||||
to.set_fullscreen(true).map_err(err)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
to.set_fullscreen(false).map_err(err)?;
|
||||
@@ -109,6 +114,13 @@ pub fn select(app: &AppHandle, caller: &str, target: &str) -> IdeResult<()> {
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn join(app: &AppHandle, anchor: &str, new_window: &str) -> IdeResult<()> {
|
||||
let state = app.state::<WindowsState>();
|
||||
state.groups.lock().join(anchor, new_window)?;
|
||||
select(app, anchor, new_window)
|
||||
}
|
||||
|
||||
pub fn merge(app: &AppHandle, caller: &str) -> IdeResult<()> {
|
||||
let members = windows::project_window_labels(app)
|
||||
.into_iter()
|
||||
@@ -235,7 +247,7 @@ pub async fn restore(app: &AppHandle, caller: &str) -> IdeResult<()> {
|
||||
} else if path.is_dir() {
|
||||
let window = match windows::find_project_window(app, path) {
|
||||
Some(w) => w,
|
||||
None => windows::create_window(app, Some(path.clone()))?,
|
||||
None => windows::create_restored_window(app, path.clone())?,
|
||||
};
|
||||
Some(window.label().to_owned())
|
||||
} else {
|
||||
|
||||
@@ -357,18 +357,35 @@ pub fn create_main_window(app: &AppHandle) -> IdeResult<WebviewWindow> {
|
||||
|
||||
/// Creates a new project window; `project` is opened by its frontend on load.
|
||||
pub fn create_window(app: &AppHandle, project: Option<PathBuf>) -> IdeResult<WebviewWindow> {
|
||||
create_window_with(app, project, Vec::new())
|
||||
create_window_with(app, project, Vec::new(), false)
|
||||
}
|
||||
|
||||
/// [`create_window`] that also opens `files` (without `project`: in LightEdit).
|
||||
fn create_window_with(app: &AppHandle, project: Option<PathBuf>, files: Vec<PathBuf>) -> IdeResult<WebviewWindow> {
|
||||
fn create_window_with(
|
||||
app: &AppHandle,
|
||||
project: Option<PathBuf>,
|
||||
files: Vec<PathBuf>,
|
||||
restoring: bool,
|
||||
) -> IdeResult<WebviewWindow> {
|
||||
let title = project.as_deref().or(files.first().map(PathBuf::as_path)).and_then(Path::file_name);
|
||||
let title = title.map(|n| n.to_string_lossy().into_owned());
|
||||
let files = files.into_iter().map(Into::into).collect();
|
||||
create_window_for(app, title, Launch { project: project.map(Into::into), files, ..Default::default() })
|
||||
create_window_for(app, title, Launch { project: project.map(Into::into), files, ..Default::default() }, restoring)
|
||||
}
|
||||
|
||||
fn create_window_for(app: &AppHandle, title: Option<String>, launch: Launch) -> IdeResult<WebviewWindow> {
|
||||
/// Restoring a saved group preserves its membership and selected tab.
|
||||
pub fn create_restored_window(app: &AppHandle, project: PathBuf) -> IdeResult<WebviewWindow> {
|
||||
create_window_with(app, Some(project), Vec::new(), true)
|
||||
}
|
||||
|
||||
fn create_window_for(
|
||||
app: &AppHandle,
|
||||
title: Option<String>,
|
||||
launch: Launch,
|
||||
_restoring: bool,
|
||||
) -> IdeResult<WebviewWindow> {
|
||||
#[cfg(target_os = "macos")]
|
||||
let tab_target = if _restoring { None } else { crate::macos::new_project_tab_target(app, &launch)? };
|
||||
let windows = app.webview_windows();
|
||||
let label = next_label(windows.keys().map(String::as_str));
|
||||
let mut config = window_config(app)?;
|
||||
@@ -388,6 +405,11 @@ fn create_window_for(app: &AppHandle, title: Option<String>, launch: Launch) ->
|
||||
}
|
||||
app.state::<WindowsState>().set_launch(&label, launch);
|
||||
let window = build_project_window(app, config)?;
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(target) = tab_target {
|
||||
crate::window_groups::join(app, target.label(), window.label())?;
|
||||
return Ok(window);
|
||||
}
|
||||
focus(&window);
|
||||
Ok(window)
|
||||
}
|
||||
@@ -401,7 +423,7 @@ pub fn spawn_window(app: &AppHandle, project: Option<PathBuf>) {
|
||||
fn spawn_window_with(app: &AppHandle, project: Option<PathBuf>, files: Vec<PathBuf>) {
|
||||
let app = app.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
if let Err(e) = create_window_with(&app, project, files) {
|
||||
if let Err(e) = create_window_with(&app, project, files, false) {
|
||||
tracing::warn!("cannot open window: {}", e.message);
|
||||
}
|
||||
});
|
||||
@@ -440,7 +462,7 @@ pub fn open_project_window(
|
||||
if !new_window {
|
||||
return Ok(OpenOutcome::Current);
|
||||
}
|
||||
let window = create_window_with(app, Some(root), files)?;
|
||||
let window = create_window_with(app, Some(root), files, false)?;
|
||||
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
|
||||
}
|
||||
|
||||
@@ -450,7 +472,7 @@ pub fn open_project_window(
|
||||
/// detach an editor tab out of its window.
|
||||
pub fn open_file_in_new_window(app: &AppHandle, project: Option<PathBuf>, file: PathBuf) -> IdeResult<OpenOutcome> {
|
||||
tracing::info!("open_file_in_new_window {} project={:?}", file.display(), project);
|
||||
let window = create_window_with(app, project, vec![file])?;
|
||||
let window = create_window_with(app, project, vec![file], false)?;
|
||||
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
|
||||
}
|
||||
|
||||
@@ -478,7 +500,7 @@ pub fn open_remote_window(app: &AppHandle, ssh_config: &str, path: &str, new_win
|
||||
}
|
||||
let title = path.trim_end_matches('/').rsplit('/').next().filter(|n| !n.is_empty() && *n != "~").map(str::to_owned);
|
||||
let remote = RemoteLaunch { ssh_config: ssh_config.to_owned(), path: path.to_owned() };
|
||||
let window = create_window_for(app, title, Launch { remote: Some(remote), ..Default::default() })?;
|
||||
let window = create_window_for(app, title, Launch { remote: Some(remote), ..Default::default() }, false)?;
|
||||
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { basename, dirname, useProject } from "@/platform/project";
|
||||
import { isRemoteWindow } from "@/platform/remote";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { Button, Dialog, Icon, IconButton, Input, T } from "@/ui";
|
||||
import { openProjectInWindow } from "./windows";
|
||||
import { openProjectWithPolicy } from "./windows";
|
||||
import styles from "./WorkspaceDialog.module.css";
|
||||
|
||||
const useWorkspaceDialog = create<{ mode: "create" | "manage" | null }>(() => ({ mode: null }));
|
||||
@@ -53,7 +53,7 @@ function WorkspaceDialog({ mode, onClose }: { mode: "create" | "manage"; onClose
|
||||
const included = adding.trim() ? [...new Set([...projects, adding.trim()])] : projects;
|
||||
if (mode === "create") {
|
||||
const root = await call(commands.workspaceCreate(path.trim(), name.trim(), included));
|
||||
await openProjectInWindow(root, !!project);
|
||||
await openProjectWithPolicy(root);
|
||||
} else {
|
||||
const updated = await call(commands.workspaceUpdate(name.trim(), included));
|
||||
useProject.setState({ project: updated, settings: updated.settings });
|
||||
|
||||
@@ -3,13 +3,14 @@ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { commands } from "@/platform/ipc";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { WorkspaceDialogHost, showWorkspaceDialog } from "../WorkspaceDialog";
|
||||
import { openProjectInWindow } from "../windows";
|
||||
import { openProjectWithPolicy } from "../windows";
|
||||
|
||||
vi.mock("../windows", () => ({ openProjectInWindow: vi.fn(async () => {}) }));
|
||||
vi.mock("../windows", () => ({ openProjectWithPolicy: vi.fn(async () => {}) }));
|
||||
vi.mock("@/platform/remote", () => ({ isRemoteWindow: () => false }));
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.mocked(openProjectWithPolicy).mockClear();
|
||||
useProject.setState({ project: null, settings: null, lightEdit: false });
|
||||
});
|
||||
|
||||
@@ -21,7 +22,21 @@ it("creates a workspace from the pending project field without an extra Add clic
|
||||
fireEvent.change(screen.getByLabelText("Project folder path"), { target: { value: "/work/frontend" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: /^Create$/ }));
|
||||
await waitFor(() => expect(save).toHaveBeenCalledWith("/work/full", "Workspace", ["/work/frontend"]));
|
||||
expect(openProjectInWindow).toHaveBeenCalledWith("/work/full", false);
|
||||
expect(openProjectWithPolicy).toHaveBeenCalledWith("/work/full");
|
||||
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
||||
});
|
||||
|
||||
it("uses the shared opening policy after creation from an already open project", async () => {
|
||||
useProject.setState({
|
||||
project: { root: "/work/frontend", name: "frontend", trusted: true, isGit: false, settings: {} },
|
||||
settings: { general: { openProjectIn: "ask" } },
|
||||
} as never);
|
||||
vi.spyOn(commands, "workspaceCreate").mockResolvedValue({ status: "ok", data: "/work/full" });
|
||||
showWorkspaceDialog("create");
|
||||
render(<WorkspaceDialogHost />);
|
||||
fireEvent.change(screen.getByLabelText("Workspace location"), { target: { value: "/work/full" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: /^Create$/ }));
|
||||
await waitFor(() => expect(openProjectWithPolicy).toHaveBeenCalledWith("/work/full"));
|
||||
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import type { RemoteEntry, Settings } from "@/platform/ipc";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { useRemoteProgress } from "@/platform/remote";
|
||||
import { ConnectHost } from "./ConnectDialog";
|
||||
import { closeConnectDialog, openConnectDialog, useConnectDialog } from "./model";
|
||||
|
||||
const { list, openRemote, executeAction } = vi.hoisted(() => ({
|
||||
list: vi.fn(),
|
||||
openRemote: vi.fn(),
|
||||
executeAction: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
return { ...real, commands: { ...real.commands, remoteBootstrapList: list } };
|
||||
});
|
||||
vi.mock("@/workbench/projectWindow", () => ({ openRemoteProject: openRemote }));
|
||||
vi.mock("@/platform/actions", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@/platform/actions")>()),
|
||||
executeAction,
|
||||
}));
|
||||
|
||||
const entry = (path: string, isDir = true): RemoteEntry => ({
|
||||
name: path.slice(path.lastIndexOf("/") + 1),
|
||||
path,
|
||||
isDir,
|
||||
isSymlink: false,
|
||||
size: 0,
|
||||
mtime: null,
|
||||
permissions: "rwxr-xr-x",
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
useProject.setState({
|
||||
project: null,
|
||||
opening: false,
|
||||
settings: {
|
||||
ssh: {
|
||||
configs: [
|
||||
{ id: "first", name: "First host", host: "first.example", user: "dev" },
|
||||
{ id: "selected", name: "Selected host", host: "selected.example", user: "dev" },
|
||||
],
|
||||
},
|
||||
} as Settings,
|
||||
});
|
||||
useRemoteProgress.setState({ progress: null, location: null });
|
||||
openConnectDialog();
|
||||
});
|
||||
|
||||
afterEach(() => closeConnectDialog());
|
||||
|
||||
it("browses the selected SSH host, navigates folders and opens one without a current project", async () => {
|
||||
let finish!: (result: { status: "ok"; data: RemoteEntry[] }) => void;
|
||||
list.mockImplementationOnce(() => new Promise((resolve) => (finish = resolve)));
|
||||
openRemote.mockResolvedValue(true);
|
||||
render(<ConnectHost />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "SSH configuration" }));
|
||||
fireEvent.click(screen.getByRole("option", { name: /Selected host/ }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Browse" }));
|
||||
expect(list).toHaveBeenCalledWith("selected", "~");
|
||||
expect(screen.getByRole("button", { name: "Browse" })).toBeDisabled();
|
||||
expect(screen.getByRole("button", { name: "Open" })).toBeDisabled();
|
||||
finish({
|
||||
status: "ok",
|
||||
data: [entry("/home/dev/.cache"), entry("/home/dev/projects"), entry("/home/dev/README.md", false)],
|
||||
});
|
||||
const folders = await screen.findByRole("list", { name: "Remote folders" });
|
||||
expect(
|
||||
within(folders)
|
||||
.getAllByRole("button")
|
||||
.map((button) => button.textContent),
|
||||
).toEqual(["..", "projects", ".cache"]);
|
||||
expect(screen.getByLabelText("Project directory:")).toHaveValue("/home/dev");
|
||||
list.mockResolvedValueOnce({ status: "ok", data: [entry("/home/dev/projects/demo")] });
|
||||
fireEvent.click(within(folders).getByRole("button", { name: "projects" }));
|
||||
await screen.findByRole("button", { name: "demo" });
|
||||
expect(list).toHaveBeenLastCalledWith("selected", "/home/dev/projects");
|
||||
list.mockResolvedValueOnce({ status: "ok", data: [entry("/home/dev/projects")] });
|
||||
fireEvent.click(within(folders).getByRole("button", { name: ".." }));
|
||||
await waitFor(() => expect(screen.getByLabelText("Project directory:")).toHaveValue("/home/dev"));
|
||||
expect(list).toHaveBeenLastCalledWith("selected", "/home/dev");
|
||||
list.mockResolvedValueOnce({ status: "ok", data: [] });
|
||||
fireEvent.click(within(folders).getByRole("button", { name: "projects" }));
|
||||
await waitFor(() => expect(screen.getByLabelText("Project directory:")).toHaveValue("/home/dev/projects"));
|
||||
expect(within(folders).getAllByRole("button")).toHaveLength(1);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Open" }));
|
||||
await waitFor(() => expect(openRemote).toHaveBeenCalledWith("selected", "/home/dev/projects"));
|
||||
expect(useConnectDialog.getState().open).toBe(false);
|
||||
expect(useProject.getState().project).toBeNull();
|
||||
});
|
||||
|
||||
it("shows a connection error and lets the welcome window retry Browse", async () => {
|
||||
list.mockRejectedValueOnce(new Error("Connection refused"));
|
||||
render(<ConnectHost />);
|
||||
fireEvent.change(screen.getByLabelText("Project directory:"), { target: { value: " /srv/projects " } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Browse" }));
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent("Connection refused");
|
||||
expect(list).toHaveBeenCalledWith("first", "/srv/projects");
|
||||
expect(screen.getByRole("button", { name: "Browse" })).toBeEnabled();
|
||||
list.mockResolvedValueOnce({ status: "ok", data: [] });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Browse" }));
|
||||
await screen.findByRole("list", { name: "Remote folders" });
|
||||
expect(screen.queryByRole("alert")).toBeNull();
|
||||
expect(openRemote).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("offers SSH configuration from the welcome window and disables Browse until one exists", () => {
|
||||
useProject.setState({ settings: { ssh: { configs: [] } } as unknown as Settings });
|
||||
render(<ConnectHost />);
|
||||
expect(screen.getByRole("button", { name: "Browse" })).toBeDisabled();
|
||||
expect(screen.getByRole("button", { name: "Open" })).toBeDisabled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Configure…" }));
|
||||
expect(executeAction).toHaveBeenCalledWith("file.settings", "tools.sshConfigurations");
|
||||
expect(list).not.toHaveBeenCalled();
|
||||
});
|
||||
Reference in new issue
Заблокувати користувача