tix-tracker 0.12.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
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
//! WP02 integration target: the normalized roadmap filter and the chronological order.
//!
//! The pure rules live beside the code (`src/roadmap/filter.rs` and
//! `src/roadmap/order.rs`); this file is the *contract* half the mission's
//! `contracts/roadmap-projection.md` and `contracts/surface-parity.md` promise:
//!
//! * one DTO with stable serialized field names that every surface normalizes into,
//! * syntax refused before projection, while a well-formed value that names nothing
//!   yields an empty set rather than an error,
//! * inclusive period bounds, normalized owner/status matches, AND-of-labels, and a
//!   substring query,
//! * target-date-then-stable-id ordering with a deterministic tie-break and an
//!   explicit missing-date state,
//! * read-only behaviour: the registry is untouched by filtering and ordering.
//!
//! Requirement ids in the test names: FR-003 (chronological order), FR-004 (filter
//! by period/owner/status/labels), C-003 (calendar-only periods), C-007 (one
//! normalized DTO), NFR-003 (equivalent sets across surfaces).
//!
//! Deterministic and filesystem-free: every fixture is an in-memory [`Config`], and
//! nothing reads a clock, locale, or the environment.

use serde_json::json;
use tix::config::{Config, InitiativeConfiguration};
use tix::roadmap::{RoadmapEntry, RoadmapFilter, TargetDateState, filter_and_order, order};

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

/// The populated filter, used for serialization tests.
fn populated() -> RoadmapFilter {
    RoadmapFilter {
        period_from: Some("2026-01-01".into()),
        period_to: Some("2026-12-31".into()),
        owner: Some("alice".into()),
        status: Some("active".into()),
        labels: vec!["strategic".into(), "q4".into()],
        query: Some("platform".into()),
    }
}

/// A registry with four initiatives: three dated (two sharing a date) and one
/// undated, declared deliberately out of both calendar and id order.
///
/// Ordered by the contract the fixture produces:
/// `i-early` (2025-01-01), `i-alpha` and `i-zulu` (both 2026-03-31, id tie-break),
/// then `i-someday` (no target date).
fn seeded() -> Config {
    let mut config = Config::default();
    config.initiatives.insert(
        "i-zulu".into(),
        InitiativeConfiguration {
            name: "Zulu".into(),
            owner: Some("bob".into()),
            status: Some("paused".into()),
            labels: vec!["internal".into()],
            target_date: Some("2026-03-31".into()),
            ..InitiativeConfiguration::default()
        },
    );
    config.initiatives.insert(
        "i-someday".into(),
        InitiativeConfiguration {
            name: "Someday".into(),
            owner: Some("carol".into()),
            status: Some("proposed".into()),
            labels: vec!["exploratory".into()],
            // No target date: the explicit undated state.
            ..InitiativeConfiguration::default()
        },
    );
    config.initiatives.insert(
        "i-alpha".into(),
        InitiativeConfiguration {
            name: "Alpha".into(),
            outcome: Some("Ship the platform".into()),
            owner: Some("alice".into()),
            status: Some("active".into()),
            labels: vec!["strategic".into(), "Q4".into()],
            target_date: Some("2026-03-31".into()),
            ..InitiativeConfiguration::default()
        },
    );
    config.initiatives.insert(
        "i-early".into(),
        InitiativeConfiguration {
            name: "Early".into(),
            owner: Some("alice".into()),
            status: Some("active".into()),
            labels: vec!["strategic".into()],
            target_date: Some("2025-01-01".into()),
            ..InitiativeConfiguration::default()
        },
    );
    config
}

/// The ordered stable ids a filter selects from `config`.
fn ordered<'a>(config: &'a Config, filter: &RoadmapFilter) -> Vec<&'a str> {
    filter_and_order(config, filter)
        .unwrap()
        .iter()
        .map(|entry| entry.id)
        .collect()
}

// ---------------------------------------------------------------------------
// T007 — the normalized DTO
// ---------------------------------------------------------------------------

#[test]
fn the_default_filter_is_the_one_no_filter_value() {
    let empty = RoadmapFilter::default();
    assert!(empty.is_empty(), "the default must be the empty filter");
    assert!(!populated().is_empty());

    // A blank query narrows nothing, so it is not a criterion either.
    let blank_query = RoadmapFilter {
        query: Some("   ".into()),
        ..RoadmapFilter::default()
    };
    assert!(blank_query.is_empty());

    // The empty filter selects every initiative, undated ones included.
    assert_eq!(
        ordered(&seeded(), &empty),
        ["i-early", "i-alpha", "i-zulu", "i-someday"]
    );
}

#[test]
fn filter_fields_serialize_with_stable_names() {
    let value = serde_json::to_value(populated()).unwrap();
    let mut keys: Vec<&str> = value
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    keys.sort_unstable();
    assert_eq!(
        keys,
        [
            "labels",
            "owner",
            "period_from",
            "period_to",
            "query",
            "status"
        ]
    );

    // An empty filter serializes to an empty object: unset criteria write no key.
    assert_eq!(
        serde_json::to_value(RoadmapFilter::default()).unwrap(),
        json!({})
    );
}

#[test]
fn a_filter_round_trips_through_json_and_toml() {
    let filter = populated();
    let json = serde_json::to_string(&filter).unwrap();
    assert_eq!(
        serde_json::from_str::<RoadmapFilter>(&json).unwrap(),
        filter
    );

    let toml = toml::to_string(&filter).unwrap();
    assert_eq!(toml::from_str::<RoadmapFilter>(&toml).unwrap(), filter);
}

#[test]
fn an_unknown_serialized_field_is_refused() {
    let err = serde_json::from_value::<RoadmapFilter>(json!({ "ownerr": "alice" })).unwrap_err();
    assert!(
        err.to_string().contains("ownerr"),
        "the refusal must name the unknown key: {err}"
    );
}

#[test]
fn a_partial_surface_payload_fills_the_remaining_criteria_with_defaults() {
    let filter: RoadmapFilter = toml::from_str("status = \"active\"\n").unwrap();
    assert_eq!(filter.status.as_deref(), Some("active"));
    assert_eq!(
        filter,
        RoadmapFilter {
            status: Some("active".into()),
            ..RoadmapFilter::default()
        }
    );
}

// ---------------------------------------------------------------------------
// T008 — syntax validation before projection
// ---------------------------------------------------------------------------

#[test]
fn a_malformed_period_bound_is_refused() {
    for bad in ["2026-13-01", "2026-02-30", "20261201", "1969-12-31", "soon"] {
        let filter = RoadmapFilter {
            period_from: Some(bad.into()),
            ..RoadmapFilter::default()
        };
        let err = filter.validate().unwrap_err();
        assert!(
            err.to_string().contains("period_from"),
            "`{bad}` must be refused naming the field: {err}"
        );

        let filter = RoadmapFilter {
            period_to: Some(bad.into()),
            ..RoadmapFilter::default()
        };
        let err = filter.validate().unwrap_err();
        assert!(err.to_string().contains("period_to"), "`{bad}`: {err}");

        // The refusal happens before any row is produced.
        let err = filter_and_order(&seeded(), &filter).unwrap_err();
        assert!(err.to_string().contains("period_to"));
    }
}

#[test]
fn a_reversed_period_is_refused_but_an_equal_bound_is_inclusive() {
    let reversed = RoadmapFilter {
        period_from: Some("2026-06-01".into()),
        period_to: Some("2026-01-01".into()),
        ..RoadmapFilter::default()
    };
    let err = reversed.validate().unwrap_err();
    assert!(err.to_string().contains("period_from"), "{err}");

    // `from == to` is a one-day period, not a reversal.
    let one_day = RoadmapFilter {
        period_from: Some("2026-03-31".into()),
        period_to: Some("2026-03-31".into()),
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &one_day), ["i-alpha", "i-zulu"]);
}

#[test]
fn a_blank_owner_status_or_label_is_refused() {
    for blank in ["", "   ", "\t"] {
        let owner = RoadmapFilter {
            owner: Some(blank.into()),
            ..RoadmapFilter::default()
        };
        assert!(owner.validate().unwrap_err().to_string().contains("owner"));

        let status = RoadmapFilter {
            status: Some(blank.into()),
            ..RoadmapFilter::default()
        };
        assert!(
            status
                .validate()
                .unwrap_err()
                .to_string()
                .contains("status")
        );

        let labels = RoadmapFilter {
            labels: vec!["strategic".into(), blank.into()],
            ..RoadmapFilter::default()
        };
        assert!(
            labels
                .validate()
                .unwrap_err()
                .to_string()
                .contains("labels")
        );
    }
}

// ---------------------------------------------------------------------------
// T009 — owner / status / label predicates
// ---------------------------------------------------------------------------

#[test]
fn a_period_bound_is_inclusive_and_needs_a_target_date() {
    // A lower bound only: excludes the earlier dated row and the undated row.
    let from = RoadmapFilter {
        period_from: Some("2026-01-01".into()),
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &from), ["i-alpha", "i-zulu"]);

    // An upper bound only.
    let to = RoadmapFilter {
        period_to: Some("2025-12-31".into()),
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &to), ["i-early"]);

    // A bounded period never invents a date for the undated initiative.
    let bounded = RoadmapFilter {
        period_from: Some("2025-01-01".into()),
        period_to: Some("2026-12-31".into()),
        ..RoadmapFilter::default()
    };
    let config = seeded();
    let ids = ordered(&config, &bounded);
    assert_eq!(ids, ["i-early", "i-alpha", "i-zulu"]);
    assert!(!ids.contains(&"i-someday"));
}

#[test]
fn owner_and_status_are_exact_normalized_matches() {
    let owner = RoadmapFilter {
        owner: Some("  ALICE ".into()),
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &owner), ["i-early", "i-alpha"]);

    let status = RoadmapFilter {
        status: Some("Active".into()),
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &status), ["i-early", "i-alpha"]);

    // Normalization is trim + case-fold, not a prefix/substring match.
    let not_a_prefix = RoadmapFilter {
        owner: Some("ali".into()),
        ..RoadmapFilter::default()
    };
    assert!(ordered(&seeded(), &not_a_prefix).is_empty());
}

#[test]
fn labels_are_an_and_of_every_requested_value() {
    let one = RoadmapFilter {
        labels: vec!["strategic".into()],
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &one), ["i-early", "i-alpha"]);

    // Case- and order-insensitive: `Q4` on the record matches `q4` asked for.
    let both = RoadmapFilter {
        labels: vec!["Q4".into(), "STRATEGIC".into()],
        ..RoadmapFilter::default()
    };
    assert_eq!(ordered(&seeded(), &both), ["i-alpha"]);

    let contradictory = RoadmapFilter {
        labels: vec!["strategic".into(), "internal".into()],
        ..RoadmapFilter::default()
    };
    assert!(ordered(&seeded(), &contradictory).is_empty());
}

#[test]
fn an_unknown_owner_status_or_label_is_an_empty_set_not_an_error() {
    let config = seeded();
    for filter in [
        RoadmapFilter {
            owner: Some("nobody".into()),
            ..RoadmapFilter::default()
        },
        RoadmapFilter {
            status: Some("archived".into()),
            ..RoadmapFilter::default()
        },
        RoadmapFilter {
            labels: vec!["unused".into()],
            ..RoadmapFilter::default()
        },
    ] {
        let entries = filter_and_order(&config, &filter)
            .expect("a well-formed value naming nothing is not an error");
        assert!(entries.is_empty(), "a non-matching filter selects no row");
    }
}

#[test]
fn query_matches_each_searchable_field_case_insensitively() {
    for (query, expected) in [
        ("platform", vec!["i-alpha"]),         // outcome
        ("ALICE", vec!["i-early", "i-alpha"]), // owner
        ("Exploratory", vec!["i-someday"]),    // label
        ("proposed", vec!["i-someday"]),       // status
        ("zulu", vec!["i-zulu"]),              // name
        ("nothing-here", vec![]),              // no match is an empty set
    ] {
        let filter = RoadmapFilter {
            query: Some(query.into()),
            ..RoadmapFilter::default()
        };
        assert_eq!(ordered(&seeded(), &filter), expected, "query `{query}`");
    }
}

#[test]
fn query_does_not_match_across_field_boundaries() {
    let config = seeded();

    // "Alpha" is the name and "alice" the owner; no join may make "alpha alice" a
    // match, or the haystack would depend on field order.
    let spaced = RoadmapFilter {
        query: Some("alpha alice".into()),
        ..RoadmapFilter::default()
    };
    assert!(ordered(&config, &spaced).is_empty());

    // A newline is not a separator either: a former newline join would let this
    // query span the name's end and the owner's start.
    let newline = RoadmapFilter {
        query: Some("Alpha\nalice".into()),
        ..RoadmapFilter::default()
    };
    assert!(ordered(&config, &newline).is_empty());
}

#[test]
fn folding_covers_titlecase_glyphs_and_multi_character_expansions() {
    let mut config = Config::default();
    config.initiatives.insert(
        "i-title".into(),
        InitiativeConfiguration {
            name: "Title".into(),
            // U+01C6 (small dz with caron) folds from U+01C5 (titlecase), which the
            // Unicode *Uppercase* property excludes; U+0130 folds to two characters.
            labels: vec!["\u{01C6}x".into(), "\u{0130}stanbul".into()],
            ..InitiativeConfiguration::default()
        },
    );

    for filter in [
        // A titlecase criterion against the lowercase record label.
        RoadmapFilter {
            labels: vec!["\u{01C5}x".into()],
            ..RoadmapFilter::default()
        },
        // The same glyph through the query needle, whose haystack is folded too.
        RoadmapFilter {
            query: Some("\u{01C5}x".into()),
            ..RoadmapFilter::default()
        },
        // Multi-character expansion: U+0130 folds to `i` + U+0307 on both sides.
        RoadmapFilter {
            labels: vec!["i\u{0307}stanbul".into()],
            ..RoadmapFilter::default()
        },
    ] {
        assert_eq!(ordered(&config, &filter), ["i-title"], "{filter:?}");
    }
}

#[test]
fn every_filter_dimension_combines_as_an_and() {
    let only_alpha = RoadmapFilter {
        period_from: Some("2026-01-01".into()),
        period_to: Some("2026-12-31".into()),
        owner: Some("alice".into()),
        status: Some("active".into()),
        labels: vec!["strategic".into(), "q4".into()],
        query: Some("platform".into()),
    };
    let config = seeded();
    let entries = filter_and_order(&config, &only_alpha).unwrap();
    assert_eq!(
        entries.iter().map(|entry| entry.id).collect::<Vec<_>>(),
        ["i-alpha"]
    );

    // Changing one criterion to a non-matching value empties the set.
    let none = RoadmapFilter {
        query: Some("unrelated".into()),
        ..only_alpha
    };
    assert!(filter_and_order(&config, &none).unwrap().is_empty());
}

// ---------------------------------------------------------------------------
// T010 — target-date-then-stable-id ordering and the missing-date state
// ---------------------------------------------------------------------------

#[test]
fn order_is_target_date_ascending_then_stable_id() {
    let config = seeded();
    let mut entries: Vec<RoadmapEntry<'_>> = filter_and_order(&config, &RoadmapFilter::default())
        .unwrap()
        .into_iter()
        .collect();
    order(&mut entries);
    assert_eq!(
        entries.iter().map(|entry| entry.id).collect::<Vec<_>>(),
        ["i-early", "i-alpha", "i-zulu", "i-someday"]
    );
}

#[test]
fn equal_target_dates_break_the_tie_by_stable_id() {
    // Declared zulu first, alpha second: the tie-break ignores declaration order.
    let mut config = Config::default();
    for (id, name) in [
        ("i-zulu", "Zulu"),
        ("i-alpha", "Alpha"),
        ("i-bravo", "Bravo"),
    ] {
        config.initiatives.insert(
            id.into(),
            InitiativeConfiguration {
                name: name.into(),
                target_date: Some("2026-03-31".into()),
                ..InitiativeConfiguration::default()
            },
        );
    }
    assert_eq!(
        ordered(&config, &RoadmapFilter::default()),
        ["i-alpha", "i-bravo", "i-zulu"]
    );
}

#[test]
fn undated_initiatives_sort_after_every_dated_one_and_keep_id_order() {
    let mut config = Config::default();
    for (id, date) in [
        ("i-undated-b", None),
        ("i-dated", Some("2027-01-01")),
        ("i-undated-a", None),
    ] {
        config.initiatives.insert(
            id.into(),
            InitiativeConfiguration {
                name: id.into(),
                target_date: date.map(str::to_string),
                ..InitiativeConfiguration::default()
            },
        );
    }
    assert_eq!(
        ordered(&config, &RoadmapFilter::default()),
        ["i-dated", "i-undated-a", "i-undated-b"]
    );
}

#[test]
fn a_row_reports_the_explicit_missing_date_state() {
    let config = seeded();
    let entries = filter_and_order(&config, &RoadmapFilter::default()).unwrap();

    let someday = entries
        .iter()
        .find(|entry| entry.id == "i-someday")
        .unwrap();
    assert_eq!(someday.target_date, TargetDateState::Undated);
    assert!(someday.target_date.is_undated());
    assert_eq!(someday.target_date.date(), None);

    let early = entries.iter().find(|entry| entry.id == "i-early").unwrap();
    assert_eq!(early.target_date, TargetDateState::Dated("2025-01-01"));
    assert!(!early.target_date.is_undated());
    assert_eq!(early.target_date.date(), Some("2025-01-01"));

    // The row carries the stable id, not the display name.
    assert_eq!(early.initiative.name, "Early");
}

// ---------------------------------------------------------------------------
// T011 — parity, read-only behaviour, and determinism
// ---------------------------------------------------------------------------

#[test]
fn equivalent_surface_normalizations_replay_the_same_ordered_ids() {
    let config = seeded();

    // The four stand-ins for the four surfaces: a programmatic (CLI/TUI) filter,
    // the JSON an MCP tool call carries, the TOML a portable DTO round-trips, and a
    // sparsely-populated (MCP "only what was passed") form with the same meaning.
    let programmatic = RoadmapFilter {
        period_from: Some("2026-01-01".into()),
        owner: Some("alice".into()),
        status: Some("active".into()),
        ..RoadmapFilter::default()
    };
    let from_json: RoadmapFilter = serde_json::from_value(json!({
        "period_from": "2026-01-01",
        "owner": "alice",
        "status": "active",
    }))
    .unwrap();
    let from_toml: RoadmapFilter =
        toml::from_str("period_from = \"2026-01-01\"\nowner = \"alice\"\nstatus = \"active\"\n")
            .unwrap();
    let sparse = RoadmapFilter {
        period_from: Some("2026-01-01".into()),
        owner: Some("alice".into()),
        status: Some("active".into()),
        labels: Vec::new(),
        query: None,
        period_to: None,
    };

    let expected = ["i-alpha"];
    for filter in [&programmatic, &from_json, &from_toml, &sparse] {
        assert_eq!(ordered(&config, filter), expected);
    }
}

#[test]
fn filtering_and_ordering_leave_the_registry_untouched() {
    let config = seeded();
    let before = config.clone();
    let filter = populated();

    let entries = filter_and_order(&config, &filter).unwrap();
    assert!(!entries.is_empty());

    // No mutation: the registry compares equal to its snapshot, and every row still
    // borrows the original record.
    assert_eq!(config, before);
    for entry in &entries {
        assert!(std::ptr::eq(
            entry.initiative,
            config.initiatives.get(entry.id).unwrap()
        ));
    }
}

#[test]
fn the_order_is_total_and_independent_of_declaration_order() {
    let config = seeded();
    let canonical = ordered(&config, &RoadmapFilter::default());

    // The same records inserted in the reverse declaration order.
    let mut reversed = Config::default();
    for (id, record) in config.initiatives.iter().rev() {
        reversed.initiatives.insert(id.clone(), record.clone());
    }
    assert_eq!(ordered(&reversed, &RoadmapFilter::default()), canonical);
}