test_server.rs

  1use crate::{
  2    db::{tests::TestDb, NewUserParams, UserId},
  3    executor::Executor,
  4    rpc::{Server, CLEANUP_TIMEOUT, RECONNECT_TIMEOUT},
  5    AppState, Config,
  6};
  7use anyhow::anyhow;
  8use call::ActiveCall;
  9use channel::{ChannelBuffer, ChannelStore};
 10use client::{
 11    self, proto::PeerId, Client, Connection, Credentials, EstablishConnectionError, UserStore,
 12};
 13use collections::{HashMap, HashSet};
 14use fs::FakeFs;
 15use futures::{channel::oneshot, StreamExt as _};
 16use gpui::{BackgroundExecutor, Context, Model, TestAppContext, View, VisualTestContext};
 17use language::LanguageRegistry;
 18use node_runtime::FakeNodeRuntime;
 19
 20use notifications::NotificationStore;
 21use parking_lot::Mutex;
 22use project::{Project, WorktreeId};
 23use rpc::{
 24    proto::{self, ChannelRole},
 25    RECEIVE_TIMEOUT,
 26};
 27use serde_json::json;
 28use settings::SettingsStore;
 29use std::{
 30    cell::{Ref, RefCell, RefMut},
 31    env,
 32    ops::{Deref, DerefMut},
 33    path::Path,
 34    sync::{
 35        atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
 36        Arc,
 37    },
 38};
 39use util::http::FakeHttpClient;
 40use workspace::{Workspace, WorkspaceStore};
 41
 42pub struct TestServer {
 43    pub app_state: Arc<AppState>,
 44    pub test_live_kit_server: Arc<live_kit_client::TestServer>,
 45    server: Arc<Server>,
 46    connection_killers: Arc<Mutex<HashMap<PeerId, Arc<AtomicBool>>>>,
 47    forbid_connections: Arc<AtomicBool>,
 48    _test_db: TestDb,
 49}
 50
 51pub struct TestClient {
 52    pub username: String,
 53    pub app_state: Arc<workspace::AppState>,
 54    channel_store: Model<ChannelStore>,
 55    notification_store: Model<NotificationStore>,
 56    state: RefCell<TestClientState>,
 57}
 58
 59#[derive(Default)]
 60struct TestClientState {
 61    local_projects: Vec<Model<Project>>,
 62    remote_projects: Vec<Model<Project>>,
 63    buffers: HashMap<Model<Project>, HashSet<Model<language::Buffer>>>,
 64    channel_buffers: HashSet<Model<ChannelBuffer>>,
 65}
 66
 67pub struct ContactsSummary {
 68    pub current: Vec<String>,
 69    pub outgoing_requests: Vec<String>,
 70    pub incoming_requests: Vec<String>,
 71}
 72
 73impl TestServer {
 74    pub async fn start(deterministic: BackgroundExecutor) -> Self {
 75        static NEXT_LIVE_KIT_SERVER_ID: AtomicUsize = AtomicUsize::new(0);
 76
 77        let use_postgres = env::var("USE_POSTGRES").ok();
 78        let use_postgres = use_postgres.as_deref();
 79        let test_db = if use_postgres == Some("true") || use_postgres == Some("1") {
 80            TestDb::postgres(deterministic.clone())
 81        } else {
 82            TestDb::sqlite(deterministic.clone())
 83        };
 84        let live_kit_server_id = NEXT_LIVE_KIT_SERVER_ID.fetch_add(1, SeqCst);
 85        let live_kit_server = live_kit_client::TestServer::create(
 86            format!("http://livekit.{}.test", live_kit_server_id),
 87            format!("devkey-{}", live_kit_server_id),
 88            format!("secret-{}", live_kit_server_id),
 89            deterministic.clone(),
 90        )
 91        .unwrap();
 92        let app_state = Self::build_app_state(&test_db, &live_kit_server).await;
 93        let epoch = app_state
 94            .db
 95            .create_server(&app_state.config.zed_environment)
 96            .await
 97            .unwrap();
 98        let server = Server::new(
 99            epoch,
100            app_state.clone(),
101            Executor::Deterministic(deterministic.clone()),
102        );
103        server.start().await.unwrap();
104        // Advance clock to ensure the server's cleanup task is finished.
105        deterministic.advance_clock(CLEANUP_TIMEOUT);
106        Self {
107            app_state,
108            server,
109            connection_killers: Default::default(),
110            forbid_connections: Default::default(),
111            _test_db: test_db,
112            test_live_kit_server: live_kit_server,
113        }
114    }
115
116    pub async fn reset(&self) {
117        self.app_state.db.reset();
118        let epoch = self
119            .app_state
120            .db
121            .create_server(&self.app_state.config.zed_environment)
122            .await
123            .unwrap();
124        self.server.reset(epoch);
125    }
126
127    pub async fn create_client(&mut self, cx: &mut TestAppContext, name: &str) -> TestClient {
128        cx.update(|cx| {
129            if cx.has_global::<SettingsStore>() {
130                panic!("Same cx used to create two test clients")
131            }
132            let settings = SettingsStore::test(cx);
133            cx.set_global(settings);
134        });
135
136        let http = FakeHttpClient::with_404_response();
137        let user_id = if let Ok(Some(user)) = self.app_state.db.get_user_by_github_login(name).await
138        {
139            user.id
140        } else {
141            self.app_state
142                .db
143                .create_user(
144                    &format!("{name}@example.com"),
145                    false,
146                    NewUserParams {
147                        github_login: name.into(),
148                        github_user_id: 0,
149                    },
150                )
151                .await
152                .expect("creating user failed")
153                .user_id
154        };
155        let client_name = name.to_string();
156        let mut client = cx.update(|cx| Client::new(http.clone(), cx));
157        let server = self.server.clone();
158        let db = self.app_state.db.clone();
159        let connection_killers = self.connection_killers.clone();
160        let forbid_connections = self.forbid_connections.clone();
161
162        Arc::get_mut(&mut client)
163            .unwrap()
164            .set_id(user_id.to_proto())
165            .override_authenticate(move |cx| {
166                cx.spawn(|_| async move {
167                    let access_token = "the-token".to_string();
168                    Ok(Credentials {
169                        user_id: user_id.to_proto(),
170                        access_token,
171                    })
172                })
173            })
174            .override_establish_connection(move |credentials, cx| {
175                assert_eq!(credentials.user_id, user_id.0 as u64);
176                assert_eq!(credentials.access_token, "the-token");
177
178                let server = server.clone();
179                let db = db.clone();
180                let connection_killers = connection_killers.clone();
181                let forbid_connections = forbid_connections.clone();
182                let client_name = client_name.clone();
183                cx.spawn(move |cx| async move {
184                    if forbid_connections.load(SeqCst) {
185                        Err(EstablishConnectionError::other(anyhow!(
186                            "server is forbidding connections"
187                        )))
188                    } else {
189                        let (client_conn, server_conn, killed) =
190                            Connection::in_memory(cx.background_executor().clone());
191                        let (connection_id_tx, connection_id_rx) = oneshot::channel();
192                        let user = db
193                            .get_user_by_id(user_id)
194                            .await
195                            .expect("retrieving user failed")
196                            .unwrap();
197                        cx.background_executor()
198                            .spawn(server.handle_connection(
199                                server_conn,
200                                client_name,
201                                user,
202                                Some(connection_id_tx),
203                                Executor::Deterministic(cx.background_executor().clone()),
204                            ))
205                            .detach();
206                        let connection_id = connection_id_rx.await.unwrap();
207                        connection_killers
208                            .lock()
209                            .insert(connection_id.into(), killed);
210                        Ok(client_conn)
211                    }
212                })
213            });
214
215        let fs = FakeFs::new(cx.executor());
216        let user_store = cx.new_model(|cx| UserStore::new(client.clone(), cx));
217        let workspace_store = cx.new_model(|cx| WorkspaceStore::new(client.clone(), cx));
218        let mut language_registry = LanguageRegistry::test();
219        language_registry.set_executor(cx.executor());
220        let app_state = Arc::new(workspace::AppState {
221            client: client.clone(),
222            user_store: user_store.clone(),
223            workspace_store,
224            languages: Arc::new(language_registry),
225            fs: fs.clone(),
226            build_window_options: |_, _, _| Default::default(),
227            node_runtime: FakeNodeRuntime::new(),
228        });
229
230        cx.update(|cx| {
231            theme::init(theme::LoadThemes::JustBase, cx);
232            Project::init(&client, cx);
233            client::init(&client, cx);
234            language::init(cx);
235            editor::init(cx);
236            workspace::init(app_state.clone(), cx);
237            audio::init((), cx);
238            call::init(client.clone(), user_store.clone(), cx);
239            channel::init(&client, user_store.clone(), cx);
240            notifications::init(client.clone(), user_store, cx);
241            collab_ui::init(&app_state, cx);
242            file_finder::init(cx);
243            menu::init();
244            settings::KeymapFile::load_asset("keymaps/default.json", cx).unwrap();
245        });
246
247        client
248            .authenticate_and_connect(false, &cx.to_async())
249            .await
250            .unwrap();
251
252        let client = TestClient {
253            app_state,
254            username: name.to_string(),
255            channel_store: cx.read(ChannelStore::global).clone(),
256            notification_store: cx.read(NotificationStore::global).clone(),
257            state: Default::default(),
258        };
259        client.wait_for_current_user(cx).await;
260        client
261    }
262
263    pub fn disconnect_client(&self, peer_id: PeerId) {
264        self.connection_killers
265            .lock()
266            .remove(&peer_id)
267            .unwrap()
268            .store(true, SeqCst);
269    }
270
271    pub fn simulate_long_connection_interruption(
272        &self,
273        peer_id: PeerId,
274        deterministic: BackgroundExecutor,
275    ) {
276        self.forbid_connections();
277        self.disconnect_client(peer_id);
278        deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
279        self.allow_connections();
280        deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
281        deterministic.run_until_parked();
282    }
283
284    pub fn forbid_connections(&self) {
285        self.forbid_connections.store(true, SeqCst);
286    }
287
288    pub fn allow_connections(&self) {
289        self.forbid_connections.store(false, SeqCst);
290    }
291
292    pub async fn make_contacts(&self, clients: &mut [(&TestClient, &mut TestAppContext)]) {
293        for ix in 1..clients.len() {
294            let (left, right) = clients.split_at_mut(ix);
295            let (client_a, cx_a) = left.last_mut().unwrap();
296            for (client_b, cx_b) in right {
297                client_a
298                    .app_state
299                    .user_store
300                    .update(*cx_a, |store, cx| {
301                        store.request_contact(client_b.user_id().unwrap(), cx)
302                    })
303                    .await
304                    .unwrap();
305                cx_a.executor().run_until_parked();
306                client_b
307                    .app_state
308                    .user_store
309                    .update(*cx_b, |store, cx| {
310                        store.respond_to_contact_request(client_a.user_id().unwrap(), true, cx)
311                    })
312                    .await
313                    .unwrap();
314            }
315        }
316    }
317
318    pub async fn make_channel(
319        &self,
320        channel: &str,
321        parent: Option<u64>,
322        admin: (&TestClient, &mut TestAppContext),
323        members: &mut [(&TestClient, &mut TestAppContext)],
324    ) -> u64 {
325        let (_, admin_cx) = admin;
326        let channel_id = admin_cx
327            .read(ChannelStore::global)
328            .update(admin_cx, |channel_store, cx| {
329                channel_store.create_channel(channel, parent, cx)
330            })
331            .await
332            .unwrap();
333
334        for (member_client, member_cx) in members {
335            admin_cx
336                .read(ChannelStore::global)
337                .update(admin_cx, |channel_store, cx| {
338                    channel_store.invite_member(
339                        channel_id,
340                        member_client.user_id().unwrap(),
341                        ChannelRole::Member,
342                        cx,
343                    )
344                })
345                .await
346                .unwrap();
347
348            admin_cx.executor().run_until_parked();
349
350            member_cx
351                .read(ChannelStore::global)
352                .update(*member_cx, |channels, cx| {
353                    channels.respond_to_channel_invite(channel_id, true, cx)
354                })
355                .await
356                .unwrap();
357        }
358
359        channel_id
360    }
361
362    pub async fn make_public_channel(
363        &self,
364        channel: &str,
365        client: &TestClient,
366        cx: &mut TestAppContext,
367    ) -> u64 {
368        let channel_id = self
369            .make_channel(channel, None, (client, cx), &mut [])
370            .await;
371
372        client
373            .channel_store()
374            .update(cx, |channel_store, cx| {
375                channel_store.set_channel_visibility(
376                    channel_id,
377                    proto::ChannelVisibility::Public,
378                    cx,
379                )
380            })
381            .await
382            .unwrap();
383
384        channel_id
385    }
386
387    pub async fn make_channel_tree(
388        &self,
389        channels: &[(&str, Option<&str>)],
390        creator: (&TestClient, &mut TestAppContext),
391    ) -> Vec<u64> {
392        let mut observed_channels = HashMap::default();
393        let mut result = Vec::new();
394        for (channel, parent) in channels {
395            let id;
396            if let Some(parent) = parent {
397                if let Some(parent_id) = observed_channels.get(parent) {
398                    id = self
399                        .make_channel(channel, Some(*parent_id), (creator.0, creator.1), &mut [])
400                        .await;
401                } else {
402                    panic!(
403                        "Edge {}->{} referenced before {} was created",
404                        parent, channel, parent
405                    )
406                }
407            } else {
408                id = self
409                    .make_channel(channel, None, (creator.0, creator.1), &mut [])
410                    .await;
411            }
412
413            observed_channels.insert(channel, id);
414            result.push(id);
415        }
416
417        result
418    }
419
420    pub async fn create_room(&self, clients: &mut [(&TestClient, &mut TestAppContext)]) {
421        self.make_contacts(clients).await;
422
423        let (left, right) = clients.split_at_mut(1);
424        let (_client_a, cx_a) = &mut left[0];
425        let active_call_a = cx_a.read(ActiveCall::global);
426
427        for (client_b, cx_b) in right {
428            let user_id_b = client_b.current_user_id(*cx_b).to_proto();
429            active_call_a
430                .update(*cx_a, |call, cx| call.invite(user_id_b, None, cx))
431                .await
432                .unwrap();
433
434            cx_b.executor().run_until_parked();
435            let active_call_b = cx_b.read(ActiveCall::global);
436            active_call_b
437                .update(*cx_b, |call, cx| call.accept_incoming(cx))
438                .await
439                .unwrap();
440        }
441    }
442
443    pub async fn build_app_state(
444        test_db: &TestDb,
445        fake_server: &live_kit_client::TestServer,
446    ) -> Arc<AppState> {
447        Arc::new(AppState {
448            db: test_db.db().clone(),
449            live_kit_client: Some(Arc::new(fake_server.create_api_client())),
450            config: Config {
451                http_port: 0,
452                database_url: "".into(),
453                database_max_connections: 0,
454                api_token: "".into(),
455                invite_link_prefix: "".into(),
456                live_kit_server: None,
457                live_kit_key: None,
458                live_kit_secret: None,
459                rust_log: None,
460                log_json: None,
461                zed_environment: "test".into(),
462            },
463        })
464    }
465}
466
467impl Deref for TestServer {
468    type Target = Server;
469
470    fn deref(&self) -> &Self::Target {
471        &self.server
472    }
473}
474
475impl Drop for TestServer {
476    fn drop(&mut self) {
477        self.server.teardown();
478        self.test_live_kit_server.teardown().unwrap();
479    }
480}
481
482impl Deref for TestClient {
483    type Target = Arc<Client>;
484
485    fn deref(&self) -> &Self::Target {
486        &self.app_state.client
487    }
488}
489
490impl TestClient {
491    pub fn fs(&self) -> &FakeFs {
492        self.app_state.fs.as_fake()
493    }
494
495    pub fn channel_store(&self) -> &Model<ChannelStore> {
496        &self.channel_store
497    }
498
499    pub fn notification_store(&self) -> &Model<NotificationStore> {
500        &self.notification_store
501    }
502
503    pub fn user_store(&self) -> &Model<UserStore> {
504        &self.app_state.user_store
505    }
506
507    pub fn language_registry(&self) -> &Arc<LanguageRegistry> {
508        &self.app_state.languages
509    }
510
511    pub fn client(&self) -> &Arc<Client> {
512        &self.app_state.client
513    }
514
515    pub fn current_user_id(&self, cx: &TestAppContext) -> UserId {
516        UserId::from_proto(
517            self.app_state
518                .user_store
519                .read_with(cx, |user_store, _| user_store.current_user().unwrap().id),
520        )
521    }
522
523    pub async fn wait_for_current_user(&self, cx: &TestAppContext) {
524        let mut authed_user = self
525            .app_state
526            .user_store
527            .read_with(cx, |user_store, _| user_store.watch_current_user());
528        while authed_user.next().await.unwrap().is_none() {}
529    }
530
531    pub async fn clear_contacts(&self, cx: &mut TestAppContext) {
532        self.app_state
533            .user_store
534            .update(cx, |store, _| store.clear_contacts())
535            .await;
536    }
537
538    pub fn local_projects<'a>(&'a self) -> impl Deref<Target = Vec<Model<Project>>> + 'a {
539        Ref::map(self.state.borrow(), |state| &state.local_projects)
540    }
541
542    pub fn remote_projects<'a>(&'a self) -> impl Deref<Target = Vec<Model<Project>>> + 'a {
543        Ref::map(self.state.borrow(), |state| &state.remote_projects)
544    }
545
546    pub fn local_projects_mut<'a>(&'a self) -> impl DerefMut<Target = Vec<Model<Project>>> + 'a {
547        RefMut::map(self.state.borrow_mut(), |state| &mut state.local_projects)
548    }
549
550    pub fn remote_projects_mut<'a>(&'a self) -> impl DerefMut<Target = Vec<Model<Project>>> + 'a {
551        RefMut::map(self.state.borrow_mut(), |state| &mut state.remote_projects)
552    }
553
554    pub fn buffers_for_project<'a>(
555        &'a self,
556        project: &Model<Project>,
557    ) -> impl DerefMut<Target = HashSet<Model<language::Buffer>>> + 'a {
558        RefMut::map(self.state.borrow_mut(), |state| {
559            state.buffers.entry(project.clone()).or_default()
560        })
561    }
562
563    pub fn buffers<'a>(
564        &'a self,
565    ) -> impl DerefMut<Target = HashMap<Model<Project>, HashSet<Model<language::Buffer>>>> + 'a
566    {
567        RefMut::map(self.state.borrow_mut(), |state| &mut state.buffers)
568    }
569
570    pub fn channel_buffers<'a>(
571        &'a self,
572    ) -> impl DerefMut<Target = HashSet<Model<ChannelBuffer>>> + 'a {
573        RefMut::map(self.state.borrow_mut(), |state| &mut state.channel_buffers)
574    }
575
576    pub fn summarize_contacts(&self, cx: &TestAppContext) -> ContactsSummary {
577        self.app_state
578            .user_store
579            .read_with(cx, |store, _| ContactsSummary {
580                current: store
581                    .contacts()
582                    .iter()
583                    .map(|contact| contact.user.github_login.clone())
584                    .collect(),
585                outgoing_requests: store
586                    .outgoing_contact_requests()
587                    .iter()
588                    .map(|user| user.github_login.clone())
589                    .collect(),
590                incoming_requests: store
591                    .incoming_contact_requests()
592                    .iter()
593                    .map(|user| user.github_login.clone())
594                    .collect(),
595            })
596    }
597
598    pub async fn build_local_project(
599        &self,
600        root_path: impl AsRef<Path>,
601        cx: &mut TestAppContext,
602    ) -> (Model<Project>, WorktreeId) {
603        let project = self.build_empty_local_project(cx);
604        let (worktree, _) = project
605            .update(cx, |p, cx| {
606                p.find_or_create_local_worktree(root_path, true, cx)
607            })
608            .await
609            .unwrap();
610        worktree
611            .read_with(cx, |tree, _| tree.as_local().unwrap().scan_complete())
612            .await;
613        (project, worktree.read_with(cx, |tree, _| tree.id()))
614    }
615
616    pub async fn build_test_project(&self, cx: &mut TestAppContext) -> Model<Project> {
617        self.fs()
618            .insert_tree(
619                "/a",
620                json!({
621                    "1.txt": "one\none\none",
622                    "2.txt": "two\ntwo\ntwo",
623                    "3.txt": "three\nthree\nthree",
624                }),
625            )
626            .await;
627        self.build_local_project("/a", cx).await.0
628    }
629
630    pub fn build_empty_local_project(&self, cx: &mut TestAppContext) -> Model<Project> {
631        cx.update(|cx| {
632            Project::local(
633                self.client().clone(),
634                self.app_state.node_runtime.clone(),
635                self.app_state.user_store.clone(),
636                self.app_state.languages.clone(),
637                self.app_state.fs.clone(),
638                cx,
639            )
640        })
641    }
642
643    pub async fn build_remote_project(
644        &self,
645        host_project_id: u64,
646        guest_cx: &mut TestAppContext,
647    ) -> Model<Project> {
648        let active_call = guest_cx.read(ActiveCall::global);
649        let room = active_call.read_with(guest_cx, |call, _| call.room().unwrap().clone());
650        room.update(guest_cx, |room, cx| {
651            room.join_project(
652                host_project_id,
653                self.app_state.languages.clone(),
654                self.app_state.fs.clone(),
655                cx,
656            )
657        })
658        .await
659        .unwrap()
660    }
661
662    pub fn build_workspace<'a>(
663        &'a self,
664        project: &Model<Project>,
665        cx: &'a mut TestAppContext,
666    ) -> (View<Workspace>, &'a mut VisualTestContext) {
667        cx.add_window_view(|cx| {
668            cx.activate_window();
669            Workspace::new(0, project.clone(), self.app_state.clone(), cx)
670        })
671    }
672
673    pub fn active_workspace<'a>(
674        &'a self,
675        cx: &'a mut TestAppContext,
676    ) -> (View<Workspace>, &'a mut VisualTestContext) {
677        let window = cx.update(|cx| cx.active_window().unwrap().downcast::<Workspace>().unwrap());
678
679        let view = window.root_view(cx).unwrap();
680        let cx = Box::new(VisualTestContext::from_window(*window.deref(), cx));
681        // it might be nice to try and cleanup these at the end of each test.
682        (view, Box::leak(cx))
683    }
684}
685
686impl Drop for TestClient {
687    fn drop(&mut self) {
688        self.app_state.client.teardown();
689    }
690}