test_support.rs

 1use acp_thread::{AgentConnection, StubAgentConnection};
 2use agent_client_protocol as acp;
 3use agent_servers::{AgentServer, AgentServerDelegate};
 4use gpui::{Entity, Task, TestAppContext, VisualTestContext};
 5use project::AgentId;
 6use settings::SettingsStore;
 7use std::any::Any;
 8use std::rc::Rc;
 9
10use crate::AgentPanel;
11use crate::agent_panel;
12
13pub struct StubAgentServer<C> {
14    connection: C,
15}
16
17impl<C> StubAgentServer<C> {
18    pub fn new(connection: C) -> Self {
19        Self { connection }
20    }
21}
22
23impl StubAgentServer<StubAgentConnection> {
24    pub fn default_response() -> Self {
25        let conn = StubAgentConnection::new();
26        conn.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
27            acp::ContentChunk::new("Default response".into()),
28        )]);
29        Self::new(conn)
30    }
31}
32
33impl<C> AgentServer for StubAgentServer<C>
34where
35    C: 'static + AgentConnection + Send + Clone,
36{
37    fn logo(&self) -> ui::IconName {
38        ui::IconName::Ai
39    }
40
41    fn agent_id(&self) -> AgentId {
42        "Test".into()
43    }
44
45    fn connect(
46        &self,
47        _delegate: AgentServerDelegate,
48        _cx: &mut gpui::App,
49    ) -> Task<gpui::Result<Rc<dyn AgentConnection>>> {
50        Task::ready(Ok(Rc::new(self.connection.clone())))
51    }
52
53    fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
54        self
55    }
56}
57
58pub fn init_test(cx: &mut TestAppContext) {
59    cx.update(|cx| {
60        let settings_store = SettingsStore::test(cx);
61        cx.set_global(settings_store);
62        theme::init(theme::LoadThemes::JustBase, cx);
63        editor::init(cx);
64        release_channel::init("0.0.0".parse().unwrap(), cx);
65        agent_panel::init(cx);
66    });
67}
68
69pub fn open_thread_with_connection(
70    panel: &Entity<AgentPanel>,
71    connection: StubAgentConnection,
72    cx: &mut VisualTestContext,
73) {
74    panel.update_in(cx, |panel, window, cx| {
75        panel.open_external_thread_with_server(
76            Rc::new(StubAgentServer::new(connection)),
77            window,
78            cx,
79        );
80    });
81    cx.run_until_parked();
82}
83
84pub fn send_message(panel: &Entity<AgentPanel>, cx: &mut VisualTestContext) {
85    let thread_view = panel.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
86    let message_editor = thread_view.read_with(cx, |view, _cx| view.message_editor.clone());
87    message_editor.update_in(cx, |editor, window, cx| {
88        editor.set_text("Hello", window, cx);
89    });
90    thread_view.update_in(cx, |view, window, cx| view.send(window, cx));
91    cx.run_until_parked();
92}
93
94pub fn active_session_id(panel: &Entity<AgentPanel>, cx: &VisualTestContext) -> acp::SessionId {
95    panel.read_with(cx, |panel, cx| {
96        let thread = panel.active_agent_thread(cx).unwrap();
97        thread.read(cx).session_id().clone()
98    })
99}