forge
Explore
Log in

Don't you dare go Hollow

Snippet streams follow writes

Browse files at this commit

6388b13f7621f793b830cda42f1f3346855279e7Ahmet Kaan GümüşParent: 8122bc8
  • test
    • snippet streams follow writes
    • docs list the wiki and snippet streams
Show full message
✅ test: snippet streams follow writes; docs list the wiki and snippet streams
crates/forge-web/tests/snippets_e2e.rsmodified+156 -0
@@ -215,6 +215,162 @@ async fn create(
215 .to_owned()215 .to_owned()
216 }216 }
217 217
218+/// Opens a live stream as `cookie` (or anonymously) and reads its `retry` field. Returns the
219+/// status with the response, so a refused stream can be checked too.
220+async fn open_stream(http: &Http, path: &str, cookie: Option<&str>) -> reqwest::Response {
221+ let mut req = http.client.get(format!("{}{path}", http.base));
222+ if let Some(cookie) = cookie {
223+ req = req.header(COOKIE, cookie);
224+ }
225+ let mut res = req.send().await.expect("stream request");
226+ if res.status().is_success() {
227+ let retry = tokio::time::timeout(Duration::from_secs(5), res.chunk())
228+ .await
229+ .expect("the retry field")
230+ .expect("stream chunk")
231+ .expect("stream still open");
232+ assert!(String::from_utf8_lossy(&retry).contains("retry: 1000"));
233+ }
234+ res
235+}
236+
237+/// Waits for the next event of a stream, which must be `changed`.
238+async fn next_change(res: &mut reqwest::Response, what: &str) {
239+ let chunk = tokio::time::timeout(Duration::from_secs(5), res.chunk())
240+ .await
241+ .unwrap_or_else(|_| panic!("no event after {what}"))
242+ .expect("stream chunk")
243+ .expect("stream still open");
244+ let text = String::from_utf8_lossy(&chunk);
245+ assert!(text.contains("event: changed"), "{what}: {text}");
246+}
247+
248+/// The list and the snippet page follow their streams. A create, an edit and a delete each wake
249+/// them, and a private snippet's stream answers 404 to everyone but its owner.
250+#[tokio::test]
251+async fn snippet_streams_follow_writes() {
252+ let env = start().await;
253+ let tahinli = login(&env.app, "tahinli").await;
254+ let bob = login(&env.app, "bob").await;
255+
256+ let id = create(
257+ &env.app,
258+ &tahinli,
259+ "Live",
260+ "public",
261+ &[("live.txt", "one\n")],
262+ )
263+ .await;
264+ let item = format!("/snippets/{id}/events");
265+
266+ // The pages render the streams they follow.
267+ let listing = env.app.get("/snippets", Some(&tahinli)).await;
268+ assert!(
269+ listing.body.contains(r#"data-live="/snippets/events""#),
270+ "list follows its stream"
271+ );
272+ let page = env
273+ .app
274+ .get(&format!("/snippets/{id}"), Some(&tahinli))
275+ .await;
276+ assert!(
277+ page.body.contains(&format!(r#"data-live="{item}""#)),
278+ "snippet page follows its stream"
279+ );
280+
281+ let mut list_events = open_stream(&env.app, "/snippets/events", Some(&tahinli)).await;
282+ assert_eq!(list_events.status().as_u16(), 200, "list stream");
283+ let mut item_events = open_stream(&env.app, &item, Some(&tahinli)).await;
284+ assert_eq!(item_events.status().as_u16(), 200, "snippet stream");
285+
286+ // An edit wakes both.
287+ let edit_path = format!("/snippets/{id}/edit");
288+ let csrf = input_value(&env.app.get(&edit_path, Some(&tahinli)).await.body, "csrf");
289+ let saved = env
290+ .app
291+ .post_form(
292+ &edit_path,
293+ &[
294+ ("csrf", &csrf),
295+ ("title", "Live v2"),
296+ ("description", ""),
297+ ("visibility", "public"),
298+ ("name_0", "live.txt"),
299+ ("content_0", "two\n"),
300+ ],
301+ Some(&tahinli),
302+ )
303+ .await;
304+ assert_eq!(saved.status, 303, "edit: {}", saved.body);
305+ next_change(&mut list_events, "an edit").await;
306+ next_change(&mut item_events, "an edit").await;
307+
308+ // A create wakes the list only.
309+ let other = create(
310+ &env.app,
311+ &tahinli,
312+ "Second",
313+ "public",
314+ &[("two.txt", "2\n")],
315+ )
316+ .await;
317+ next_change(&mut list_events, "a create").await;
318+
319+ // A delete wakes the list, and the snippet's stream sends `changed` too: its page now 404s.
320+ let csrf = input_value(
321+ &env.app
322+ .get(&format!("/snippets/{other}"), Some(&tahinli))
323+ .await
324+ .body,
325+ "csrf",
326+ );
327+ let deleted = env
328+ .app
329+ .post_form(
330+ &format!("/snippets/{other}/delete"),
331+ &[("csrf", &csrf)],
332+ Some(&tahinli),
333+ )
334+ .await;
335+ assert_eq!(deleted.status, 303, "delete: {}", deleted.body);
336+ next_change(&mut list_events, "a delete").await;
337+
338+ // A private snippet: the owner's stream opens, everyone else gets 404 (anonymous too).
339+ let secret = create(
340+ &env.app,
341+ &tahinli,
342+ "Secret",
343+ "private",
344+ &[("s.txt", "shh\n")],
345+ )
346+ .await;
347+ let secret_item = format!("/snippets/{secret}/events");
348+ assert_eq!(
349+ open_stream(&env.app, &secret_item, Some(&tahinli))
350+ .await
351+ .status()
352+ .as_u16(),
353+ 200,
354+ "owner opens the private stream"
355+ );
356+ assert_eq!(
357+ open_stream(&env.app, &secret_item, Some(&bob))
358+ .await
359+ .status()
360+ .as_u16(),
361+ 404,
362+ "others get 404"
363+ );
364+ assert_eq!(
365+ open_stream(&env.app, &secret_item, None)
366+ .await
367+ .status()
368+ .as_u16(),
369+ 404,
370+ "anonymous gets 404"
371+ );
372+}
373+
218 #[tokio::test]374 #[tokio::test]
219 async fn create_edit_and_revisions() {375 async fn create_edit_and_revisions() {
220 let env = start().await;376 let env = start().await;
docs/architecture.mdmodified+2 -0
@@ -260,6 +260,8 @@ Pages update without a reload. A write that changes what a page shows calls `Liv
260 | Issues `/{owner}/{repo}/issues` and an issue | `/{owner}/{repo}/issues/live`, `/{owner}/{repo}/issues/{number}/live` | the repo issues (list, labels, milestones, tabs) and the one issue |260 | Issues `/{owner}/{repo}/issues` and an issue | `/{owner}/{repo}/issues/live`, `/{owner}/{repo}/issues/{number}/live` | the repo issues (list, labels, milestones, tabs) and the one issue |
261 | Pull requests `/{owner}/{repo}/pulls` and a pull | `/{owner}/{repo}/pulls/events`, `/{owner}/{repo}/pulls/{number}/events` | the repo pull list, and the one pull (conversation, commits, files) |261 | Pull requests `/{owner}/{repo}/pulls` and a pull | `/{owner}/{repo}/pulls/events`, `/{owner}/{repo}/pulls/{number}/events` | the repo pull list, and the one pull (conversation, commits, files) |
262 | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |262 | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |
263+| Wiki `/{owner}/{repo}/wiki`, page list, a page and its history | `/{owner}/{repo}/wiki/-/events` (home and list), `/{owner}/{repo}/wiki/{page}/events` (a page and its history) | the repo's wiki: a web save, or a push to the wiki repo |
264+| Snippets `/snippets`, a snippet and its revisions | `/snippets/events` (list), `/snippets/{id}/events` (one snippet) | the snippet list and the snippet; a private snippet answers 404 to everyone but its owner |
263 265
264 The user topics are keyed by user id, and their streams take the viewer from the session, so no URL names another user's inbox. Repo streams follow the same visibility as the repo page (a private repo answers 404 to a viewer who cannot read it). Explore lists public repos only.266 The user topics are keyed by user id, and their streams take the viewer from the session, so no URL names another user's inbox. Repo streams follow the same visibility as the repo page (a private repo answers 404 to a viewer who cannot read it). Explore lists public repos only.
265 267