forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.4 MiB·7b1d61e·Tahinli·5 minutes ago·365 commits
| 1 | use topcoat::Result; |
| 2 | use topcoat::context::{Cx, context}; |
| 3 | use topcoat::router::response::Response; |
| 4 | use topcoat::router::{path_param, route}; |
| 5 | |
| 6 | use crate::live::{LiveHub, Topic, stream_wakes}; |
| 7 | use crate::pages::repo_ctx::load_repo; |
| 8 | use crate::state::AppState; |
| 9 | |
| 10 | path_param!(owner); |
| 11 | path_param!(repo); |
| 12 | |
| 13 | pub fn live_url(owner: &str, repo: &str) -> String { |
| 14 | format!("/{owner}/{repo}/refs/events") |
| 15 | } |
| 16 | |
| 17 | pub 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")] |
| 26 | async 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)] |
| 35 | mod 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 | } |