use forge_core::Visibility; use forge_web_app::test_support::fixture::{Fixture, git}; use forge_web_pages::pages::issues_list::test_util::{CSRF, OWNER, REPO, TOKEN, send}; const BUG: &str = "---\nname: Bug report\nabout: Something broke\ntitle: \"[Bug]: \"\nlabels: bug, needs triage\n---\n## Steps to reproduce\n\n1. Do the thing\n"; const FEATURE: &str = "---\nname: Feature request\nabout: Suggest an idea\n---\n## Idea\n"; async fn setup(files: &[(&str, &str)]) -> Fixture { let fx = Fixture::new(Visibility::Public).await; for (path, text) in files { let full = fx.work.join(path); std::fs::create_dir_all(full.parent().unwrap()).unwrap(); std::fs::write(full, text).unwrap(); } git(&fx.work, &["add", "-A"]); git(&fx.work, &["commit", "-q", "-m", "Add templates"]); git( &fx.work, &[ "push", "-q", &fx.bare.to_string_lossy(), "HEAD:refs/heads/main", ], ); let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); forge_web_app::test_support::create_session(&fx.store, &owner.id, TOKEN, CSRF).await; fx } fn new_issue_uri(query: &str) -> String { format!("/{OWNER}/{REPO}/issues/new{query}") } fn tag_at(html: &str, needle: &str) -> String { let at = html.find(needle).expect("needle in page"); let end = html[at..].find('>').expect("tag end") + at; html[at..end].to_owned() } #[tokio::test] async fn chosen_template_prefills_and_preselects_existing_labels() { let fx = setup(&[ (".forge/ISSUE_TEMPLATE/bug.md", BUG), (".forge/ISSUE_TEMPLATE/feature.md", FEATURE), ]) .await; let repo = fx .store .repo_by_owner_and_name(OWNER, REPO) .await .unwrap() .unwrap(); fx.store .create_label(&repo.id, "bug", "#d73a4a", "") .await .unwrap(); fx.store .create_label(&repo.id, "docs", "#0075ca", "") .await .unwrap(); let bug_id = fx .store .list_labels(&repo.id) .await .unwrap() .into_iter() .find(|l| l.name == "bug") .unwrap() .id; let reply = send( &fx.router, "GET", &new_issue_uri("?template=bug"), true, None, &[], ) .await; assert_eq!(reply.status, 200); let html = reply.text(); assert!(html.contains("name=\"title\"")); assert!(html.contains("value=\"[Bug]:\"")); assert!(html.contains("## Steps to reproduce")); assert!(!html.contains("Choose a template")); assert!(tag_at(&html, &format!("value=\"{bug_id}\"")).contains("checked")); assert!(!html.contains("value=\"needs triage\"")); assert_eq!(html.matches("name=\"label\"").count(), 2); let docs_id = fx .store .list_labels(&repo.id) .await .unwrap() .into_iter() .find(|l| l.name == "docs") .unwrap() .id; assert!(!tag_at(&html, &format!("value=\"{docs_id}\"")).contains("checked")); } #[tokio::test] async fn chooser_lists_at_most_twenty_templates() { let names: Vec = (0..21).map(|n| format!("t{n:02}")).collect(); let files: Vec<(String, String)> = names .iter() .map(|n| { ( format!(".forge/ISSUE_TEMPLATE/{n}.md"), format!("body {n}\n"), ) }) .collect(); let refs: Vec<(&str, &str)> = files .iter() .map(|(p, t)| (p.as_str(), t.as_str())) .collect(); let fx = setup(&refs).await; let html = send(&fx.router, "GET", &new_issue_uri(""), true, None, &[]) .await .text(); assert!(html.contains("issues/new?template=t19\"")); assert!(!html.contains("issues/new?template=t20\"")); } #[tokio::test] async fn submitting_with_a_label_applies_it_to_the_issue() { let fx = setup(&[(".forge/ISSUE_TEMPLATE/bug.md", BUG)]).await; let repo = fx .store .repo_by_owner_and_name(OWNER, REPO) .await .unwrap() .unwrap(); fx.store .create_label(&repo.id, "bug", "#d73a4a", "") .await .unwrap(); let bug_id = fx .store .list_labels(&repo.id) .await .unwrap() .into_iter() .next() .unwrap() .id; let form = format!("csrf={CSRF}&title=Crash+on+start&body=details&label={bug_id}"); let reply = send( &fx.router, "POST", &new_issue_uri(""), true, Some(&form), &[], ) .await; assert_eq!(reply.status, 303); let issue = fx .store .issue_by_number(&repo.id, 1) .await .unwrap() .unwrap(); let labels = fx.store.issue_labels(&issue.id).await.unwrap(); assert_eq!(labels.len(), 1); assert_eq!(labels[0].name, "bug"); }