Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: skip URL template expansion when UrlStatus is skipped (#923)
Maximilian Roos770a546ae745
diff --git a/src/commands/list/collect/execution.rs b/src/commands/list/collect/execution.rsindex 630d64e38..ad2122f60 100644--- a/src/commands/list/collect/execution.rs+++ b/src/commands/list/collect/execution.rs@@ -145,18 +145,25 @@ pub fn work_items_for_worktree(let skip = &options.skip_tasks;- // Expand URL template for this item- let item_url = options.url_template.as_ref().and_then(|template| {- wt.branch.as_ref().and_then(|branch| {- let mut vars = std::collections::HashMap::new();- vars.insert("branch", branch.as_str());- worktrunk::config::expand_template(template, &vars, false, repo, "url-template").ok()+ let include_url = !skip.contains(&TaskKind::UrlStatus);++ // Expand URL template for this item (only if URL status is enabled).+ let item_url = if include_url {+ options.url_template.as_ref().and_then(|template| {+ wt.branch.as_ref().and_then(|branch| {+ let mut vars = std::collections::HashMap::new();+ vars.insert("branch", branch.as_str());+ worktrunk::config::expand_template(template, &vars, false, repo, "url-template")+ .ok()+ })})- });+ } else {+ None+ };// Send URL immediately (before health check) so it appears right away.// The UrlStatusTask will later update with active status.- if let Some(ref url) = item_url {+ if include_url && let Some(ref url) = item_url {expected_results.expect(item_idx, TaskKind::UrlStatus);let _ = tx.send(Ok(TaskResult::UrlStatus {item_idx,@@ -218,7 +225,7 @@ pub fn work_items_for_worktree(// Note: We already registered and sent an immediate UrlStatus above with url + active=None.// This work item will send a second UrlStatus with active=Some(bool) after health check.// Both results must be registered and expected.- if !skip.contains(&TaskKind::UrlStatus) && ctx.item_url.is_some() {+ if include_url && ctx.item_url.is_some() {expected_results.expect(item_idx, TaskKind::UrlStatus);items.push(WorkItem {ctx: ctx.clone(),@@ -299,3 +306,56 @@ pub fn work_items_for_branch(items}++#[cfg(test)]+mod tests {+ use super::*;+ use std::collections::HashSet;+ use worktrunk::shell_exec::Cmd;++ #[test]+ fn test_skip_url_status_suppresses_placeholder_and_task() {+ let dir = tempfile::tempdir().expect("tempdir");+ Cmd::new("git")+ .args(["init"])+ .current_dir(dir.path())+ .run()+ .expect("git init");++ let repo = Repository::at(dir.path()).expect("repo");+ let wt = WorktreeInfo {+ path: dir.path().to_path_buf(),+ head: "deadbeef".to_string(),+ branch: Some("main".to_string()),+ bare: false,+ detached: false,+ locked: None,+ prunable: None,+ };++ let skip_tasks: HashSet<TaskKind> = [TaskKind::UrlStatus].into_iter().collect();+ let options = CollectOptions {+ skip_tasks,+ url_template: Some("http://localhost/{{ branch }}".to_string()),+ stale_branches: HashSet::new(),+ };++ let expected_results = Arc::new(ExpectedResults::default());+ let (tx, rx) = chan::unbounded::<Result<TaskResult, TaskError>>();++ let items = work_items_for_worktree(&repo, &wt, 0, &options, &expected_results, &tx);++ // No placeholder sent+ assert!(rx.try_recv().is_err());+ // No UrlStatus work item created+ assert!(!items.iter().any(|item| item.kind == TaskKind::UrlStatus));+ // No UrlStatus in expected results+ assert!(+ !expected_results+ .results_for(0)+ .contains(&TaskKind::UrlStatus)+ );+ // item_url is None for all items+ assert!(items.iter().all(|item| item.ctx.item_url.is_none()));+ }+}diff --git a/src/utils.rs b/src/utils.rsindex 14a7138d1..6d36967f0 100644--- a/src/utils.rs+++ b/src/utils.rs@@ -5,12 +5,17 @@ use std::time::{SystemTime, UNIX_EPOCH};/// Format a Unix timestamp as ISO 8601 string (e.g., "2025-01-01T00:00:00Z").////// Used for human-readable timestamps in diagnostic reports and logs.+///+/// If the timestamp is out of range for chrono's date handling, returns an+/// explicit placeholder string rather than a misleading value.pub fn format_timestamp_iso8601(timestamp: u64) -> String {- let timestamp = timestamp as i64;+ let Ok(timestamp) = i64::try_from(timestamp) else {+ return format!("invalid-timestamp({timestamp})");+ };+chrono::DateTime::from_timestamp(timestamp, 0)- .unwrap_or_else(chrono::Utc::now)- .format("%Y-%m-%dT%H:%M:%SZ")- .to_string()+ .map(|dt| dt.format("%Y-%m-%dT%H:%M:%SZ").to_string())+ .unwrap_or_else(|| format!("invalid-timestamp({timestamp})"))}/// Format the current time as ISO 8601 string.@@ -64,4 +69,20 @@ mod tests {assert_eq!(get_now(), expected);}}++ #[test]+ fn test_format_timestamp_iso8601_u64_overflow() {+ // Timestamps exceeding i64::MAX are handled by try_from+ let too_large = (i64::MAX as u64) + 1;+ let formatted = format_timestamp_iso8601(too_large);+ assert!(formatted.starts_with("invalid-timestamp("));+ }++ #[test]+ fn test_format_timestamp_iso8601_chrono_out_of_range() {+ // Timestamps within i64 but beyond chrono's range (~year 262143)+ let chrono_out_of_range: u64 = 9_000_000_000_000; // ~year 287396+ let formatted = format_timestamp_iso8601(chrono_out_of_range);+ assert!(formatted.starts_with("invalid-timestamp("));+ }}