pinto-cli 0.3.2

A lightweight, local-first, Git-friendly Scrum backlog and Kanban board for the CLI and TUI
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
//! Unit tests for field edits, parent changes, and $EDITOR-based editing.

use super::super::*;
use crate::error::Error;
use crate::service::test_support::{create_sprint_for_test, init_temp, parent_edit};
use crate::sprint::SprintId;
use crate::storage::{BacklogItemRepository, FileRepository};
use tempfile::TempDir;

#[tokio::test]
async fn edit_updates_title_and_persists() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Old title", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        title: Some("New title".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit)
        .await
        .expect("edit succeeds");

    assert_eq!(edited.title, "New title");
    assert!(edited.updated >= added.created, "updated advanced");

    // It is made permanent.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "New title");
    assert_eq!(reloaded.updated, edited.updated);
}

#[tokio::test]
async fn edit_updates_each_optional_field() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-2").await;
    let added = add_item(dir.path(), "Task", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        points: Some(8),
        labels: Some(vec!["backend".to_string(), "urgent".to_string()]),
        assignee: Some("alice".to_string()),
        sprint: Some("S-2".to_string()),
        body: Some("Acceptance criteria".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit).await.unwrap();

    assert_eq!(edited.points, Some(8));
    assert_eq!(edited.labels, ["backend", "urgent"]);
    assert_eq!(edited.assignee.as_deref(), Some("alice"));
    assert_eq!(edited.sprint.as_deref(), Some("S-2"));
    assert_eq!(edited.body, "Acceptance criteria");
    // Title and status that are not specified will not change.
    assert_eq!(edited.title, "Task");
    assert_eq!(edited.status, added.status);
}

#[tokio::test]
async fn edit_rejects_invalid_or_missing_sprint_without_saving() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let added = add_item(
        dir.path(),
        "Task",
        NewItem {
            sprint: Some("S-1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();

    let invalid = edit_item(
        dir.path(),
        &added.id,
        ItemEdit {
            sprint: Some("S 2".to_string()),
            ..ItemEdit::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(invalid, Error::InvalidSprintId("S 2".to_string()));

    let missing = edit_item(
        dir.path(),
        &added.id,
        ItemEdit {
            sprint: Some("S-9".to_string()),
            ..ItemEdit::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(
        missing,
        Error::SprintNotFound(SprintId::new("S-9").unwrap())
    );

    let stored = show_item(dir.path(), &added.id).await.unwrap();
    assert_eq!(stored.sprint.as_deref(), Some("S-1"));
}

#[tokio::test]
async fn edit_leaves_unspecified_fields_unchanged() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let new = NewItem {
        points: Some(3),
        labels: vec!["keep".to_string()],
        sprint: Some("S-1".to_string()),
        body: "original body".to_string(),
        parent: None,
        depends_on: Vec::new(),
    };
    let added = add_item(dir.path(), "Keep me", new).await.unwrap();

    // Change only the title.
    let edit = ItemEdit {
        title: Some("Renamed".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit).await.unwrap();

    assert_eq!(edited.title, "Renamed");
    assert_eq!(edited.points, Some(3));
    assert_eq!(edited.labels, ["keep"]);
    assert_eq!(edited.sprint.as_deref(), Some("S-1"));
    assert_eq!(edited.body, "original body");
}

#[tokio::test]
async fn edit_with_no_fields_is_rejected() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Untouched", NewItem::default())
        .await
        .unwrap();

    let err = edit_item(dir.path(), &added.id, ItemEdit::default())
        .await
        .unwrap_err();

    assert_eq!(err, Error::NothingToUpdate);
}

#[tokio::test]
async fn edit_rejects_empty_title_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Original", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        title: Some("   ".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &added.id, edit).await.unwrap_err();

    assert_eq!(err, Error::EmptyTitle);
    // The title on the disc remains the same.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "Original");
}

#[tokio::test]
async fn edit_missing_id_returns_not_found() {
    let dir = init_temp().await;
    let edit = ItemEdit {
        title: Some("x".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &ItemId::new("T", 99), edit)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn edit_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");
    let edit = ItemEdit {
        title: Some("x".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &ItemId::new("T", 1), edit)
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn edit_sets_parent_and_persists() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();

    let updated = edit_item(dir.path(), &story.id, parent_edit(Some(epic.id.clone())))
        .await
        .expect("set parent succeeds");
    assert_eq!(updated.parent.as_ref(), Some(&epic.id));

    // It is made permanent.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(
        repo.load(&story.id).await.unwrap().parent.as_ref(),
        Some(&epic.id)
    );
}

#[tokio::test]
async fn edit_no_parent_clears_existing_parent() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();
    edit_item(dir.path(), &story.id, parent_edit(Some(epic.id)))
        .await
        .unwrap();

    let cleared = edit_item(dir.path(), &story.id, parent_edit(None))
        .await
        .expect("clear parent succeeds");
    assert_eq!(cleared.parent, None);
}

#[tokio::test]
async fn edit_parent_rejects_cycle_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    // a ← b (parent of b is a). If the parent of a is set to b, it becomes a cycle.
    edit_item(dir.path(), &b.id, parent_edit(Some(a.id.clone())))
        .await
        .unwrap();

    let err = edit_item(dir.path(), &a.id, parent_edit(Some(b.id)))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::ParentCycle { .. }), "got {err:?}");

    // The parent of a remains unset.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(repo.load(&a.id).await.unwrap().parent, None);
}

#[tokio::test]
async fn edit_parent_with_other_field_is_atomic_on_failure() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();

    // Parent is valid but title is empty → EmptyTitle. Parent changes are also not saved (atomicity).
    let edit = ItemEdit {
        title: Some("  ".to_string()),
        parent: Some(Some(epic.id.clone())),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &story.id, edit).await.unwrap_err();
    assert!(matches!(err, Error::EmptyTitle), "got {err:?}");

    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(
        repo.load(&story.id).await.unwrap().parent,
        None,
        "parent must not be persisted when the edit fails"
    );
}

#[tokio::test]
async fn edit_parent_to_missing_parent_returns_not_found() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let err = edit_item(dir.path(), &a.id, parent_edit(Some(ItemId::new("T", 99))))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn editor_template_has_frontmatter_and_guidance() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Template me", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id)
        .await
        .expect("template");

    assert!(
        tpl.starts_with("+++\n"),
        "starts with frontmatter delimiter"
    );
    assert!(tpl.contains("# pinto:"), "includes guidance comment");
    assert!(tpl.contains("title = \"Template me\""));
    // It can be parsed even with guidance comments, and editable fields match (no changes) in a round trip.
    let outcome = apply_item_edit(dir.path(), &added.id, &tpl).await.unwrap();
    assert_eq!(outcome, EditOutcome::Unchanged);
}

#[tokio::test]
async fn editor_apply_updates_title_and_body_and_persists() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Before", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Before\"", "title = \"After\"");
    let edited = format!("{edited}\nRewritten body");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => {
            assert_eq!(item.title, "After");
            assert!(item.body.contains("Rewritten body"), "body applied");
            assert!(item.updated >= added.updated, "updated advanced");
        }
        other => panic!("expected Updated, got {other:?}"),
    }

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "After");
    assert!(reloaded.body.contains("Rewritten body"));
}

#[tokio::test]
async fn editor_apply_without_changes_returns_unchanged_and_keeps_updated() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Same", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let outcome = apply_item_edit(dir.path(), &added.id, &tpl)
        .await
        .expect("apply");
    assert_eq!(outcome, EditOutcome::Unchanged);

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.updated, added.updated, "updated not bumped");
}

#[tokio::test]
async fn editor_apply_rejects_invalid_content_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Intact", NewItem::default())
        .await
        .unwrap();

    let err = apply_item_edit(dir.path(), &added.id, "not valid frontmatter\n")
        .await
        .unwrap_err();
    assert!(matches!(err, Error::EditorInvalid { .. }), "got {err:?}");

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded, added, "data untouched on invalid edit");
}

#[tokio::test]
async fn editor_apply_rejects_empty_title() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Has title", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Has title\"", "title = \"\"");
    let err = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::EditorInvalid { .. }), "got {err:?}");
}

#[tokio::test]
async fn editor_apply_ignores_managed_fields() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Managed", NewItem::default())
        .await
        .unwrap();

    // Even if you rewrite status / rank / id, it will not be reflected, only the editable title will be reflected.
    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl
        .replace("status = \"todo\"", "status = \"done\"")
        .replace(
            &format!("id = \"{}\"", added.id),
            &format!("id = \"{}-999\"", added.id.prefix()),
        )
        .replace("title = \"Managed\"", "title = \"Managed v2\"");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => {
            assert_eq!(item.id, added.id, "id preserved");
            assert_eq!(item.status, added.status, "status preserved");
            assert_eq!(item.rank, added.rank, "rank preserved");
            assert_eq!(item.title, "Managed v2", "title applied");
        }
        other => panic!("expected Updated, got {other:?}"),
    }
}

#[tokio::test]
async fn editor_apply_can_clear_optional_field() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let new = NewItem {
        sprint: Some("S-1".to_string()),
        ..NewItem::default()
    };
    let added = add_item(dir.path(), "Assigned", new).await.unwrap();

    // Delete sprint line = return to unset (editing not possible with field specification CLI).
    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    assert!(tpl.contains("sprint = \"S-1\""));
    let edited: String = tpl
        .lines()
        .filter(|l| !l.starts_with("sprint = "))
        .collect::<Vec<_>>()
        .join("\n");
    let edited = format!("{edited}\n");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => assert_eq!(item.sprint, None, "sprint cleared"),
        other => panic!("expected Updated, got {other:?}"),
    }
}

#[tokio::test]
async fn editor_apply_rejects_missing_sprint_without_saving() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let added = add_item(
        dir.path(),
        "Assigned",
        NewItem {
            sprint: Some("S-1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();

    let template = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = template.replace("sprint = \"S-1\"", "sprint = \"S-9\"");
    let error = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(
        error,
        Error::EditorInvalid { message } if message.contains("sprint not found")
    ));

    let stored = show_item(dir.path(), &added.id).await.unwrap();
    assert_eq!(stored.sprint.as_deref(), Some("S-1"));
}

#[tokio::test]
async fn editor_apply_rejects_missing_parent() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Child", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Child\"", "title = \"Child\"\nparent = \"T-404\"");
    let err = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}