forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.4 MiB7b1d61eTahinli5 minutes ago365 commits

ref_live.rs105 lines, 3.3 KiBHistoryRawDownload
1use topcoat::Result;
2use topcoat::context::{Cx, context};
3use topcoat::router::response::Response;
4use topcoat::router::{path_param, route};
5
6use crate::live::{LiveHub, Topic, stream_wakes};
7use crate::pages::repo_ctx::load_repo;
8use crate::state::AppState;
9
10path_param!(owner);
11path_param!(repo);
12
13pub fn live_url(owner: &str, repo: &str) -> String {
14 format!("/{owner}/{repo}/refs/events")
15}
16
17pub fn topics(repo: &forge_core::Repo) -> [Topic; 3] {
18 [
19 Topic::Repo(repo.id.clone()),
20 Topic::RepoRefs(repo.id.clone()),
21 Topic::CommitStatuses(repo.id.clone()),
22 ]
23}
24
25#[route(GET "/{owner}/{repo}/refs/events")]
26async fn ref_events(cx: &Cx) -> Result<Response> {
27 let owner = owner(cx);
28 let name = repo(cx);
29 let ctx = load_repo(cx, owner, name).await?;
30 let hub: &LiveHub = &context::<AppState>(cx).live;
31 stream_wakes(cx, hub, &topics(&ctx.repo))
32}
33
34#[cfg(test)]
35mod tests {
36 use std::time::Duration;
37
38 use forge_core::Visibility;
39 use topcoat::router::Body;
40 use topcoat::router::request::Request;
41
42 use crate::live::Topic;
43 use crate::test_support::fixture::{Fixture, OWNER, REPO};
44
45 async fn open(fx: &Fixture, path: &str) -> topcoat::router::response::Response {
46 let req = Request::builder()
47 .method("GET")
48 .uri(path)
49 .body(Body::empty())
50 .unwrap();
51 fx.router.handle(req).await
52 }
53
54 #[tokio::test]
55 async fn a_ref_write_or_a_status_wakes_the_stream() {
56 use futures_util::StreamExt;
57
58 let fx = Fixture::new(Visibility::Public).await;
59 let repo = fx
60 .store
61 .repo_by_owner_and_name(OWNER, REPO)
62 .await
63 .unwrap()
64 .unwrap();
65 let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await;
66 assert_eq!(res.status().as_u16(), 200);
67 assert_eq!(
68 res.headers().get("content-type").unwrap(),
69 "text/event-stream"
70 );
71 let mut frames = res.into_body().into_data_stream();
72 let retry = frames.next().await.expect("a frame").expect("data");
73 assert!(retry.starts_with(b"retry: 1000"), "{retry:?}");
74
75 fx.live.bump(&Topic::RepoRefs(repo.id.clone()));
76 let first = tokio::time::timeout(Duration::from_secs(5), frames.next())
77 .await
78 .expect("woken")
79 .expect("a frame")
80 .expect("data");
81 assert_eq!(&first[..], b"event: changed\ndata: 1\n\n");
82
83 fx.live.bump(&Topic::CommitStatuses(repo.id.clone()));
84 let second = tokio::time::timeout(Duration::from_secs(5), frames.next())
85 .await
86 .expect("woken")
87 .expect("a frame")
88 .expect("data");
89 assert_eq!(&second[..], b"event: changed\ndata: 1\n\n");
90 }
91
92 #[tokio::test]
93 async fn a_private_repo_stream_is_404_for_anonymous() {
94 let fx = Fixture::new(Visibility::Private).await;
95 let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await;
96 assert_eq!(res.status().as_u16(), 404);
97 }
98
99 #[tokio::test]
100 async fn an_unknown_repo_stream_is_404() {
101 let fx = Fixture::new(Visibility::Public).await;
102 let res = open(&fx, &format!("/{OWNER}/nope/refs/events")).await;
103 assert_eq!(res.status().as_u16(), 404);
104 }
105}