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 start2(
117        cx_a: &mut TestAppContext,
118        cx_b: &mut TestAppContext,
119    ) -> (TestClient, TestClient, u64) {
120        let mut server = Self::start(cx_a.executor()).await;
121        let client_a = server.create_client(cx_a, "user_a").await;
122        let client_b = server.create_client(cx_b, "user_b").await;
123        let channel_id = server
124            .make_channel("a", None, (&client_a, cx_a), &mut [(&client_b, cx_b)])
125            .await;
126
127        (client_a, client_b, channel_id)
128    }
129
130    pub async fn reset(&self) {
131        self.app_state.db.reset();
132        let epoch = self
133            .app_state
134            .db
135            .create_server(&self.app_state.config.zed_environment)
136            .await
137            .unwrap();
138        self.server.reset(epoch);
139    }
140
141    pub async fn create_client(&mut self, cx: &mut TestAppContext, name: &str) -> TestClient {
142        cx.update(|cx| {
143            if cx.has_global::<SettingsStore>() {
144                panic!("Same cx used to create two test clients")
145            }
146            let settings = SettingsStore::test(cx);
147            cx.set_global(settings);
148        });
149
150        let http = FakeHttpClient::with_404_response();
151        let user_id = if let Ok(Some(user)) = self.app_state.db.get_user_by_github_login(name).await
152        {
153            user.id
154        } else {
155            self.app_state
156                .db
157                .create_user(
158                    &format!("{name}@example.com"),
159                    false,
160                    NewUserParams {
161                        github_login: name.into(),
162                        github_user_id: 0,
163                    },
164                )
165                .await
166                .expect("creating user failed")
167                .user_id
168        };
169        let client_name = name.to_string();
170        let mut client = cx.update(|cx| Client::new(http.clone(), cx));
171        let server = self.server.clone();
172        let db = self.app_state.db.clone();
173        let connection_killers = self.connection_killers.clone();
174        let forbid_connections = self.forbid_connections.clone();
175
176        Arc::get_mut(&mut client)
177            .unwrap()
178            .set_id(user_id.to_proto())
179            .override_authenticate(move |cx| {
180                cx.spawn(|_| async move {
181                    let access_token = "the-token".to_string();
182                    Ok(Credentials {
183                        user_id: user_id.to_proto(),
184                        access_token,
185                    })
186                })
187            })
188            .override_establish_connection(move |credentials, cx| {
189                assert_eq!(credentials.user_id, user_id.0 as u64);
190                assert_eq!(credentials.access_token, "the-token");
191
192                let server = server.clone();
193                let db = db.clone();
194                let connection_killers = connection_killers.clone();
195                let forbid_connections = forbid_connections.clone();
196                let client_name = client_name.clone();
197                cx.spawn(move |cx| async move {
198                    if forbid_connections.load(SeqCst) {
199                        Err(EstablishConnectionError::other(anyhow!(
200                            "server is forbidding connections"
201                        )))
202                    } else {
203                        let (client_conn, server_conn, killed) =
204                            Connection::in_memory(cx.background_executor().clone());
205                        let (connection_id_tx, connection_id_rx) = oneshot::channel();
206                        let user = db
207                            .get_user_by_id(user_id)
208                            .await
209                            .expect("retrieving user failed")
210                            .unwrap();
211                        cx.background_executor()
212                            .spawn(server.handle_connection(
213                                server_conn,
214                                client_name,
215                                user,
216                                None,
217                                Some(connection_id_tx),
218                                Executor::Deterministic(cx.background_executor().clone()),
219                            ))
220                            .detach();
221                        let connection_id = connection_id_rx.await.unwrap();
222                        connection_killers
223                            .lock()
224                            .insert(connection_id.into(), killed);
225                        Ok(client_conn)
226                    }
227                })
228            });
229
230        let fs = FakeFs::new(cx.executor());
231        let user_store = cx.new_model(|cx| UserStore::new(client.clone(), cx));
232        let workspace_store = cx.new_model(|cx| WorkspaceStore::new(client.clone(), cx));
233        let mut language_registry = LanguageRegistry::test();
234        language_registry.set_executor(cx.executor());
235        let app_state = Arc::new(workspace::AppState {
236            client: client.clone(),
237            user_store: user_store.clone(),
238            workspace_store,
239            languages: Arc::new(language_registry),
240            fs: fs.clone(),
241            build_window_options: |_, _, _| Default::default(),
242            node_runtime: FakeNodeRuntime::new(),
243        });
244
245        cx.update(|cx| {
246            theme::init(theme::LoadThemes::JustBase, cx);
247            Project::init(&client, cx);
248            client::init(&client, cx);
249            language::init(cx);
250            editor::init(cx);
251            workspace::init(app_state.clone(), cx);
252            call::init(client.clone(), user_store.clone(), cx);
253            channel::init(&client, user_store.clone(), cx);
254            notifications::init(client.clone(), user_store, cx);
255            collab_ui::init(&app_state, cx);
256            file_finder::init(cx);
257            menu::init();
258            settings::KeymapFile::load_asset("keymaps/default.json", cx).unwrap();
259        });
260
261        client
262            .authenticate_and_connect(false, &cx.to_async())
263            .await
264            .unwrap();
265
266        let client = TestClient {
267            app_state,
268            username: name.to_string(),
269            channel_store: cx.read(ChannelStore::global).clone(),
270            notification_store: cx.read(NotificationStore::global).clone(),
271            state: Default::default(),
272        };
273        client.wait_for_current_user(cx).await;
274        client
275    }
276
277    pub fn disconnect_client(&self, peer_id: PeerId) {
278        self.connection_killers
279            .lock()
280            .remove(&peer_id)
281            .unwrap()
282            .store(true, SeqCst);
283    }
284
285    pub fn simulate_long_connection_interruption(
286        &self,
287        peer_id: PeerId,
288        deterministic: BackgroundExecutor,
289    ) {
290        self.forbid_connections();
291        self.disconnect_client(peer_id);
292        deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
293        self.allow_connections();
294        deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
295        deterministic.run_until_parked();
296    }
297
298    pub fn forbid_connections(&self) {
299        self.forbid_connections.store(true, SeqCst);
300    }
301
302    pub fn allow_connections(&self) {
303        self.forbid_connections.store(false, SeqCst);
304    }
305
306    pub async fn make_contacts(&self, clients: &mut [(&TestClient, &mut TestAppContext)]) {
307        for ix in 1..clients.len() {
308            let (left, right) = clients.split_at_mut(ix);
309            let (client_a, cx_a) = left.last_mut().unwrap();
310            for (client_b, cx_b) in right {
311                client_a
312                    .app_state
313                    .user_store
314                    .update(*cx_a, |store, cx| {
315                        store.request_contact(client_b.user_id().unwrap(), cx)
316                    })
317                    .await
318                    .unwrap();
319                cx_a.executor().run_until_parked();
320                client_b
321                    .app_state
322                    .user_store
323                    .update(*cx_b, |store, cx| {
324                        store.respond_to_contact_request(client_a.user_id().unwrap(), true, cx)
325                    })
326                    .await
327                    .unwrap();
328            }
329        }
330    }
331
332    pub async fn make_channel(
333        &self,
334        channel: &str,
335        parent: Option<u64>,
336        admin: (&TestClient, &mut TestAppContext),
337        members: &mut [(&TestClient, &mut TestAppContext)],
338    ) -> u64 {
339        let (_, admin_cx) = admin;
340        let channel_id = admin_cx
341            .read(ChannelStore::global)
342            .update(admin_cx, |channel_store, cx| {
343                channel_store.create_channel(channel, parent, cx)
344            })
345            .await
346            .unwrap();
347
348        for (member_client, member_cx) in members {
349            admin_cx
350                .read(ChannelStore::global)
351                .update(admin_cx, |channel_store, cx| {
352                    channel_store.invite_member(
353                        channel_id,
354                        member_client.user_id().unwrap(),
355                        ChannelRole::Member,
356                        cx,
357                    )
358                })
359                .await
360                .unwrap();
361
362            admin_cx.executor().run_until_parked();
363
364            member_cx
365                .read(ChannelStore::global)
366                .update(*member_cx, |channels, cx| {
367                    channels.respond_to_channel_invite(channel_id, true, cx)
368                })
369                .await
370                .unwrap();
371        }
372
373        channel_id
374    }
375
376    pub async fn make_public_channel(
377        &self,
378        channel: &str,
379        client: &TestClient,
380        cx: &mut TestAppContext,
381    ) -> u64 {
382        let channel_id = self
383            .make_channel(channel, None, (client, cx), &mut [])
384            .await;
385
386        client
387            .channel_store()
388            .update(cx, |channel_store, cx| {
389                channel_store.set_channel_visibility(
390                    channel_id,
391                    proto::ChannelVisibility::Public,
392                    cx,
393                )
394            })
395            .await
396            .unwrap();
397
398        channel_id
399    }
400
401    pub async fn make_channel_tree(
402        &self,
403        channels: &[(&str, Option<&str>)],
404        creator: (&TestClient, &mut TestAppContext),
405    ) -> Vec<u64> {
406        let mut observed_channels = HashMap::default();
407        let mut result = Vec::new();
408        for (channel, parent) in channels {
409            let id;
410            if let Some(parent) = parent {
411                if let Some(parent_id) = observed_channels.get(parent) {
412                    id = self
413                        .make_channel(channel, Some(*parent_id), (creator.0, creator.1), &mut [])
414                        .await;
415                } else {
416                    panic!(
417                        "Edge {}->{} referenced before {} was created",
418                        parent, channel, parent
419                    )
420                }
421            } else {
422                id = self
423                    .make_channel(channel, None, (creator.0, creator.1), &mut [])
424                    .await;
425            }
426
427            observed_channels.insert(channel, id);
428            result.push(id);
429        }
430
431        result
432    }
433
434    pub async fn create_room(&self, clients: &mut [(&TestClient, &mut TestAppContext)]) {
435        self.make_contacts(clients).await;
436
437        let (left, right) = clients.split_at_mut(1);
438        let (_client_a, cx_a) = &mut left[0];
439        let active_call_a = cx_a.read(ActiveCall::global);
440
441        for (client_b, cx_b) in right {
442            let user_id_b = client_b.current_user_id(*cx_b).to_proto();
443            active_call_a
444                .update(*cx_a, |call, cx| call.invite(user_id_b, None, cx))
445                .await
446                .unwrap();
447
448            cx_b.executor().run_until_parked();
449            let active_call_b = cx_b.read(ActiveCall::global);
450            active_call_b
451                .update(*cx_b, |call, cx| call.accept_incoming(cx))
452                .await
453                .unwrap();
454        }
455    }
456
457    pub async fn build_app_state(
458        test_db: &TestDb,
459        fake_server: &live_kit_client::TestServer,
460    ) -> Arc<AppState> {
461        Arc::new(AppState {
462            db: test_db.db().clone(),
463            live_kit_client: Some(Arc::new(fake_server.create_api_client())),
464            config: Config {
465                http_port: 0,
466                database_url: "".into(),
467                database_max_connections: 0,
468                api_token: "".into(),
469                invite_link_prefix: "".into(),
470                live_kit_server: None,
471                live_kit_key: None,
472                live_kit_secret: None,
473                rust_log: None,
474                log_json: None,
475                zed_environment: "test".into(),
476            },
477        })
478    }
479}
480
481impl Deref for TestServer {
482    type Target = Server;
483
484    fn deref(&self) -> &Self::Target {
485        &self.server
486    }
487}
488
489impl Drop for TestServer {
490    fn drop(&mut self) {
491        self.server.teardown();
492        self.test_live_kit_server.teardown().unwrap();
493    }
494}
495
496impl Deref for TestClient {
497    type Target = Arc<Client>;
498
499    fn deref(&self) -> &Self::Target {
500        &self.app_state.client
501    }
502}
503
504impl TestClient {
505    pub fn fs(&self) -> &FakeFs {
506        self.app_state.fs.as_fake()
507    }
508
509    pub fn channel_store(&self) -> &Model<ChannelStore> {
510        &self.channel_store
511    }
512
513    pub fn notification_store(&self) -> &Model<NotificationStore> {
514        &self.notification_store
515    }
516
517    pub fn user_store(&self) -> &Model<UserStore> {
518        &self.app_state.user_store
519    }
520
521    pub fn language_registry(&self) -> &Arc<LanguageRegistry> {
522        &self.app_state.languages
523    }
524
525    pub fn client(&self) -> &Arc<Client> {
526        &self.app_state.client
527    }
528
529    pub fn current_user_id(&self, cx: &TestAppContext) -> UserId {
530        UserId::from_proto(
531            self.app_state
532                .user_store
533                .read_with(cx, |user_store, _| user_store.current_user().unwrap().id),
534        )
535    }
536
537    pub async fn wait_for_current_user(&self, cx: &TestAppContext) {
538        let mut authed_user = self
539            .app_state
540            .user_store
541            .read_with(cx, |user_store, _| user_store.watch_current_user());
542        while authed_user.next().await.unwrap().is_none() {}
543    }
544
545    pub async fn clear_contacts(&self, cx: &mut TestAppContext) {
546        self.app_state
547            .user_store
548            .update(cx, |store, _| store.clear_contacts())
549            .await;
550    }
551
552    pub fn local_projects<'a>(&'a self) -> impl Deref<Target = Vec<Model<Project>>> + 'a {
553        Ref::map(self.state.borrow(), |state| &state.local_projects)
554    }
555
556    pub fn remote_projects<'a>(&'a self) -> impl Deref<Target = Vec<Model<Project>>> + 'a {
557        Ref::map(self.state.borrow(), |state| &state.remote_projects)
558    }
559
560    pub fn local_projects_mut<'a>(&'a self) -> impl DerefMut<Target = Vec<Model<Project>>> + 'a {
561        RefMut::map(self.state.borrow_mut(), |state| &mut state.local_projects)
562    }
563
564    pub fn remote_projects_mut<'a>(&'a self) -> impl DerefMut<Target = Vec<Model<Project>>> + 'a {
565        RefMut::map(self.state.borrow_mut(), |state| &mut state.remote_projects)
566    }
567
568    pub fn buffers_for_project<'a>(
569        &'a self,
570        project: &Model<Project>,
571    ) -> impl DerefMut<Target = HashSet<Model<language::Buffer>>> + 'a {
572        RefMut::map(self.state.borrow_mut(), |state| {
573            state.buffers.entry(project.clone()).or_default()
574        })
575    }
576
577    pub fn buffers<'a>(
578        &'a self,
579    ) -> impl DerefMut<Target = HashMap<Model<Project>, HashSet<Model<language::Buffer>>>> + 'a
580    {
581        RefMut::map(self.state.borrow_mut(), |state| &mut state.buffers)
582    }
583
584    pub fn channel_buffers<'a>(
585        &'a self,
586    ) -> impl DerefMut<Target = HashSet<Model<ChannelBuffer>>> + 'a {
587        RefMut::map(self.state.borrow_mut(), |state| &mut state.channel_buffers)
588    }
589
590    pub fn summarize_contacts(&self, cx: &TestAppContext) -> ContactsSummary {
591        self.app_state
592            .user_store
593            .read_with(cx, |store, _| ContactsSummary {
594                current: store
595                    .contacts()
596                    .iter()
597                    .map(|contact| contact.user.github_login.clone())
598                    .collect(),
599                outgoing_requests: store
600                    .outgoing_contact_requests()
601                    .iter()
602                    .map(|user| user.github_login.clone())
603                    .collect(),
604                incoming_requests: store
605                    .incoming_contact_requests()
606                    .iter()
607                    .map(|user| user.github_login.clone())
608                    .collect(),
609            })
610    }
611
612    pub async fn build_local_project(
613        &self,
614        root_path: impl AsRef<Path>,
615        cx: &mut TestAppContext,
616    ) -> (Model<Project>, WorktreeId) {
617        let project = self.build_empty_local_project(cx);
618        let (worktree, _) = project
619            .update(cx, |p, cx| {
620                p.find_or_create_local_worktree(root_path, true, cx)
621            })
622            .await
623            .unwrap();
624        worktree
625            .read_with(cx, |tree, _| tree.as_local().unwrap().scan_complete())
626            .await;
627        (project, worktree.read_with(cx, |tree, _| tree.id()))
628    }
629
630    pub async fn build_test_project(&self, cx: &mut TestAppContext) -> Model<Project> {
631        self.fs()
632            .insert_tree(
633                "/a",
634                json!({
635                    "1.txt": "one\none\none",
636                    "2.js": "function two() { return 2; }",
637                    "3.rs": "mod test",
638                }),
639            )
640            .await;
641        self.build_local_project("/a", cx).await.0
642    }
643
644    pub async fn host_workspace(
645        &self,
646        workspace: &View<Workspace>,
647        channel_id: u64,
648        cx: &mut VisualTestContext,
649    ) {
650        cx.update(|cx| {
651            let active_call = ActiveCall::global(cx);
652            active_call.update(cx, |call, cx| call.join_channel(channel_id, cx))
653        })
654        .await
655        .unwrap();
656        cx.update(|cx| {
657            let active_call = ActiveCall::global(cx);
658            let project = workspace.read(cx).project().clone();
659            active_call.update(cx, |call, cx| call.share_project(project, cx))
660        })
661        .await
662        .unwrap();
663        cx.executor().run_until_parked();
664    }
665
666    pub async fn join_workspace<'a>(
667        &'a self,
668        channel_id: u64,
669        cx: &'a mut TestAppContext,
670    ) -> (View<Workspace>, &'a mut VisualTestContext) {
671        cx.update(|cx| workspace::join_channel(channel_id, self.app_state.clone(), None, cx))
672            .await
673            .unwrap();
674        cx.run_until_parked();
675
676        self.active_workspace(cx)
677    }
678
679    pub fn build_empty_local_project(&self, cx: &mut TestAppContext) -> Model<Project> {
680        cx.update(|cx| {
681            Project::local(
682                self.client().clone(),
683                self.app_state.node_runtime.clone(),
684                self.app_state.user_store.clone(),
685                self.app_state.languages.clone(),
686                self.app_state.fs.clone(),
687                cx,
688            )
689        })
690    }
691
692    pub async fn build_remote_project(
693        &self,
694        host_project_id: u64,
695        guest_cx: &mut TestAppContext,
696    ) -> Model<Project> {
697        let active_call = guest_cx.read(ActiveCall::global);
698        let room = active_call.read_with(guest_cx, |call, _| call.room().unwrap().clone());
699        room.update(guest_cx, |room, cx| {
700            room.join_project(
701                host_project_id,
702                self.app_state.languages.clone(),
703                self.app_state.fs.clone(),
704                cx,
705            )
706        })
707        .await
708        .unwrap()
709    }
710
711    pub fn build_workspace<'a>(
712        &'a self,
713        project: &Model<Project>,
714        cx: &'a mut TestAppContext,
715    ) -> (View<Workspace>, &'a mut VisualTestContext) {
716        cx.add_window_view(|cx| {
717            cx.activate_window();
718            Workspace::new(0, project.clone(), self.app_state.clone(), cx)
719        })
720    }
721
722    pub async fn build_test_workspace<'a>(
723        &'a self,
724        cx: &'a mut TestAppContext,
725    ) -> (View<Workspace>, &'a mut VisualTestContext) {
726        let project = self.build_test_project(cx).await;
727        cx.add_window_view(|cx| {
728            cx.activate_window();
729            Workspace::new(0, project.clone(), self.app_state.clone(), cx)
730        })
731    }
732
733    pub fn active_workspace<'a>(
734        &'a self,
735        cx: &'a mut TestAppContext,
736    ) -> (View<Workspace>, &'a mut VisualTestContext) {
737        let window = cx.update(|cx| cx.active_window().unwrap().downcast::<Workspace>().unwrap());
738
739        let view = window.root_view(cx).unwrap();
740        let cx = Box::new(VisualTestContext::from_window(*window.deref(), cx));
741        // it might be nice to try and cleanup these at the end of each test.
742        (view, Box::leak(cx))
743    }
744}
745
746impl Drop for TestClient {
747    fn drop(&mut self) {
748        self.app_state.client.teardown();
749    }
750}