shiplog 0.8.0

CLI evidence compiler for review-cycle packets with receipts, coverage, gaps, and safe share profiles.
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
//! Manual YAML ingestor for non-GitHub work.
//!
//! Reads `manual_events.yaml`, filters entries by date window, converts entries
//! into canonical events, and emits coverage metadata for the import.

use anyhow::Result;
use chrono::Utc;
use shiplog::ports::{IngestOutput, Ingestor};
use shiplog::schema::coverage::{Completeness, CoverageManifest, CoverageSlice, TimeWindow};
use shiplog::schema::freshness::{FreshnessStatus, SourceFreshness};
use std::path::Path;

pub mod events;

pub use events::{
    create_empty_file, create_entry, entry_date_range, entry_to_event, events_in_window,
    read_manual_events, write_manual_events,
};

/// Ingestor for manual events from YAML files.
///
/// This allows users to include non-GitHub work in their packets:
/// - Incidents handled
/// - Design docs written
/// - Mentoring
/// - Migrations planned
/// - etc.
///
/// # Examples
///
/// ```rust,no_run
/// use shiplog::ingest::manual::ManualIngestor;
/// use shiplog::ports::Ingestor;
/// use chrono::NaiveDate;
///
/// let ingestor = ManualIngestor::new(
///     "manual_events.yaml",
///     "octocat".to_string(),
///     NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
///     NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
/// );
/// let output = ingestor.ingest()?;
/// println!("Found {} manual events", output.events.len());
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct ManualIngestor {
    pub events_path: std::path::PathBuf,
    pub user: String,
    pub window: TimeWindow,
}

impl ManualIngestor {
    /// Create a new manual ingestor for the given YAML file and date window.
    ///
    /// # Examples
    ///
    /// ```
    /// use shiplog::ingest::manual::ManualIngestor;
    /// use chrono::NaiveDate;
    ///
    /// let ingestor = ManualIngestor::new(
    ///     "manual_events.yaml",
    ///     "octocat".to_string(),
    ///     NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
    ///     NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
    /// );
    /// ```
    pub fn new(
        events_path: impl AsRef<Path>,
        user: String,
        since: chrono::NaiveDate,
        until: chrono::NaiveDate,
    ) -> Self {
        Self {
            events_path: events_path.as_ref().to_path_buf(),
            user,
            window: TimeWindow { since, until },
        }
    }
}

impl Ingestor for ManualIngestor {
    fn ingest(&self) -> Result<IngestOutput> {
        if !self.events_path.exists() {
            // Return empty output if file doesn't exist
            let observed_at = Utc::now();
            return Ok(IngestOutput {
                events: Vec::new(),
                coverage: CoverageManifest {
                    run_id: shiplog::ids::RunId::now("manual"),
                    generated_at: observed_at,
                    user: self.user.clone(),
                    window: self.window.clone(),
                    mode: "manual".to_string(),
                    sources: vec!["manual".to_string()],
                    slices: vec![CoverageSlice {
                        window: self.window.clone(),
                        query: format!("file:{:?}", self.events_path),
                        total_count: 0,
                        fetched: 0,
                        incomplete_results: Some(false),
                        notes: vec!["manual_events_file_not_found".to_string()],
                    }],
                    warnings: vec![format!(
                        "Manual events file not found: {:?}",
                        self.events_path
                    )],
                    completeness: Completeness::Unknown,
                },
                freshness: vec![SourceFreshness {
                    source: "manual".to_string(),
                    status: FreshnessStatus::Unavailable,
                    cache_hits: 0,
                    cache_misses: 0,
                    fetched_at: Some(observed_at),
                    reason: Some(format!(
                        "manual events file not found at {:?}",
                        self.events_path
                    )),
                }],
            });
        }

        let file = read_manual_events(&self.events_path)?;
        let (events, warnings) = events_in_window(&file.events, &self.user, &self.window);

        let observed_at = Utc::now();
        let coverage = CoverageManifest {
            run_id: shiplog::ids::RunId::now("manual"),
            generated_at: observed_at,
            user: self.user.clone(),
            window: self.window.clone(),
            mode: "manual".to_string(),
            sources: vec!["manual".to_string()],
            slices: vec![CoverageSlice {
                window: self.window.clone(),
                query: format!("file:{:?}", self.events_path),
                total_count: file.events.len() as u64,
                fetched: events.len() as u64,
                incomplete_results: Some(false),
                notes: vec!["manual_events".to_string()],
            }],
            warnings,
            completeness: Completeness::Complete,
        };

        let freshness = vec![SourceFreshness {
            source: "manual".to_string(),
            status: FreshnessStatus::Fresh,
            cache_hits: 0,
            cache_misses: 0,
            fetched_at: Some(observed_at),
            reason: None,
        }];

        Ok(IngestOutput {
            events,
            coverage,
            freshness,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use shiplog::schema::event::{ManualDate, ManualEventEntry, ManualEventType, ManualEventsFile};

    fn make_test_entry(id: &str) -> ManualEventEntry {
        ManualEventEntry {
            id: id.to_string(),
            event_type: ManualEventType::Note,
            date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 3, 15).unwrap()),
            title: "Test Event".to_string(),
            description: Some("A test event".to_string()),
            workstream: Some("test-workstream".to_string()),
            tags: vec!["test".to_string()],
            receipts: vec![shiplog::schema::event::Link {
                label: "doc".to_string(),
                url: "https://example.com/doc".to_string(),
            }],
            impact: Some("Made things better".to_string()),
        }
    }

    #[test]
    fn reads_and_writes_manual_events() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");

        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![make_test_entry("test-1")],
        };

        write_manual_events(&path, &file).unwrap();
        let read = read_manual_events(&path).unwrap();

        assert_eq!(read.events.len(), 1);
        assert_eq!(read.events[0].id, "test-1");
    }

    #[test]
    fn ingest_filters_by_date() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");

        // Create file with events inside and outside window
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![
                ManualEventEntry {
                    id: "inside".to_string(),
                    event_type: ManualEventType::Note,
                    date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 3, 15).unwrap()),
                    title: "Inside".to_string(),
                    description: None,
                    workstream: None,
                    tags: vec![],
                    receipts: vec![],
                    impact: None,
                },
                ManualEventEntry {
                    id: "outside".to_string(),
                    event_type: ManualEventType::Note,
                    date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 6, 15).unwrap()),
                    title: "Outside".to_string(),
                    description: None,
                    workstream: None,
                    tags: vec![],
                    receipts: vec![],
                    impact: None,
                },
            ],
        };

        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );

        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 1);
        assert_eq!(
            output.events[0].source.opaque_id,
            Some("inside".to_string())
        );
        // Freshness receipt: the manual source reads a YAML file from
        // disk every run, so a successful ingest always reports Fresh
        // with an observed_at timestamp. There is no cache wired to
        // ManualIngestor, so hits/misses stay at zero.
        assert_eq!(
            output.freshness.len(),
            1,
            "manual ingest must emit exactly one freshness receipt per run"
        );
        let entry = &output.freshness[0];
        assert_eq!(entry.source, "manual");
        assert!(matches!(entry.status, FreshnessStatus::Fresh));
        assert_eq!(entry.cache_hits, 0);
        assert_eq!(entry.cache_misses, 0);
        assert!(entry.fetched_at.is_some());
        assert!(entry.reason.is_none());
    }

    #[test]
    fn ingest_missing_events_file_emits_unavailable_freshness() -> anyhow::Result<()> {
        let temp = tempfile::tempdir()?;
        let path = temp.path().join("does_not_exist.yaml");

        let since = NaiveDate::from_ymd_opt(2025, 1, 1)
            .ok_or_else(|| anyhow::anyhow!("since date construction"))?;
        let until = NaiveDate::from_ymd_opt(2025, 4, 1)
            .ok_or_else(|| anyhow::anyhow!("until date construction"))?;
        let ing = ManualIngestor::new(&path, "testuser".to_string(), since, until);

        let output = ing.ingest()?;
        assert_eq!(output.events.len(), 0);
        assert_eq!(output.freshness.len(), 1);
        let entry = &output.freshness[0];
        assert_eq!(entry.source, "manual");
        assert!(matches!(entry.status, FreshnessStatus::Unavailable));
        assert!(entry.reason.is_some());
        Ok(())
    }

    #[test]
    fn event_with_end_date_equal_to_window_since_is_included() {
        // end_date == window.since → NOT skipped (since `end_date < since` is false)
        // This kills the `<` → `<=` mutation at line 88.
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "boundary".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 3, 1).unwrap()),
                title: "Boundary Event".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        // Window since = 2025-03-01 (same as event date), until = 2025-04-01
        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        // Event's end_date (2025-03-01) == window.since (2025-03-01)
        // Condition: end_date < since → false, so it is NOT excluded.
        assert_eq!(output.events.len(), 1);
    }

    #[test]
    fn event_ending_before_window_is_excluded() {
        // end_date < window.since → entirely before window, excluded
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "before".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 2, 28).unwrap()),
                title: "Before Window".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 0);
    }

    #[test]
    fn event_starting_at_window_until_is_excluded() {
        // start_date >= window.until → entirely after window, excluded
        // Kills `>=` → `>` mutation at line 88
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "at-until".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 4, 1).unwrap()),
                title: "At Until Boundary".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 0);
    }

    #[test]
    fn event_spanning_before_window_start_triggers_warning() {
        // Range starts before window, ends inside → included but warns "partially outside"
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "span-before".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Range {
                    start: NaiveDate::from_ymd_opt(2025, 2, 15).unwrap(),
                    end: NaiveDate::from_ymd_opt(2025, 3, 15).unwrap(),
                },
                title: "Spans Before".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 1);
        assert!(
            output
                .coverage
                .warnings
                .iter()
                .any(|w| w.contains("partially outside"))
        );
    }

    #[test]
    fn event_spanning_after_window_end_triggers_warning() {
        // Range starts inside window, ends at or after until → included but warns "partially outside"
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "span-after".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Range {
                    start: NaiveDate::from_ymd_opt(2025, 3, 15).unwrap(),
                    end: NaiveDate::from_ymd_opt(2025, 4, 15).unwrap(),
                },
                title: "Spans After".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 1);
        assert!(
            output
                .coverage
                .warnings
                .iter()
                .any(|w| w.contains("partially outside"))
        );
    }

    #[test]
    fn event_entirely_outside_window_excluded() {
        // Both start and end well outside the window → excluded, 0 events
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "outside".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Range {
                    start: NaiveDate::from_ymd_opt(2025, 5, 1).unwrap(),
                    end: NaiveDate::from_ymd_opt(2025, 6, 1).unwrap(),
                },
                title: "Entirely Outside".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(output.events.len(), 0);
        assert!(output.coverage.warnings.is_empty());
    }

    #[test]
    fn handles_missing_file() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("nonexistent.yaml");

        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );

        let output = ing.ingest().unwrap();
        assert!(output.events.is_empty());
        assert!(!output.coverage.warnings.is_empty());
    }

    #[test]
    fn event_ending_one_day_before_window_since_is_excluded() {
        // end_date == window.since - 1 → end_date < since → excluded.
        // Combined with event_with_end_date_equal_to_window_since_is_included,
        // this kills the < → <= mutation on the boundary check.
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("manual_events.yaml");
        let file = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "day-before".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 2, 28).unwrap()),
                title: "Day Before Window".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file).unwrap();

        // Window since = 2025-03-01; event ends 2025-02-28 (one day before)
        let ing = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output = ing.ingest().unwrap();
        assert_eq!(
            output.events.len(),
            0,
            "event ending before window.since must be excluded"
        );

        // Now verify the boundary: end_date == window.since IS included
        let file2 = ManualEventsFile {
            version: 1,
            generated_at: Utc::now(),
            events: vec![ManualEventEntry {
                id: "at-since".to_string(),
                event_type: ManualEventType::Note,
                date: ManualDate::Single(NaiveDate::from_ymd_opt(2025, 3, 1).unwrap()),
                title: "At Window Since".to_string(),
                description: None,
                workstream: None,
                tags: vec![],
                receipts: vec![],
                impact: None,
            }],
        };
        write_manual_events(&path, &file2).unwrap();

        let ing2 = ManualIngestor::new(
            &path,
            "testuser".to_string(),
            NaiveDate::from_ymd_opt(2025, 3, 1).unwrap(),
            NaiveDate::from_ymd_opt(2025, 4, 1).unwrap(),
        );
        let output2 = ing2.ingest().unwrap();
        assert_eq!(
            output2.events.len(),
            1,
            "event on window.since boundary must be included"
        );
    }
}