subspace_chat/agent_chat/
lifecycle.rs1use super::{ChatPanel, Context, Window};
4
5impl ChatPanel {
6 pub fn dismiss_popups(&mut self, window: &mut Window, cx: &mut Context<Self>) {
8 self.toolbar
9 .update(cx, |toolbar, cx| toolbar.dismiss_popups(window, cx));
10 }
11}
12
13#[cfg(test)]
14mod tests {
15 use super::ChatPanel;
16 use crate::prefs;
17 use gpui_kit::base::GlobalState;
18 use gpui_kit::test::TestWindowExt as _;
19 use gpui_kit::{Entity, TestAppContext, VisualTestContext};
20 use subspace_agent::{ChatEvent, Command, Event, SessionSummary};
21 use subspace_core::config::ChatEngine;
22
23 fn setup(cx: &mut TestAppContext) -> (Entity<ChatPanel>, &mut VisualTestContext) {
24 setup_engine(cx, ChatEngine::Copilot)
25 }
26
27 fn setup_engine(
28 cx: &mut TestAppContext,
29 engine: ChatEngine,
30 ) -> (Entity<ChatPanel>, &mut VisualTestContext) {
31 cx.update(|cx| {
32 cx.set_reduce_motion(true);
33 gpui_kit::init(cx);
34 prefs::init_file(None, cx);
35 });
36 let (chat, cx) =
37 cx.add_window_view(move |window, cx| ChatPanel::with_engine(engine, window, cx));
38 settle(cx);
39 (chat, cx)
40 }
41
42 #[gpui_kit::test]
43 fn opening_chat_while_off_shows_setup_guidance_without_starting_a_runtime(
44 cx: &mut TestAppContext,
45 ) {
46 let (chat, cx) = setup_engine(cx, ChatEngine::default());
47 cx.update(|window, cx| {
48 chat.update(cx, |chat, cx| {
49 chat.on_open(window, cx);
50 assert!(chat.is_disabled());
51 assert!(chat.handle.is_none());
52 assert!(!chat.is_unavailable());
53 });
54 });
55 settle(cx);
56 cx.update(|window, _| {
57 assert!(window.find("chat-off").visible());
58 assert!(window.try_find("chat-new-tab").is_none());
59 assert!(window.try_find("chat-reconnect").is_none());
60 });
61 }
62
63 fn settle(cx: &mut VisualTestContext) {
64 for _ in 0..3 {
65 cx.update(|window, cx| window.draw(cx).clear(cx));
66 cx.run_until_parked();
67 }
68 }
69
70 #[gpui_kit::test]
71 fn sessions_dismisses_retained_and_keyed_toolbar_hosts_before_composer_reopens(
72 cx: &mut TestAppContext,
73 ) {
74 let (chat, cx) = setup(cx);
75 for (trigger, body) in [
76 ("chat-usage", "chat-usage-body"),
77 ("chat-add", "popup-menu"),
78 ] {
79 cx.update(|window, cx| window.click(trigger, cx));
80 settle(cx);
81 cx.update(|window, cx| {
82 assert!(window.find(body).visible());
83 assert!(GlobalState::is_in_deferred_context(cx));
84 chat.update(cx, |chat, cx| chat.show_sessions(window, cx));
85 });
86 settle(cx);
87 cx.update(|window, cx| {
88 assert!(window.try_find(trigger).is_none());
89 assert!(!GlobalState::is_in_deferred_context(cx));
90 chat.update(cx, |chat, cx| chat.focus_chat(0, window, cx));
91 });
92 settle(cx);
93 cx.update(|window, _| {
94 assert!(window.find(trigger).visible());
95 assert!(window.try_find(body).is_none());
96 });
97 }
98 cx.update(|window, cx| window.click("chat-add", cx));
99 settle(cx);
100 cx.update(|window, _| assert!(window.find("popup-menu").visible()));
101 }
102
103 #[gpui_kit::test]
104 fn reopen_retains_runtime_and_reconnect_shuts_it_down(cx: &mut TestAppContext) {
105 let directory = tempfile::tempdir().unwrap();
106 let (handle, mut commands) =
107 subspace_agent::AgentHandle::test_offline(directory.path().into(), Vec::new());
108 let original = handle.clone();
109 let (chat, cx) = setup(cx);
110 cx.update(|window, cx| {
111 chat.update(cx, |chat, cx| {
112 chat.connected(Ok(handle));
113 chat.on_open(window, cx);
114 chat.dismiss_popups(window, cx);
115 chat.on_open(window, cx);
116 assert!(chat.handle.as_ref().unwrap().same_runtime(&original));
117 assert!(
118 commands.try_recv().is_err(),
119 "hide/reopen must not restart the runtime"
120 );
121 chat.reconnect(window, cx);
122 assert!(matches!(commands.try_recv(), Ok(Command::Shutdown)));
123 assert!(commands.try_recv().is_err());
124 assert!(chat.handle.is_none());
125 assert_eq!(
126 chat.error.as_deref(),
127 Some("Service configuration is unavailable.")
128 );
129 })
130 });
131 }
132
133 #[gpui_kit::test]
134 fn startup_rejection_reaches_panel_polling_and_reconnect_clears_sign_in(
135 cx: &mut TestAppContext,
136 ) {
137 let directory = tempfile::tempdir().unwrap();
138 let rejection = "GitHub rejected the gh CLI token for test-user. Click Sign in to GitHub.";
139 let (handle, mut commands) = subspace_agent::AgentHandle::test_offline(
140 directory.path().into(),
141 vec![
142 Event::SignInRequired(subspace_agent::SignInOffer::GhRefresh {
143 login: "test-user".into(),
144 }),
145 Event::Error(rejection.into()),
146 ],
147 );
148 let (chat, cx) = setup(cx);
149 cx.update(|window, cx| chat.update(cx, |chat, cx| {
150 chat.connected(Ok(handle));
151 chat.poll(cx);
152 assert_eq!(chat.error.as_deref(), Some(rejection));
153 assert!(matches!(&chat.sign_in, Some(super::super::sign_in::SignIn::Offered { offer: subspace_agent::SignInOffer::GhRefresh { login }, .. }) if login == "test-user"));
154 assert!(!chat.tab().is_ready());
155 chat.reconnect(window, cx);
156 assert!(matches!(commands.try_recv(), Ok(Command::Shutdown)));
157 assert!(chat.sign_in.is_none());
158 }));
159 }
160
161 #[gpui_kit::test]
162 fn restored_runtime_events_publish_saved_sessions_with_tab_activity(cx: &mut TestAppContext) {
163 let directory = tempfile::tempdir().unwrap();
164 let events = vec![
165 Event::of(2, ChatEvent::History(Vec::new())),
166 Event::of(2, ChatEvent::Title("Restored second".into())),
167 Event::of(
168 2,
169 ChatEvent::Ready {
170 session_id: "saved-second".into(),
171 },
172 ),
173 Event::of(
174 1,
175 ChatEvent::Ready {
176 session_id: "saved-first".into(),
177 },
178 ),
179 Event::Restore {
180 tabs: vec![(1, "saved-first".into()), (2, "saved-second".into())],
181 active: 2,
182 },
183 Event::Sessions(vec![SessionSummary {
184 id: "saved-second".into(),
185 title: "Saved second".into(),
186 modified: "now".into(),
187 modified_at: "2026-11-04T00:00:00Z".into(),
188 created_at: "2026-11-03T00:00:00Z".into(),
189 }]),
190 ];
191 let (handle, mut commands) =
192 subspace_agent::AgentHandle::test_offline(directory.path().into(), events);
193 let (chat, cx) = setup(cx);
194 cx.update(|_, cx| {
195 chat.update(cx, |chat, cx| {
196 chat.connected(Ok(handle));
197 chat.poll(cx);
198 assert_eq!(
199 chat.tabs.iter().map(|tab| tab.id).collect::<Vec<_>>(),
200 [1, 2]
201 );
202 assert_eq!(chat.tab().title.as_ref(), "Restored second");
203 assert_eq!(chat.tab().session.as_deref(), Some("saved-second"));
204 assert!(matches!(commands.try_recv(), Ok(Command::ListSessions)));
205 chat.tab_mut().begin();
206 chat.publish_sessions(cx);
207 let sessions = crate::shared::sessions(cx);
208 assert_eq!(sessions.len(), 1);
209 assert!(sessions[0].open && sessions[0].busy);
210 assert_eq!(sessions[0].id, "saved-second");
211 })
212 });
213 }
214
215 #[gpui_kit::test]
216 fn application_quit_sends_shutdown_to_the_retained_runtime(cx: &mut TestAppContext) {
217 let directory = tempfile::tempdir().unwrap();
218 let (handle, mut commands) =
219 subspace_agent::AgentHandle::test_offline(directory.path().into(), Vec::new());
220 cx.update(|cx| {
221 gpui_kit::init(cx);
222 prefs::init_file(None, cx);
223 });
224 let window = cx.add_window(ChatPanel::new);
225 window
226 .update(cx, |chat, _, _| chat.connected(Ok(handle)))
227 .unwrap();
228 cx.quit();
229 assert!(matches!(commands.try_recv(), Ok(Command::Shutdown)));
230 assert!(commands.try_recv().is_err());
231 }
232
233 #[gpui_kit::test]
236 fn a_message_after_an_error_during_a_turn_steers_it(cx: &mut TestAppContext) {
237 let directory = tempfile::tempdir().unwrap();
238 let (handle, _commands) =
239 subspace_agent::AgentHandle::test_offline(directory.path().into(), Vec::new());
240 let (chat, cx) = setup(cx);
241 chat.update(cx, |chat, cx| {
242 chat.connected(Ok(handle));
243 chat.tab_mut().ready();
244 let mut first = chat.prepare_submission(
245 super::super::InputIntent::Message("Fix it".into()),
246 Vec::new(),
247 );
248 let command = first.command.take().expect("a send");
249 let sent = chat.send(command, cx);
250 chat.finish_submission(first, sent);
251 let running = chat.tab().running().copied();
252 super::super::attention::apply_chat_event(
253 chat.tab_mut(),
254 ChatEvent::Error("refused".into()),
255 );
256 let steer = chat.prepare_submission(
257 super::super::InputIntent::Message("Also".into()),
258 Vec::new(),
259 );
260 assert!(steer.steering, "the turn is still running");
261 assert!(steer.last_turn.is_none(), "Retry keeps the first message");
262 assert_eq!(chat.tab().running().copied(), running, "and its timer");
263 });
264 }
265}