use crate::common::{ BOB_CMD, Http, PW, TAHINLI_CMD, boot_forge, commit_file, create_repo, create_token, git_text, login, relaunch, text_or_empty, tool_on_path, }; use std::future::Future; use std::time::{Duration, Instant}; use forge_core::Store; async fn eventually(what: &str, mut check: F) where F: FnMut() -> Fut, Fut: Future, { let deadline = Instant::now() + Duration::from_secs(5); while !check().await { assert!( Instant::now() < deadline, "timed out after 5s waiting for {what}" ); tokio::time::sleep(Duration::from_millis(100)).await; } } async fn issue_page(http: &Http, session: &str, number: u32) -> String { let res = http .get_cookie(&format!("/tahinli/pub/issues/{number}"), session) .await; assert_eq!(res.status, 200, "GET issue {number}"); res.body } #[tokio::test] async fn pushes_close_and_reference_issues() { if !tool_on_path("git") { eprintln!("SKIP issue_close_e2e: `git` was not found on PATH"); return; } let root = crate::common::temp_dir("ice2e"); let dir = root.path().to_path_buf(); let config = dir.join("forge.toml"); let log_path = dir.join("serve.log"); let (mut server, ports) = boot_forge(&dir, "", &[TAHINLI_CMD, BOB_CMD]).await; let http_port = ports.http; let http = Http::new(http_port); let tahinli = login(&http, "tahinli", PW).await; let token = create_token(&http, &tahinli, "issue-close-token").await; create_repo(&http, &tahinli, "pub", "public").await; drop(server); { let store = Store::open(&dir.join("forge.db")) .await .expect("open store"); let author = store .user_by_username("tahinli") .await .expect("user lookup") .expect("tahinli exists"); let repo = store .repo_by_owner_and_name("tahinli", "pub") .await .expect("repo lookup") .expect("repo exists"); for n in 1..=3 { store .create_issue(&repo.id, &author.id, &format!("issue {n}"), "body") .await .expect("create issue"); } } server = relaunch(&config, &log_path, ports).await; let tahinli = login(&http, "tahinli", PW).await; let url = format!("http://tahinli:{token}@127.0.0.1:{http_port}/tahinli/pub.git"); let work = dir.join("work"); git_text( &dir, &["clone", "-q", &url, work.to_str().expect("utf-8 path")], ); let base = commit_file(&work, "base.txt", "base commit", "base commit"); git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); git_text(&work, &["checkout", "-q", "-b", "topic"]); let topic_oid = commit_file( &work, "topic.txt", "fixes #3 on a topic branch", "fixes #3 on a topic branch", ); git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/topic"]); git_text(&work, &["checkout", "-q", "-B", "mainline", &base]); let main_oid = commit_file(&work, "main.txt", "Fixes #1, refs #2", "Fixes #1, refs #2"); git_text( &work, &["push", "-q", "origin", "HEAD:refs/heads/dup-of-main"], ); git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); let commit_link = format!("/commit/{main_oid}"); let link_ref = commit_link.as_str(); let (http_ref, tahinli_ref) = (&http, tahinli.as_str()); eventually("#1 closed by the push", || async move { text_or_empty(&issue_page(http_ref, tahinli_ref, 1).await, ".issue-badge") == "Closed" }) .await; let page1 = issue_page(&http, &tahinli, 1).await; assert!( page1.contains(&commit_link), "#1 timeline should link the closing commit" ); eventually("#2 referenced by the push", || async move { let page = issue_page(http_ref, tahinli_ref, 2).await; page.contains("referenced this") && page.contains(link_ref) }) .await; let page2 = issue_page(&http, &tahinli, 2).await; assert_eq!( text_or_empty(&page2, ".issue-badge"), "Open", "#2 is only referenced" ); tokio::time::sleep(Duration::from_secs(2)).await; let page2 = issue_page(&http, &tahinli, 2).await; assert_eq!( page2.matches("referenced this").count(), 1, "referenced event must not be duplicated" ); let page3 = issue_page(&http, &tahinli, 3).await; assert_eq!( text_or_empty(&page3, ".issue-badge"), "Open", "#3 must not be closed from topic" ); assert!( !page3.contains(&format!("/commit/{topic_oid}")), "non-default branch pushes are not processed" ); drop(server); }