patchloom 0.11.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
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
use super::*;
use std::fs;
use tempfile::TempDir;

/// Helper: write a file into a temp directory and return its path.
fn write_file(dir: &TempDir, name: &str, content: &str) -> String {
    let path = dir.path().join(name);
    fs::write(&path, content).unwrap();
    path.to_str().unwrap().to_string()
}

// -- Helper to run a doc write action through run() -------------------

fn run_doc(action: DocAction, global: &GlobalFlags) -> anyhow::Result<u8> {
    run(
        DocArgs {
            action,
            write: Default::default(),
        },
        global,
    )
}

mod basic {
    use super::*;

    // -- get ----------------------------------------------------------------

    #[test]
    fn get_returns_value_from_json() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello", "count": 42}"#);
        let action = DocAction::Get {
            file: path,
            selector: "name".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "hello");
    }

    #[test]
    fn get_returns_value_from_yaml() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.yaml", "name: hello\ncount: 42\n");
        let action = DocAction::Get {
            file: path,
            selector: "name".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "hello");
    }

    #[test]
    fn get_returns_value_from_toml() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.toml", "name = \"hello\"\ncount = 42\n");
        let action = DocAction::Get {
            file: path,
            selector: "name".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "hello");
    }

    // -- has ----------------------------------------------------------------

    #[test]
    fn has_returns_true_for_existing_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Has {
            file: path,
            selector: "name".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "true");
    }

    #[test]
    fn has_returns_no_matches_for_missing_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Has {
            file: path,
            selector: "missing".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(
            code,
            exit::NO_MATCHES,
            "doc has should return NO_MATCHES when selector is absent"
        );
        assert_eq!(output, "false");
    }

    #[test]
    fn has_exit_code_consistent_with_get() {
        // Regression: doc has always returned SUCCESS even for missing keys,
        // while doc get returned NO_MATCHES. Both should use NO_MATCHES.
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "data.json", r#"{"a": 1}"#);

        // Existing key: both return SUCCESS.
        let (_, has_code) = execute_with_mode(
            &DocAction::Has {
                file: path.clone(),
                selector: "a".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        let (_, get_code) = execute_with_mode(
            &DocAction::Get {
                file: path.clone(),
                selector: "a".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        assert_eq!(has_code, exit::SUCCESS);
        assert_eq!(get_code, exit::SUCCESS);

        // Missing key: both return NO_MATCHES.
        let (_, has_code) = execute_with_mode(
            &DocAction::Has {
                file: path.clone(),
                selector: "missing".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        let (_, get_code) = execute_with_mode(
            &DocAction::Get {
                file: path,
                selector: "missing".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        assert_eq!(has_code, exit::NO_MATCHES);
        assert_eq!(get_code, exit::NO_MATCHES);
    }

    // -- keys ---------------------------------------------------------------

    #[test]
    fn keys_lists_object_keys() {
        let dir = TempDir::new().unwrap();
        let path = write_file(
            &dir,
            "test.json",
            r#"{"scripts": {"build": "tsc", "lint": "eslint", "test": "jest"}}"#,
        );
        let action = DocAction::Keys {
            file: path,
            selector: "scripts".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let keys: Vec<&str> = output.split('\n').collect();
        assert_eq!(keys.len(), 3);
        assert!(keys.contains(&"build"));
        assert!(keys.contains(&"lint"));
        assert!(keys.contains(&"test"));
    }

    // -- len ----------------------------------------------------------------

    #[test]
    fn len_counts_array_elements() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"items": [1, 2, 3, 4, 5]}"#);
        let action = DocAction::Len {
            file: path,
            selector: "items".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "5");
    }

    // -- set ----------------------------------------------------------------

    #[test]
    fn set_creates_new_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Set {
            file: path.clone(),
            selector: "age".into(),
            value: "42".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["age"], serde_json::json!(42));
    }

    #[test]
    fn set_updates_existing_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Set {
            file: path.clone(),
            selector: "name".into(),
            value: "world".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["name"], serde_json::json!("world"));
    }

    // -- delete -------------------------------------------------------------

    #[test]
    fn delete_removes_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello", "age": 42}"#);
        let action = DocAction::Delete {
            file: path.clone(),
            selector: "age".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert!(val.get("age").is_none());
    }

    // -- delete-where -------------------------------------------------------

    #[test]
    fn delete_where_removes_matching_items() {
        let dir = TempDir::new().unwrap();
        let path = write_file(
            &dir,
            "test.json",
            r#"{"users": [{"name": "alice"}, {"name": "bob"}, {"name": "carol"}]}"#,
        );
        let action = DocAction::DeleteWhere {
            file: path.clone(),
            selector: "users".into(),
            predicate: "name=bob".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        let arr = val["users"].as_array().unwrap();
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["name"], serde_json::json!("alice"));
        assert_eq!(arr[1]["name"], serde_json::json!("carol"));
    }

    #[test]
    fn delete_where_removes_multiple_matches() {
        let dir = TempDir::new().unwrap();
        let path = write_file(
            &dir,
            "test.json",
            r#"{"items": [{"status": "done"}, {"status": "pending"}, {"status": "done"}]}"#,
        );
        let action = DocAction::DeleteWhere {
            file: path.clone(),
            selector: "items".into(),
            predicate: "status=done".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        let arr = val["items"].as_array().unwrap();
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0]["status"], serde_json::json!("pending"));
    }

    // -- append / prepend ---------------------------------------------------

    #[test]
    fn append_adds_to_array() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"items": [1, 2, 3]}"#);
        let action = DocAction::Append {
            file: path.clone(),
            selector: "items".into(),
            value: "4".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["items"], serde_json::json!([1, 2, 3, 4]));
    }

    #[test]
    fn prepend_adds_to_beginning_of_array() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"items": [1, 2, 3]}"#);
        let action = DocAction::Prepend {
            file: path.clone(),
            selector: "items".into(),
            value: "0".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["items"], serde_json::json!([0, 1, 2, 3]));
    }

    #[test]
    fn merge_combines_objects() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Merge {
            file: path.clone(),
            stdin: false,
            value: Some(r#"{"age": 42}"#.into()),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["name"], serde_json::json!("hello"));
        assert_eq!(val["age"], serde_json::json!(42));
    }

    #[test]
    fn merge_rejects_stdin_and_value_together() {
        // Regression: --stdin and --value are mutually exclusive. Previously
        // providing both silently ignored --value.
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"a": 1}"#);
        let action = DocAction::Merge {
            file: path,
            stdin: true,
            value: Some(r#"{"b": 2}"#.into()),
        };
        let global = GlobalFlags::test_with_cwd(dir.path());
        let result = run_doc(action, &global);
        assert!(result.is_err(), "merge --stdin --value should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("mutually exclusive"),
            "error should mention mutual exclusivity: {err_msg}"
        );
    }

    #[test]
    fn ensure_creates_missing_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Ensure {
            file: path.clone(),
            selector: "age".into(),
            value: "30".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["age"], serde_json::json!(30));
        assert_eq!(val["name"], serde_json::json!("hello"));
    }

    // -- move ---------------------------------------------------------------

    #[test]
    fn move_renames_key() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"old_name": "value"}"#);
        let action = DocAction::Move {
            file: path.clone(),
            from: "old_name".into(),
            to: "new_name".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["new_name"], serde_json::json!("value"));
        assert!(val.get("old_name").is_none());
    }

    // -- apply writes file --------------------------------------------------

    #[test]
    fn set_with_apply_writes_file() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Set {
            file: path.clone(),
            selector: "name".into(),
            value: "world".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["name"], serde_json::json!("world"));
    }

    // -- check mode ---------------------------------------------------------

    #[test]
    fn set_with_check_reports_changes() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Set {
            file: path,
            selector: "name".into(),
            value: "world".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.check = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::CHANGES_DETECTED);
    }

    #[test]
    fn diff_detects_added_removed_changed() {
        let dir = TempDir::new().unwrap();
        let a = write_file(
            &dir,
            "a.json",
            r#"{"name":"old","version":1,"removed":true}"#,
        );
        let b = write_file(
            &dir,
            "b.json",
            r#"{"name":"new","version":1,"added":"yes"}"#,
        );
        let action = DocAction::Diff {
            file_a: a,
            file_b: b,
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::CHANGES_DETECTED);
        assert!(output.contains("~ name"), "should show changed: {output}");
        assert!(
            output.contains("- removed"),
            "should show removed: {output}"
        );
        assert!(output.contains("+ added"), "should show added: {output}");
        assert!(
            !output.contains("version"),
            "unchanged key should not appear: {output}"
        );
    }

    #[test]
    fn len_counts_object_keys() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"a": 1, "b": 2, "c": 3}"#);
        let action = DocAction::Len {
            file: path,
            selector: String::new(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "3");
    }

    // -- flatten ------------------------------------------------------------

    #[test]
    fn set_apply_creates_backup_session() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"version": "1.0"}"#);
        let action = DocAction::Set {
            file: path.clone(),
            selector: "version".into(),
            value: "\"2.0\"".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);

        let backup_dir = dir.path().join(".patchloom/backups");
        assert!(
            backup_dir.exists(),
            "backup directory should exist after doc set --apply"
        );
        let sessions: Vec<_> = fs::read_dir(&backup_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().is_dir())
            .collect();
        assert!(
            !sessions.is_empty(),
            "at least one backup session should be created"
        );
    }

    #[test]
    fn flatten_enumerates_leaf_paths() {
        let dir = TempDir::new().unwrap();
        let path = write_file(
            &dir,
            "test.json",
            r#"{"a":1,"b":{"c":2,"d":3},"e":[10,20]}"#,
        );
        let action = DocAction::Flatten { file: path };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(output.contains("a = 1"), "missing a: {output}");
        assert!(output.contains("b.c = 2"), "missing b.c: {output}");
        assert!(output.contains("b.d = 3"), "missing b.d: {output}");
        assert!(output.contains("e[0] = 10"), "missing e[0]: {output}");
        assert!(output.contains("e[1] = 20"), "missing e[1]: {output}");
    }
}

mod edge_cases {
    use super::*;

    #[test]
    fn delete_where_no_match_is_idempotent() {
        let dir = TempDir::new().unwrap();
        let path = write_file(
            &dir,
            "test.json",
            r#"{"users": [{"name": "alice"}, {"name": "bob"}]}"#,
        );
        let action = DocAction::DeleteWhere {
            file: path,
            selector: "users".into(),
            predicate: "name=nobody".into(),
        };
        let global = GlobalFlags::test_with_cwd(dir.path());
        // Engine treats delete-where no-match as success (idempotent).
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
    }

    #[test]
    fn delete_missing_key_is_idempotent() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Delete {
            file: path,
            selector: "nonexistent".into(),
        };
        let global = GlobalFlags::test_with_cwd(dir.path());
        // Engine treats delete no-match as success (idempotent).
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
    }

    // -- ensure -------------------------------------------------------------

    #[test]
    fn ensure_is_idempotent() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "original"}"#);
        // Ensure on an existing key should not overwrite.
        let action = DocAction::Ensure {
            file: path.clone(),
            selector: "name".into(),
            value: "overwritten".into(),
        };
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        // Value should remain "original" (not overwritten).
        let val: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(val["name"], serde_json::json!("original"));
    }

    #[test]
    fn diff_identical_files() {
        let dir = TempDir::new().unwrap();
        let a = write_file(&dir, "a.json", r#"{"k":1}"#);
        let b = write_file(&dir, "b.json", r#"{"k":1}"#);
        let action = DocAction::Diff {
            file_a: a,
            file_b: b,
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert_eq!(output, "identical");
    }

    #[test]
    fn diff_json_wraps_differences_in_object() {
        let dir = TempDir::new().unwrap();
        let a = write_file(&dir, "a.json", r#"{"name":"old","version":1}"#);
        let b = write_file(&dir, "b.json", r#"{"name":"new","version":1}"#);
        let action = DocAction::Diff {
            file_a: a,
            file_b: b,
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::CHANGES_DETECTED);
        let v: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(v["identical"], false);
        assert!(v["differences"].is_array());
        assert!(!v["differences"].as_array().unwrap().is_empty());
    }

    #[test]
    fn flatten_includes_empty_arrays() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"tags":[],"name":"foo","items":[1]}"#);
        let action = DocAction::Flatten { file: path };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["tags"], serde_json::json!([]));
        assert_eq!(parsed["name"], serde_json::json!("foo"));
        assert_eq!(parsed["items[0]"], serde_json::json!(1));
    }

    #[test]
    fn flatten_includes_empty_objects() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"config":{},"name":"bar"}"#);
        let action = DocAction::Flatten { file: path };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["config"], serde_json::json!({}));
        assert_eq!(parsed["name"], serde_json::json!("bar"));
    }

    #[test]
    fn flatten_includes_nested_empty_containers() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"a":{"b":[],"c":{}},"d":[1]}"#);
        let action = DocAction::Flatten { file: path };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::SUCCESS);
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["a.b"], serde_json::json!([]));
        assert_eq!(parsed["a.c"], serde_json::json!({}));
        assert_eq!(parsed["d[0]"], serde_json::json!(1));
    }
}

mod error_handling {
    use super::*;

    // -- missing selector ---------------------------------------------------

    #[test]
    fn get_missing_selector_returns_exit_3() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Get {
            file: path,
            selector: "nonexistent".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Text).unwrap();
        assert_eq!(code, exit::NO_MATCHES);
        assert!(output.is_empty());
    }

    #[test]
    fn get_missing_selector_json_emits_error_object() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Get {
            file: path,
            selector: "nonexistent".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::NO_MATCHES);
        let v: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(v["ok"], false);
        assert!(v["error"].as_str().unwrap().contains("no match"));
    }

    #[test]
    fn keys_missing_selector_json_emits_error_object() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Keys {
            file: path,
            selector: "nonexistent".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::NO_MATCHES);
        let v: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(v["ok"], false);
        assert!(v["error"].as_str().unwrap().contains("no match"));
    }

    #[test]
    fn len_missing_selector_json_emits_error_object() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Len {
            file: path,
            selector: "nonexistent".into(),
        };
        let (output, code) = execute_with_mode(&action, OutputMode::Json).unwrap();
        assert_eq!(code, exit::NO_MATCHES);
        let v: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(v["ok"], false);
        assert!(v["error"].as_str().unwrap().contains("no match"));
    }

    // -- error path tests ---------------------------------------------------

    #[test]
    fn keys_on_scalar_returns_failure() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        // Text mode: error goes to stderr, output is empty. Verify exit code.
        let (_output, code) = execute_with_mode(
            &DocAction::Keys {
                file: path.clone(),
                selector: "name".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        assert_eq!(code, exit::FAILURE);
        // JSON mode: error is wrapped in a JSON envelope.
        let (json_output, json_code) = execute_with_mode(
            &DocAction::Keys {
                file: path,
                selector: "name".into(),
            },
            OutputMode::Json,
        )
        .unwrap();
        assert_eq!(json_code, exit::FAILURE);
        assert!(json_output.contains("not an object"));
    }

    #[test]
    fn len_on_scalar_returns_failure() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        // Text mode: error goes to stderr, output is empty. Verify exit code.
        let (_output, code) = execute_with_mode(
            &DocAction::Len {
                file: path.clone(),
                selector: "name".into(),
            },
            OutputMode::Text,
        )
        .unwrap();
        assert_eq!(code, exit::FAILURE);
        // JSON mode: error is wrapped in a JSON envelope.
        let (json_output, json_code) = execute_with_mode(
            &DocAction::Len {
                file: path,
                selector: "name".into(),
            },
            OutputMode::Json,
        )
        .unwrap();
        assert_eq!(json_code, exit::FAILURE);
        assert!(json_output.contains("not an array or object"));
    }

    #[test]
    fn append_to_non_array_returns_failure() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Append {
            file: path,
            selector: "name".into(),
            value: "42".into(),
        };
        let global = GlobalFlags::test_with_cwd(dir.path());
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::FAILURE);
    }

    #[test]
    fn prepend_to_non_array_returns_failure() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"name": "hello"}"#);
        let action = DocAction::Prepend {
            file: path,
            selector: "name".into(),
            value: "42".into(),
        };
        let global = GlobalFlags::test_with_cwd(dir.path());
        let code = run_doc(action, &global).unwrap();
        assert_eq!(code, exit::FAILURE);
    }
}

mod security {
    #[allow(unused_imports)]
    use super::*;

    // -- merge --------------------------------------------------------------

    #[test]
    fn deep_merge_depth_guard_caps_recursion() {
        use crate::ops::doc::deep_merge;
        // Build a JSON tree nested 200 levels deep (exceeds MAX_MERGE_DEPTH of 128).
        // The guard stops recursing at depth 128 and overwrites with the
        // remaining subtree, preventing stack overflow on adversarial input.
        let mut base = serde_json::json!(null);
        let mut other = serde_json::json!({"leaf": true});
        for _ in 0..200 {
            other = serde_json::json!({"nested": other});
        }
        deep_merge(&mut base, &other);
        // Verify the result is a valid object (not a crash) and the
        // top-level structure was preserved.
        assert!(base.is_object());
        assert!(
            base.get("nested").is_some(),
            "top-level 'nested' key must exist"
        );
        // Walk down to verify nesting was preserved (not just top level).
        let mut cursor = &base;
        for _ in 0..10 {
            cursor = cursor
                .get("nested")
                .expect("nesting should be at least 10 levels deep");
        }
        assert!(cursor.is_object());
    }

    #[test]
    fn execute_with_mode_rejects_write_action() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "t.json", r#"{"a":1}"#);
        let action = DocAction::Delete {
            file: path,
            selector: "a".into(),
        };
        let result = execute_with_mode(&action, OutputMode::Text);
        assert!(result.is_err(), "expected error, got Ok: {result:?}");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("write operations require"),
            "should reject write actions routed through execute_with_mode"
        );
    }
}

// -- format_no_match helper tests ------------------------------------------

mod format_no_match_tests {
    use super::*;

    #[test]
    fn json_mode_returns_structured_error() {
        let (output, code) = format_no_match("selector missed", OutputMode::Json, false).unwrap();
        assert_eq!(code, crate::exit::NO_MATCHES);
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["ok"], false);
        assert_eq!(parsed["error"], "selector missed");
    }

    #[test]
    fn jsonl_mode_returns_compact_error() {
        let (output, code) = format_no_match("selector missed", OutputMode::Jsonl, false).unwrap();
        assert_eq!(code, crate::exit::NO_MATCHES);
        // JSONL output should be a single compact line (no internal newlines).
        assert!(!output.contains('\n'));
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["ok"], false);
    }

    #[test]
    fn text_mode_returns_empty_string_and_emits_stderr() {
        // In text mode, format_no_match emits to stderr and returns empty
        // stdout. We can't capture stderr in a unit test, but we verify
        // the return value is correct.
        let (output, code) = format_no_match("selector missed", OutputMode::Text, false).unwrap();
        assert_eq!(code, crate::exit::NO_MATCHES);
        assert!(output.is_empty());
    }

    #[test]
    fn text_mode_quiet_suppresses_stderr() {
        let (output, code) = format_no_match("selector missed", OutputMode::Text, true).unwrap();
        assert_eq!(code, crate::exit::NO_MATCHES);
        assert!(output.is_empty());
    }
}

// -- error path coverage --------------------------------------------------

mod error_paths {
    use super::*;

    #[test]
    fn merge_requires_stdin_or_value() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"a": 1}"#);
        let action = DocAction::Merge {
            file: path,
            stdin: false,
            value: None,
        };
        let err = run_doc(action, &GlobalFlags::test_default()).unwrap_err();
        assert!(
            err.to_string()
                .contains("merge requires --stdin or --value"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn merge_rejects_stdin_and_value_together() {
        let dir = TempDir::new().unwrap();
        let path = write_file(&dir, "test.json", r#"{"a": 1}"#);
        let action = DocAction::Merge {
            file: path,
            stdin: true,
            value: Some(r#"{"b": 2}"#.to_string()),
        };
        let err = run_doc(action, &GlobalFlags::test_default()).unwrap_err();
        assert!(
            err.to_string()
                .contains("--stdin and --value are mutually exclusive"),
            "unexpected error: {err}"
        );
    }
}