test_server.rs

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