onetaskgraph 0.2.35

One interface over the ticketing systems your work lives in.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
//! Comments on a task, driven the way a user drives them.
//!
//! Every journey here spawns the compiled binary and asserts on its exit code, stdout and
//! stderr. A row whose source keeps what it is given — a folder of Markdown, a GitHub board,
//! a Linear workspace — is driven through a whole add, list, edit and delete across separate
//! invocations, over the in-process boundary and the stdio plugin protocol alike. **The
//! in-memory rows are driven against comments seeded in their configuration instead, each
//! assertion read from the one invocation that made it, because an in-memory source's work
//! dies with the process that held it and no later command can read it back** — its
//! add-then-list within one process is proven as a library call, in
//! `crates/onetaskgraph-core/tests/comments.rs`.

use std::path::PathBuf;
use std::process::Output;

use serde_json::{Value, json};

use crate::common::{SOURCE_BOUNDARIES, Sandbox, stderr, stdout};
use crate::fixtures::{
    GITHUB_DRAFT_TASK, GitHubBoardFields, ROWS, Row, SOURCE, document, empty_folder,
    github_projects_with_board, github_projects_with_draft, qualified,
};
use onetaskgraph_plugin_api::Support;

/// The plugins whose source outlives one invocation, and the credentials each names when it
/// is hosted a process away — where §3.1 hands a plugin only what its configuration names.
const KEPT: &[(&str, &[&str])] = &[
    ("local-md", &[]),
    ("github-projects", &["GITHUB_PROJECTS_FIXTURE_TOKEN"]),
    ("linear", &["LINEAR_API_KEY"]),
];

/// The plugins whose work dies with the process that held it.
const TRANSIENT: &[&str] = &["in-memory", "subprocess"];

/// Every member a comment carries on the wire, and nothing else.
const COMMENT_MEMBERS: [&str; 6] = ["author", "body", "created_at", "id", "updated_at", "url"];

/// The task every journey comments on. Every row's source holds it.
const TASK: &str = "T-1";

fn run(sandbox: &Sandbox, arguments: &[&str], input: Option<&str>) -> Output {
    let mut command = sandbox.command();
    command.args(arguments);
    command.write_stdin(input.unwrap_or_default());
    command.assert().get_output().clone()
}

/// Standard output of a run that had to succeed, quoting stderr when it did not.
fn ok(who: &str, sandbox: &Sandbox, arguments: &[&str], input: Option<&str>) -> String {
    let output = run(sandbox, arguments, input);
    assert_eq!(
        output.status.code(),
        Some(0),
        "{who}: `onetaskgraph {}` exited {:?}\n{}",
        arguments.join(" "),
        output.status.code(),
        stderr(&output)
    );
    stdout(&output)
}

/// Standard error of a run that had to fail, which must say what to do next.
fn refused(who: &str, sandbox: &Sandbox, arguments: &[&str], input: Option<&str>) -> String {
    let output = run(sandbox, arguments, input);
    let said = stderr(&output);
    assert_eq!(
        output.status.code(),
        Some(1),
        "{who}: `onetaskgraph {}` was expected to exit 1\n{}{said}",
        arguments.join(" "),
        stdout(&output),
    );
    assert!(
        said.contains("next:"),
        "{who}: a refusal names a next action:\n{said}"
    );
    said
}

fn parsed(who: &str, rendered: &str) -> Value {
    serde_json::from_str(rendered)
        .unwrap_or_else(|error| panic!("{who}: not one JSON document ({error}):\n{rendered}"))
}

/// A comment in exactly the shape C1 states: six members, `id` and `body` always strings,
/// every other member a string or `null`.
fn a_comment(who: &str, comment: &Value) {
    let mut members: Vec<&str> = comment
        .as_object()
        .unwrap_or_else(|| panic!("{who}: a comment is an object: {comment}"))
        .keys()
        .map(String::as_str)
        .collect();
    members.sort_unstable();
    assert_eq!(members, COMMENT_MEMBERS, "{who}: {comment}");
    assert!(
        comment["id"].as_str().is_some_and(|id| !id.is_empty()),
        "{who}: {comment}"
    );
    assert!(comment["body"].is_string(), "{who}: {comment}");
    for member in ["author", "created_at", "updated_at", "url"] {
        assert!(
            comment[member].is_string() || comment[member].is_null(),
            "{who}: `{member}` is a string or null: {comment}"
        );
    }
}

/// A file under the sandbox holding exactly `text`, for `--body-file`.
fn body_file(sandbox: &Sandbox, name: &str, text: &[u8]) -> PathBuf {
    let path = sandbox.subdirectory("bodies").join(name);
    std::fs::write(&path, text).expect("the body file");
    path
}

fn row_of(plugin: &str) -> &'static Row {
    ROWS.iter()
        .find(|row| row.plugin == plugin)
        .unwrap_or_else(|| panic!("the shared table has a {plugin} row"))
}

/// Every row of the shared table whose source says its tasks have comments.
fn commented_rows() -> impl Iterator<Item = &'static Row> {
    ROWS.iter()
        .filter(|row| row.declared().comments == Support::Native)
}

/// A sandbox configuring `row`'s own source as `work`, on the boundary given.
fn kept(row: &Row, boundary: crate::common::SourceBoundary) -> Sandbox {
    let secrets = KEPT
        .iter()
        .find(|(plugin, _)| *plugin == row.plugin)
        .map_or(&[][..], |(_, secrets)| *secrets);
    let sandbox = Sandbox::new();
    let block = (row.fixture.block)(&sandbox);
    sandbox.project_document(&document(&json!({
        SOURCE: boundary.source_with_secrets(row.plugin, block, secrets)
    })));
    sandbox
}

fn list(who: &str, sandbox: &Sandbox, task: &str) -> Value {
    parsed(
        who,
        &ok(
            who,
            sandbox,
            &["task", "comment", "list", task, "--json"],
            None,
        ),
    )
}

#[test]
fn every_row_whose_tasks_have_comments_is_driven_by_one_of_the_comment_journeys() {
    // The journeys below split the table in two by whether a later command can read a
    // source back. A row landing in neither list would declare comments and never be driven.
    for row in commented_rows() {
        assert!(
            KEPT.iter().any(|(plugin, _)| *plugin == row.plugin) || TRANSIENT.contains(&row.plugin),
            "{}: declares comments, and no comment journey drives it",
            row.name
        );
    }
    for (plugin, _) in KEPT {
        assert_eq!(
            row_of(plugin).declared().comments,
            Support::Native,
            "{plugin}: every plugin C1 names has comments"
        );
    }
}

#[test]
fn a_comment_added_through_the_binary_is_listed_edited_and_deleted_on_every_source_that_keeps_it() {
    for (plugin, _) in KEPT {
        let row = row_of(plugin);
        for boundary in SOURCE_BOUNDARIES {
            let who = format!("{} across the {boundary:?} boundary", row.name);
            let sandbox = kept(row, boundary);
            let task = qualified(SOURCE, TASK);

            // From a file, with a heading of its own and a trailing newline — both of which a
            // body that is trimmed or re-rendered on the way through would lose.
            let first_body = "Seen again on main:\n\n## Evidence\n\n`cargo test -p x` failed\n";
            let first_file = body_file(&sandbox, "first.md", first_body.as_bytes());
            let first = parsed(
                &who,
                &ok(
                    &who,
                    &sandbox,
                    &[
                        "task",
                        "comment",
                        "add",
                        &task,
                        "--body-file",
                        first_file.to_str().expect("a UTF-8 path"),
                        "--json",
                    ],
                    None,
                ),
            );
            a_comment(&who, &first);
            assert_eq!(first["body"], first_body, "{who}: stored byte for byte");

            // From standard input, ending in a blank line.
            let second_body = "A second note, from standard input.\n\n";
            let second = parsed(
                &who,
                &ok(
                    &who,
                    &sandbox,
                    &["task", "comment", "add", &task, "--json"],
                    Some(second_body),
                ),
            );
            a_comment(&who, &second);
            assert_eq!(second["body"], second_body, "{who}: stored byte for byte");
            assert_ne!(first["id"], second["id"], "{who}");

            // Listed by a later invocation, oldest first, exactly as each add answered.
            assert_eq!(
                list(&who, &sandbox, &task),
                json!({"comments": [first.clone(), second.clone()]}),
                "{who}"
            );

            // An edit moves that comment's body and time and nothing else of it — and nothing
            // of the comment beside it.
            let edited_body = "Seen again on main, twice.\n";
            let edit_file = body_file(&sandbox, "edit.md", edited_body.as_bytes());
            let first_id = first["id"].as_str().expect("an id").to_owned();
            let edited = parsed(
                &who,
                &ok(
                    &who,
                    &sandbox,
                    &[
                        "task",
                        "comment",
                        "edit",
                        &task,
                        &first_id,
                        "--body-file",
                        edit_file.to_str().expect("a UTF-8 path"),
                        "--json",
                    ],
                    None,
                ),
            );
            a_comment(&who, &edited);
            assert_eq!(edited["body"], edited_body, "{who}");
            for member in ["id", "author", "created_at", "url"] {
                assert_eq!(
                    edited[member], first[member],
                    "{who}: `{member}` is unmoved"
                );
            }
            assert!(edited["updated_at"].is_string(), "{who}: {edited}");
            assert_eq!(
                list(&who, &sandbox, &task),
                json!({"comments": [edited.clone(), second.clone()]}),
                "{who}"
            );

            // A delete removes that comment and only that one.
            assert_eq!(
                parsed(
                    &who,
                    &ok(
                        &who,
                        &sandbox,
                        &["task", "comment", "delete", &task, &first_id, "--json"],
                        None,
                    ),
                ),
                json!({"deleted": first_id}),
                "{who}"
            );
            assert_eq!(
                list(&who, &sandbox, &task),
                json!({"comments": [second.clone()]}),
                "{who}"
            );

            // `task show` carries what is left, beside the task rather than inside it.
            let shown = parsed(
                &who,
                &ok(&who, &sandbox, &["task", "show", &task, "--json"], None),
            );
            assert_eq!(shown["comments"], json!([second.clone()]), "{who}");
            assert_eq!(shown["items"][0]["id"], json!(task), "{who}");
            let rendered = ok(&who, &sandbox, &["task", "show", &task], None);
            let after_body = rendered
                .split_once("comments: 1")
                .unwrap_or_else(|| panic!("{who}: the comments follow the task:\n{rendered}"))
                .1;
            assert!(
                after_body.contains("A second note, from standard input."),
                "{who}:\n{rendered}"
            );
            let listed = ok(&who, &sandbox, &["task", "comment", "list", &task], None);
            assert!(
                listed.contains(second["id"].as_str().expect("an id"))
                    && listed.contains("A second note, from standard input."),
                "{who}:\n{listed}"
            );

            let deleted = ok(
                &who,
                &sandbox,
                &[
                    "task",
                    "comment",
                    "delete",
                    &task,
                    second["id"].as_str().expect("an id"),
                ],
                None,
            );
            assert!(deleted.contains("deleted comment"), "{who}: {deleted}");
            assert_eq!(
                list(&who, &sandbox, &task),
                json!({"comments": []}),
                "{who}"
            );
            assert!(
                ok(&who, &sandbox, &["task", "comment", "list", &task], None)
                    .contains("no comments"),
                "{who}"
            );
        }
    }
}

#[test]
fn a_folder_of_markdown_records_the_author_it_is_given() {
    for boundary in SOURCE_BOUNDARIES {
        let row = row_of("local-md");
        let who = format!("{} across the {boundary:?} boundary", row.name);
        let sandbox = kept(row, boundary);
        let task = qualified(SOURCE, TASK);
        let added = parsed(
            &who,
            &ok(
                &who,
                &sandbox,
                &["task", "comment", "add", &task, "--author", "ada", "--json"],
                Some("hello\n"),
            ),
        );
        assert_eq!(added["author"], "ada", "{who}");
        assert_eq!(
            list(&who, &sandbox, &task)["comments"][0]["author"],
            "ada",
            "{who}"
        );
        let rendered = ok(
            &who,
            &sandbox,
            &["task", "comment", "add", &task],
            Some("x"),
        );
        assert!(rendered.contains("comment:"), "{who}: {rendered}");
    }
}

#[test]
fn a_source_that_records_its_own_author_refuses_one_given_and_writes_nothing() {
    for plugin in ["github-projects", "linear"] {
        let row = row_of(plugin);
        for boundary in SOURCE_BOUNDARIES {
            let who = format!("{} across the {boundary:?} boundary", row.name);
            let sandbox = kept(row, boundary);
            let task = qualified(SOURCE, TASK);
            let said = refused(
                &who,
                &sandbox,
                &["task", "comment", "add", &task, "--author", "ada"],
                Some("posted under a name GitHub or Linear would not record\n"),
            );
            assert!(said.contains("author"), "{who}: {said}");
            assert_eq!(
                list(&who, &sandbox, &task),
                json!({"comments": []}),
                "{who}: nothing was posted"
            );
        }
    }
}

/// Three comments on `T-1`, the first with an author and a body of its own headings.
fn seeded_comments() -> Value {
    json!([
        {"task": TASK, "comment": {"id": "C-1", "author": "ada",
            "created_at": "2026-09-13T15:11:07Z", "updated_at": "2026-09-13T15:11:07Z",
            "body": "Seeded evidence:\n\n## heading\n", "url": null}},
        {"task": TASK, "comment": {"id": "C-2", "author": null,
            "created_at": "2026-09-13T15:12:00Z", "updated_at": "2026-09-13T15:12:00Z",
            "body": "second", "url": null}},
        {"task": TASK, "comment": {"id": "C-3", "author": "grace",
            "created_at": "2026-09-13T15:13:00Z", "updated_at": "2026-09-13T15:13:00Z",
            "body": "third\n", "url": "https://example.invalid/C-3"}}
    ])
}

/// `row`'s own configuration with [`seeded_comments`] in it, wherever that row keeps its
/// in-memory source's settings.
fn seeded(row: &Row, sandbox: &Sandbox) -> Value {
    let mut block = (row.fixture.block)(sandbox);
    let settings = if row.plugin == "subprocess" {
        &mut block["settings"]
    } else {
        &mut block
    };
    settings["comments"] = seeded_comments();
    json!({"plugin": row.plugin, "config": block})
}

#[test]
fn an_in_memory_source_lists_edits_and_deletes_the_comments_it_holds_in_one_invocation_each() {
    let held: Vec<Value> = seeded_comments()
        .as_array()
        .expect("a list")
        .iter()
        .map(|seed| seed["comment"].clone())
        .collect();
    for row in ROWS.iter().filter(|row| TRANSIENT.contains(&row.plugin)) {
        let who = row.name;
        let sandbox = Sandbox::new();
        sandbox.project_document(&document(&json!({ SOURCE: seeded(row, &sandbox) })));
        let task = qualified(SOURCE, TASK);

        // Listed oldest first and byte for byte — across more than one page on the row that
        // serves two rows at a time.
        let listed = list(who, &sandbox, &task);
        assert_eq!(listed, json!({"comments": held.clone()}), "{who}");
        for comment in &held {
            a_comment(who, comment);
        }

        let added = parsed(
            who,
            &ok(
                who,
                &sandbox,
                &[
                    "task", "comment", "add", &task, "--author", "hopper", "--json",
                ],
                Some("Added from standard input.\n\n## and a heading\n"),
            ),
        );
        a_comment(who, &added);
        assert_eq!(
            added["body"], "Added from standard input.\n\n## and a heading\n",
            "{who}"
        );
        assert_eq!(added["author"], "hopper", "{who}");
        assert_eq!(added["id"], "C-4", "{who}");

        let edit = body_file(&sandbox, "edit.md", b"Seeded evidence, corrected.\n");
        let edited = parsed(
            who,
            &ok(
                who,
                &sandbox,
                &[
                    "task",
                    "comment",
                    "edit",
                    &task,
                    "C-1",
                    "--body-file",
                    edit.to_str().expect("a UTF-8 path"),
                    "--json",
                ],
                None,
            ),
        );
        assert_eq!(edited["body"], "Seeded evidence, corrected.\n", "{who}");
        for member in ["id", "author", "created_at", "url"] {
            assert_eq!(
                edited[member], held[0][member],
                "{who}: `{member}` is unmoved"
            );
        }
        assert_ne!(edited["updated_at"], held[0]["updated_at"], "{who}");

        assert_eq!(
            parsed(
                who,
                &ok(
                    who,
                    &sandbox,
                    &["task", "comment", "delete", &task, "C-2", "--json"],
                    None,
                ),
            ),
            json!({"deleted": "C-2"}),
            "{who}"
        );

        let shown = parsed(
            who,
            &ok(who, &sandbox, &["task", "show", &task, "--json"], None),
        );
        assert_eq!(shown["comments"], json!(held.clone()), "{who}");
    }
}

#[test]
fn task_show_says_nothing_about_comments_for_a_source_whose_tasks_have_none() {
    let row = row_of("in-memory");
    let sandbox = Sandbox::new();
    let mut block = (row.fixture.block)(&sandbox);
    block["capabilities"]
        .as_object_mut()
        .expect("a capability block")
        .remove("comments");
    sandbox.project_document(&document(&json!({
        SOURCE: {"plugin": row.plugin, "config": block}
    })));
    let task = qualified(SOURCE, TASK);

    let shown = parsed(
        row.name,
        &ok(row.name, &sandbox, &["task", "show", &task, "--json"], None),
    );
    assert!(
        shown.get("comments").is_none(),
        "absent rather than an empty list, which would read as a source holding none: {shown}"
    );
    let rendered = ok(row.name, &sandbox, &["task", "show", &task], None);
    assert!(!rendered.contains("comments"), "{rendered}");

    // And a source that has comments but none on this task says so, both ways.
    let local = row_of("local-md");
    let sandbox = kept(local, crate::common::SourceBoundary::Direct);
    let shown = parsed(
        local.name,
        &ok(
            local.name,
            &sandbox,
            &["task", "show", &task, "--json"],
            None,
        ),
    );
    assert_eq!(shown["comments"], json!([]), "{shown}");
    assert!(ok(local.name, &sandbox, &["task", "show", &task], None).contains("comments: none"));
}

#[test]
fn a_task_or_a_comment_that_is_not_there_is_refused_by_name_on_every_row() {
    for row in commented_rows() {
        let who = row.name;
        let sandbox = Sandbox::new();
        let block = (row.fixture.block)(&sandbox);
        let secrets = KEPT
            .iter()
            .find(|(plugin, _)| *plugin == row.plugin)
            .map_or(&[][..], |(_, secrets)| *secrets);
        sandbox.project_document(&document(&json!({
            SOURCE: crate::common::SourceBoundary::Direct
                .source_with_secrets(row.plugin, block, secrets)
        })));
        let missing = qualified(SOURCE, "T-404");
        for arguments in [
            vec!["task", "comment", "list", missing.as_str()],
            vec!["task", "comment", "add", missing.as_str()],
            vec!["task", "comment", "delete", missing.as_str(), "C-1"],
        ] {
            let said = refused(who, &sandbox, &arguments, Some("a body\n"));
            assert!(
                said.contains(&format!("no task with the id {missing}")),
                "{who}: {said}"
            );
        }

        let task = qualified(SOURCE, TASK);
        for arguments in [
            vec!["task", "comment", "edit", task.as_str(), "NO-SUCH-COMMENT"],
            vec![
                "task",
                "comment",
                "delete",
                task.as_str(),
                "NO-SUCH-COMMENT",
            ],
        ] {
            let said = refused(who, &sandbox, &arguments, Some("a body\n"));
            assert!(
                said.contains(&format!(
                    "task {task} has no comment with the id NO-SUCH-COMMENT"
                )),
                "{who}: {said}"
            );
        }
    }
}

#[test]
fn an_empty_or_unreadable_body_is_refused_before_anything_is_written() {
    let row = row_of("local-md");
    let who = row.name;
    let sandbox = kept(row, crate::common::SourceBoundary::Direct);
    let task = qualified(SOURCE, TASK);

    let said = refused(who, &sandbox, &["task", "comment", "add", &task], Some(""));
    assert!(said.contains("is empty"), "{said}");
    let empty = body_file(&sandbox, "empty.md", b"");
    let said = refused(
        who,
        &sandbox,
        &[
            "task",
            "comment",
            "add",
            &task,
            "--body-file",
            empty.to_str().expect("a UTF-8 path"),
        ],
        None,
    );
    assert!(said.contains("is empty"), "{said}");
    let binary = body_file(&sandbox, "binary.md", &[0xff, 0xfe, 0x00]);
    let said = refused(
        who,
        &sandbox,
        &[
            "task",
            "comment",
            "add",
            &task,
            "--body-file",
            binary.to_str().expect("a UTF-8 path"),
        ],
        None,
    );
    assert!(said.contains("not UTF-8"), "{said}");
    let said = refused(
        who,
        &sandbox,
        &[
            "task",
            "comment",
            "add",
            &task,
            "--body-file",
            "no/such/file.md",
        ],
        None,
    );
    assert!(said.contains("could not read it"), "{said}");
    assert_eq!(list(who, &sandbox, &task), json!({"comments": []}));

    let added = parsed(
        who,
        &ok(
            who,
            &sandbox,
            &["task", "comment", "add", &task, "--json"],
            Some("kept\n"),
        ),
    );
    let said = refused(
        who,
        &sandbox,
        &[
            "task",
            "comment",
            "edit",
            &task,
            added["id"].as_str().expect("an id"),
        ],
        Some(""),
    );
    assert!(said.contains("is empty"), "{said}");
    assert_eq!(list(who, &sandbox, &task)["comments"][0]["body"], "kept\n");
}

#[test]
fn a_stdio_plugin_whose_handshake_says_nothing_of_comments_is_refused_before_anything_is_read() {
    // The document store beside this module was written before there were comments, so its
    // handshake does not mention them — which the protocol reads as a source without any.
    // Its log records every method it is asked for, so "refused before anything is read"
    // is the log holding the handshake and nothing after it.
    let sandbox = Sandbox::new();
    let store = sandbox.subdirectory("store");
    let log = store.join("asked.log");
    sandbox.project_document(&document(&json!({
        "store": {"plugin": "subprocess", "config": {
            "command": crate::document_store::interpreter().to_string_lossy(),
            "args": [crate::document_store::peer().to_string_lossy()],
            "settings": {"store": store.join("store.json"), "log": log},
        }},
    })));
    let task = qualified("store", "D-1");
    for arguments in [
        vec!["task", "comment", "list", task.as_str()],
        vec!["task", "comment", "add", task.as_str()],
        vec!["task", "comment", "edit", task.as_str(), "C-1"],
        vec!["task", "comment", "delete", task.as_str(), "C-1"],
    ] {
        let said = refused("document-store", &sandbox, &arguments, Some("a body\n"));
        assert!(
            said.contains("source store has no comments: its plugin is document-store"),
            "{} names the source and its plugin:\n{said}",
            arguments.join(" ")
        );
        let asked = std::fs::read_to_string(&log).unwrap_or_default();
        assert!(
            asked.lines().all(|method| method == "initialize"),
            "{}: nothing but the handshake reached the plugin:\n{asked}",
            arguments.join(" ")
        );
    }
}

#[test]
fn comments_a_source_can_read_but_not_write_are_listed_and_every_write_is_refused() {
    let row = row_of("in-memory");
    let sandbox = Sandbox::new();
    let mut source = seeded(row, &sandbox);
    source["config"]["capabilities"]["writes"] = json!("unsupported");
    sandbox.project_document(&document(&json!({ SOURCE: source })));
    let task = qualified(SOURCE, TASK);

    assert_eq!(
        list(row.name, &sandbox, &task)["comments"]
            .as_array()
            .map(Vec::len),
        Some(3)
    );
    for arguments in [
        vec!["task", "comment", "add", task.as_str()],
        vec!["task", "comment", "edit", task.as_str(), "C-1"],
        vec!["task", "comment", "delete", task.as_str(), "C-1"],
    ] {
        let said = refused(row.name, &sandbox, &arguments, Some("a body\n"));
        assert!(
            said.contains("source work cannot be written: its plugin is in-memory"),
            "{said}"
        );
    }
}

#[test]
fn a_github_draft_issue_is_refused_for_every_comment_verb_and_by_task_show() {
    for boundary in SOURCE_BOUNDARIES {
        let who = format!("github-projects draft across the {boundary:?} boundary");
        let sandbox = Sandbox::new();
        let config = github_projects_with_draft(&sandbox);
        sandbox.project_document(&document(&json!({
            SOURCE: boundary.source_with_secrets(
                "github-projects",
                config,
                &["GITHUB_PROJECTS_FIXTURE_TOKEN"],
            )
        })));
        let draft = qualified(SOURCE, GITHUB_DRAFT_TASK);

        for arguments in [
            vec!["task", "comment", "list", draft.as_str()],
            vec!["task", "comment", "add", draft.as_str()],
            vec!["task", "comment", "edit", draft.as_str(), "C-1"],
            vec!["task", "comment", "delete", draft.as_str(), "C-1"],
        ] {
            let said = refused(&who, &sandbox, &arguments, Some("a body\n"));
            assert!(
                said.contains("draft"),
                "{who}: `{}` says the item is a draft:\n{said}",
                arguments.join(" ")
            );
        }

        // Showing the draft answers the task and refuses its comments by name: the command
        // fails non-zero with the problem and a next action rather than leaving the list out,
        // which would read exactly like a source without comments.
        for json_flag in [false, true] {
            let mut arguments = vec!["task", "show", draft.as_str()];
            if json_flag {
                arguments.push("--json");
            }
            let output = run(&sandbox, &arguments, None);
            let said = stderr(&output);
            assert_eq!(
                output.status.code(),
                Some(4),
                "{who}: `{}` is a partial answer:\n{said}",
                arguments.join(" ")
            );
            assert!(said.contains("draft"), "{who}: names the draft:\n{said}");
            assert!(
                said.contains("next:"),
                "{who}: names a next action:\n{said}"
            );
            if json_flag {
                let shown = parsed(&who, &stdout(&output));
                assert_eq!(shown["items"][0]["id"], json!(draft), "{who}");
                assert!(shown.get("comments").is_none(), "{who}: {shown}");
                assert!(
                    shown["errors"][0]["error"]["message"]
                        .as_str()
                        .is_some_and(|message| message.contains("draft")),
                    "{who}: {shown}"
                );
            }
        }
    }
}

/// The members of GitHub's schema a document names when it reaches a comment: an issue's
/// `comments` connection, the `IssueComment` type its read and its two mutations name, and the
/// `addComment` mutation, which names neither.
const GITHUB_COMMENT_SCHEMA: [&str; 3] = ["comments(", "IssueComment", "addComment("];

/// The documents a GitHub board is sent when a comment is read or written.
///
/// Taken out of the plugin's own inventory of every document it sends — which the plugin's
/// `documents_are_all_inventoried` holds complete — by what each one names, rather than listed
/// here: a comment document the plugin gains later is selected without an edit to this file,
/// so a copy that sends it cannot slip past the assertion. The plugin's five named comment
/// documents are then held to be among them, so a narrowing of the selection fails here
/// rather than quietly proving less.
fn github_comment_documents() -> Vec<&'static str> {
    use onetaskgraph_github_projects::graphql;

    let selected: Vec<&'static str> = graphql::DOCUMENTS
        .iter()
        .map(|(document, _)| *document)
        .filter(|document| {
            GITHUB_COMMENT_SCHEMA
                .iter()
                .any(|name| document.contains(name))
        })
        .collect();
    for named in [
        graphql::ISSUE_COMMENTS,
        graphql::COMMENT_ISSUE,
        graphql::ADD_COMMENT,
        graphql::UPDATE_COMMENT,
        graphql::DELETE_COMMENT,
    ] {
        assert!(
            selected.contains(&named),
            "a comment document the plugin sends is not selected out of its inventory:\n{named}"
        );
    }
    selected
}

/// The comment documents `board` received after its first `since` documents.
fn comment_calls(board: &GitHubBoardFields, since: usize) -> Vec<String> {
    let comment_documents = github_comment_documents();
    board.documents()[since..]
        .iter()
        .filter(|document| comment_documents.contains(&document.as_str()))
        .cloned()
        .collect()
}

/// Two GitHub boards over the shared dataset — `work` to copy out of and `board` to copy into —
/// and a handle on what each board is sent.
///
/// A board refuses to create an item carrying labels, and every item of the shared dataset
/// carries one, so the task and the project copied between the two are written onto `work`
/// first out of a folder of Markdown that labels neither: `notes:N-1`, filed under
/// `notes:NP-1`, and `notes:N-2`, filed under nothing — a task copied on its own has to be
/// one, because a destination refuses a task filed under a project it does not hold. Then
/// `work`'s copies of both tasks and `board`'s own `T-1` each get a comment, so both ends of
/// every copy below hold tasks with comments.
struct Boards {
    sandbox: Sandbox,
    from: GitHubBoardFields,
    into: GitHubBoardFields,
    /// `work`'s own id for the task filed under [`Self::project`].
    task: String,
    /// `work`'s own id for the task filed under nothing.
    orphan: String,
    /// `work`'s own id for the project it is filed under.
    project: String,
}

fn two_commented_boards(who: &str) -> Boards {
    let sandbox = Sandbox::new();
    let (from_config, from) = github_projects_with_board(&sandbox);
    let (into_config, into) = github_projects_with_board(&sandbox);
    let notes = empty_folder(&sandbox, "notes");
    let folder = sandbox.subdirectory("notes");
    std::fs::create_dir_all(folder.join("tasks")).expect("the folder's tasks");
    std::fs::create_dir_all(folder.join("projects")).expect("the folder's projects");
    std::fs::write(
        folder.join("projects/NP-1.md"),
        "---\ntitle: Unlabelled project\nstatus: todo\n---\na project with no labels\n",
    )
    .expect("the folder's project");
    std::fs::write(
        folder.join("tasks/N-1.md"),
        "---\ntitle: Unlabelled task\nstatus: todo\nproject: NP-1\n---\na task with no labels\n",
    )
    .expect("the folder's task");
    std::fs::write(
        folder.join("tasks/N-2.md"),
        "---\ntitle: Unfiled task\nstatus: todo\n---\na task in no project\n",
    )
    .expect("the folder's unfiled task");
    sandbox.project_document(&document(&json!({
        SOURCE: {"plugin": "github-projects", "config": from_config},
        "board": {"plugin": "github-projects", "config": into_config},
        "notes": {"plugin": "local-md", "config": notes},
    })));

    let written = parsed(
        who,
        &ok(
            who,
            &sandbox,
            &["project", "copy", "notes:NP-1", "--to", SOURCE, "--json"],
            None,
        ),
    );
    let landed = |source: &str| {
        written["items"]
            .as_array()
            .expect("a copy reports its items")
            .iter()
            .find(|item| item["source"] == source)
            .and_then(|item| item["destination"].as_str())
            .unwrap_or_else(|| panic!("{who}: {source} landed on `work`: {written}"))
            .to_owned()
    };
    let (project, task) = (landed("notes:NP-1"), landed("notes:N-1"));
    let unfiled = parsed(
        who,
        &ok(
            who,
            &sandbox,
            &["task", "copy", "notes:N-2", "--to", SOURCE, "--json"],
            None,
        ),
    );
    let orphan = unfiled["items"][0]["destination"]
        .as_str()
        .unwrap_or_else(|| panic!("{who}: notes:N-2 landed on `work`: {unfiled}"))
        .to_owned();

    for commented in [&task, &orphan] {
        ok(
            who,
            &sandbox,
            &["task", "comment", "add", commented],
            Some("evidence on the source\n"),
        );
    }
    ok(
        who,
        &sandbox,
        &["task", "comment", "add", &qualified("board", TASK)],
        Some("evidence on the destination\n"),
    );
    Boards {
        sandbox,
        from,
        into,
        task,
        orphan,
        project,
    }
}

/// Assert a copy made no comment call at either board, left the comments of `source_task` and
/// of `board:T-1` as they were, and carried no comment of `source_task` onto what it wrote.
fn a_copy_touched_no_comment(who: &str, boards: &Boards, source_task: &str, arguments: &[&str]) {
    let sandbox = &boards.sandbox;
    let before = (
        list(who, sandbox, source_task),
        list(who, sandbox, &qualified("board", TASK)),
    );
    let (from_seen, into_seen) = (boards.from.documents().len(), boards.into.documents().len());

    let report = parsed(who, &ok(who, sandbox, arguments, None));

    assert_eq!(
        comment_calls(&boards.from, from_seen),
        Vec::<String>::new(),
        "{who}: the copy read or wrote a comment at its source"
    );
    assert_eq!(
        comment_calls(&boards.into, into_seen),
        Vec::<String>::new(),
        "{who}: the copy read or wrote a comment at its destination"
    );
    assert_eq!(
        (
            list(who, sandbox, source_task),
            list(who, sandbox, &qualified("board", TASK)),
        ),
        before,
        "{who}: both ends' comments are as they were"
    );
    let items = report["items"]
        .as_array()
        .expect("a copy reports its items");
    assert!(!items.is_empty(), "{who}: {report}");
    for item in items {
        // Only a task written can carry a comment; a project or a document has none to carry.
        if item["source"] != json!(source_task) {
            continue;
        }
        let destination = item["destination"].as_str().expect("a copy that wrote");
        assert_eq!(
            list(who, sandbox, destination),
            json!({"comments": []}),
            "{who}: {destination} carries no comment of the task it was copied from"
        );
    }
}

#[test]
fn a_task_copy_neither_reads_nor_writes_a_comment_at_either_end() {
    let who = "task copy between two GitHub boards";
    let boards = two_commented_boards(who);
    let task = boards.orphan.clone();
    a_copy_touched_no_comment(
        who,
        &boards,
        &task,
        &["task", "copy", &task, "--to", "board", "--json"],
    );
}

#[test]
fn a_project_copy_neither_reads_nor_writes_a_comment_at_either_end() {
    let who = "project copy between two GitHub boards";
    let boards = two_commented_boards(who);
    let (project, task) = (boards.project.clone(), boards.task.clone());
    a_copy_touched_no_comment(
        who,
        &boards,
        &task,
        &["project", "copy", &project, "--to", "board", "--json"],
    );
}

#[test]
fn a_document_copy_neither_reads_nor_writes_a_comment_at_either_end() {
    // `D-3` is the one document of the shared board that carries no labels.
    let who = "document copy between two GitHub boards";
    let boards = two_commented_boards(who);
    let task = boards.task.clone();
    a_copy_touched_no_comment(
        who,
        &boards,
        &task,
        &[
            "document",
            "copy",
            &qualified(SOURCE, "D-3"),
            "--to",
            "board",
            "--json",
        ],
    );
}

#[test]
fn a_copy_into_a_markdown_task_keeps_its_comments_section_byte_for_byte() {
    // The folder already holds the copied task, recorded as coming from `work:T-1`, with a
    // comment section a person left on it. The copy updates that file from a board whose own
    // `T-1` has a comment too — and neither the folder's comment nor the board's may move.
    let who = "task copy from a GitHub board into a folder of Markdown";
    let sandbox = Sandbox::new();
    let (from, from_board) = github_projects_with_board(&sandbox);
    let notes = empty_folder(&sandbox, "notes");
    let tasks = sandbox.subdirectory("notes/tasks");
    let section = "## Comments\n\n\
        <!-- onetaskgraph:comment id=\"20260913T151107Z-1\" author=\"ada\" \
        created_at=\"2026-09-13T15:11:07Z\" updated_at=\"2026-09-13T15:11:07Z\" -->\n\
        ### ada — 2026-09-13T15:11:07Z\n\nkept across the copy\n\n\
        <!-- /onetaskgraph:comment -->\n";
    let file = tasks.join("T-1.md");
    std::fs::write(
        &file,
        format!(
            "---\ntitle: Alpha engine\nstatus: Todo\nmetadata:\n  onetaskgraph.origin: \
             work:T-1\n---\nwritten before the copy\n\n{section}"
        ),
    )
    .expect("the folder's task");
    sandbox.project_document(&document(&json!({
        SOURCE: {"plugin": "github-projects", "config": from},
        "notes": {"plugin": "local-md", "config": notes},
    })));
    ok(
        who,
        &sandbox,
        &["task", "comment", "add", &qualified(SOURCE, TASK)],
        Some("evidence the copy must not carry\n"),
    );
    let seen = from_board.documents().len();

    let report = parsed(
        who,
        &ok(
            who,
            &sandbox,
            &[
                "task",
                "copy",
                &qualified(SOURCE, TASK),
                "--to",
                "notes",
                "--json",
            ],
            None,
        ),
    );
    assert_eq!(report["items"][0]["action"], "updated", "{who}: {report}");
    assert_eq!(
        comment_calls(&from_board, seen),
        Vec::<String>::new(),
        "{who}: the copy read a comment at its source"
    );

    let written = std::fs::read_to_string(&file).expect("the folder's task");
    assert!(
        written.ends_with(section),
        "{who}: the section is kept byte for byte:\n{written}"
    );
    assert!(
        !written.contains("written before the copy"),
        "{who}: the content above it was updated:\n{written}"
    );
    assert!(
        !written.contains("evidence the copy must not carry"),
        "{who}: no source comment entered the file:\n{written}"
    );
    let listed = list(who, &sandbox, &qualified("notes", TASK));
    assert_eq!(
        listed["comments"].as_array().map(|comments| comments
            .iter()
            .map(|c| c["body"].clone())
            .collect::<Vec<_>>()),
        Some(vec![json!("kept across the copy")]),
        "{who}: {listed}"
    );
}