Skip to main content

apm_core/
validate.rs

1use crate::config::{resolve_outcome, CompletionStrategy, Config, LocalConfig};
2use crate::ticket_fmt::Ticket;
3use crate::wrapper;
4use crate::wrapper::custom::{parse_manifest, manifest_unknown_keys};
5use anyhow::{bail, Result};
6use std::collections::HashSet;
7use std::path::Path;
8
9/// Return the completion strategy configured for the `in_progress → implemented`
10/// transition.  Falls back to `None` when the transition is absent.
11pub fn active_completion_strategy(config: &Config) -> CompletionStrategy {
12    config.workflow.states.iter()
13        .find(|s| s.id == "in_progress")
14        .and_then(|s| s.transitions.iter().find(|t| t.to == "implemented"))
15        .map(|t| t.completion.clone())
16        .unwrap_or(CompletionStrategy::None)
17}
18
19fn strategy_name(strategy: &CompletionStrategy) -> &'static str {
20    match strategy {
21        CompletionStrategy::Pr => "pr",
22        CompletionStrategy::Merge => "merge",
23        CompletionStrategy::Pull => "pull",
24        CompletionStrategy::PrOrEpicMerge => "pr_or_epic_merge",
25        CompletionStrategy::None => "none",
26    }
27}
28
29/// Validate that `dep_ids` satisfy the dependency rules for `strategy`.
30///
31/// - `ticket_epic`: epic ID of the ticket being written (None if no epic)
32/// - `ticket_target_branch`: target_branch of the ticket (None = default branch)
33/// - `dep_ids`: the proposed dependency list (empty slice → always Ok)
34/// - `all_tickets`: all known tickets (used to look up dep metadata)
35/// - `default_branch`: project default branch name
36pub fn check_depends_on_rules(
37    strategy: &CompletionStrategy,
38    ticket_epic: Option<&str>,
39    ticket_target_branch: Option<&str>,
40    dep_ids: &[String],
41    all_tickets: &[crate::ticket_fmt::Ticket],
42    default_branch: &str,
43) -> Result<()> {
44    if dep_ids.is_empty() {
45        return Ok(());
46    }
47    match strategy {
48        CompletionStrategy::Pr | CompletionStrategy::None | CompletionStrategy::Pull => {
49            bail!(
50                "depends_on is not allowed under the {} completion strategy",
51                strategy_name(strategy)
52            );
53        }
54        CompletionStrategy::PrOrEpicMerge => {
55            let Some(epic) = ticket_epic else {
56                bail!(
57                    "pr_or_epic_merge requires the ticket to belong to an epic before depends_on can be set"
58                );
59            };
60            let mut offending: Vec<&str> = Vec::new();
61            for dep_id in dep_ids {
62                let dep = all_tickets.iter().find(|t| t.frontmatter.id == *dep_id)
63                    .ok_or_else(|| anyhow::anyhow!("dep {dep_id} not found"))?;
64                if dep.frontmatter.epic.as_deref() != Some(epic) {
65                    offending.push(dep_id.as_str());
66                }
67            }
68            if !offending.is_empty() {
69                bail!(
70                    "pr_or_epic_merge requires all deps to share epic {epic}; offending deps: {}",
71                    offending.join(", ")
72                );
73            }
74        }
75        CompletionStrategy::Merge => {
76            let ticket_target = ticket_target_branch.unwrap_or(default_branch);
77            let mut offending: Vec<&str> = Vec::new();
78            for dep_id in dep_ids {
79                let dep = all_tickets.iter().find(|t| t.frontmatter.id == *dep_id)
80                    .ok_or_else(|| anyhow::anyhow!("dep {dep_id} not found"))?;
81                let dep_target = dep.frontmatter.target_branch.as_deref().unwrap_or(default_branch);
82                if dep_target != ticket_target {
83                    offending.push(dep_id.as_str());
84                }
85            }
86            if !offending.is_empty() {
87                bail!(
88                    "merge requires all deps to share target_branch {ticket_target}; offending deps: {}",
89                    offending.join(", ")
90                );
91            }
92        }
93    }
94    Ok(())
95}
96
97/// Walk every non-closed ticket and return a vec of `(subject, message)` pairs
98/// for each ticket whose `depends_on` violates the active completion strategy rule.
99pub fn validate_depends_on(config: &Config, tickets: &[Ticket]) -> Vec<(String, String)> {
100    let strategy = active_completion_strategy(config);
101    let mut violations: Vec<(String, String)> = Vec::new();
102    for ticket in tickets {
103        let fm = &ticket.frontmatter;
104        if fm.state == "closed" {
105            continue;
106        }
107        let dep_ids = match &fm.depends_on {
108            Some(deps) if !deps.is_empty() => deps,
109            _ => continue,
110        };
111        if let Err(e) = check_depends_on_rules(
112            &strategy,
113            fm.epic.as_deref(),
114            fm.target_branch.as_deref(),
115            dep_ids,
116            tickets,
117            &config.project.default_branch,
118        ) {
119            violations.push((format!("#{}", fm.id), e.to_string()));
120        }
121    }
122    violations
123}
124
125pub fn validate_owner(config: &Config, local: &LocalConfig, username: &str) -> Result<()> {
126    if username == "-" {
127        return Ok(());
128    }
129    let (collaborators, warnings) = crate::config::resolve_collaborators(config, local);
130    for w in &warnings {
131        #[allow(clippy::print_stderr)]
132        { eprintln!("{w}"); }
133    }
134    if collaborators.is_empty() {
135        return Ok(());
136    }
137    if collaborators.iter().any(|c| c == username) {
138        return Ok(());
139    }
140    let list = collaborators.join(", ");
141    bail!("unknown user '{username}'; valid collaborators: {list}");
142}
143
144fn is_external_worktree(dir: &Path) -> bool {
145    let s = dir.to_string_lossy();
146    s.starts_with('/') || s.starts_with("..")
147}
148
149fn gitignore_covers_dir(content: &str, dir: &str) -> bool {
150    let normalized_dir = dir.trim_matches('/');
151    content
152        .lines()
153        .map(|line| line.trim())
154        .filter(|line| !line.is_empty() && !line.starts_with('#'))
155        .any(|line| line.trim_matches('/') == normalized_dir)
156}
157
158/// Layer 1 of the two-layer manifest validation design.
159/// Validates all configured agent names and scans `.apm/agents/` for issues.
160/// Returns (errors, warnings) so callers can route them — keeps this single
161/// directory scan from running twice when both validate_config and
162/// validate_warnings are invoked.
163pub fn validate_agents(config: &Config, root: &Path) -> (Vec<String>, Vec<String>) {
164    let mut errors: Vec<String> = Vec::new();
165    let mut warnings: Vec<String> = Vec::new();
166    validate_agents_into(config, root, &mut errors, &mut warnings);
167    (errors, warnings)
168}
169
170fn validate_agents_into(config: &Config, root: &Path, errors: &mut Vec<String>, warnings: &mut Vec<String>) {
171    // Collect configured agent names
172    let mut names: std::collections::HashSet<String> = std::collections::HashSet::new();
173    let primary = config.workers.agent.clone()
174        .unwrap_or_else(|| "claude".to_string());
175    names.insert(primary);
176    for p in config.worker_profiles.values() {
177        if let Some(ref agent) = p.agent {
178            names.insert(agent.clone());
179        }
180    }
181
182    // Validate each configured agent name (Layer 1 error check)
183    let builtins = wrapper::list_builtin_names().join(", ");
184    for name in &names {
185        match wrapper::resolve_wrapper(root, name) {
186            Ok(None) => errors.push(format!(
187                "agent '{}' not found: checked built-ins {{{builtins}}} and '.apm/agents/{}/'",
188                name, name
189            )),
190            Err(e) => errors.push(format!("agent '{name}': {e}")),
191            Ok(Some(wrapper::WrapperKind::Custom { manifest, .. })) => {
192                if let Some(m) = &manifest {
193                    if m.parser == "external" && m.parser_command.is_none() {
194                        errors.push(format!(
195                            "agent '{name}': manifest.toml declares parser = \"external\" \
196                             but parser_command is absent"
197                        ));
198                    }
199                }
200            }
201            Ok(Some(wrapper::WrapperKind::Builtin(_))) => {}
202        }
203    }
204
205    // Scan .apm/agents/ for per-directory warnings and errors
206    let agents_dir = root.join(".apm").join("agents");
207    let Ok(entries) = std::fs::read_dir(&agents_dir) else { return };
208
209    for entry in entries.filter_map(|e| e.ok()) {
210        let ft = match entry.file_type() {
211            Ok(ft) => ft,
212            Err(_) => continue,
213        };
214        if !ft.is_dir() {
215            continue;
216        }
217        let name = entry.file_name().to_string_lossy().to_string();
218
219        // Check for non-executable wrapper.* files (Unix only)
220        let wrapper_files: Vec<_> = std::fs::read_dir(entry.path())
221            .ok()
222            .into_iter()
223            .flatten()
224            .filter_map(|e| e.ok())
225            .filter(|e| e.file_name().to_string_lossy().starts_with("wrapper."))
226            .collect();
227
228        if !wrapper_files.is_empty() {
229            #[cfg(unix)]
230            {
231                use std::os::unix::fs::PermissionsExt;
232                let any_exec = wrapper_files.iter().any(|f| {
233                    f.metadata()
234                        .map(|m| m.permissions().mode() & 0o111 != 0)
235                        .unwrap_or(false)
236                });
237                if !any_exec {
238                    warnings.push(format!(
239                        "agent '{name}': .apm/agents/{name}/wrapper.* exists but is not executable; run chmod +x"
240                    ));
241                }
242            }
243        }
244
245        // Check manifest.toml
246        let manifest_path = entry.path().join("manifest.toml");
247        if manifest_path.exists() {
248            match parse_manifest(root, &name) {
249                Err(e) => {
250                    errors.push(format!("agent '{name}': manifest.toml is not valid TOML: {e}"));
251                }
252                Ok(Some(manifest)) => {
253                    if manifest.contract_version > 1 {
254                        errors.push(format!(
255                            "agent '{name}': manifest.toml declares contract_version {}; \
256                             this APM build supports version 1 only — upgrade APM",
257                            manifest.contract_version
258                        ));
259                    }
260                    if let Ok(unknown) = manifest_unknown_keys(root, &name) {
261                        for key in unknown {
262                            warnings.push(format!(
263                                "agent '{name}': manifest.toml: unknown key {key}"
264                            ));
265                        }
266                    }
267                }
268                Ok(None) => {}
269            }
270        }
271    }
272}
273
274pub fn validate_config(config: &Config, root: &Path) -> Vec<String> {
275    let mut errors = validate_config_no_agents(config, root);
276    let (agent_errors, _) = validate_agents(config, root);
277    errors.extend(agent_errors);
278    errors
279}
280
281fn validate_config_no_agents(config: &Config, root: &Path) -> Vec<String> {
282    let mut errors: Vec<String> = Vec::new();
283
284    let state_ids: HashSet<&str> = config.workflow.states.iter()
285        .map(|s| s.id.as_str())
286        .collect();
287
288    let section_names: HashSet<&str> = config.ticket.sections.iter()
289        .map(|s| s.name.as_str())
290        .collect();
291    let has_sections = !section_names.is_empty();
292
293    // Check whether any transition requires a provider.
294    let needs_provider = config.workflow.states.iter()
295        .flat_map(|s| s.transitions.iter())
296        .any(|t| matches!(t.completion, CompletionStrategy::Pr | CompletionStrategy::Merge));
297
298    let provider_ok = config.git_host.provider.as_ref()
299        .map(|p| !p.is_empty())
300        .unwrap_or(false);
301
302    if needs_provider && !provider_ok {
303        errors.push(
304            "config: workflow — completion 'pr' or 'merge' requires [git_host] with a provider".into()
305        );
306    }
307
308    // At least one non-terminal state.
309    let has_non_terminal = config.workflow.states.iter().any(|s| !s.terminal);
310    if !has_non_terminal {
311        errors.push("config: workflow — no non-terminal state exists".into());
312    }
313
314    for state in &config.workflow.states {
315        // Terminal state with outgoing transitions.
316        if state.terminal && !state.transitions.is_empty() {
317            errors.push(format!(
318                "config: state.{} — terminal but has {} outgoing transition(s)",
319                state.id,
320                state.transitions.len()
321            ));
322        }
323
324        // Non-terminal state with no outgoing transitions (tickets will be stranded).
325        if !state.terminal && state.transitions.is_empty() {
326            errors.push(format!(
327                "config: state.{} — no outgoing transitions (tickets will be stranded)",
328                state.id
329            ));
330        }
331
332        // Instructions path exists on disk.
333        if let Some(instructions) = &state.instructions {
334            if !root.join(instructions).exists() {
335                errors.push(format!(
336                    "config: state.{}.instructions — file not found: {}",
337                    state.id, instructions
338                ));
339            }
340        }
341
342        for transition in &state.transitions {
343            // Transition target must exist.  "closed" is a built-in terminal state
344            // that is always valid even when absent from [[workflow.states]].
345            if transition.to != "closed" && !state_ids.contains(transition.to.as_str()) {
346                errors.push(format!(
347                    "config: state.{}.transition({}) — target state '{}' does not exist",
348                    state.id, transition.to, transition.to
349                ));
350            }
351
352            // context_section must match a known ticket section.
353            if let Some(section) = &transition.context_section {
354                if has_sections && !section_names.contains(section.as_str()) {
355                    errors.push(format!(
356                        "config: state.{}.transition({}).context_section — unknown section '{}'",
357                        state.id, transition.to, section
358                    ));
359                }
360            }
361
362            // focus_section must match a known ticket section.
363            if let Some(section) = &transition.focus_section {
364                if has_sections && !section_names.contains(section.as_str()) {
365                    errors.push(format!(
366                        "config: state.{}.transition({}).focus_section — unknown section '{}'",
367                        state.id, transition.to, section
368                    ));
369                }
370            }
371
372            // Merge/PrOrEpicMerge transitions require on_failure.
373            if matches!(
374                transition.completion,
375                CompletionStrategy::Merge | CompletionStrategy::PrOrEpicMerge
376            ) {
377                if transition.on_failure.is_none() {
378                    errors.push(format!(
379                        "config: transition '{}' → '{}' uses completion '{}' but is missing \
380                         `on_failure`; run `apm validate --fix` to add it",
381                        state.id,
382                        transition.to,
383                        strategy_name(&transition.completion)
384                    ));
385                } else if let Some(ref name) = transition.on_failure {
386                    if name != "closed" && !state_ids.contains(name.as_str()) {
387                        errors.push(format!(
388                            "config: transition '{}' → '{}' has `on_failure = \"{}\"` but \
389                             state \"{}\" is not declared in workflow.toml",
390                            state.id, transition.to, name, name
391                        ));
392                    }
393                }
394            }
395        }
396    }
397
398    if let Some(ref path) = config.workers.instructions {
399        if !root.join(path).exists() {
400            errors.push(format!(
401                "config: [workers].instructions — file not found: {path}"
402            ));
403        }
404    }
405
406    if !is_external_worktree(&config.worktrees.dir) {
407        let dir_str = config.worktrees.dir.to_string_lossy();
408        let gitignore = root.join(".gitignore");
409        match std::fs::read_to_string(&gitignore) {
410            Err(_) => errors.push(format!(
411                "config: worktrees.dir '{dir_str}' is in-repo but .gitignore is missing; \
412                 run 'apm init' or add '/{dir_str}/' manually"
413            )),
414            Ok(content) if !gitignore_covers_dir(&content, &dir_str) => errors.push(format!(
415                "config: worktrees.dir '{dir_str}' is in-repo but .gitignore does not cover it; \
416                 add '/{dir_str}/' or run 'apm init'"
417            )),
418            Ok(_) => {}
419        }
420    }
421
422    errors
423}
424
425pub fn verify_tickets(
426    root: &Path,
427    config: &Config,
428    tickets: &[Ticket],
429    merged: &HashSet<String>,
430) -> Vec<String> {
431    let valid_states: HashSet<&str> = config.workflow.states.iter()
432        .map(|s| s.id.as_str())
433        .collect();
434    let terminal = config.terminal_state_ids();
435
436    let in_progress_states: HashSet<&str> =
437        ["in_progress", "implemented"].iter().copied().collect();
438
439    let worktree_states: HashSet<&str> =
440        ["in_design", "in_progress"].iter().copied().collect();
441    let main_root = crate::git_util::main_worktree_root(root)
442        .unwrap_or_else(|| root.to_path_buf());
443    let worktrees_base = main_root.join(&config.worktrees.dir);
444
445    let mut issues: Vec<String> = Vec::new();
446
447    for t in tickets {
448        let fm = &t.frontmatter;
449
450        // Skip terminal-state tickets.
451        if terminal.contains(fm.state.as_str()) { continue; }
452
453        let prefix = format!("#{} [{}]", fm.id, fm.state);
454
455        // State value not in config.
456        if !valid_states.is_empty() && !valid_states.contains(fm.state.as_str()) {
457            issues.push(format!("{prefix}: unknown state {:?}", fm.state));
458        }
459
460        // Frontmatter id doesn't match filename numeric prefix.
461        if let Some(name) = t.path.file_name().and_then(|n| n.to_str()) {
462            let expected_prefix = format!("{:04}", fm.id);
463            if !name.starts_with(&expected_prefix) {
464                issues.push(format!("{prefix}: id {} does not match filename {name}", fm.id));
465            }
466        }
467
468        // in_progress/implemented with no branch.
469        if in_progress_states.contains(fm.state.as_str()) && fm.branch.is_none() {
470            issues.push(format!("{prefix}: state requires branch but none set"));
471        }
472
473        // Branch merged but ticket not yet closed.
474        if let Some(branch) = &fm.branch {
475            if (fm.state == "in_progress" || fm.state == "implemented")
476                && merged.contains(branch.as_str())
477            {
478                issues.push(format!("{prefix}: branch {branch} is merged but ticket not closed"));
479            }
480        }
481
482        // in_design/in_progress with missing worktree directory.
483        if worktree_states.contains(fm.state.as_str()) {
484            if let Some(branch) = &fm.branch {
485                let wt_name = branch.replace('/', "-");
486                let wt_path = worktrees_base.join(&wt_name);
487                if !wt_path.is_dir() {
488                    issues.push(format!(
489                        "{prefix}: worktree at {} is missing",
490                        wt_path.display()
491                    ));
492                }
493            }
494        }
495
496        // Missing ## Spec section.
497        if !t.body.contains("## Spec") {
498            issues.push(format!("{prefix}: missing ## Spec section"));
499        }
500
501        // Missing ## History section.
502        if !t.body.contains("## History") {
503            issues.push(format!("{prefix}: missing ## History section"));
504        }
505
506        // Validate document structure (required sections non-empty, AC items present).
507        if let Ok(doc) = t.document() {
508            for err in doc.validate(&config.ticket.sections) {
509                issues.push(format!("{prefix}: {err}"));
510            }
511        }
512
513        // Validate frontmatter agent names against known built-ins.
514        let agents_to_check: Vec<&str> = fm.agent
515            .as_deref()
516            .into_iter()
517            .chain(fm.agent_overrides.values().map(String::as_str))
518            .collect();
519
520        for name in agents_to_check {
521            match wrapper::resolve_wrapper(root, name) {
522                Ok(Some(_)) => {}
523                Ok(None) => issues.push(format!(
524                    "ticket {}: agent {:?} is not a known built-in",
525                    fm.id, name
526                )),
527                Err(e) => issues.push(format!(
528                    "ticket {}: agent {:?}: {e}",
529                    fm.id, name
530                )),
531            }
532        }
533    }
534
535    issues
536}
537
538pub fn validate_warnings(config: &crate::config::Config, root: &Path) -> Vec<String> {
539    let mut warnings = validate_warnings_no_agents(config, root);
540    let (_, agent_warnings) = validate_agents(config, root);
541    warnings.extend(agent_warnings);
542    warnings
543}
544
545fn validate_warnings_no_agents(config: &crate::config::Config, _root: &Path) -> Vec<String> {
546    let mut warnings = config.load_warnings.clone();
547    if let Some(container) = &config.workers.container {
548        if !container.is_empty() {
549            let docker_ok = std::process::Command::new("docker")
550                .arg("--version")
551                .output()
552                .map(|o| o.status.success())
553                .unwrap_or(false);
554            if !docker_ok {
555                warnings.push(
556                    "workers.container is set but 'docker' is not in PATH".to_string()
557                );
558            }
559        }
560    }
561
562    // Dead-end reachability check: warn when no agent-actionable state can reach a
563    // transition whose outcome resolves to "success".
564    let state_map: std::collections::HashMap<&str, &crate::config::StateConfig> =
565        config.workflow.states.iter()
566            .map(|s| (s.id.as_str(), s))
567            .collect();
568
569    let agent_startable: Vec<&str> = config.workflow.states.iter()
570        .filter(|s| s.actionable.iter().any(|a| a == "agent" || a == "any"))
571        .map(|s| s.id.as_str())
572        .collect();
573
574    if !agent_startable.is_empty() {
575        let mut visited: std::collections::HashSet<&str> = std::collections::HashSet::new();
576        let mut queue: std::collections::VecDeque<&str> = std::collections::VecDeque::new();
577        let mut found_success = false;
578
579        for &start in &agent_startable {
580            if visited.insert(start) {
581                queue.push_back(start);
582            }
583        }
584
585        'bfs: while let Some(state_id) = queue.pop_front() {
586            let Some(state) = state_map.get(state_id) else { continue };
587            for t in &state.transitions {
588                // Unknown targets are reported as errors by validate_config; skip
589                // here so reachability isn't computed against malformed transitions.
590                let Some(&target) = state_map.get(t.to.as_str()) else { continue };
591                if resolve_outcome(t, target) == "success" {
592                    found_success = true;
593                    break 'bfs;
594                }
595                if !target.terminal && visited.insert(t.to.as_str()) {
596                    queue.push_back(t.to.as_str());
597                }
598            }
599        }
600
601        if !found_success {
602            warnings.push(
603                "workflow has no reachable 'success' outcome from any agent-actionable state; \
604                 workers may never complete successfully".to_string()
605            );
606        }
607    }
608
609    warnings
610}
611
612/// Single-pass equivalent of calling `validate_config` followed by
613/// `validate_warnings` — runs the agent-directory scan once and dedupes.
614pub fn validate_all(config: &Config, root: &Path) -> (Vec<String>, Vec<String>) {
615    let mut errors = validate_config_no_agents(config, root);
616    let mut warnings = validate_warnings_no_agents(config, root);
617    let (agent_errors, agent_warnings) = validate_agents(config, root);
618    errors.extend(agent_errors);
619    warnings.extend(agent_warnings);
620    (errors, warnings)
621}
622
623#[cfg(test)]
624mod tests {
625    use super::*;
626    use crate::config::{Config, CompletionStrategy, LocalConfig};
627    use crate::ticket::Ticket;
628    use crate::git_util;
629    use std::path::Path;
630    use std::collections::HashSet;
631
632    fn git_cmd(dir: &std::path::Path, args: &[&str]) {
633        std::process::Command::new("git")
634            .args(args)
635            .current_dir(dir)
636            .env("GIT_AUTHOR_NAME", "test")
637            .env("GIT_AUTHOR_EMAIL", "test@test.com")
638            .env("GIT_COMMITTER_NAME", "test")
639            .env("GIT_COMMITTER_EMAIL", "test@test.com")
640            .status()
641            .unwrap();
642    }
643
644    fn setup_verify_repo() -> tempfile::TempDir {
645        let dir = tempfile::tempdir().unwrap();
646        let p = dir.path();
647
648        git_cmd(p, &["init", "-q", "-b", "main"]);
649        git_cmd(p, &["config", "user.email", "test@test.com"]);
650        git_cmd(p, &["config", "user.name", "test"]);
651
652        std::fs::write(
653            p.join("apm.toml"),
654            r#"[project]
655name = "test"
656
657[tickets]
658dir = "tickets"
659
660[worktrees]
661dir = "worktrees"
662
663[[workflow.states]]
664id = "in_design"
665label = "In Design"
666
667[[workflow.states]]
668id = "in_progress"
669label = "In Progress"
670
671[[workflow.states]]
672id = "specd"
673label = "Specd"
674"#,
675        )
676        .unwrap();
677
678        git_cmd(p, &["add", "apm.toml"]);
679        git_cmd(p, &["-c", "commit.gpgsign=false", "commit", "-m", "init"]);
680
681        dir
682    }
683
684    fn make_verify_ticket(root: &std::path::Path, id: &str, state: &str, branch: Option<&str>) -> Ticket {
685        let branch_line = match branch {
686            Some(b) => format!("branch = \"{b}\"\n"),
687            None => String::new(),
688        };
689        let raw = format!(
690            "+++\nid = \"{id}\"\ntitle = \"Test ticket\"\nstate = \"{state}\"\n{branch_line}+++\n\n## Spec\n\n## History\n"
691        );
692        let path = root.join("tickets").join(format!("{id}-test.md"));
693        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
694        std::fs::write(&path, &raw).unwrap();
695        Ticket::parse(&path, &raw).unwrap()
696    }
697
698    fn make_ticket(id: &str, epic: Option<&str>, target_branch: Option<&str>) -> Ticket {
699        let epic_line = epic.map(|e| format!("epic = \"{e}\"\n")).unwrap_or_default();
700        let target_line = target_branch.map(|b| format!("target_branch = \"{b}\"\n")).unwrap_or_default();
701        let raw = format!(
702            "+++\nid = \"{id}\"\ntitle = \"T\"\nstate = \"ready\"\n{epic_line}{target_line}+++\n\n"
703        );
704        Ticket::parse(Path::new(&format!("tickets/{id}-t.md")), &raw).unwrap()
705    }
706
707    fn strategy_config(completion: &str) -> Config {
708        let toml = format!(
709            r#"
710[project]
711name = "test"
712
713[tickets]
714dir = "tickets"
715
716[[workflow.states]]
717id    = "in_progress"
718label = "In Progress"
719
720[[workflow.states.transitions]]
721to         = "implemented"
722completion = "{completion}"
723
724[[workflow.states]]
725id       = "implemented"
726label    = "Implemented"
727terminal = true
728"#
729        );
730        toml::from_str(&toml).unwrap()
731    }
732
733    #[test]
734    fn strategy_finds_in_progress_to_implemented() {
735        let config = strategy_config("pr_or_epic_merge");
736        assert_eq!(active_completion_strategy(&config), CompletionStrategy::PrOrEpicMerge);
737    }
738
739    #[test]
740    fn strategy_defaults_to_none_when_absent() {
741        let toml = r#"
742[project]
743name = "test"
744
745[tickets]
746dir = "tickets"
747
748[[workflow.states]]
749id    = "new"
750label = "New"
751
752[[workflow.states.transitions]]
753to = "closed"
754
755[[workflow.states]]
756id       = "closed"
757label    = "Closed"
758terminal = true
759"#;
760        let config: Config = toml::from_str(toml).unwrap();
761        assert_eq!(active_completion_strategy(&config), CompletionStrategy::None);
762    }
763
764    #[test]
765    fn dep_rules_pr_rejects_dep() {
766        let dep = make_ticket("dep1", None, None);
767        let result = check_depends_on_rules(
768            &CompletionStrategy::Pr,
769            None,
770            None,
771            &["dep1".to_string()],
772            &[dep],
773            "main",
774        );
775        assert!(result.is_err());
776        let msg = result.unwrap_err().to_string();
777        assert!(msg.contains("pr"), "expected strategy name in: {msg}");
778    }
779
780    #[test]
781    fn dep_rules_none_rejects_dep() {
782        let dep = make_ticket("dep1", None, None);
783        let result = check_depends_on_rules(
784            &CompletionStrategy::None,
785            None,
786            None,
787            &["dep1".to_string()],
788            &[dep],
789            "main",
790        );
791        assert!(result.is_err());
792        let msg = result.unwrap_err().to_string();
793        assert!(msg.contains("none"), "expected strategy name in: {msg}");
794    }
795
796    #[test]
797    fn dep_rules_pr_or_epic_merge_same_epic_ok() {
798        let dep = make_ticket("dep1", Some("abc"), None);
799        let result = check_depends_on_rules(
800            &CompletionStrategy::PrOrEpicMerge,
801            Some("abc"),
802            None,
803            &["dep1".to_string()],
804            &[dep],
805            "main",
806        );
807        assert!(result.is_ok(), "expected Ok, got {result:?}");
808    }
809
810    #[test]
811    fn dep_rules_pr_or_epic_merge_different_epic_fails() {
812        let dep = make_ticket("dep1", Some("xyz"), None);
813        let result = check_depends_on_rules(
814            &CompletionStrategy::PrOrEpicMerge,
815            Some("abc"),
816            None,
817            &["dep1".to_string()],
818            &[dep],
819            "main",
820        );
821        assert!(result.is_err());
822        let msg = result.unwrap_err().to_string();
823        assert!(msg.contains("dep1"), "expected dep ID in: {msg}");
824    }
825
826    #[test]
827    fn dep_rules_pr_or_epic_merge_ticket_no_epic_fails() {
828        let dep = make_ticket("dep1", Some("abc"), None);
829        let result = check_depends_on_rules(
830            &CompletionStrategy::PrOrEpicMerge,
831            None,
832            None,
833            &["dep1".to_string()],
834            &[dep],
835            "main",
836        );
837        assert!(result.is_err());
838        let msg = result.unwrap_err().to_string();
839        assert!(msg.contains("epic"), "expected epic mention in: {msg}");
840    }
841
842    #[test]
843    fn dep_rules_merge_both_default_branch_ok() {
844        let dep = make_ticket("dep1", None, None);
845        let result = check_depends_on_rules(
846            &CompletionStrategy::Merge,
847            None,
848            None,
849            &["dep1".to_string()],
850            &[dep],
851            "main",
852        );
853        assert!(result.is_ok(), "expected Ok, got {result:?}");
854    }
855
856    #[test]
857    fn dep_rules_merge_different_target_fails() {
858        let dep = make_ticket("dep1", None, Some("epic/other"));
859        let result = check_depends_on_rules(
860            &CompletionStrategy::Merge,
861            None,
862            None,
863            &["dep1".to_string()],
864            &[dep],
865            "main",
866        );
867        assert!(result.is_err());
868        let msg = result.unwrap_err().to_string();
869        assert!(msg.contains("dep1"), "expected dep ID in: {msg}");
870    }
871
872    fn make_full_ticket(id: &str, state: &str, epic: Option<&str>, target_branch: Option<&str>, depends_on: &[&str]) -> Ticket {
873        let epic_line = epic.map(|e| format!("epic = \"{e}\"\n")).unwrap_or_default();
874        let target_line = target_branch.map(|b| format!("target_branch = \"{b}\"\n")).unwrap_or_default();
875        let deps_line = if depends_on.is_empty() {
876            String::new()
877        } else {
878            let quoted: Vec<String> = depends_on.iter().map(|d| format!("\"{d}\"")).collect();
879            format!("depends_on = [{}]\n", quoted.join(", "))
880        };
881        let raw = format!(
882            "+++\nid = \"{id}\"\ntitle = \"T\"\nstate = \"{state}\"\n{epic_line}{target_line}{deps_line}+++\n\n"
883        );
884        Ticket::parse(Path::new(&format!("tickets/{id}-t.md")), &raw).unwrap()
885    }
886
887    #[test]
888    fn validate_depends_on_no_deps_clean() {
889        let config = strategy_config("pr_or_epic_merge");
890        let t1 = make_full_ticket("aa000001", "ready", Some("epic1"), None, &[]);
891        let t2 = make_full_ticket("aa000002", "in_progress", Some("epic1"), None, &[]);
892        let result = validate_depends_on(&config, &[t1, t2]);
893        assert!(result.is_empty(), "expected no violations, got {result:?}");
894    }
895
896    #[test]
897    fn validate_depends_on_closed_ticket_skipped() {
898        let config = strategy_config("pr");
899        let dep = make_full_ticket("bb000001", "closed", None, None, &[]);
900        let ticket = make_full_ticket("bb000002", "closed", None, None, &["bb000001"]);
901        let result = validate_depends_on(&config, &[dep, ticket]);
902        assert!(result.is_empty(), "closed ticket should be skipped, got {result:?}");
903    }
904
905    #[test]
906    fn validate_depends_on_pr_or_epic_merge_same_epic_ok() {
907        let config = strategy_config("pr_or_epic_merge");
908        let dep = make_full_ticket("cc000001", "ready", Some("abc"), None, &[]);
909        let ticket = make_full_ticket("cc000002", "ready", Some("abc"), None, &["cc000001"]);
910        let result = validate_depends_on(&config, &[dep, ticket]);
911        assert!(result.is_empty(), "same-epic deps should pass, got {result:?}");
912    }
913
914    #[test]
915    fn validate_depends_on_pr_or_epic_merge_cross_epic_fails() {
916        let config = strategy_config("pr_or_epic_merge");
917        let dep = make_full_ticket("dd000001", "ready", Some("xyz"), None, &[]);
918        let ticket = make_full_ticket("dd000002", "ready", Some("abc"), None, &["dd000001"]);
919        let result = validate_depends_on(&config, &[dep, ticket]);
920        assert_eq!(result.len(), 1, "expected one violation, got {result:?}");
921        assert!(result[0].1.contains("dd000001"), "message should mention dep ID: {}", result[0].1);
922    }
923
924    #[test]
925    fn validate_depends_on_merge_same_target_ok() {
926        let config = strategy_config("merge");
927        let dep = make_full_ticket("ee000001", "ready", None, Some("feat"), &[]);
928        let ticket = make_full_ticket("ee000002", "ready", None, Some("feat"), &["ee000001"]);
929        let result = validate_depends_on(&config, &[dep, ticket]);
930        assert!(result.is_empty(), "same-target deps should pass, got {result:?}");
931    }
932
933    #[test]
934    fn validate_depends_on_merge_different_target_fails() {
935        let config = strategy_config("merge");
936        let dep = make_full_ticket("ff000001", "ready", None, Some("other"), &[]);
937        let ticket = make_full_ticket("ff000002", "ready", None, Some("feat"), &["ff000001"]);
938        let result = validate_depends_on(&config, &[dep, ticket]);
939        assert_eq!(result.len(), 1, "expected one violation, got {result:?}");
940        assert!(result[0].1.contains("ff000001"), "message should mention dep ID: {}", result[0].1);
941    }
942
943    #[test]
944    fn validate_depends_on_pr_strategy_rejects_any_dep() {
945        let config = strategy_config("pr");
946        let dep = make_full_ticket("gg000001", "ready", None, None, &[]);
947        let ticket = make_full_ticket("gg000002", "ready", None, None, &["gg000001"]);
948        let result = validate_depends_on(&config, &[dep, ticket]);
949        assert_eq!(result.len(), 1, "expected one violation, got {result:?}");
950        assert!(result[0].1.contains("pr"), "message should mention strategy: {}", result[0].1);
951    }
952
953    fn load_config(toml: &str) -> Config {
954        toml::from_str(toml).expect("config parse failed")
955    }
956
957    fn state_ids(config: &Config) -> std::collections::HashSet<&str> {
958        config.workflow.states.iter().map(|s| s.id.as_str()).collect()
959    }
960
961    // Test 1: correct config passes all checks
962    #[test]
963    fn correct_config_passes() {
964        let toml = r#"
965[project]
966name = "test"
967
968[tickets]
969dir = "tickets"
970
971[[workflow.states]]
972id    = "new"
973label = "New"
974
975[[workflow.states.transitions]]
976to = "in_progress"
977
978[[workflow.states]]
979id       = "in_progress"
980label    = "In Progress"
981terminal = false
982
983[[workflow.states.transitions]]
984to = "closed"
985
986[[workflow.states]]
987id       = "closed"
988label    = "Closed"
989terminal = true
990"#;
991        let config = load_config(toml);
992        let errors = validate_config(&config, Path::new("/tmp"));
993        assert!(errors.is_empty(), "unexpected errors: {errors:?}");
994    }
995
996    // Test 2: transition to non-existent state is detected
997    #[test]
998    fn transition_to_nonexistent_state_detected() {
999        let toml = r#"
1000[project]
1001name = "test"
1002
1003[tickets]
1004dir = "tickets"
1005
1006[[workflow.states]]
1007id    = "new"
1008label = "New"
1009
1010[[workflow.states.transitions]]
1011to = "ghost"
1012"#;
1013        let config = load_config(toml);
1014        let errors = validate_config(&config, Path::new("/tmp"));
1015        assert!(errors.iter().any(|e| e.contains("ghost")), "expected ghost error in {errors:?}");
1016    }
1017
1018    // Test 3: terminal state with outgoing transitions is detected
1019    #[test]
1020    fn terminal_state_with_transitions_detected() {
1021        let toml = r#"
1022[project]
1023name = "test"
1024
1025[tickets]
1026dir = "tickets"
1027
1028[[workflow.states]]
1029id       = "closed"
1030label    = "Closed"
1031terminal = true
1032
1033[[workflow.states.transitions]]
1034to = "new"
1035
1036[[workflow.states]]
1037id    = "new"
1038label = "New"
1039
1040[[workflow.states.transitions]]
1041to = "closed"
1042"#;
1043        let config = load_config(toml);
1044        let errors = validate_config(&config, Path::new("/tmp"));
1045        assert!(
1046            errors.iter().any(|e| e.contains("state.closed") && e.contains("terminal")),
1047            "expected terminal error in {errors:?}"
1048        );
1049    }
1050
1051    // Test 5: ticket with unknown state is detected
1052    #[test]
1053    fn ticket_with_unknown_state_detected() {
1054        use crate::ticket::Ticket;
1055
1056        let raw = "+++\nid = 1\ntitle = \"Test\"\nstate = \"phantom\"\n+++\n\n## Spec\n";
1057        let ticket = Ticket::parse(Path::new("tickets/0001-test.md"), raw).unwrap();
1058
1059        let known_states: std::collections::HashSet<&str> =
1060            ["new", "ready", "closed"].iter().copied().collect();
1061
1062        assert!(!known_states.contains(ticket.frontmatter.state.as_str()));
1063    }
1064
1065    // Test 6: dead-end non-terminal state is detected
1066    #[test]
1067    fn dead_end_non_terminal_detected() {
1068        let toml = r#"
1069[project]
1070name = "test"
1071
1072[tickets]
1073dir = "tickets"
1074
1075[[workflow.states]]
1076id    = "stuck"
1077label = "Stuck"
1078
1079[[workflow.states]]
1080id       = "closed"
1081label    = "Closed"
1082terminal = true
1083"#;
1084        let config = load_config(toml);
1085        let errors = validate_config(&config, Path::new("/tmp"));
1086        assert!(
1087            errors.iter().any(|e| e.contains("state.stuck") && e.contains("no outgoing transitions")),
1088            "expected dead-end error in {errors:?}"
1089        );
1090    }
1091
1092    // Test 7: context_section mismatch is detected
1093    #[test]
1094    fn context_section_mismatch_detected() {
1095        let toml = r#"
1096[project]
1097name = "test"
1098
1099[tickets]
1100dir = "tickets"
1101
1102[[ticket.sections]]
1103name = "Problem"
1104type = "free"
1105
1106[[workflow.states]]
1107id    = "new"
1108label = "New"
1109
1110[[workflow.states.transitions]]
1111to              = "ready"
1112context_section = "NonExistent"
1113
1114[[workflow.states]]
1115id    = "ready"
1116label = "Ready"
1117
1118[[workflow.states.transitions]]
1119to = "closed"
1120
1121[[workflow.states]]
1122id       = "closed"
1123label    = "Closed"
1124terminal = true
1125"#;
1126        let config = load_config(toml);
1127        let errors = validate_config(&config, Path::new("/tmp"));
1128        assert!(
1129            errors.iter().any(|e| e.contains("context_section") && e.contains("NonExistent")),
1130            "expected context_section error in {errors:?}"
1131        );
1132    }
1133
1134    // Test 8: focus_section mismatch is detected
1135    #[test]
1136    fn focus_section_mismatch_detected() {
1137        let toml = r#"
1138[project]
1139name = "test"
1140
1141[tickets]
1142dir = "tickets"
1143
1144[[ticket.sections]]
1145name = "Problem"
1146type = "free"
1147
1148[[workflow.states]]
1149id    = "new"
1150label = "New"
1151
1152[[workflow.states.transitions]]
1153to             = "ready"
1154focus_section  = "BadSection"
1155
1156[[workflow.states]]
1157id    = "ready"
1158label = "Ready"
1159
1160[[workflow.states.transitions]]
1161to = "closed"
1162
1163[[workflow.states]]
1164id       = "closed"
1165label    = "Closed"
1166terminal = true
1167"#;
1168        let config = load_config(toml);
1169        let errors = validate_config(&config, Path::new("/tmp"));
1170        assert!(
1171            errors.iter().any(|e| e.contains("focus_section") && e.contains("BadSection")),
1172            "expected focus_section error in {errors:?}"
1173        );
1174    }
1175
1176    // Test 9: completion=pr without provider is detected
1177    #[test]
1178    fn completion_pr_without_provider_detected() {
1179        let toml = r#"
1180[project]
1181name = "test"
1182
1183[tickets]
1184dir = "tickets"
1185
1186[[workflow.states]]
1187id    = "new"
1188label = "New"
1189
1190[[workflow.states.transitions]]
1191to         = "closed"
1192completion = "pr"
1193
1194[[workflow.states]]
1195id       = "closed"
1196label    = "Closed"
1197terminal = true
1198"#;
1199        let config = load_config(toml);
1200        let errors = validate_config(&config, Path::new("/tmp"));
1201        assert!(
1202            errors.iter().any(|e| e.contains("provider")),
1203            "expected provider error in {errors:?}"
1204        );
1205    }
1206
1207    // Test 10: completion=pr with provider configured passes
1208    #[test]
1209    fn completion_pr_with_provider_passes() {
1210        let toml = r#"
1211[project]
1212name = "test"
1213
1214[tickets]
1215dir = "tickets"
1216
1217[git_host]
1218provider = "github"
1219
1220[[workflow.states]]
1221id    = "new"
1222label = "New"
1223
1224[[workflow.states.transitions]]
1225to         = "closed"
1226completion = "pr"
1227
1228[[workflow.states]]
1229id       = "closed"
1230label    = "Closed"
1231terminal = true
1232"#;
1233        let config = load_config(toml);
1234        let errors = validate_config(&config, Path::new("/tmp"));
1235        assert!(
1236            !errors.iter().any(|e| e.contains("provider")),
1237            "unexpected provider error in {errors:?}"
1238        );
1239    }
1240
1241    // Test 11: context_section with empty ticket.sections is skipped
1242    #[test]
1243    fn context_section_skipped_when_no_sections_defined() {
1244        let toml = r#"
1245[project]
1246name = "test"
1247
1248[tickets]
1249dir = "tickets"
1250
1251[[workflow.states]]
1252id    = "new"
1253label = "New"
1254
1255[[workflow.states.transitions]]
1256to              = "closed"
1257context_section = "AnySection"
1258
1259[[workflow.states]]
1260id       = "closed"
1261label    = "Closed"
1262terminal = true
1263"#;
1264        let config = load_config(toml);
1265        let errors = validate_config(&config, Path::new("/tmp"));
1266        assert!(
1267            !errors.iter().any(|e| e.contains("context_section")),
1268            "unexpected context_section error in {errors:?}"
1269        );
1270    }
1271
1272    // Test: closed state is not flagged as unknown even when absent from config
1273    #[test]
1274    fn closed_state_not_flagged_as_unknown() {
1275        use crate::ticket::Ticket;
1276
1277        // Config with no "closed" state
1278        let toml = r#"
1279[project]
1280name = "test"
1281
1282[tickets]
1283dir = "tickets"
1284
1285[[workflow.states]]
1286id    = "new"
1287label = "New"
1288
1289[[workflow.states.transitions]]
1290to = "done"
1291
1292[[workflow.states]]
1293id       = "done"
1294label    = "Done"
1295terminal = true
1296"#;
1297        let config = load_config(toml);
1298        let state_ids: std::collections::HashSet<&str> = config.workflow.states.iter()
1299            .map(|s| s.id.as_str())
1300            .collect();
1301
1302        let raw = "+++\nid = 1\ntitle = \"Test\"\nstate = \"closed\"\n+++\n\n## Spec\n";
1303        let ticket = Ticket::parse(Path::new("tickets/0001-test.md"), raw).unwrap();
1304
1305        // "closed" is not in state_ids, but the validate logic skips it.
1306        assert!(!state_ids.contains("closed"));
1307        // Simulate the validate check: closed should be exempt.
1308        let fm = &ticket.frontmatter;
1309        let flagged = !state_ids.is_empty() && fm.state != "closed" && !state_ids.contains(fm.state.as_str());
1310        assert!(!flagged, "closed state should not be flagged as unknown");
1311    }
1312
1313    // Test for state_ids helper (kept for compatibility)
1314    #[test]
1315    fn state_ids_helper() {
1316        let toml = r#"
1317[project]
1318name = "test"
1319
1320[tickets]
1321dir = "tickets"
1322
1323[[workflow.states]]
1324id    = "new"
1325label = "New"
1326"#;
1327        let config = load_config(toml);
1328        let ids = state_ids(&config);
1329        assert!(ids.contains("new"));
1330    }
1331
1332    #[test]
1333    fn validate_warnings_no_container() {
1334        let toml = r#"
1335[project]
1336name = "test"
1337
1338[tickets]
1339dir = "tickets"
1340"#;
1341        let config = load_config(toml);
1342        let warnings = super::validate_warnings(&config, Path::new("/tmp"));
1343        assert!(warnings.is_empty());
1344    }
1345
1346    #[test]
1347    fn valid_collaborator_accepted() {
1348        let toml = r#"
1349[project]
1350name = "test"
1351collaborators = ["alice", "bob"]
1352
1353[tickets]
1354dir = "tickets"
1355"#;
1356        let config = load_config(toml);
1357        assert!(super::validate_owner(&config, &LocalConfig::default(), "alice").is_ok());
1358    }
1359
1360    #[test]
1361    fn unknown_user_rejected() {
1362        let toml = r#"
1363[project]
1364name = "test"
1365collaborators = ["alice", "bob"]
1366
1367[tickets]
1368dir = "tickets"
1369"#;
1370        let config = load_config(toml);
1371        let err = super::validate_owner(&config, &LocalConfig::default(), "charlie").unwrap_err();
1372        let msg = err.to_string();
1373        assert!(msg.contains("unknown user 'charlie'"), "unexpected message: {msg}");
1374        assert!(msg.contains("alice, bob"), "unexpected message: {msg}");
1375    }
1376
1377    #[test]
1378    fn empty_collaborators_skips_validation() {
1379        let toml = r#"
1380[project]
1381name = "test"
1382
1383[tickets]
1384dir = "tickets"
1385"#;
1386        let config = load_config(toml);
1387        assert!(super::validate_owner(&config, &LocalConfig::default(), "anyone").is_ok());
1388    }
1389
1390    #[test]
1391    fn clear_owner_always_allowed() {
1392        let toml = r#"
1393[project]
1394name = "test"
1395collaborators = ["alice"]
1396
1397[tickets]
1398dir = "tickets"
1399"#;
1400        let config = load_config(toml);
1401        assert!(super::validate_owner(&config, &LocalConfig::default(), "-").is_ok());
1402    }
1403
1404    #[test]
1405    fn github_mode_known_user_accepted() {
1406        let toml = r#"
1407[project]
1408name = "test"
1409collaborators = ["alice", "bob"]
1410
1411[tickets]
1412dir = "tickets"
1413
1414[git_host]
1415provider = "github"
1416repo = "org/repo"
1417"#;
1418        let config = load_config(toml);
1419        // No token in LocalConfig::default() — falls back to project.collaborators
1420        assert!(super::validate_owner(&config, &LocalConfig::default(), "alice").is_ok());
1421    }
1422
1423    #[test]
1424    fn github_mode_unknown_user_rejected() {
1425        let toml = r#"
1426[project]
1427name = "test"
1428collaborators = ["alice", "bob"]
1429
1430[tickets]
1431dir = "tickets"
1432
1433[git_host]
1434provider = "github"
1435repo = "org/repo"
1436"#;
1437        let config = load_config(toml);
1438        // No token — falls back to project.collaborators; charlie is not in the list
1439        let err = super::validate_owner(&config, &LocalConfig::default(), "charlie").unwrap_err();
1440        assert!(err.to_string().contains("charlie"), "expected charlie in: {err}");
1441    }
1442
1443    #[test]
1444    fn github_mode_no_collaborators_skips_check() {
1445        let toml = r#"
1446[project]
1447name = "test"
1448
1449[tickets]
1450dir = "tickets"
1451
1452[git_host]
1453provider = "github"
1454repo = "org/repo"
1455"#;
1456        let config = load_config(toml);
1457        // Empty collaborators list — no validation
1458        assert!(super::validate_owner(&config, &LocalConfig::default(), "anyone").is_ok());
1459    }
1460
1461    #[test]
1462    fn github_mode_clear_owner_accepted() {
1463        let toml = r#"
1464[project]
1465name = "test"
1466collaborators = ["alice"]
1467
1468[tickets]
1469dir = "tickets"
1470
1471[git_host]
1472provider = "github"
1473repo = "org/repo"
1474"#;
1475        let config = load_config(toml);
1476        assert!(super::validate_owner(&config, &LocalConfig::default(), "-").is_ok());
1477    }
1478
1479    #[test]
1480    fn non_github_mode_unknown_user_rejected() {
1481        let toml = r#"
1482[project]
1483name = "test"
1484collaborators = ["alice", "bob"]
1485
1486[tickets]
1487dir = "tickets"
1488"#;
1489        let config = load_config(toml);
1490        let err = super::validate_owner(&config, &LocalConfig::default(), "charlie").unwrap_err();
1491        assert!(err.to_string().contains("charlie"), "expected charlie in: {err}");
1492    }
1493
1494    #[test]
1495    fn validate_warnings_empty_container() {
1496        let toml = r#"
1497[project]
1498name = "test"
1499
1500[tickets]
1501dir = "tickets"
1502
1503[workers]
1504container = ""
1505"#;
1506        let config = load_config(toml);
1507        let warnings = super::validate_warnings(&config, Path::new("/tmp"));
1508        assert!(warnings.is_empty(), "empty container string should not warn");
1509    }
1510
1511    #[test]
1512    fn dead_end_workflow_warning_emitted() {
1513        // A workflow where the only agent-actionable state cycles back to itself
1514        // with no completion strategy — no "success" outcome is reachable.
1515        let toml = r#"
1516[project]
1517name = "test"
1518
1519[tickets]
1520dir = "tickets"
1521
1522[[workflow.states]]
1523id         = "start"
1524label      = "Start"
1525actionable = ["agent"]
1526
1527[[workflow.states.transitions]]
1528to = "middle"
1529
1530[[workflow.states]]
1531id    = "middle"
1532label = "Middle"
1533
1534[[workflow.states.transitions]]
1535to = "start"
1536"#;
1537        let config = load_config(toml);
1538        let warnings = super::validate_warnings(&config, Path::new("/tmp"));
1539        assert!(
1540            warnings.iter().any(|w| w.contains("success")),
1541            "expected dead-end warning containing 'success'; got: {warnings:?}"
1542        );
1543    }
1544
1545    #[test]
1546    fn default_workflow_no_dead_end_warning() {
1547        // The default workflow has in_progress → implemented with completion = pr_or_epic_merge,
1548        // reachable from the agent-actionable "ready" state. No dead-end warning should fire.
1549        let base = r#"
1550[project]
1551name = "test"
1552
1553[tickets]
1554dir = "tickets"
1555"#;
1556        let combined = format!("{}\n{}", base, crate::init::default_workflow_toml());
1557        let config: Config = toml::from_str(&combined).unwrap();
1558        let warnings = super::validate_warnings(&config, Path::new("/tmp"));
1559        assert!(
1560            !warnings.iter().any(|w| w.contains("no reachable") && w.contains("success")),
1561            "unexpected dead-end warning for default workflow; got: {warnings:?}"
1562        );
1563    }
1564
1565    #[test]
1566    fn worktree_missing_in_design() {
1567        let dir = setup_verify_repo();
1568        let root = dir.path();
1569        let config = Config::load(root).unwrap();
1570        let ticket = make_verify_ticket(root, "abcd1234", "in_design", Some("ticket/abcd1234-test"));
1571
1572        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1573
1574        let main_root = git_util::main_worktree_root(root).unwrap_or_else(|| root.to_path_buf());
1575        let wt_path = main_root.join("worktrees").join("ticket-abcd1234-test");
1576        let expected = format!(
1577            "#abcd1234 [in_design]: worktree at {} is missing",
1578            wt_path.display()
1579        );
1580        assert!(
1581            issues.iter().any(|i| i == &expected),
1582            "expected worktree missing issue; got: {issues:?}"
1583        );
1584    }
1585
1586    #[test]
1587    fn worktree_present_no_issue() {
1588        let dir = setup_verify_repo();
1589        let root = dir.path();
1590        let config = Config::load(root).unwrap();
1591        let ticket = make_verify_ticket(root, "abcd1234", "in_design", Some("ticket/abcd1234-test"));
1592
1593        std::fs::create_dir_all(root.join("worktrees").join("ticket-abcd1234-test")).unwrap();
1594
1595        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1596        assert!(
1597            !issues.iter().any(|i| i.contains("worktree")),
1598            "unexpected worktree issue; got: {issues:?}"
1599        );
1600    }
1601
1602    #[test]
1603    fn worktree_check_skipped_for_other_states() {
1604        let dir = setup_verify_repo();
1605        let root = dir.path();
1606        let config = Config::load(root).unwrap();
1607        let ticket = make_verify_ticket(root, "abcd1234", "specd", Some("ticket/abcd1234-test"));
1608
1609        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1610        assert!(
1611            !issues.iter().any(|i| i.contains("worktree")),
1612            "unexpected worktree issue for specd state; got: {issues:?}"
1613        );
1614    }
1615
1616    fn in_repo_wt_config(dir: &str) -> Config {
1617        let toml = format!(
1618            r#"
1619[project]
1620name = "test"
1621
1622[tickets]
1623dir = "tickets"
1624
1625[worktrees]
1626dir = "{dir}"
1627"#
1628        );
1629        toml::from_str(&toml).expect("config parse failed")
1630    }
1631
1632    #[test]
1633    fn validate_config_gitignore_missing_in_repo_wt() {
1634        let tmp = tempfile::TempDir::new().unwrap();
1635        let config = in_repo_wt_config("worktrees");
1636        let errors = validate_config(&config, tmp.path());
1637        assert!(
1638            errors.iter().any(|e| e.contains("worktrees") && e.contains(".gitignore")),
1639            "expected gitignore missing error; got: {errors:?}"
1640        );
1641    }
1642
1643    #[test]
1644    fn validate_config_gitignore_covered_anchored_slash() {
1645        let tmp = tempfile::TempDir::new().unwrap();
1646        std::fs::write(tmp.path().join(".gitignore"), "/worktrees/\n").unwrap();
1647        let config = in_repo_wt_config("worktrees");
1648        let errors = validate_config(&config, tmp.path());
1649        assert!(
1650            !errors.iter().any(|e| e.contains("gitignore")),
1651            "unexpected gitignore error; got: {errors:?}"
1652        );
1653    }
1654
1655    #[test]
1656    fn validate_config_gitignore_covered_anchored_no_slash() {
1657        let tmp = tempfile::TempDir::new().unwrap();
1658        std::fs::write(tmp.path().join(".gitignore"), "/worktrees\n").unwrap();
1659        let config = in_repo_wt_config("worktrees");
1660        let errors = validate_config(&config, tmp.path());
1661        assert!(
1662            !errors.iter().any(|e| e.contains("gitignore")),
1663            "unexpected gitignore error; got: {errors:?}"
1664        );
1665    }
1666
1667    #[test]
1668    fn validate_config_gitignore_covered_unanchored_slash() {
1669        let tmp = tempfile::TempDir::new().unwrap();
1670        std::fs::write(tmp.path().join(".gitignore"), "worktrees/\n").unwrap();
1671        let config = in_repo_wt_config("worktrees");
1672        let errors = validate_config(&config, tmp.path());
1673        assert!(
1674            !errors.iter().any(|e| e.contains("gitignore")),
1675            "unexpected gitignore error; got: {errors:?}"
1676        );
1677    }
1678
1679    #[test]
1680    fn validate_config_gitignore_covered_bare() {
1681        let tmp = tempfile::TempDir::new().unwrap();
1682        std::fs::write(tmp.path().join(".gitignore"), "worktrees\n").unwrap();
1683        let config = in_repo_wt_config("worktrees");
1684        let errors = validate_config(&config, tmp.path());
1685        assert!(
1686            !errors.iter().any(|e| e.contains("gitignore")),
1687            "unexpected gitignore error; got: {errors:?}"
1688        );
1689    }
1690
1691    #[test]
1692    fn validate_config_gitignore_not_covered() {
1693        let tmp = tempfile::TempDir::new().unwrap();
1694        std::fs::write(tmp.path().join(".gitignore"), "node_modules\n").unwrap();
1695        let config = in_repo_wt_config("worktrees");
1696        let errors = validate_config(&config, tmp.path());
1697        assert!(
1698            errors.iter().any(|e| e.contains("worktrees") && e.contains("gitignore")),
1699            "expected gitignore not covered error; got: {errors:?}"
1700        );
1701    }
1702
1703    #[test]
1704    fn validate_config_gitignore_no_false_positive() {
1705        let tmp = tempfile::TempDir::new().unwrap();
1706        std::fs::write(tmp.path().join(".gitignore"), "wt-old/\n").unwrap();
1707        let config = in_repo_wt_config("wt");
1708        let errors = validate_config(&config, tmp.path());
1709        assert!(
1710            errors.iter().any(|e| e.contains("wt") && e.contains("gitignore")),
1711            "wt-old should not match wt; got: {errors:?}"
1712        );
1713    }
1714
1715    #[test]
1716    fn validate_config_external_dotdot_no_check() {
1717        let tmp = tempfile::TempDir::new().unwrap();
1718        // No .gitignore at all
1719        let config = in_repo_wt_config("../ext");
1720        let errors = validate_config(&config, tmp.path());
1721        assert!(
1722            !errors.iter().any(|e| e.contains("gitignore")),
1723            "external dotdot path should skip gitignore check; got: {errors:?}"
1724        );
1725    }
1726
1727    #[test]
1728    fn validate_config_external_absolute_no_check() {
1729        let tmp = tempfile::TempDir::new().unwrap();
1730        // No .gitignore at all
1731        let config = in_repo_wt_config("/abs/path");
1732        let errors = validate_config(&config, tmp.path());
1733        assert!(
1734            !errors.iter().any(|e| e.contains("gitignore")),
1735            "absolute path should skip gitignore check; got: {errors:?}"
1736        );
1737    }
1738
1739    fn config_with_merge_transition(completion: &str, on_failure: Option<&str>, declare_failure_state: bool) -> Config {
1740        let on_failure_line = on_failure
1741            .map(|v| format!("on_failure = \"{v}\"\n"))
1742            .unwrap_or_default();
1743        let merge_failed_state = if declare_failure_state {
1744            r#"
1745[[workflow.states]]
1746id       = "merge_failed"
1747label    = "Merge failed"
1748
1749[[workflow.states.transitions]]
1750to = "closed"
1751"#
1752        } else {
1753            ""
1754        };
1755        let toml = format!(
1756            r#"
1757[project]
1758name = "test"
1759
1760[tickets]
1761dir = "tickets"
1762
1763[[workflow.states]]
1764id    = "in_progress"
1765label = "In Progress"
1766
1767[[workflow.states.transitions]]
1768to         = "implemented"
1769completion = "{completion}"
1770{on_failure_line}
1771[[workflow.states]]
1772id       = "implemented"
1773label    = "Implemented"
1774terminal = true
1775
1776[[workflow.states]]
1777id       = "closed"
1778label    = "Closed"
1779terminal = true
1780{merge_failed_state}
1781"#
1782        );
1783        toml::from_str(&toml).expect("config parse failed")
1784    }
1785
1786    #[test]
1787    fn test_on_failure_missing_for_merge() {
1788        let config = config_with_merge_transition("merge", None, false);
1789        let errors = validate_config(&config, std::path::Path::new("/tmp"));
1790        assert!(
1791            errors.iter().any(|e| e.contains("missing `on_failure`")),
1792            "expected missing on_failure error; got: {errors:?}"
1793        );
1794    }
1795
1796    #[test]
1797    fn test_on_failure_missing_for_pr_or_epic_merge() {
1798        // No ticket with target_branch — rule fires on transition definition alone.
1799        let config = config_with_merge_transition("pr_or_epic_merge", None, false);
1800        let errors = validate_config(&config, std::path::Path::new("/tmp"));
1801        assert!(
1802            errors.iter().any(|e| e.contains("missing `on_failure`")),
1803            "expected missing on_failure error for pr_or_epic_merge; got: {errors:?}"
1804        );
1805    }
1806
1807    #[test]
1808    fn test_on_failure_unknown_state() {
1809        let config = config_with_merge_transition("merge", Some("ghost_state"), false);
1810        let errors = validate_config(&config, std::path::Path::new("/tmp"));
1811        assert!(
1812            errors.iter().any(|e| e.contains("ghost_state")),
1813            "expected unknown state error for ghost_state; got: {errors:?}"
1814        );
1815    }
1816
1817    #[test]
1818    fn test_on_failure_valid() {
1819        let config = config_with_merge_transition("merge", Some("merge_failed"), true);
1820        let errors = validate_config(&config, std::path::Path::new("/tmp"));
1821        let on_failure_errors: Vec<&String> = errors.iter()
1822            .filter(|e| e.contains("on_failure") || e.contains("ghost_state") || e.contains("merge_failed"))
1823            .collect();
1824        assert!(
1825            on_failure_errors.is_empty(),
1826            "unexpected on_failure errors: {on_failure_errors:?}"
1827        );
1828    }
1829
1830    // --- frontmatter agent validation ---
1831
1832    fn make_agent_verify_ticket(root: &std::path::Path, id: &str, state: &str, extra_fm: &str) -> Ticket {
1833        let raw = format!(
1834            "+++\nid = \"{id}\"\ntitle = \"Test ticket\"\nstate = \"{state}\"\n{extra_fm}+++\n\n## Spec\n\n## History\n"
1835        );
1836        let path = root.join("tickets").join(format!("{id}-test.md"));
1837        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
1838        std::fs::write(&path, &raw).unwrap();
1839        Ticket::parse(&path, &raw).unwrap()
1840    }
1841
1842    #[test]
1843    fn validate_unknown_frontmatter_agent_is_error() {
1844        let dir = setup_verify_repo();
1845        let root = dir.path();
1846        let config = Config::load(root).unwrap();
1847        let ticket = make_agent_verify_ticket(root, "abcd1234", "specd", "agent = \"nonexistent-bot\"\n");
1848
1849        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1850
1851        assert!(
1852            issues.iter().any(|i| i.contains("abcd1234") && i.contains("nonexistent-bot")),
1853            "expected error with ticket id and agent name; got: {issues:?}"
1854        );
1855    }
1856
1857    #[test]
1858    fn validate_unknown_agent_in_overrides_is_error() {
1859        let dir = setup_verify_repo();
1860        let root = dir.path();
1861        let config = Config::load(root).unwrap();
1862        let ticket = make_agent_verify_ticket(
1863            root,
1864            "abcd1234",
1865            "specd",
1866            "[agent_overrides]\nimpl_agent = \"nonexistent-bot\"\n",
1867        );
1868
1869        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1870
1871        assert!(
1872            issues.iter().any(|i| i.contains("abcd1234") && i.contains("nonexistent-bot")),
1873            "expected error with ticket id and agent name; got: {issues:?}"
1874        );
1875    }
1876
1877    #[test]
1878    fn validate_known_frontmatter_agent_passes() {
1879        let dir = setup_verify_repo();
1880        let root = dir.path();
1881        let config = Config::load(root).unwrap();
1882        let ticket = make_agent_verify_ticket(root, "abcd1234", "specd", "agent = \"claude\"\n");
1883
1884        let issues = verify_tickets(root, &config, &[ticket], &HashSet::new());
1885
1886        assert!(
1887            !issues.iter().any(|i| i.contains("is not a known built-in")),
1888            "expected no agent error for known built-in; got: {issues:?}"
1889        );
1890    }
1891}