cpm-planner 0.2.2

Critical Path Method planner as an MCP server: schedules task graphs (slack, critical path, resource levelling, Monte Carlo), leases lock-aware parallel cohorts to agents, versions plan-as-code variants, and tracks earned value.
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
//! Planner-level tests for the earned-value operations: `baseline`,
//! `earned_value` and `snapshot` (P5 Task 3). Every test drives a fake
//! clock so baseline starts, `as_of` defaults and snapshot instants are
//! deterministic.

#![allow(clippy::float_cmp)]

use std::sync::{Arc, Mutex};

use chrono::{DateTime, Duration, TimeZone, Utc};
use cpm_planner::BasicCpmPlanner;
use cpm_planner::audit::MemoryAuditSink;
use cpm_planner::earned_value::{
    BaselineRequest, Calendar, EvRowStatus, SnapshotFormat, SnapshotRequest,
};
use cpm_planner::plan::{
    AcquireRequest, CallerId, Deliverable, DeliverableStatus, MarkStatusRequest, PlanGraph, PlanId,
    PlannerError, ReviseRequest, SyncRequest,
};
use cpm_planner::ports::Planner;

fn t0() -> DateTime<Utc> {
    Utc.with_ymd_and_hms(2026, 1, 5, 0, 0, 0).unwrap()
}

struct Fixture {
    planner: BasicCpmPlanner,
    now: Arc<Mutex<DateTime<Utc>>>,
    audit: Arc<MemoryAuditSink>,
}

impl Fixture {
    fn new() -> Self {
        let now = Arc::new(Mutex::new(t0()));
        let clock = now.clone();
        let audit = Arc::new(MemoryAuditSink::new());
        let planner = BasicCpmPlanner::with_parts(
            audit.clone(),
            std::time::Duration::from_secs(3600),
            Arc::new(move || *clock.lock().unwrap()),
        );
        Self {
            planner,
            now,
            audit,
        }
    }

    fn advance(&self, hours: i64) {
        let mut now = self.now.lock().unwrap();
        *now += Duration::hours(hours);
    }

    async fn plan(&self) -> PlanId {
        self.planner.submit_plan(graph()).await.unwrap()
    }

    async fn baselined(&self) -> PlanId {
        let plan_id = self.plan().await;
        self.planner
            .baseline(BaselineRequest::new(plan_id.clone()))
            .await
            .unwrap();
        plan_id
    }

    /// Lease `a`, mark it in progress with 50% earned and `hours` spent.
    async fn report_a(&self, plan_id: &PlanId, hours: f32) {
        self.planner
            .acquire_cohort(AcquireRequest::new(plan_id.clone(), worker(), 1))
            .await
            .unwrap();
        self.planner
            .mark_status(
                MarkStatusRequest::new(
                    plan_id.clone(),
                    "a",
                    worker(),
                    DeliverableStatus::InProgress,
                )
                .with_earned_pct(50)
                .with_actual_effort_hours(hours),
            )
            .await
            .unwrap();
    }
}

fn worker() -> CallerId {
    CallerId("w1".into())
}

fn deliverable(id: &str, effort: f32, prereqs: &[&str]) -> Deliverable {
    Deliverable {
        id: id.to_string(),
        owned_files: vec![format!("src/{id}.rs").as_str().into()],
        prerequisites: prereqs.iter().map(|p| (*p).into()).collect(),
        estimated_effort_hours: Some(effort),
        duration_hours: None,
        estimate: None,
        metadata: serde_json::Value::Null,
        milestone: false,
        earning_rule: Some(cpm_planner::earned_value::EarningRule::Weighted),
    }
}

/// `a` (10h) then `b` (10h): BAC 20, finish at 20h.
fn graph() -> PlanGraph {
    PlanGraph {
        deliverables: vec![deliverable("a", 10.0, &[]), deliverable("b", 10.0, &["a"])],
        max_chained_dispatch: None,
    }
}

fn err_text<T: std::fmt::Debug>(r: Result<T, PlannerError>) -> String {
    r.expect_err("expected an error").to_string()
}

/// A named line `web` in project `p`: `main` (selected) and the draft `alt`.
async fn main_and_draft(f: &Fixture) -> (PlanId, PlanId) {
    let main = f
        .planner
        .sync_plan(SyncRequest::new("p", "web", "main", graph()))
        .await
        .unwrap()
        .plan_id;
    let mut alt_graph = graph();
    alt_graph.deliverables[1].estimated_effort_hours = Some(5.0);
    let alt = f
        .planner
        .sync_plan(SyncRequest::new("p", "web", "alt", alt_graph))
        .await
        .unwrap()
        .plan_id;
    (main, alt)
}

// ── plan.baseline ───────────────────────────────────────────────────────

#[tokio::test]
async fn first_baseline_is_number_one() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let out = f
        .planner
        .baseline(BaselineRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.baseline_number, 1);
}

#[tokio::test]
async fn baseline_start_defaults_to_the_clock() {
    let f = Fixture::new();
    f.advance(3);
    let plan_id = f.plan().await;
    let out = f
        .planner
        .baseline(BaselineRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.start, t0() + Duration::hours(3));
}

#[tokio::test]
async fn baseline_reports_budget_at_completion() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let out = f
        .planner
        .baseline(BaselineRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.bac, 20.0);
}

#[tokio::test]
async fn rebaseline_without_reason_is_rejected() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let e = err_text(f.planner.baseline(BaselineRequest::new(plan_id)).await);
    assert!(e.contains("reason"), "{e}");
}

#[tokio::test]
async fn rebaseline_with_blank_reason_is_rejected() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let r = f
        .planner
        .baseline(BaselineRequest::new(plan_id).with_reason("   "))
        .await;
    assert!(matches!(r, Err(PlannerError::InvalidGraph { .. })));
}

#[tokio::test]
async fn rebaseline_with_reason_takes_the_next_number() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let out = f
        .planner
        .baseline(BaselineRequest::new(plan_id).with_reason("scope change"))
        .await
        .unwrap();
    assert_eq!(out.baseline_number, 2);
}

#[tokio::test]
async fn baseline_reason_over_2048_chars_is_rejected() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let r = f
        .planner
        .baseline(BaselineRequest::new(plan_id).with_reason("x".repeat(2049)))
        .await;
    assert!(matches!(r, Err(PlannerError::InvalidGraph { .. })));
}

#[tokio::test]
async fn baseline_of_unknown_plan_is_plan_not_found() {
    let f = Fixture::new();
    let r = f
        .planner
        .baseline(BaselineRequest::new(PlanId("nope".into())))
        .await;
    assert!(matches!(r, Err(PlannerError::PlanNotFound { .. })));
}

#[tokio::test]
async fn baseline_on_draft_variant_is_variant_not_selected() {
    let f = Fixture::new();
    let (_, alt) = main_and_draft(&f).await;
    let r = f.planner.baseline(BaselineRequest::new(alt)).await;
    assert!(matches!(r, Err(PlannerError::VariantNotSelected { .. })));
}

#[tokio::test]
async fn baseline_is_audited_as_plan_ev_baselined() {
    let f = Fixture::new();
    f.baselined().await;
    assert!(
        f.audit
            .event_types()
            .contains(&"plan.ev.baselined".to_string())
    );
}

#[tokio::test]
async fn rebaseline_audit_carries_the_reason() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.planner
        .baseline(BaselineRequest::new(plan_id).with_reason("scope change"))
        .await
        .unwrap();
    let last = f.audit.snapshot().pop().unwrap();
    assert_eq!(last.payload["reason"], "scope change");
}

// ── plan.ev ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn baseline_then_ev_roundtrip_reports_planned_value() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.advance(5);
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.pv, 5.0);
}

#[tokio::test]
async fn ev_before_baseline_is_not_baselined() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let e = err_text(f.planner.ev(&plan_id, None).await);
    assert!(e.starts_with("NOT_BASELINED:"), "{e}");
}

#[tokio::test]
async fn ev_of_unknown_plan_is_plan_not_found() {
    let f = Fixture::new();
    let r = f.planner.ev(&PlanId("nope".into()), None).await;
    assert!(matches!(r, Err(PlannerError::PlanNotFound { .. })));
}

#[tokio::test]
async fn ev_honours_an_explicit_as_of() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let r = f
        .planner
        .ev(&plan_id, Some(t0() + Duration::hours(15)))
        .await
        .unwrap();
    assert_eq!(r.pv, 15.0);
}

#[tokio::test]
async fn ev_reports_earned_value_from_mark_status() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 4.0).await;
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.ev, 5.0);
}

#[tokio::test]
async fn rebaseline_keeps_actuals() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 4.0).await;
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("replan"))
        .await
        .unwrap();
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!((r.baseline_number, r.ac), (2, 4.0));
}

#[tokio::test]
async fn rebaseline_resets_the_planned_value_curve() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.advance(10);
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("replan"))
        .await
        .unwrap();
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.pv, 0.0);
}

#[tokio::test]
async fn ev_reads_a_deselected_variant() {
    let f = Fixture::new();
    let (main, alt) = main_and_draft(&f).await;
    f.planner
        .baseline(BaselineRequest::new(main.clone()))
        .await
        .unwrap();
    f.planner.select_variant(&alt, false).await.unwrap();
    let r = f.planner.ev(&main, None).await.unwrap();
    assert_eq!(r.bac, 20.0);
}

// ── plan.snapshot ───────────────────────────────────────────────────────

#[tokio::test]
async fn snapshot_before_baseline_is_not_baselined() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let e = err_text(f.planner.snapshot(SnapshotRequest::new(plan_id)).await);
    assert!(e.starts_with("NOT_BASELINED:"), "{e}");
}

#[tokio::test]
async fn snapshot_markdown_contains_header_row() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id).with_format(SnapshotFormat::Markdown))
        .await
        .unwrap();
    assert!(
        out.export
            .as_str()
            .is_some_and(|md| md.contains("| date | PV | EV | AC | SPI | CPI | EAC |"))
    );
}

#[tokio::test]
async fn snapshot_json_export_lists_every_snapshot() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.planner
        .snapshot(SnapshotRequest::new(plan_id.clone()))
        .await
        .unwrap();
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.export.as_array().map(Vec::len), Some(2));
}

#[tokio::test]
async fn snapshot_summary_is_taken_at_the_clock() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.advance(2);
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.summary.taken_at, t0() + Duration::hours(2));
}

#[tokio::test]
async fn snapshots_at_one_clock_instant_are_both_kept() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.planner
        .snapshot(SnapshotRequest::new(plan_id.clone()))
        .await
        .unwrap();
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id))
        .await
        .unwrap();
    assert_eq!(out.snapshot_count, 2);
}

/// Baseline at t0, report `a` 50% at 5h (EV 5), then snapshot as of 10h
/// and 12h: PV 10 and 12, so SPI 0.5 and about 0.42.
async fn two_low_snapshots(f: &Fixture) -> (PlanId, Vec<String>) {
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 1.0).await;
    let mut alerts = Vec::new();
    for hours in [10, 12] {
        let out = f
            .planner
            .snapshot(
                SnapshotRequest::new(plan_id.clone()).with_as_of(t0() + Duration::hours(hours)),
            )
            .await
            .unwrap();
        alerts = out.summary.alerts;
    }
    (plan_id, alerts)
}

#[tokio::test]
async fn two_low_snapshots_raise_spi_alert() {
    let f = Fixture::new();
    let (_, alerts) = two_low_snapshots(&f).await;
    assert!(alerts.contains(&"SPI_BELOW_0_9".to_string()), "{alerts:?}");
}

#[tokio::test]
async fn ev_after_two_low_snapshots_raises_spi_alert() {
    let f = Fixture::new();
    let (plan_id, _) = two_low_snapshots(&f).await;
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert!(r.alerts.contains(&"SPI_BELOW_0_9".to_string()));
}

#[tokio::test]
async fn first_low_snapshot_raises_no_alert() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id).with_as_of(t0() + Duration::hours(10)))
        .await
        .unwrap();
    assert!(out.summary.alerts.is_empty());
}

#[tokio::test]
async fn snapshot_on_draft_variant_is_variant_not_selected() {
    let f = Fixture::new();
    let (_, alt) = main_and_draft(&f).await;
    let r = f.planner.snapshot(SnapshotRequest::new(alt)).await;
    assert!(matches!(r, Err(PlannerError::VariantNotSelected { .. })));
}

#[tokio::test]
async fn snapshot_export_keeps_the_newest_100() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let mut last = None;
    for _ in 0..101 {
        last = Some(
            f.planner
                .snapshot(SnapshotRequest::new(plan_id.clone()))
                .await
                .unwrap(),
        );
    }
    let last = last.unwrap();
    assert_eq!(
        (last.export.as_array().map(Vec::len), last.snapshot_count),
        (Some(100), 101)
    );
}

// ── Fix round 1: ordering, per-baseline alerts, gating ───────────────────

#[tokio::test]
async fn rebaseline_keeps_earned_value() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 4.0).await;
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("replan"))
        .await
        .unwrap();
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.ev, 5.0);
}

#[tokio::test]
async fn newly_selected_variant_takes_baseline_one_without_reason() {
    let f = Fixture::new();
    let (main, alt) = main_and_draft(&f).await;
    f.planner
        .baseline(BaselineRequest::new(main))
        .await
        .unwrap();
    f.planner.select_variant(&alt, false).await.unwrap();
    let out = f.planner.baseline(BaselineRequest::new(alt)).await.unwrap();
    assert_eq!(out.baseline_number, 1);
}

#[tokio::test]
async fn snapshot_on_archived_variant_is_archive_refused() {
    let f = Fixture::new();
    let (main, _) = main_and_draft(&f).await;
    f.planner
        .archive("p", "web", None, true, false)
        .await
        .unwrap();
    let r = f.planner.snapshot(SnapshotRequest::new(main)).await;
    assert!(matches!(r, Err(PlannerError::ArchiveRefused { .. })));
}

#[tokio::test]
async fn ev_alerts_ignore_the_current_reading() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.planner
        .snapshot(SnapshotRequest::new(plan_id.clone()).with_as_of(t0() + Duration::hours(10)))
        .await
        .unwrap();
    let r = f
        .planner
        .ev(&plan_id, Some(t0() + Duration::hours(12)))
        .await
        .unwrap();
    assert!(r.alerts.is_empty(), "{:?}", r.alerts);
}

#[tokio::test]
async fn snapshot_alerts_ignore_snapshots_from_previous_baseline() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.planner
        .snapshot(SnapshotRequest::new(plan_id.clone()).with_as_of(t0() + Duration::hours(10)))
        .await
        .unwrap();
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("replan"))
        .await
        .unwrap();
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id).with_as_of(t0() + Duration::hours(12)))
        .await
        .unwrap();
    assert!(out.summary.alerts.is_empty(), "{:?}", out.summary.alerts);
}

#[tokio::test]
async fn backfilled_snapshot_sorts_by_as_of() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let mut last = None;
    for hours in [10, 5] {
        last = Some(
            f.planner
                .snapshot(
                    SnapshotRequest::new(plan_id.clone()).with_as_of(t0() + Duration::hours(hours)),
                )
                .await
                .unwrap(),
        );
    }
    let export = last.unwrap().export;
    let order: Vec<serde_json::Value> = export
        .as_array()
        .unwrap()
        .iter()
        .map(|s| s["as_of"].clone())
        .collect();
    let want: Vec<serde_json::Value> = [5, 10]
        .iter()
        .map(|h| serde_json::to_value(t0() + Duration::hours(*h)).unwrap())
        .collect();
    assert_eq!(order, want);
}

#[tokio::test]
async fn snapshot_summary_explains_undefined_cpi() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id))
        .await
        .unwrap();
    assert!(out.summary.undefined.iter().any(|u| u.field == "cpi"));
}

// ── Fix round 2: backfill-correct alerts, per-baseline window ────────────

async fn snapshot_at(f: &Fixture, plan_id: &PlanId, hours: i64) -> Vec<String> {
    f.planner
        .snapshot(SnapshotRequest::new(plan_id.clone()).with_as_of(t0() + Duration::hours(hours)))
        .await
        .unwrap()
        .summary
        .alerts
}

#[tokio::test]
async fn backfilled_snapshot_alerts_use_only_earlier_readings() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    snapshot_at(&f, &plan_id, 10).await;
    snapshot_at(&f, &plan_id, 12).await;
    let backfill = snapshot_at(&f, &plan_id, 0).await;
    assert!(backfill.is_empty(), "{backfill:?}");
}

#[tokio::test]
async fn alert_window_is_not_crowded_out_by_previous_baseline_history() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    for hours in 50..150 {
        snapshot_at(&f, &plan_id, hours).await;
    }
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("replan"))
        .await
        .unwrap();
    snapshot_at(&f, &plan_id, 10).await;
    let alerts = snapshot_at(&f, &plan_id, 12).await;
    assert!(alerts.contains(&"SPI_BELOW_0_9".to_string()), "{alerts:?}");
}

// ── Final fix wave: removed scope, backfill flag, calendar inheritance ───

/// Only `b`, with no prerequisites (removes `a`).
fn graph_without_a() -> PlanGraph {
    PlanGraph {
        deliverables: vec![deliverable("b", 10.0, &[])],
        max_chained_dispatch: None,
    }
}

/// Only `a` (removes `b`).
fn graph_without_b() -> PlanGraph {
    PlanGraph {
        deliverables: vec![deliverable("a", 10.0, &[])],
        max_chained_dispatch: None,
    }
}

impl Fixture {
    async fn revise(&self, plan_id: &PlanId, graph: PlanGraph) {
        self.planner
            .revise_plan(ReviseRequest::new(plan_id.clone(), graph).force(true))
            .await
            .unwrap();
    }

    async fn complete_a(&self, plan_id: &PlanId) {
        self.planner
            .acquire_cohort(AcquireRequest::new(plan_id.clone(), worker(), 1))
            .await
            .unwrap();
        self.planner
            .mark_status(MarkStatusRequest::new(
                plan_id.clone(),
                "a",
                worker(),
                DeliverableStatus::Complete,
            ))
            .await
            .unwrap();
    }
}

#[tokio::test]
async fn removed_complete_deliverable_keeps_its_earned_value() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.complete_a(&plan_id).await;
    f.revise(&plan_id, graph_without_a()).await;
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.ev, 10.0);
}

#[tokio::test]
async fn removed_deliverable_reports_removed_status() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.revise(&plan_id, graph_without_b()).await;
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    let b = r.rows.iter().find(|row| row.id == "b").unwrap();
    assert_eq!(b.status, EvRowStatus::Removed);
}

#[tokio::test]
async fn actual_cost_survives_rebaseline_for_removed_deliverable() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 4.0).await;
    f.revise(&plan_id, graph_without_a()).await;
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_reason("scope cut"))
        .await
        .unwrap();
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    assert_eq!(r.ac, 4.0);
}

#[tokio::test]
async fn readded_deliverable_restarts_earned_progress() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 4.0).await;
    f.revise(&plan_id, graph_without_a()).await;
    f.revise(&plan_id, graph()).await;
    let r = f.planner.ev(&plan_id, None).await.unwrap();
    let a = r.rows.iter().find(|row| row.id == "a").unwrap();
    assert_eq!(a.ev, 0.0);
}

#[tokio::test]
async fn snapshot_with_past_as_of_is_marked_backfilled() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.advance(5);
    let out = f
        .planner
        .snapshot(SnapshotRequest::new(plan_id).with_as_of(t0() + Duration::hours(2)))
        .await
        .unwrap();
    assert!(out.summary.backfilled);
}

#[tokio::test]
async fn alerts_ignore_backfilled_snapshots() {
    let f = Fixture::new();
    let plan_id = f.baselined().await;
    f.report_a(&plan_id, 1.0).await;
    f.advance(20);
    // SPI 0.5 as of 10h, but taken at 20h: backfilled.
    snapshot_at(&f, &plan_id, 10).await;
    // SPI 0.25 as of now: the only reading alerts may use.
    let alerts = snapshot_at(&f, &plan_id, 20).await;
    assert!(alerts.is_empty(), "{alerts:?}");
}

#[tokio::test]
async fn rebaseline_without_calendar_inherits_previous_calendar() {
    let f = Fixture::new();
    let plan_id = f.plan().await;
    let calendar = Calendar {
        hours_per_day: 6.0,
        ..Calendar::default()
    };
    f.planner
        .baseline(BaselineRequest::new(plan_id.clone()).with_calendar(calendar.clone()))
        .await
        .unwrap();
    let out = f
        .planner
        .baseline(BaselineRequest::new(plan_id).with_reason("replan"))
        .await
        .unwrap();
    assert_eq!(out.calendar, Some(calendar));
}