use std::sync::Arc;
use gpui_kit::test::TestWindowExt as _;
use gpui_kit::{AnyWindowHandle, AppContext as _, ElementId, Entity, TestAppContext, px};
use serde_json::Value;
use subspace_chat::OPEN_KEY;
use subspace_core::config::Config;
use subspace_dashboard::{CurrentProduct, Services, prefs};
use super::navigation::NAVIGATION_KEY;
use super::space_menu::{CLOSE_SPACE, LAST_SPACE, SPACE_SETTINGS};
use super::{ACTIVE_KEY, SubspaceApp};
use crate::settings::Settings;
use crate::{BoardRegistration, registry};
mod add_space;
mod fixture;
mod guards;
use fixture::{Plain, product};
const IDS: [&str; 3] = ["alpha", "beta", "gamma"];
const TITLES: [&str; 3] = ["Alpha", "Beta", "Gamma"];
fn open(root: &std::path::Path, cx: &mut TestAppContext) -> (AnyWindowHandle, Entity<SubspaceApp>) {
let path = root.join("config.json");
std::fs::write(&path, r#"{"boards": {"gamma": true}}"#).unwrap();
cx.update(|cx| {
crate::theme::preview_tests::init_app(cx, root, root.join("state.json"));
crate::theme::initialize(cx);
crate::theme::preview_tests::nominal_controls(cx);
prefs::set(cx, crate::settings::CHECK_UPDATES_KEY, "0");
prefs::set(cx, NAVIGATION_KEY, "horizontal");
prefs::set(cx, OPEN_KEY, "0");
let mut context = (*cx.global::<CurrentProduct>().0).clone();
context.product = product();
cx.set_global(CurrentProduct(Arc::new(context)));
let boards = vec![
BoardRegistration::new::<Plain<0>>(),
BoardRegistration::new::<Plain<1>>(),
BoardRegistration::new::<Plain<2>>(),
];
registry::retain_provider(boards.clone(), None, cx);
cx.set_global(registry::IncludedBoards(boards.clone()));
cx.set_global(registry::BoardFactories(boards));
let config = subspace_core::config::load(&path).0;
cx.set_global(Services {
config: Arc::new(config.clone()),
credentials: Arc::new(subspace_core::credentials::Credentials::from_system(
&config,
)),
tools: subspace_agent_tools::Catalog::new(Vec::new()).unwrap(),
search: None,
config_file: path,
});
});
let handle = cx.open_window(gpui_kit::size(px(1400.), px(600.)), |window, cx| {
SubspaceApp::new(Vec::new(), window, cx)
});
let app = handle.update(cx, |_, _, cx| cx.entity()).unwrap();
cx.update_window(handle.into(), |_, window, cx| {
window.replace_root(cx, |window, cx| {
gpui_kit::base::Root::new(app.clone(), window, cx)
});
})
.unwrap();
settle(handle.into(), cx);
(handle.into(), app)
}
fn settle(window: AnyWindowHandle, cx: &mut TestAppContext) {
for _ in 0..4 {
cx.update_window(window, |_, window, cx| window.render_frame(cx))
.unwrap();
cx.run_until_parked();
}
}
fn ids(app: &Entity<SubspaceApp>, cx: &mut TestAppContext) -> Vec<&'static str> {
app.read_with(cx, |app, _| {
app.dashboards.iter().map(|board| board.id).collect()
})
}
fn active(app: &Entity<SubspaceApp>, cx: &mut TestAppContext) -> &'static str {
app.read_with(cx, |app, _| app.dashboards[app.active].id)
}
fn show(window: AnyWindowHandle, app: &Entity<SubspaceApp>, id: &str, cx: &mut TestAppContext) {
cx.update_window(window, |_, window, cx| {
app.update(cx, |app, cx| {
let index = app
.dashboards
.iter()
.position(|board| board.id == id)
.unwrap();
app.switch_to(index, window, cx);
});
})
.unwrap();
settle(window, cx);
}
fn menu_item(
window: AnyWindowHandle,
id: &str,
label: &str,
cx: &mut TestAppContext,
) -> gpui_kit::test::ElementSnapshot {
cx.update_window(window, |_, window, cx| {
window.right_click(ElementId::Name(format!("space-tab-{id}").into()), cx);
})
.unwrap();
settle(window, cx);
cx.update_window(window, |_, window, _| {
window.within("popup-menu").find_by_label(label)
})
.unwrap()
}
fn menu_labels(window: AnyWindowHandle, cx: &mut TestAppContext) -> Vec<String> {
cx.update_window(window, |_, window, _| {
let menu = window.within("popup-menu");
(0usize..16)
.filter_map(|index| menu.try_find(index))
.filter_map(|item| item.label().map(str::to_owned))
.collect()
})
.unwrap()
}
fn choose(window: AnyWindowHandle, id: &str, label: &str, cx: &mut TestAppContext) {
let item = menu_item(window, id, label, cx);
let target = item.path().last().cloned().unwrap();
cx.update_window(window, |_, window, cx| {
window.within("popup-menu").click(target, cx);
})
.unwrap();
settle(window, cx);
}
fn saved_boards(root: &std::path::Path) -> Option<Value> {
let text = std::fs::read_to_string(root.join("config.json")).unwrap();
serde_json::from_str::<Value>(&text)
.unwrap()
.get("boards")
.cloned()
}
fn running_config(cx: &mut TestAppContext) -> Config {
cx.read(|cx| (*cx.global::<Services>().config).clone())
}
#[gpui_kit::test]
fn closing_a_background_space_removes_its_tab_and_keeps_the_current_one(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
assert_eq!(ids(&app, cx), IDS);
assert_eq!(active(&app, cx), "alpha");
choose(window, "beta", CLOSE_SPACE, cx);
assert_eq!(ids(&app, cx), ["alpha", "gamma"]);
assert_eq!(active(&app, cx), "alpha");
assert!(!running_config(cx).board_enabled("beta"));
assert_eq!(
saved_boards(root.path()),
Some(serde_json::json!({"beta": false, "gamma": true}))
);
app.read_with(cx, |app, _| {
let toast = &app.toasts.last().unwrap().toast.text;
assert_eq!(
toast,
"Closed Beta. Add it again from the plus in the title bar."
);
});
}
#[gpui_kit::test]
fn the_closed_notice_points_to_the_rail_when_navigation_is_vertical(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
cx.update_window(window, |_, window, cx| {
app.update(cx, |app, cx| {
app.toggle_navigation_mode(window, cx);
app.close_space("beta", window, cx);
});
})
.unwrap();
settle(window, cx);
assert_eq!(ids(&app, cx), ["alpha", "gamma"]);
app.read_with(cx, |app, _| {
let toast = &app.toasts.last().unwrap().toast.text;
assert_eq!(
toast,
"Closed Beta. Add it again from the plus at the end of the navigation rail."
);
});
}
#[gpui_kit::test]
fn closing_the_current_space_moves_to_the_previous_tab_and_focuses_it(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
show(window, &app, "gamma", cx);
choose(window, "gamma", CLOSE_SPACE, cx);
assert_eq!(ids(&app, cx), ["alpha", "beta"]);
assert_eq!(active(&app, cx), "beta");
cx.update_window(window, |_, window, cx| {
let app = app.read(cx);
assert!(
app.dashboards[app.active]
.focus_handle(cx)
.is_focused(window)
);
assert_eq!(prefs::get(cx, ACTIVE_KEY).as_deref(), Some("beta"));
})
.unwrap();
show(window, &app, "alpha", cx);
choose(window, "alpha", CLOSE_SPACE, cx);
assert_eq!(ids(&app, cx), ["beta"]);
assert_eq!(active(&app, cx), "beta");
cx.update_window(window, |_, window, cx| {
let app = app.read(cx);
assert!(
app.dashboards[app.active]
.focus_handle(cx)
.is_focused(window)
);
})
.unwrap();
}
#[gpui_kit::test]
fn the_last_space_cannot_be_closed(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
choose(window, "beta", CLOSE_SPACE, cx);
choose(window, "gamma", CLOSE_SPACE, cx);
assert_eq!(ids(&app, cx), ["alpha"]);
let saved = saved_boards(root.path());
menu_item(window, "alpha", CLOSE_SPACE, cx);
cx.update_window(window, |_, window, _| {
let reason = window.within("popup-menu").try_find_by_label(LAST_SPACE);
assert!(reason.is_some(), "the menu says why");
})
.unwrap();
assert_eq!(
menu_labels(window, cx),
[CLOSE_SPACE, LAST_SPACE, SPACE_SETTINGS],
"the reason sits under the disabled item, before the Place group"
);
choose(window, "alpha", CLOSE_SPACE, cx);
assert!(app.read_with(cx, |app, _| !app.can_close_space()));
cx.update_window(window, |_, window, cx| {
app.update(cx, |app, cx| app.close_space("alpha", window, cx));
})
.unwrap();
settle(window, cx);
assert_eq!(ids(&app, cx), ["alpha"]);
assert_eq!(saved_boards(root.path()), saved);
}
fn settings_save(
window: AnyWindowHandle,
app: &Entity<SubspaceApp>,
id: &str,
enabled: bool,
cx: &mut TestAppContext,
) {
let restart = cx
.update_window(window, |_, window, cx| {
let settings = Settings::open(window, cx).unwrap();
app.update(cx, |app, _| app.settings = Some(settings.clone()));
settings.update(cx, |settings, cx| {
settings.draft_board_enabled(id, enabled);
settings.save(window, cx)
})
})
.unwrap();
assert_eq!(restart, Ok(false));
settle(window, cx);
app.update(cx, |app, _| app.settings = None);
}
#[gpui_kit::test]
fn closing_from_the_tab_saves_what_the_settings_switch_saves(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
for id in ["gamma", "beta"] {
choose(window, id, CLOSE_SPACE, cx);
let from_tab = (saved_boards(root.path()), running_config(cx).boards);
settings_save(window, &app, id, true, cx);
assert!(ids(&app, cx).contains(&id), "Settings reopens {id}");
settings_save(window, &app, id, false, cx);
let from_settings = (saved_boards(root.path()), running_config(cx).boards);
assert_eq!(from_tab, from_settings, "{id}");
assert!(!ids(&app, cx).contains(&id));
settings_save(window, &app, id, true, cx);
}
choose(window, "gamma", CLOSE_SPACE, cx);
assert_eq!(saved_boards(root.path()), None);
}
#[gpui_kit::test]
fn space_settings_opens_that_spaces_page(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, app) = open(root.path(), cx);
choose(window, "beta", SPACE_SETTINGS, cx);
assert_eq!(active(&app, cx), "alpha");
let settings = app.read_with(cx, |app, _| app.settings.clone()).unwrap();
assert!(settings.read_with(cx, |settings, _| settings.shows_board_page("beta")));
}
#[gpui_kit::test]
fn the_menu_puts_close_in_remove_before_settings_in_place(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, _app) = open(root.path(), cx);
menu_item(window, "beta", CLOSE_SPACE, cx);
assert_eq!(menu_labels(window, cx), [CLOSE_SPACE, SPACE_SETTINGS]);
}