test_support.rs

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