trusty-mpm 1.7.0

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! Unit tests for the `tm issue` operations, driven by a fake [`TicketSystem`].
//!
//! Why: every operation must be verifiable without a live `gh`/GitHub. A
//! scripted [`FakeSystem`] records each backend call so tests assert the exact
//! label/assignee/comment mutations (and, crucially, that invalid transitions
//! error BEFORE any mutation).
//! What: [`FakeSystem`] (a `TicketSystem` impl recording calls + returning a
//! scripted issue) and the `ops_*` tests for seed-labels, transition, current,
//! and repair.
//! Test: this IS the test module (classified test file: 1500-SLOC cap).

use std::cell::RefCell;

use super::*;
use crate::commands::ticket::labels::{AssigneeTarget, RepoLabel};
use crate::commands::ticket::system::{Issue, TicketSystem};
use trusty_mpm::core::trusty_tools_config::ResolvedTicketing;

/// A recorded backend call (for assertion).
#[derive(Debug, Clone, PartialEq, Eq)]
enum Call {
    ListRepoLabels,
    CreateLabel(String),
    AddLabel(u64, String),
    RemoveLabel(u64, String),
    SwapLabels(u64, String, String),
    SetAssignee(u64, AssigneeTarget, Vec<String>),
    Comment(u64, String),
    Validate(u64),
}

/// A scripted [`TicketSystem`] recording every call.
struct FakeSystem {
    issue: Issue,
    repo_labels: Vec<RepoLabel>,
    calls: RefCell<Vec<Call>>,
}

impl FakeSystem {
    fn new(issue: Issue) -> Self {
        Self {
            issue,
            repo_labels: Vec::new(),
            calls: RefCell::new(Vec::new()),
        }
    }
    fn with_repo_labels(mut self, labels: Vec<RepoLabel>) -> Self {
        self.repo_labels = labels;
        self
    }
    fn calls(&self) -> Vec<Call> {
        self.calls.borrow().clone()
    }
    fn record(&self, c: Call) {
        self.calls.borrow_mut().push(c);
    }
}

impl TicketSystem for FakeSystem {
    fn name(&self) -> &'static str {
        "fake"
    }
    fn validate(&self, issue_number: u64) -> anyhow::Result<Issue> {
        self.record(Call::Validate(issue_number));
        Ok(self.issue.clone())
    }
    fn comment(&self, issue_number: u64, body: &str) -> anyhow::Result<()> {
        self.record(Call::Comment(issue_number, body.to_string()));
        Ok(())
    }
    fn list_repo_labels(&self) -> anyhow::Result<Vec<RepoLabel>> {
        self.record(Call::ListRepoLabels);
        Ok(self.repo_labels.clone())
    }
    fn create_label(&self, label: &RepoLabel) -> anyhow::Result<()> {
        self.record(Call::CreateLabel(label.name.clone()));
        Ok(())
    }
    fn add_label(&self, issue: u64, label: &str) -> anyhow::Result<()> {
        self.record(Call::AddLabel(issue, label.to_string()));
        Ok(())
    }
    fn remove_label(&self, issue: u64, label: &str) -> anyhow::Result<()> {
        self.record(Call::RemoveLabel(issue, label.to_string()));
        Ok(())
    }
    fn swap_labels(&self, issue: u64, add: &str, remove: &str) -> anyhow::Result<()> {
        self.record(Call::SwapLabels(issue, add.to_string(), remove.to_string()));
        Ok(())
    }
    fn set_assignee(
        &self,
        issue: u64,
        who: &AssigneeTarget,
        current: &[String],
    ) -> anyhow::Result<()> {
        self.record(Call::SetAssignee(issue, who.clone(), current.to_vec()));
        Ok(())
    }
}

fn model() -> StateModel {
    serde_yaml::from_str(super::super::config::DEFAULT_MODEL_YAML).expect("default parses")
}

fn issue_with_labels(number: u64, labels: &[&str]) -> Issue {
    Issue {
        number,
        title: "t".to_string(),
        body: String::new(),
        labels: labels.iter().map(|s| s.to_string()).collect(),
        assignees: Vec::new(),
        open: true,
    }
}

// ---- seed-labels ----------------------------------------------------------

/// The tmux session name the `ws/` policy label is derived from in these tests.
const SESSION: &str = "tm-tcode-01";

/// Every `create_label` name the fake recorded, in call order.
fn create_names(sys: &FakeSystem) -> Vec<String> {
    sys.calls()
        .into_iter()
        .filter_map(|c| match c {
            Call::CreateLabel(n) => Some(n),
            _ => None,
        })
        .collect()
}

#[test]
fn ops_seed_creates_only_missing() {
    let m = model();
    // Repo already has the base `unicorn` label and `unicorn:queued`.
    let existing = vec![
        RepoLabel::new("unicorn", "7B68EE", ""),
        RepoLabel::new("unicorn:queued", "BFD4F2", ""),
    ];
    let sys = FakeSystem::new(issue_with_labels(1, &[])).with_repo_labels(existing);
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &[],
    )
    .expect("seed");

    // Present ones not re-created.
    assert!(report.already_present.contains(&"unicorn".to_string()));
    assert!(
        report
            .already_present
            .contains(&"unicorn:queued".to_string())
    );
    // Missing ones created.
    assert!(report.created.contains(&"unicorn:approved".to_string()));
    assert!(report.created.contains(&"blast:high".to_string()));
    assert!(report.created.contains(&"approval:level-1".to_string()));
    // No create call for the already-present labels.
    let created = create_names(&sys);
    assert!(!created.contains(&"unicorn".to_string()));
    assert!(!created.contains(&"unicorn:queued".to_string()));
}

/// #6914: seeding covers the FRAMEWORK's own labels, not just the model's.
///
/// Before the fix, `trusty-mpm` and `ws/<session>` were absent from
/// `seed-labels` entirely — the first `gh issue create --label trusty-mpm` in a
/// fresh repo failed on an unknown label.
#[test]
fn ops_seed_includes_policy_labels() {
    let m = model();
    // Zero existing labels: everything the harness applies must be created.
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &[],
    )
    .expect("seed");

    let created = create_names(&sys);
    assert!(
        created.contains(&"trusty-mpm".to_string()),
        "the convention label must be seeded; got {created:?}"
    );
    assert!(
        created.contains(&"ws/tm-tcode-01".to_string()),
        "the workstream label must be seeded; got {created:?}"
    );
    assert_eq!(created, report.created, "report must match the calls made");
    assert!(!report.workstream_skipped);

    // The retired pair session launch used to seed must never come back.
    assert!(
        !created.iter().any(|n| n == "in-progress" || n == "blocked"),
        "the retired lifecycle pair must not be seeded; got {created:?}"
    );
}

/// #6918: an `agents.ticketing.extra_labels` entry is seeded by the same call
/// the built-in policy labels are, with no second table.
#[test]
fn ops_seed_includes_configured_extra_labels() {
    let m = model();
    let ticketing = ResolvedTicketing::default().with_extra_labels(vec![RepoLabel::new(
        "area/cli",
        "0E8A16",
        "CLI surface",
    )]);
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let report = seed_labels(&sys, &m, &ticketing, Some(SESSION), false, &[]).expect("seed");

    let created = create_names(&sys);
    assert!(
        created.contains(&"area/cli".to_string()),
        "a configured label must be seeded; got {created:?}"
    );
    assert!(created.contains(&"trusty-mpm".to_string()));
    assert_eq!(created, report.created);
}

/// #6918: THE regression this slice owes — an absent `agents.ticketing` block
/// must seed byte-for-byte what 6ee2b182b (#6914) seeded.
///
/// A default [`ResolvedTicketing`] is exactly what an absent block resolves to,
/// so comparing its seed output against the built-in policy table proves the
/// config surface changed nothing observable for every project that has no
/// block — which is all of them until someone writes one.
#[test]
fn ops_seed_absent_block_matches_builtin_output() {
    let m = model();
    for session in [None, Some(SESSION)] {
        let sys = FakeSystem::new(issue_with_labels(1, &[]));
        let report = seed_labels(&sys, &m, &ResolvedTicketing::default(), session, false, &[])
            .expect("seed with the default standard");

        // The built-in expectation, assembled without touching the config path.
        let mut expected: Vec<String> = m
            .states
            .iter()
            .filter_map(|s| s.label.as_ref())
            .map(|l| l.name.clone())
            .collect();
        expected.extend(m.extra_labels.iter().map(|l| l.name.clone()));
        for label in trusty_mpm::core::policy_labels::policy_labels(session) {
            if !expected.contains(&label.name) {
                expected.push(label.name);
            }
        }
        assert_eq!(report.created, expected, "session={session:?}");
        assert_eq!(create_names(&sys), expected, "session={session:?}");
    }
}

/// #6914: with no session name there is no `ws/` label — and the report says so.
#[test]
fn ops_seed_without_session_skips_workstream_label() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let report =
        seed_labels(&sys, &m, &ResolvedTicketing::default(), None, false, &[]).expect("seed");

    let created = create_names(&sys);
    assert!(created.contains(&"trusty-mpm".to_string()));
    assert!(
        !created.iter().any(|n| n.starts_with("ws/")),
        "no session name means no ws/ label; got {created:?}"
    );
    assert!(
        report.workstream_skipped,
        "the omission must be reported, not silent"
    );
}

/// #6914: a policy label the repo already carries is left untouched — no
/// re-create, and therefore no colour or description rewrite.
#[test]
fn ops_seed_leaves_present_policy_labels_untouched() {
    let m = model();
    let existing = vec![
        RepoLabel::new("trusty-mpm", "111111", "a project's own wording"),
        RepoLabel::new("ws/tm-tcode-01", "222222", "already styled"),
    ];
    let sys = FakeSystem::new(issue_with_labels(1, &[])).with_repo_labels(existing);
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &[],
    )
    .expect("seed");

    let created = create_names(&sys);
    assert!(!created.contains(&"trusty-mpm".to_string()));
    assert!(!created.contains(&"ws/tm-tcode-01".to_string()));
    assert!(report.already_present.contains(&"trusty-mpm".to_string()));
    assert!(
        report
            .already_present
            .contains(&"ws/tm-tcode-01".to_string())
    );
}

#[test]
fn ops_seed_dry_run_creates_nothing() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        true,
        &[],
    )
    .expect("seed dry");
    assert!(report.dry_run);
    // Everything reported as created (would-be), but ZERO create calls.
    assert!(!report.created.is_empty());
    let create_calls = sys
        .calls()
        .into_iter()
        .filter(|c| matches!(c, Call::CreateLabel(_)))
        .count();
    assert_eq!(create_calls, 0, "dry-run must make zero create_label calls");
}

#[test]
fn ops_seed_idempotent_when_all_present() {
    let m = model();
    let all: Vec<RepoLabel> = {
        let mut v: Vec<RepoLabel> = m
            .states
            .iter()
            .filter_map(|s| s.label.as_ref())
            .map(|l| RepoLabel::new(l.name.clone(), l.color.clone(), l.description.clone()))
            .collect();
        v.extend(
            m.extra_labels
                .iter()
                .map(|l| RepoLabel::new(l.name.clone(), l.color.clone(), l.description.clone())),
        );
        // #6914: the policy set is part of what a second run must find present.
        v.extend(trusty_mpm::core::policy_labels::policy_labels(Some(
            SESSION,
        )));
        v
    };
    let sys = FakeSystem::new(issue_with_labels(1, &[])).with_repo_labels(all);
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &[],
    )
    .expect("seed");
    assert!(
        report.created.is_empty(),
        "second run creates nothing; got {:?}",
        report.created
    );
}

/// A `CommandRunner` that emulates gh's paging for `gh label list`: it returns
/// the first `--limit` labels of `repo_labels`, defaulting to gh's own 30 when
/// the caller passes no `--limit`, and never says that it truncated.
///
/// Why: #6914 — the seed probe asked for no limit, so on a repo with more than
/// 30 labels it read a partial set, judged every policy label missing, and the
/// run died on the first `gh label create` of a label that already existed.
/// Only a runner that reproduces gh's silent cap can catch that.
struct PagedGhRunner {
    repo_labels: Vec<RepoLabel>,
}

impl crate::commands::ticket::runner::CommandRunner for PagedGhRunner {
    fn run(
        &self,
        _program: &str,
        args: &[&str],
    ) -> anyhow::Result<crate::commands::ticket::runner::CommandOutput> {
        assert_eq!(args.first(), Some(&"label"), "only `gh label` is scripted");
        assert_eq!(args.get(1), Some(&"list"), "only `label list` is scripted");
        let limit: usize = args
            .iter()
            .position(|a| *a == "--limit")
            .and_then(|i| args.get(i + 1))
            .map_or(30, |v| v.parse().expect("--limit must be a number"));
        let page: Vec<serde_json::Value> = self
            .repo_labels
            .iter()
            .take(limit)
            .map(|l| {
                serde_json::json!({
                    "name": l.name,
                    "color": l.color,
                    "description": l.description,
                })
            })
            .collect();
        Ok(crate::commands::ticket::runner::CommandOutput {
            success: true,
            stdout: serde_json::to_string(&page).expect("serialize"),
            stderr: String::new(),
        })
    }
}

/// Why: #6914's live failure — every policy label existed on the repo, yet the
/// run reported all six missing and exited 1 on `gh label create`. This drives
/// the real gh-backed system over a runner that reproduces gh's silent 30-label
/// page, with the desired labels deliberately past that page.
/// What: 40 filler labels ahead of the desired set; a correct probe reads the
/// whole list and creates nothing.
#[test]
fn ops_seed_reads_past_the_default_label_page() {
    let m = model();
    let desired = desired_labels(&m, &ResolvedTicketing::default(), Some(SESSION));
    // Filler ahead of the desired labels, so a 30-label read sees none of them.
    let mut repo_labels: Vec<RepoLabel> = (0..40)
        .map(|i| RepoLabel::new(format!("filler-{i:02}"), "CCCCCC", ""))
        .collect();
    repo_labels.extend(desired.iter().cloned());

    let sys = crate::commands::ticket::system::GhTicketSystem::new(PagedGhRunner { repo_labels });
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        true,
        &[],
    )
    .expect("seed reads the whole label set");

    assert!(
        report.created.is_empty(),
        "every desired label already exists; got created {:?}",
        report.created
    );
    assert_eq!(
        report.already_present.len(),
        desired.len(),
        "all {} desired labels must be reported present",
        desired.len()
    );
}

/// Why: a read that comes back exactly as long as the requested page may be
/// truncated, and seeding against a truncated set is the #6914 failure. It is
/// an error, never a silently-accepted "complete" set.
#[test]
fn ops_seed_errors_when_the_label_page_is_full() {
    let m = model();
    let limit = trusty_mpm::core::policy_labels::LABEL_LIST_LIMIT;
    let repo_labels: Vec<RepoLabel> = (0..limit + 5)
        .map(|i| RepoLabel::new(format!("filler-{i:04}"), "CCCCCC", ""))
        .collect();

    let sys = crate::commands::ticket::system::GhTicketSystem::new(PagedGhRunner { repo_labels });
    let err = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        true,
        &[],
    )
    .expect_err("a full page must not pass as a complete label set");
    assert!(
        err.to_string().contains("truncated"),
        "error must name the truncation; got {err}"
    );
}

// ---- seed-labels --only (#7983) -------------------------------------------

/// #7983: an agent that needed one lifecycle family got 18 repo-wide labels —
/// `blast:*`, `T2`-`T4`, `approval:level-*`, `ws/<session>` and the rest.
/// `--only unicorn:` seeds that family and nothing else.
#[test]
fn ops_seed_only_scopes_to_one_family_7983() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let only = vec!["unicorn:".to_string()];
    let report = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &only,
    )
    .expect("scoped seed");

    let created = create_names(&sys);
    assert!(!created.is_empty(), "the family must still be seeded");
    assert!(
        created.iter().all(|n| n.starts_with("unicorn:")),
        "only the named family may be created; got {created:?}"
    );
    for unwanted in ["blast:high", "approval:level-1", "trusty-mpm", "unicorn"] {
        assert!(
            !created.contains(&unwanted.to_string()),
            "`{unwanted}` is outside the scope; got {created:?}"
        );
    }
    assert!(
        !created.iter().any(|n| n.starts_with("ws/")),
        "the workstream label is outside the scope; got {created:?}"
    );
    assert_eq!(created, report.created, "report must match the calls made");
    assert!(
        !report.workstream_skipped,
        "a scoped run makes no claim about the policy set it never attempted"
    );
}

/// #7983: a filter with no family separator is an exact name, so `unicorn`
/// seeds the base label alone and not the `unicorn:*` family.
#[test]
fn ops_seed_only_matches_a_single_label_by_name_7983() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let only = vec!["unicorn".to_string()];
    seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &only,
    )
    .expect("scoped seed");
    assert_eq!(create_names(&sys), vec!["unicorn".to_string()]);
}

/// #7983: a filter that selects nothing is a typo. Creating zero labels and
/// exiting 0 would read as a completed seed, so it errors — before any `gh`
/// call — and names what the model would have seeded.
#[test]
fn ops_seed_only_rejects_a_filter_that_matches_nothing_7983() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(1, &[]));
    let only = vec!["unicorn:".to_string(), "nope:".to_string()];
    let err = seed_labels(
        &sys,
        &m,
        &ResolvedTicketing::default(),
        Some(SESSION),
        false,
        &only,
    )
    .expect_err("an unmatched filter must not pass as a scoped seed");
    let text = err.to_string();
    assert!(text.contains("nope:"), "must name the filter; got {text}");
    assert!(
        text.contains("available:"),
        "must list what is seedable; got {text}"
    );
    assert!(
        sys.calls().is_empty(),
        "a typo'd filter must cost no gh call; got {:?}",
        sys.calls()
    );
}

// ---- transition -----------------------------------------------------------

#[test]
fn ops_transition_happy_path() {
    let m = model();
    // Issue is currently `queued`; transition to `approved`.
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn", "unicorn:queued"]));
    let report = transition(&sys, &m, 5, "approved", None).expect("transition");
    assert_eq!(report.from.as_deref(), Some("queued"));
    assert_eq!(report.to, "approved");
    assert!(!report.assignee_changed, "factory rule is unchanged");

    // Exactly one swap call with the right labels.
    let swaps: Vec<Call> = sys
        .calls()
        .into_iter()
        .filter(|c| matches!(c, Call::SwapLabels(..)))
        .collect();
    assert_eq!(swaps.len(), 1);
    assert_eq!(
        swaps[0],
        Call::SwapLabels(
            5,
            "unicorn:approved".to_string(),
            "unicorn:queued".to_string()
        )
    );
}

#[test]
fn ops_transition_records_single_swap_and_no_assignee() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:approved"]));
    transition(&sys, &m, 5, "active-development", None).expect("transition");
    let calls = sys.calls();
    // No set_assignee call (factory `unchanged`).
    assert!(
        !calls.iter().any(|c| matches!(c, Call::SetAssignee(..))),
        "no assignee mutation for unchanged rule"
    );
    // Exactly one swap.
    assert_eq!(
        calls
            .iter()
            .filter(|c| matches!(c, Call::SwapLabels(..)))
            .count(),
        1
    );
}

#[test]
fn ops_transition_creation_edge_adds_label() {
    let m = model();
    // No state label present → from = None; null → queued is allowed.
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn"]));
    let report = transition(&sys, &m, 5, "queued", None).expect("transition");
    assert_eq!(report.from, None);
    // Creation edge uses add_label (not swap).
    let calls = sys.calls();
    assert!(
        calls
            .iter()
            .any(|c| matches!(c, Call::AddLabel(5, l) if l == "unicorn:queued"))
    );
    assert!(!calls.iter().any(|c| matches!(c, Call::SwapLabels(..))));
}

#[test]
fn ops_transition_rejects_invalid_terminal() {
    let m = model();
    // done → active-development is not a valid edge (terminal source).
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:done"]));
    let err = transition(&sys, &m, 5, "active-development", None)
        .unwrap_err()
        .to_string();
    assert!(err.contains("invalid transition"), "got: {err}");
    // NO mutation recorded (only the read).
    assert!(
        !sys.calls().iter().any(|c| matches!(
            c,
            Call::SwapLabels(..) | Call::AddLabel(..) | Call::RemoveLabel(..)
        )),
        "must not mutate on invalid transition"
    );
}

#[test]
fn ops_transition_rejects_invalid_skip_gate() {
    let m = model();
    // queued → done has no edge.
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:queued"]));
    let err = transition(&sys, &m, 5, "done", None)
        .unwrap_err()
        .to_string();
    assert!(err.contains("invalid transition"), "got: {err}");
    assert!(
        !sys.calls()
            .iter()
            .any(|c| matches!(c, Call::SwapLabels(..)))
    );
}

#[test]
fn ops_transition_rejects_unknown_target() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:queued"]));
    let err = transition(&sys, &m, 5, "nonsense", None)
        .unwrap_err()
        .to_string();
    assert!(err.contains("unknown target state"), "got: {err}");
    // Rejected before even reading the issue.
    assert!(
        sys.calls().is_empty(),
        "must reject unknown target before any gh call"
    );
}

#[test]
fn ops_transition_rejects_zero_state() {
    let m = model();
    // No state label, but target is not reachable via the creation edge.
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn"]));
    let err = transition(&sys, &m, 5, "approved", None)
        .unwrap_err()
        .to_string();
    assert!(err.contains("invalid transition"), "got: {err}");
}

#[test]
fn ops_transition_rejects_multi_state() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(
        5,
        &["unicorn:queued", "unicorn:approved"],
    ));
    let err = transition(&sys, &m, 5, "active-development", None)
        .unwrap_err()
        .to_string();
    assert!(err.contains("multiple state labels"), "got: {err}");
    assert!(err.contains("repair"), "should hint repair, got: {err}");
}

/// #8003: asking for the state the issue already holds is a REPORTED no-op.
///
/// Why this shape: a self-edge is in no transition graph, so before the fix
/// this fell through to the edge check and came back as
/// `invalid transition queued → queued` — an error for a caller that already
/// has what it asked for. The report must carry `no_op` so the dispatcher can
/// say "already there" instead of "moved".
/// Test: this function IS the test.
#[test]
fn ops_transition_same_state_is_a_reported_no_op() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn", "unicorn:queued"]));
    let report = transition(&sys, &m, 5, "queued", None).expect("a same-state call must succeed");
    assert!(
        report.no_op,
        "the report must say nothing changed: {report:?}"
    );
    assert_eq!(report.from.as_deref(), Some("queued"));
    assert_eq!(report.to, "queued");
    assert!(!report.assignee_changed);
}

/// #8003: the no-op costs exactly one read and not one write.
///
/// Why separately from the report: "reports a no-op" and "wrote nothing" are
/// two claims, and the second is the one that matters — a same-state swap would
/// have added and removed the same label and posted an audit comment saying the
/// issue moved from a state to itself.
/// Test: this function IS the test.
#[test]
fn ops_transition_same_state_makes_no_gh_mutation() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:queued"]));
    transition(&sys, &m, 5, "queued", Some("a note is irrelevant here")).expect("transition");
    assert_eq!(
        sys.calls(),
        vec![Call::Validate(5)],
        "a no-op must read the issue and write nothing"
    );
}

#[test]
fn ops_transition_posts_audit_comment_with_note() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:queued"]));
    transition(&sys, &m, 5, "approved", Some("approved by reviewer")).expect("transition");
    let comment = sys.calls().into_iter().find_map(|c| match c {
        Call::Comment(_, b) => Some(b),
        _ => None,
    });
    let body = comment.expect("audit comment posted");
    assert!(body.contains("queued"));
    assert!(body.contains("approved"));
    assert!(body.contains("approved by reviewer"));
}

#[test]
fn ops_transition_applies_assignee_for_self_rule() {
    // Synthetic model whose target state uses `assignees: self`.
    let yaml = r#"
version: 1
label_config: { base: x, approved: x:a, blast_prefix: "b:", status_prefix: "x:" }
states:
  - { name: open, label: { name: "x:open", color: "AABBCC" } }
  - { name: working, label: { name: "x:working", color: "AABBCC" } }
transitions:
  - { from: null, to: open, trigger: issue_created }
  - { from: open, to: working, trigger: executor_start }
assignee_model:
  strategy: self
  per_state:
    working:
      assignees: self
"#;
    let m: StateModel = serde_yaml::from_str(yaml).expect("synthetic parses");
    super::super::validate::validate_model(&m).expect("valid");
    let sys = FakeSystem::new(issue_with_labels(9, &["x:open"]));
    let report = transition(&sys, &m, 9, "working", None).expect("transition");
    assert!(report.assignee_changed);
    assert!(
        sys.calls()
            .iter()
            .any(|c| matches!(c, Call::SetAssignee(9, AssigneeTarget::SelfUser, _)))
    );
}

// ---- current --------------------------------------------------------------

#[test]
fn ops_current_reports_state() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(
        5,
        &["unicorn", "unicorn:active-development"],
    ));
    assert_eq!(current(&sys, &m, 5).expect("current"), "active-development");
}

#[test]
fn ops_current_errors_on_none() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn"]));
    let err = current(&sys, &m, 5).unwrap_err().to_string();
    assert!(err.contains("no recognised state label"), "got: {err}");
}

#[test]
fn ops_current_errors_on_many() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:queued", "unicorn:done"]));
    let err = current(&sys, &m, 5).unwrap_err().to_string();
    assert!(err.contains("multiple state labels"), "got: {err}");
}

// ---- repair ---------------------------------------------------------------

#[test]
fn ops_repair_resolves_two_labels() {
    let m = model();
    // Mid-transition: both queued (order 1) and approved (order 2) present.
    // repair keeps the most-advanced (approved) and removes queued.
    let sys = FakeSystem::new(issue_with_labels(
        5,
        &["unicorn:queued", "unicorn:approved"],
    ));
    let kept = repair(&sys, &m, 5).expect("repair");
    assert_eq!(kept, "approved");
    let removed: Vec<String> = sys
        .calls()
        .into_iter()
        .filter_map(|c| match c {
            Call::RemoveLabel(_, l) => Some(l),
            _ => None,
        })
        .collect();
    assert_eq!(removed, vec!["unicorn:queued".to_string()]);
}

#[test]
fn ops_repair_noop_when_single() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn:approved"]));
    let kept = repair(&sys, &m, 5).expect("repair");
    assert_eq!(kept, "approved");
    // No remove calls.
    assert!(
        !sys.calls()
            .iter()
            .any(|c| matches!(c, Call::RemoveLabel(..)))
    );
}

#[test]
fn ops_repair_errors_on_zero() {
    let m = model();
    let sys = FakeSystem::new(issue_with_labels(5, &["unicorn"]));
    let err = repair(&sys, &m, 5).unwrap_err().to_string();
    assert!(err.contains("no state label"), "got: {err}");
}