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