codingest-cli 0.2.28

CLI for Codingest code graphs — build/status .kgl artifacts and install the code-review Agent Skill
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
//! Executable contract for the query recipes shipped inside the CLI crate.

use std::collections::{BTreeMap, HashMap};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Arc;

use kglite::api::io::{load_file, save_graph};
use kglite::api::param::{json_object_to_value_map, kglite_value_to_json};
use kglite::api::session::{execute_mut, execute_read, ExecuteOptions};
use kglite::api::storage::{new_dir_graph_in_mode, StorageMode};
use serde_json::{json, Value};

const SHIPPED_QUERIES: &str = include_str!("../skills/codingest-code-review/references/queries.md");

const WRITE_TARGET: &str = "fixture::graph::execute_mut";
const READ_TARGET: &str = "fixture::graph::execute_read";
const UNUSED_TARGET: &str = "fixture::graph::unused_target";
const MISSING_TARGET: &str = "fixture::graph::missing_target";
const WRITE_ENTRY: &str = "fixture::service::prod_write_000";
const PATH_START: &str = "fixture::service::path_start";
const PATH_FINISH: &str = "fixture::service::path_finish";
const USED_TYPE: &str = "fixture::model::Request";
const USED_TRAIT: &str = "fixture::service::Handler";
const IMPLEMENTOR: &str = "fixture::service::ConcreteHandler";
const TYPE_CONSUMER: &str = "fixture::service::takes_request";
const BOTH_TYPE_CONSUMER: &str = "fixture::service::round_trips_request";
const UNRESOLVED_CALLER: &str = "fixture::service::unresolved_entry";

struct Fixture {
    _dir: tempfile::TempDir,
    source: PathBuf,
    graph: PathBuf,
}

fn cypher_literal(value: &str) -> String {
    format!("'{}'", value.replace('\'', "''"))
}

fn mutate(graph: &mut kglite::api::DirGraph, query: &str) {
    let params = HashMap::new();
    let options = ExecuteOptions::eager(&params);
    execute_mut(graph, query, &options)
        .unwrap_or_else(|error| panic!("fixture mutation failed: {query}\n{error}"));
}

fn add_function(
    graph: &mut kglite::api::DirGraph,
    qualified_name: &str,
    file_path: &str,
    line_number: usize,
    is_test: bool,
) {
    let name = qualified_name.rsplit("::").next().unwrap();
    mutate(
        graph,
        &format!(
            "CREATE (:Function {{qualified_name: {}, name: {}, file_path: {}, \
             line_number: {line_number}, is_test: {is_test}}})",
            cypher_literal(qualified_name),
            cypher_literal(name),
            cypher_literal(file_path),
        ),
    );
}

struct CallMetadata<'a> {
    resolution: &'a str,
    candidates: usize,
    import_backed: bool,
    call_lines: &'a str,
}

fn add_call(
    graph: &mut kglite::api::DirGraph,
    caller: &str,
    callee: &str,
    metadata: CallMetadata<'_>,
) {
    mutate(
        graph,
        &format!(
            "MATCH (caller:Function {{qualified_name: {caller}}}), \
                   (callee:Function {{qualified_name: {callee}}}) \
             CREATE (caller)-[:CALLS {{resolution: {resolution}, \
                     candidates: {candidates}, import_backed: {import_backed}, \
                     call_lines: {call_lines}, call_count: 1}}]->(callee)",
            caller = cypher_literal(caller),
            callee = cypher_literal(callee),
            resolution = cypher_literal(metadata.resolution),
            candidates = metadata.candidates,
            import_backed = metadata.import_backed,
            call_lines = cypher_literal(metadata.call_lines),
        ),
    );
}

fn source_lines(count: usize, replacements: &[(usize, &str)]) -> String {
    let mut lines = (1..=count)
        .map(|line| format!("// fixture line {line}"))
        .collect::<Vec<_>>();
    for &(line, text) in replacements {
        lines[line - 1] = text.to_string();
    }
    lines.join("\n") + "\n"
}

fn caller_source(prefix: &str, target: &str, count: usize) -> String {
    (0..count)
        .map(|index| format!("pub fn {prefix}_{index:03}() {{\n    {target}();\n}}\n\n"))
        .collect()
}

fn fixture() -> Fixture {
    let dir = tempfile::tempdir().unwrap();
    let source = dir.path().join("project");
    for path in ["src", "tests"] {
        std::fs::create_dir_all(source.join(path)).unwrap();
    }
    std::fs::write(
        source.join("src/session.rs"),
        "pub fn execute_mut() {}\npub fn execute_read() {}\npub fn unused_target() {}\n",
    )
    .unwrap();
    std::fs::write(
        source.join("src/alternative.rs"),
        "pub fn execute_mut() {}\n",
    )
    .unwrap();
    std::fs::write(
        source.join("src/service.rs"),
        source_lines(
            130,
            &[
                (
                    10,
                    "pub fn path_start() { path_mid_00(); path_mid_01(); path_mid_02(); path_mid_03(); path_mid_04(); path_mid_05(); path_mid_06(); path_mid_07(); path_mid_08(); path_mid_09(); path_mid_10(); path_mid_11(); }",
                ),
                (30, "pub fn path_finish() {}"),
                (40, "pub fn takes_request(_: Request) {}"),
                (
                    41,
                    "pub fn round_trips_request(value: Request) -> Request { value }",
                ),
                (42, "pub fn returns_request() -> Request { Request }"),
                (50, "pub fn unresolved_entry() { external_missing(); }"),
                (55, "pub trait Handler {}"),
                (60, "pub struct ConcreteHandler;"),
                (61, "impl Handler for ConcreteHandler {}"),
                (100, "pub fn path_mid_00() { path_finish(); }"),
                (101, "pub fn path_mid_01() { path_finish(); }"),
                (102, "pub fn path_mid_02() { path_finish(); }"),
                (103, "pub fn path_mid_03() { path_finish(); }"),
                (104, "pub fn path_mid_04() { path_finish(); }"),
                (105, "pub fn path_mid_05() { path_finish(); }"),
                (106, "pub fn path_mid_06() { path_finish(); }"),
                (107, "pub fn path_mid_07() { path_finish(); }"),
                (108, "pub fn path_mid_08() { path_finish(); }"),
                (109, "pub fn path_mid_09() { path_finish(); }"),
                (110, "pub fn path_mid_10() { path_finish(); }"),
                (111, "pub fn path_mid_11() { path_finish(); }"),
            ],
        ),
    )
    .unwrap();
    std::fs::write(
        source.join("src/prod_write.rs"),
        caller_source("prod_write", "execute_mut", 9),
    )
    .unwrap();
    std::fs::write(
        source.join("src/prod_read.rs"),
        caller_source("prod_read", "execute_read", 13),
    )
    .unwrap();
    std::fs::write(source.join("src/model.rs"), "pub struct Request;\n").unwrap();
    std::fs::write(
        source.join("tests/write_callers.rs"),
        caller_source("test_write", "execute_mut", 111),
    )
    .unwrap();

    let mut graph = new_dir_graph_in_mode(StorageMode::Memory, None).unwrap();
    for (qualified_name, file_path, line) in [
        (WRITE_TARGET, "src/session.rs", 1),
        (READ_TARGET, "src/session.rs", 2),
        (UNUSED_TARGET, "src/session.rs", 3),
        (PATH_START, "src/service.rs", 10),
        (PATH_FINISH, "src/service.rs", 30),
        (TYPE_CONSUMER, "src/service.rs", 40),
        (BOTH_TYPE_CONSUMER, "src/service.rs", 41),
        (UNRESOLVED_CALLER, "src/service.rs", 50),
    ] {
        add_function(&mut graph, qualified_name, file_path, line, false);
    }

    let ambiguous_alternative = "fixture::alternative::execute_mut";
    add_function(
        &mut graph,
        ambiguous_alternative,
        "src/alternative.rs",
        1,
        false,
    );
    for index in 0..9 {
        let caller = format!("fixture::service::prod_write_{index:03}");
        let function_line = index * 4 + 1;
        let call_line = (function_line + 1).to_string();
        add_function(
            &mut graph,
            &caller,
            "src/prod_write.rs",
            function_line,
            false,
        );
        let metadata = if index == 0 {
            CallMetadata {
                resolution: "lang_group",
                candidates: 2,
                import_backed: false,
                call_lines: &call_line,
            }
        } else {
            CallMetadata {
                resolution: "exact_qualified",
                candidates: 1,
                import_backed: true,
                call_lines: &call_line,
            }
        };
        add_call(&mut graph, &caller, WRITE_TARGET, metadata);
        if index == 0 {
            add_call(
                &mut graph,
                &caller,
                ambiguous_alternative,
                CallMetadata {
                    resolution: "lang_group",
                    candidates: 2,
                    import_backed: false,
                    call_lines: &call_line,
                },
            );
        }
    }

    for index in 0..111 {
        let caller = format!("fixture::tests::test_write_{index:03}");
        let function_line = index * 4 + 1;
        let call_line = (function_line + 1).to_string();
        add_function(
            &mut graph,
            &caller,
            "tests/write_callers.rs",
            function_line,
            true,
        );
        add_call(
            &mut graph,
            &caller,
            WRITE_TARGET,
            CallMetadata {
                resolution: "exact_qualified",
                candidates: 1,
                import_backed: true,
                call_lines: &call_line,
            },
        );
    }

    for index in 0..13 {
        let caller = format!("fixture::service::prod_read_{index:03}");
        let function_line = index * 4 + 1;
        let call_line = (function_line + 1).to_string();
        add_function(
            &mut graph,
            &caller,
            "src/prod_read.rs",
            function_line,
            false,
        );
        add_call(
            &mut graph,
            &caller,
            READ_TARGET,
            CallMetadata {
                resolution: "same_file",
                candidates: 1,
                import_backed: true,
                call_lines: &call_line,
            },
        );
    }

    for index in 0..12 {
        let middle = format!("fixture::service::path_mid_{index:02}");
        add_function(&mut graph, &middle, "src/service.rs", 100 + index, false);
        add_call(
            &mut graph,
            PATH_START,
            &middle,
            CallMetadata {
                resolution: "exact_qualified",
                candidates: 1,
                import_backed: true,
                call_lines: "10",
            },
        );
        add_call(
            &mut graph,
            &middle,
            PATH_FINISH,
            CallMetadata {
                resolution: "same_file",
                candidates: 1,
                import_backed: true,
                call_lines: &(100 + index).to_string(),
            },
        );
    }

    mutate(
        &mut graph,
        &format!(
            "CREATE (:Struct {{qualified_name: {}, name: 'Request', \
             file_path: 'src/model.rs', line_number: 1}})",
            cypher_literal(USED_TYPE)
        ),
    );
    let return_consumer = "fixture::service::returns_request";
    add_function(&mut graph, return_consumer, "src/service.rs", 42, false);
    for (consumer, position) in [
        (TYPE_CONSUMER, "parameter"),
        (BOTH_TYPE_CONSUMER, "both"),
        (return_consumer, "return"),
    ] {
        mutate(
            &mut graph,
            &format!(
                "MATCH (consumer:Function {{qualified_name: {consumer}}}), \
                       (used:Struct {{qualified_name: {used}}}) \
                 CREATE (consumer)-[:USES_TYPE {{position: {position}}}]->(used)",
                consumer = cypher_literal(consumer),
                used = cypher_literal(USED_TYPE),
                position = cypher_literal(position),
            ),
        );
    }

    mutate(
        &mut graph,
        &format!(
            "CREATE (:Struct {{qualified_name: {}, name: 'ConcreteHandler', \
             file_path: 'src/service.rs', line_number: 60}})",
            cypher_literal(IMPLEMENTOR)
        ),
    );
    mutate(
        &mut graph,
        &format!(
            "CREATE (:Trait {{qualified_name: {}, name: 'Handler', \
             file_path: 'src/service.rs', line_number: 55}})",
            cypher_literal(USED_TRAIT)
        ),
    );
    mutate(
        &mut graph,
        &format!(
            "MATCH (implementor:Struct {{qualified_name: {implementor}}}), \
                   (implemented:Trait {{qualified_name: {implemented}}}) \
             CREATE (implementor)-[:IMPLEMENTS]->(implemented)",
            implementor = cypher_literal(IMPLEMENTOR),
            implemented = cypher_literal(USED_TRAIT),
        ),
    );

    let graph_path = dir.path().join("recipes.kgl");
    let mut graph = Arc::new(graph);
    save_graph(&mut graph, graph_path.to_str().unwrap()).unwrap();
    Fixture {
        _dir: dir,
        source,
        graph: graph_path,
    }
}

fn extract_recipes() -> BTreeMap<String, String> {
    let lines = SHIPPED_QUERIES.lines().collect::<Vec<_>>();
    let mut recipes = BTreeMap::new();
    let mut index = 0;
    while index < lines.len() {
        let Some(name) = lines[index]
            .strip_prefix("<!-- codingest-query: ")
            .and_then(|line| line.strip_suffix(" -->"))
        else {
            index += 1;
            continue;
        };
        assert_eq!(lines.get(index + 1), Some(&"```sh"), "recipe {name}");
        assert_eq!(
            lines.get(index + 2),
            Some(&"codingest query --graph \"$GRAPH\" --format json - <<'CYPHER'"),
            "recipe {name} must use the documented quoted-heredoc stdin form"
        );
        let body_start = index + 3;
        let body_end = (body_start..lines.len())
            .find(|&line| lines[line] == "CYPHER")
            .unwrap_or_else(|| panic!("recipe {name} has no CYPHER terminator"));
        assert_eq!(lines.get(body_end + 1), Some(&"```"), "recipe {name}");
        let query = lines[body_start..body_end].join("\n");
        assert!(recipes.insert(name.to_string(), query).is_none());
        index = body_end + 2;
    }
    recipes
}

fn instantiate(query: &str) -> String {
    let replacements = [
        ("crate::src::graph::session::execute_mut", WRITE_TARGET),
        ("crate::src::graph::session::execute_read", READ_TARGET),
        ("crate::src::graph::session::unused_target", UNUSED_TARGET),
        ("crate::src::graph::session::missing_target", MISSING_TARGET),
        ("crate::src::service::write_entry", WRITE_ENTRY),
        ("crate::src::service::path_start", PATH_START),
        ("crate::src::service::path_finish", PATH_FINISH),
        ("crate::src::model::Request", USED_TYPE),
        ("crate::src::service::Handler", USED_TRAIT),
    ];
    replacements
        .iter()
        .fold(query.to_string(), |query, (from, to)| {
            query.replace(&cypher_literal(from), &cypher_literal(to))
        })
}

fn run_stdin_query(graph: &Path, query: &str) -> Value {
    let mut child = Command::new(env!("CARGO_BIN_EXE_codingest"))
        .args([
            "query",
            "--graph",
            graph.to_str().unwrap(),
            "--format",
            "json",
            "-",
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(query.as_bytes())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "query failed ({:?}):\n{query}\nstdout:\n{}\nstderr:\n{}",
        output.status.code(),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    serde_json::from_slice(&output.stdout).unwrap_or_else(|error| {
        panic!(
            "query did not return JSON ({error}): {}",
            String::from_utf8_lossy(&output.stdout)
        )
    })
}

fn run_recipe(fixture: &Fixture, recipes: &BTreeMap<String, String>, name: &str) -> Value {
    let query = instantiate(
        recipes
            .get(name)
            .unwrap_or_else(|| panic!("missing recipe {name}")),
    );
    run_stdin_query(&fixture.graph, &query)
}

fn assert_source_line(fixture: &Fixture, file: &str, line: usize, expected: &str) {
    let source = std::fs::read_to_string(fixture.source.join(file)).unwrap();
    let actual = source
        .lines()
        .nth(line - 1)
        .unwrap_or_else(|| panic!("missing {file}:{line}"));
    assert!(
        actual.contains(expected),
        "{file}:{line} is {actual:?}, expected {expected:?}"
    );
}

fn assert_caller_row_source(fixture: &Fixture, row: &Value, target_name: &str) {
    let caller = row[1].as_str().unwrap();
    let file = row[2].as_str().unwrap();
    let function_line = row[3].as_u64().unwrap() as usize;
    assert_source_line(
        fixture,
        file,
        function_line,
        caller.rsplit("::").next().unwrap(),
    );
    for line in row[7].as_str().unwrap().split(',') {
        assert_source_line(fixture, file, line.parse().unwrap(), target_name);
    }
}

#[test]
fn shipped_recipe_fences_execute_via_stdin_json() {
    let fixture = fixture();
    let recipes = extract_recipes();
    assert_eq!(
        recipes.keys().map(String::as_str).collect::<Vec<_>>(),
        [
            "bounded-call-path",
            "direct-callees",
            "production-callers",
            "read-caller-coverage",
            "target-coverage",
            "test-callers",
            "trait-implementations",
            "type-consumers",
            "write-caller-coverage",
        ]
    );
    assert!(!SHIPPED_QUERIES.contains("--params"));

    for name in recipes.keys() {
        let output = run_recipe(&fixture, &recipes, name);
        assert!(output["columns"].is_array(), "{name}: {output}");
        assert!(output["rows"].is_array(), "{name}: {output}");
    }
}

#[test]
fn caller_recipes_keep_targets_and_prod_tests_separate() {
    let fixture = fixture();
    let recipes = extract_recipes();

    let targets = run_recipe(&fixture, &recipes, "target-coverage");
    assert_eq!(
        targets["rows"],
        json!([
            [WRITE_TARGET, WRITE_TARGET, 120],
            [MISSING_TARGET, null, 0],
            [UNUSED_TARGET, UNUSED_TARGET, 0],
        ])
    );

    let write = run_recipe(&fixture, &recipes, "write-caller-coverage");
    assert_eq!(
        write["rows"],
        json!([[WRITE_TARGET, false, 9], [WRITE_TARGET, true, 111]])
    );
    let read = run_recipe(&fixture, &recipes, "read-caller-coverage");
    assert_eq!(read["rows"], json!([[READ_TARGET, false, 13]]));

    let production = run_recipe(&fixture, &recipes, "production-callers");
    assert_eq!(production["rows"].as_array().unwrap().len(), 9);
    assert!(production["rows"]
        .as_array()
        .unwrap()
        .iter()
        .all(|row| { row[0] == WRITE_TARGET && row[2].as_str().unwrap().starts_with("src/") }));
    assert_eq!(
        production["rows"][0],
        json!([
            WRITE_TARGET,
            WRITE_ENTRY,
            "src/prod_write.rs",
            1,
            "lang_group",
            2,
            false,
            "2",
        ])
    );
    for row in production["rows"].as_array().unwrap() {
        assert_caller_row_source(&fixture, row, "execute_mut();");
    }

    let read_production_recipe = instantiate(recipes.get("production-callers").unwrap())
        .replace(&cypher_literal(WRITE_TARGET), &cypher_literal(READ_TARGET));
    assert_eq!(read_production_recipe.matches("SKIP 0 LIMIT 25").count(), 1);
    let mut all_read_callers = Vec::new();
    for offset in [0, 25] {
        let page_query =
            read_production_recipe.replace("SKIP 0 LIMIT 25", &format!("SKIP {offset} LIMIT 25"));
        let page = run_stdin_query(&fixture.graph, &page_query);
        let rows = page["rows"].as_array().unwrap();
        assert_eq!(rows.len(), if offset == 0 { 13 } else { 0 });
        for row in rows {
            assert_eq!(row[0], READ_TARGET);
            assert!(row[2].as_str().unwrap().starts_with("src/"));
            assert_caller_row_source(&fixture, row, "execute_read();");
            all_read_callers.push(row[1].as_str().unwrap().to_string());
        }
    }
    let expected_read_callers = (0..13)
        .map(|index| format!("fixture::service::prod_read_{index:03}"))
        .collect::<Vec<_>>();
    assert_eq!(all_read_callers, expected_read_callers);

    let test_recipe = instantiate(recipes.get("test-callers").unwrap());
    assert_eq!(test_recipe.matches("SKIP 0 LIMIT 25").count(), 1);
    let mut all_test_callers = Vec::new();
    for offset in (0..=125).step_by(25) {
        let page_query = test_recipe.replace("SKIP 0 LIMIT 25", &format!("SKIP {offset} LIMIT 25"));
        let page = run_stdin_query(&fixture.graph, &page_query);
        let rows = page["rows"].as_array().unwrap();
        let expected_page_len = match offset {
            0 | 25 | 50 | 75 => 25,
            100 => 11,
            125 => 0,
            _ => unreachable!(),
        };
        assert_eq!(rows.len(), expected_page_len, "SKIP {offset}");
        for row in rows {
            assert_eq!(row[0], WRITE_TARGET);
            assert!(row[2].as_str().unwrap().starts_with("tests/"));
            assert_caller_row_source(&fixture, row, "execute_mut();");
            all_test_callers.push(row[1].as_str().unwrap().to_string());
        }
    }
    let expected_test_callers = (0..111)
        .map(|index| format!("fixture::tests::test_write_{index:03}"))
        .collect::<Vec<_>>();
    assert_eq!(all_test_callers, expected_test_callers);

    // Regression witness for the reported starvation shape: changing these
    // per-target recipes back to one ordered global LIMIT returns all 120 write
    // callers and silently excludes all 13 read callers.
    let legacy_global_limit = format!(
        "MATCH (caller:Function)-[:CALLS]->(target:Function) \
         WHERE target.qualified_name IN [{write}, {read}] \
         RETURN target.qualified_name AS target, caller.qualified_name AS caller \
         ORDER BY target, caller LIMIT 120",
        write = cypher_literal(WRITE_TARGET),
        read = cypher_literal(READ_TARGET),
    );
    let starved = run_stdin_query(&fixture.graph, &legacy_global_limit);
    assert_eq!(starved["rows"].as_array().unwrap().len(), 120);
    assert!(
        starved["rows"]
            .as_array()
            .unwrap()
            .iter()
            .all(|row| row[0] == WRITE_TARGET),
        "the acceptance fixture no longer reproduces target starvation"
    );
}

#[test]
fn bounded_path_type_and_impl_recipes_project_verified_schema() {
    let fixture = fixture();
    let recipes = extract_recipes();

    let callees = run_recipe(&fixture, &recipes, "direct-callees");
    assert_eq!(callees["rows"].as_array().unwrap().len(), 2);
    assert!(callees["rows"].as_array().unwrap().iter().all(|row| {
        row[0] == WRITE_ENTRY
            && row[2] == "src/prod_write.rs"
            && row[3] == 1
            && row[6] == "lang_group"
            && row[7] == 2
            && row[8] == false
            && row[9] == "2"
    }));
    for row in callees["rows"].as_array().unwrap() {
        assert_source_line(&fixture, row[2].as_str().unwrap(), 1, "prod_write_000");
        assert_source_line(&fixture, row[2].as_str().unwrap(), 2, "execute_mut();");
        assert_source_line(
            &fixture,
            row[4].as_str().unwrap(),
            row[5].as_u64().unwrap() as usize,
            row[1].as_str().unwrap().rsplit("::").next().unwrap(),
        );
    }

    let paths = run_recipe(&fixture, &recipes, "bounded-call-path");
    assert_eq!(paths["rows"].as_array().unwrap().len(), 10);
    assert!(paths["rows"].as_array().unwrap().iter().all(|row| {
        row[0].as_array().unwrap().first() == Some(&json!(PATH_START))
            && row[0].as_array().unwrap().last() == Some(&json!(PATH_FINISH))
            && row[1] == 2
    }));
    let service_source = std::fs::read_to_string(fixture.source.join("src/service.rs")).unwrap();
    for row in paths["rows"].as_array().unwrap() {
        let middle = row[0][1].as_str().unwrap().rsplit("::").next().unwrap();
        assert!(service_source.contains(&format!("pub fn {middle}()")));
    }

    let consumers = run_recipe(&fixture, &recipes, "type-consumers");
    assert_eq!(
        consumers["rows"],
        json!([
            [USED_TYPE, BOTH_TYPE_CONSUMER, "src/service.rs", 41, "both"],
            [USED_TYPE, TYPE_CONSUMER, "src/service.rs", 40, "parameter"]
        ])
    );
    let implementations = run_recipe(&fixture, &recipes, "trait-implementations");
    assert_eq!(
        implementations["rows"],
        json!([[USED_TRAIT, IMPLEMENTOR, "src/service.rs", 60]])
    );

    assert_source_line(&fixture, "src/service.rs", 40, "takes_request(_: Request)");
    assert_source_line(
        &fixture,
        "src/service.rs",
        41,
        "round_trips_request(value: Request) -> Request",
    );
    assert_source_line(&fixture, "src/service.rs", 55, "trait Handler");
    assert_source_line(&fixture, "src/service.rs", 60, "struct ConcreteHandler");
    assert_source_line(
        &fixture,
        "src/service.rs",
        61,
        "impl Handler for ConcreteHandler",
    );
    for file in [
        "src/session.rs",
        "src/alternative.rs",
        "src/service.rs",
        "src/prod_write.rs",
        "src/prod_read.rs",
        "src/model.rs",
        "tests/write_callers.rs",
    ] {
        assert!(
            fixture.source.join(file).is_file(),
            "missing fixture path {file}"
        );
    }

    let unresolved = run_stdin_query(
        &fixture.graph,
        &format!(
            "MATCH (caller:Function {{qualified_name: {caller}}}) \
             OPTIONAL MATCH (caller)-[:CALLS]->(callee:Function) \
             RETURN caller.file_path AS file, callee.qualified_name AS callee",
            caller = cypher_literal(UNRESOLVED_CALLER),
        ),
    );
    assert_eq!(unresolved["rows"], json!([["src/service.rs", null]]));
    assert!(
        std::fs::read_to_string(fixture.source.join("src/service.rs"))
            .unwrap()
            .contains("external_missing();"),
        "an absent CALLS edge must not become proof that source has no call"
    );
}

// ── The recipe corpus is the documented queries, parametrised ──────────────
//
// `codingest::methodology` ships the same nine patterns as seven
// `run_recipe_query` recipes (two pairs differ only by a literal that became a
// parameter). `queries.md` keeps its CLI-literal form because `codingest
// query` binds no parameters; the two representations are pinned to each
// other here, through the engine, with the recipe's values genuinely bound as
// `$params` rather than substituted into the text.

fn engine_rows(graph: &kglite::api::DirGraph, query: &str, params: &Value) -> Value {
    let params = json_object_to_value_map(params.as_object().expect("params is an object"));
    let options = ExecuteOptions::eager(&params);
    let outcome = execute_read(graph, query, &options)
        .unwrap_or_else(|error| panic!("engine refused the query:\n{query}\n{error}"));
    json!({
        "columns": outcome.result.columns,
        "rows": outcome
            .result
            .rows
            .iter()
            .map(|row| row.iter().map(kglite_value_to_json).collect::<Vec<_>>())
            .collect::<Vec<_>>(),
    })
}

#[test]
fn methodology_recipes_return_the_documented_queries_rows() {
    let fixture = fixture();
    let documented = extract_recipes();
    let corpus: BTreeMap<String, codingest::methodology::RecipeRecord> =
        codingest::methodology::recipe_records()
            .into_iter()
            .map(|record| (record.name.clone(), record))
            .collect();
    let graph = load_file(fixture.graph.to_str().unwrap()).expect("fixture graph loads");

    // (documented marker, recipe, the recipe's parameters for the same rows)
    let cases = [
        (
            "target-coverage",
            "target_coverage",
            json!({"requested": [WRITE_TARGET, UNUSED_TARGET, MISSING_TARGET]}),
        ),
        (
            "write-caller-coverage",
            "caller_coverage",
            json!({"qualified_name": WRITE_TARGET}),
        ),
        (
            "read-caller-coverage",
            "caller_coverage",
            json!({"qualified_name": READ_TARGET}),
        ),
        (
            "production-callers",
            "callers_page",
            json!({"qualified_name": WRITE_TARGET, "is_test": false}),
        ),
        (
            "test-callers",
            "callers_page",
            json!({"qualified_name": WRITE_TARGET, "is_test": true}),
        ),
        (
            "direct-callees",
            "direct_callees",
            json!({"qualified_name": WRITE_ENTRY}),
        ),
        (
            "bounded-call-path",
            "bounded_call_path",
            json!({"start": PATH_START, "finish": PATH_FINISH}),
        ),
        (
            "type-consumers",
            "type_consumers",
            json!({"qualified_name": USED_TYPE}),
        ),
        (
            "trait-implementations",
            "trait_implementations",
            json!({"qualified_name": USED_TRAIT}),
        ),
    ];

    let mut markers_seen = std::collections::BTreeSet::new();
    let mut recipes_seen = std::collections::BTreeSet::new();
    for (marker, recipe, params) in cases {
        let expected = engine_rows(&graph, &instantiate(&documented[marker]), &json!({}));
        assert!(
            !expected["rows"].as_array().unwrap().is_empty(),
            "{marker} returns no rows on the fixture, so equality would prove nothing"
        );
        let actual = engine_rows(&graph, &corpus[recipe].cypher, &params);
        assert_eq!(
            actual, expected,
            "recipe {recipe} diverges from documented {marker}"
        );
        markers_seen.insert(marker.to_string());
        recipes_seen.insert(recipe.to_string());
    }
    assert_eq!(
        markers_seen.into_iter().collect::<Vec<_>>(),
        documented.keys().cloned().collect::<Vec<_>>(),
        "every documented pattern must be pinned to a recipe"
    );
    assert_eq!(
        recipes_seen.into_iter().collect::<Vec<_>>(),
        corpus.keys().cloned().collect::<Vec<_>>(),
        "every shipped recipe must be pinned to a documented pattern"
    );
}