qua 0.75.0

Structure-aware query CLI for the Quarb engine
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
//! The surface a source declares (the user's ruling of 2026-11-10 on
//! the outside review's first concern, a misspelled edge label or
//! property that was silent): where a source declares every property
//! its members have and every label that leaves or reaches them (a
//! table's columns and keys, a CSV's header), a step, a predicate or
//! a projection that names what the surface lacks raises a warning
//! naming what is there. A source that declares nothing is judged
//! by the run itself (ruling 2026-11-25): a property no node read
//! has is told of when the run ends. No answer changes.

use std::path::{Path, PathBuf};

fn fixture(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-surface-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let db = rusqlite::Connection::open(dir.join("org.db")).unwrap();
    db.execute_batch(
        "CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, title TEXT,
                                 manager_id INTEGER REFERENCES employees(id));
         INSERT INTO employees VALUES (1,'Ada','Director',NULL),(2,'Bo','Lead',1),(3,'Cy',NULL,1);",
    )
    .unwrap();
    std::fs::write(dir.join("people.csv"), "name,age\nAda,36\nBo,\n").unwrap();
    std::fs::write(
        dir.join("people.json"),
        r#"{"people":[{"name":"Ada","age":36},{"name":"Bo"}]}"#,
    )
    .unwrap();
    dir
}

/// The answer, the warnings of the two codes, and whether it ran.
fn run(dir: &Path, args: &[&str]) -> (String, Vec<String>) {
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
        .env("QUARB_LOCALE", "C")
        .current_dir(dir)
        .args(args)
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    let err = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(out.status.success(), "{args:?}: {err}");
    (
        String::from_utf8(out.stdout).unwrap().trim().to_string(),
        err.lines()
            .filter(|l| l.contains("UNKNOWN_LABEL") || l.contains("UNKNOWN_PROPERTY"))
            .map(str::to_string)
            .collect(),
    )
}

const NO_LABEL: &str = "warning UNKNOWN_LABEL (/employees): no edge of /employees is labelled \
                        'manager_id'; the labels there: ->manager, <-manager";
const NO_PROPERTY: &str = "warning UNKNOWN_PROPERTY (/employees): no property 'nosuch' on \
                           /employees; its properties: id, name, title, manager_id. Spell it another way: qua man UNKNOWN_PROPERTY";

#[test]
fn a_table_warns_of_a_label_and_a_property_it_lacks() {
    let d = fixture("table");
    // the step: the label is `manager`, the column's name without its
    // suffix; the answer is as empty as it was, and the exit is 0
    let (out, warned) = run(&d, &["/employees/*[2](->manager_id)+ | ::name", "org.db"]);
    assert_eq!(out, "");
    assert_eq!(warned, [NO_LABEL]);
    // the label is asked in every direction a step walks
    for step in ["<-manager_id", "--manager_id", "(->manager_id){1;3}"] {
        let q = format!("/employees/*[2]{step} | ::name");
        let (out, warned) = run(&d, &[&q, "org.db"]);
        assert_eq!(out, "", "{step}");
        assert_eq!(warned, [NO_LABEL], "{step}");
    }
    // the navop wildcard is not asked: its name may be a child's or
    // the parent's, which are data (here the cell `manager_id`)
    let (out, warned) = run(&d, &["/employees/*[2]~manager_id | :name", "org.db"]);
    assert_eq!(out, "manager_id");
    assert!(warned.is_empty(), "{warned:?}");
    let (_, warned) = run(&d, &["/employees~99 | :name", "org.db"]);
    assert!(warned.is_empty(), "{warned:?}");
    // the projection and the predicate
    let (out, warned) = run(&d, &["/employees/* | ::nosuch", "org.db"]);
    assert_eq!(out, "null\nnull\nnull");
    assert_eq!(warned, [NO_PROPERTY]);
    let (out, warned) = run(&d, &["/employees/*[::nosuch = 1] @| count", "org.db"]);
    assert_eq!(out, "0");
    assert_eq!(warned, [NO_PROPERTY]);
    // once per name, however many rows read it
    let (_, warned) = run(
        &d,
        &[
            "/employees/* | %(::name; a = ::nosuch; b = ::nosuch)",
            "org.db",
        ],
    );
    assert_eq!(warned, [NO_PROPERTY]);

    // what the surface has is quiet: a label that reaches no node
    // here, a column whose cell is null
    let (out, warned) = run(&d, &["/employees/*[1]->manager | ::name", "org.db"]);
    assert_eq!(out, "");
    assert!(warned.is_empty(), "{warned:?}");
    let (out, warned) = run(&d, &["/employees/*[3] | ::title", "org.db"]);
    assert_eq!(out, "null");
    assert!(warned.is_empty(), "{warned:?}");
    // a label that points at the table is one of its labels
    let (out, warned) = run(&d, &["/employees/1<-manager | ::name", "org.db"]);
    assert_eq!(out, "Bo\nCy");
    assert!(warned.is_empty(), "{warned:?}");
    // the surface is a row's: the table node and a cell declare
    // nothing of their own
    let (_, warned) = run(&d, &["/employees | ::nosuch", "org.db"]);
    assert!(warned.is_empty(), "{warned:?}");
    let (_, warned) = run(&d, &["/employees/1/name | ::nosuch", "org.db"]);
    assert!(warned.is_empty(), "{warned:?}");
}

/// Under a mount the container is named as the query walks to it.
#[test]
fn a_mounted_source_is_named_from_the_mount() {
    let d = fixture("mount");
    let mounts = ["org=org.db", "people=people.csv"];
    let (out, warned) = run(&d, &["/org/employees/* | ::nosuch", mounts[0], mounts[1]]);
    assert_eq!(out, "null\nnull\nnull");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/org/employees): no property 'nosuch' on /org/employees; \
          its properties: id, name, title, manager_id. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let (_, warned) = run(
        &d,
        &["/org/employees/*[2]->manager_id", mounts[0], mounts[1]],
    );
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_LABEL (/org/employees): no edge of /org/employees is labelled \
          'manager_id'; the labels there: ->manager, <-manager"
        ]
    );
    let (_, warned) = run(&d, &["/people/row | ::agee", mounts[0], mounts[1]]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/people/row): no property 'agee' on /people/row; \
          its properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // the same name on two containers is told of each, and the nodes
    // that are no members (the tables, the cells) say nothing
    let (out, warned) = run(
        &d,
        &["//*[::name] | ::nosuch @| count", mounts[0], mounts[1]],
    );
    assert_eq!(out, "5");
    assert_eq!(warned.len(), 2, "{warned:?}");
}

#[test]
fn a_csv_declares_its_columns_and_a_documents_absent_key_is_data() {
    let d = fixture("open");
    let (out, warned) = run(&d, &["/row | ::agee", "people.csv"]);
    assert_eq!(out, "null\nnull");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/row): no property 'agee' on /row; its properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let (_, warned) = run(&d, &[r#"/row[::nme = "Ada"] | ::name"#, "people.csv"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/row): no property 'nme' on /row; its properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // an empty cell of a column it has; a label (the header says
    // nothing of what links the rows)
    let (out, warned) = run(&d, &["/row | ::age", "people.csv"]);
    assert_eq!(out, "36\nnull");
    assert!(warned.is_empty(), "{warned:?}");
    let (_, warned) = run(&d, &["/row->boss | ::name", "people.csv"]);
    assert!(warned.is_empty(), "{warned:?}");

    // JSON: a key absent from one object is ordinary data; one no
    // object read has is told of when the run ends (ruling
    // 2026-11-25; it was silence: `a_document_is_judged_by_the_run`)
    let (out, warned) = run(&d, &["/people/* | ::age", "people.json"]);
    assert_eq!(out, "36\nnull");
    assert!(warned.is_empty(), "{warned:?}");
    let (out, warned) = run(&d, &["/people/* | ::agee", "people.json"]);
    assert_eq!(out, "null\nnull");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::agee): none of the 2 nodes read has the property \
          'agee'; their properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let (_, warned) = run(&d, &["/people/*->boss", "people.json"]);
    assert!(warned.is_empty(), "{warned:?}");
}

/// Under a model the base's properties stand with the model's own,
/// and the labels are open: a model's edge is labeled as it
/// declares.
#[test]
fn a_model_adds_its_properties_and_opens_the_labels() {
    let d = fixture("model");
    std::fs::write(
        d.join("org.model"),
        "property /employees/*::shout = (::name | upper);\n",
    )
    .unwrap();
    let m = ["--model", "org.model"];
    let (out, warned) = run(&d, &[m[0], m[1], "/employees/*[1] | ::shout", "org.db"]);
    assert_eq!(out, "ADA");
    assert!(warned.is_empty(), "{warned:?}");
    let (_, warned) = run(&d, &[m[0], m[1], "/employees/*[1] | ::nosuch", "org.db"]);
    assert_eq!(warned.len(), 1);
    assert!(
        warned[0].contains("no property 'nosuch' on /employees")
            && warned[0].contains("manager_id, shout"),
        "{warned:?}"
    );
    let (_, warned) = run(&d, &[m[0], m[1], "/employees/*[2]->nosuch", "org.db"]);
    assert!(warned.is_empty(), "{warned:?}");

    // a property declared under a predicate is a property of the
    // table: the rows outside the predicate read it null, in silence
    std::fs::write(
        d.join("lead.model"),
        "property /employees/*[::title = \"Lead\"]::lead<bool> = true;\n",
    )
    .unwrap();
    let m = ["--model", "lead.model"];
    let (out, warned) = run(&d, &[m[0], m[1], "/employees/*[::lead] @| count", "org.db"]);
    assert_eq!(out, "1");
    assert!(warned.is_empty(), "{warned:?}");
    let (out, warned) = run(&d, &[m[0], m[1], "/employees/* | ::lead", "org.db"]);
    assert_eq!(out, "null\ntrue\nnull");
    assert!(warned.is_empty(), "{warned:?}");
}

/// The names there are listed whole up to sixteen; past that the
/// sixteen nearest to the name asked, and the count.
#[test]
fn a_long_list_of_names_is_cut_to_the_nearest() {
    let d = fixture("long");
    let header: Vec<String> = (1..=20).map(|i| format!("col{i:02}")).collect();
    std::fs::write(
        d.join("wide.csv"),
        format!("{}\n{}\n", header.join(","), "1,".repeat(19) + "1"),
    )
    .unwrap();
    let (_, warned) = run(&d, &["/row | ::col21", "wide.csv"]);
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(
        warned[0].ends_with("… (20 in all). Spell it another way: qua man UNKNOWN_PROPERTY"),
        "{warned:?}"
    );
    // the name asked, and the sixteen nearest of the twenty
    assert_eq!(warned[0].matches("col").count(), 1 + 16, "{warned:?}");
    // thirteen names are listed whole
    let header: Vec<String> = (1..=13).map(|i| format!("c{i:02}")).collect();
    std::fs::write(
        d.join("mid.csv"),
        format!("{}\n{}\n", header.join(","), "1,".repeat(12) + "1"),
    )
    .unwrap();
    let (_, warned) = run(&d, &["/row | ::c14", "mid.csv"]);
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(
        warned[0].ends_with("c11, c12, c13. Spell it another way: qua man UNKNOWN_PROPERTY"),
        "{warned:?}"
    );
}

/// Two tables, one with a `name` column and one without.
fn two_tables(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-presence-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let db = rusqlite::Connection::open(dir.join("two.db")).unwrap();
    db.execute_batch(
        "CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, title TEXT,
                                 manager_id INTEGER REFERENCES employees(id));
         INSERT INTO employees VALUES (1,'Ada','Director',NULL),(2,'Bo','Lead',1),(3,'Cy',NULL,1);
         CREATE TABLE rooms (id INTEGER PRIMARY KEY, floor INTEGER);
         INSERT INTO rooms VALUES (10,2),(11,NULL);",
    )
    .unwrap();
    std::fs::write(
        dir.join("people.json"),
        r#"{"people":[{"name":"Ada","age":36},{"name":"Bo"}]}"#,
    )
    .unwrap();
    dir
}

const ROOMS_NO_NAME: &str = "warning UNKNOWN_PROPERTY (/rooms): no property 'name' on /rooms; \
                             its properties: id, floor. Spell it another way: qua man UNKNOWN_PROPERTY";

/// A flag predicate (the user's ruling of 2026-11-11): a predicate
/// that is a property alone, `[::name]` or `[!::name]`, asks whether
/// it is there. Its warning is kept and raised only when nothing the
/// test asked has the name; every other read warns at once.
#[test]
fn a_test_of_presence_is_told_of_only_when_nothing_has_the_name() {
    let d = two_tables("presence");
    // one table has the column: the test tells the tables apart,
    // which is what it was written for, and nothing is told
    let (out, warned) = run(&d, &["/*/*[::name] | ::name", "two.db"]);
    assert_eq!(out, "Ada\nBo\nCy");
    assert_eq!(warned, [] as [&str; 0]);
    let (_, warned) = run(&d, &["//*[::name] @| count", "two.db"]);
    assert_eq!(warned, [] as [&str; 0]);
    // the other polarity is the same test
    let (out, warned) = run(&d, &["/*/*[!::name] | ::id", "two.db"]);
    assert_eq!(out, "10\n11");
    assert_eq!(warned, [] as [&str; 0]);
    // nothing asked has the name: a misspelling is told of, with
    // the columns, and so is a column the one table lacks
    let (out, warned) = run(&d, &["/employees/*[::manger_id] | ::name", "two.db"]);
    assert_eq!(out, "");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/employees): no property 'manger_id' on /employees; \
          its properties: id, name, title, manager_id. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let (out, warned) = run(&d, &["/rooms/*[::name] | ::id", "two.db"]);
    assert_eq!(out, "");
    assert_eq!(warned, [ROOMS_NO_NAME]);
    let (out, warned) = run(&d, &["/rooms/*[!::name] | ::id", "two.db"]);
    assert_eq!(out, "10\n11");
    assert_eq!(warned, [ROOMS_NO_NAME]);
    // every container that lacks it, once each
    let (out, warned) = run(&d, &["/*/*[::nosuch] | ::id", "two.db"]);
    assert_eq!(out, "");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/employees): no property 'nosuch' on /employees; \
             its properties: id, name, title, manager_id. Spell it another way: qua man UNKNOWN_PROPERTY",
            "warning UNKNOWN_PROPERTY (/rooms): no property 'nosuch' on /rooms; \
             its properties: id, floor. Spell it another way: qua man UNKNOWN_PROPERTY",
        ]
    );
    // a column the surface declares is there, though this row
    // reads null
    let (out, warned) = run(&d, &["/rooms/*[::floor] | ::id", "two.db"]);
    assert_eq!(out, "10");
    assert_eq!(warned, [] as [&str; 0]);
    let (out, warned) = run(&d, &["/rooms/*[2][::floor] | ::id", "two.db"]);
    assert_eq!(out, "");
    assert_eq!(warned, [] as [&str; 0]);
    // each operand of `&&` and `||` is such a test
    let (out, warned) = run(&d, &["/*/*[::name && ::title] | ::name", "two.db"]);
    assert_eq!(out, "Ada\nBo");
    assert_eq!(warned, [] as [&str; 0]);
    let (out, warned) = run(&d, &["/rooms/*[::floor && !::name] | ::id", "two.db"]);
    assert_eq!(out, "10");
    assert_eq!(warned, [ROOMS_NO_NAME]);
    // where a path leads to the tested property, the steps before it
    // ask as ever and the last name is the test
    let (out, warned) = run(
        &d,
        &["/employees/*[->manager::nickname] | ::name", "two.db"],
    );
    assert_eq!(out, "");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/employees): no property 'nickname' on /employees; \
          its properties: id, name, title, manager_id. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let (out, warned) = run(&d, &["/employees/*[->manager::title] | ::name", "two.db"]);
    assert_eq!(out, "Bo\nCy");
    assert_eq!(warned, [] as [&str; 0]);
    let (out, warned) = run(&d, &["/employees/*[->manger::name] | ::name", "two.db"]);
    assert_eq!(out, "");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_LABEL (/employees): no edge of /employees is labelled 'manger'; \
          the labels there: ->manager, <-manager"
        ]
    );
    // every other read warns at once, beside a test that is quiet:
    // the rooms pass by their id, and their name is then read
    let (out, warned) = run(&d, &["/*/*[::name || ::id] | ::name", "two.db"]);
    assert_eq!(out, "Ada\nBo\nCy\nnull\nnull");
    assert_eq!(warned, [ROOMS_NO_NAME]);
    let (_, warned) = run(&d, &["/rooms/*[::name = \"x\"]", "two.db"]);
    assert_eq!(warned, [ROOMS_NO_NAME]);
    let (_, warned) = run(&d, &["/rooms/* | ::name ?? \"none\"", "two.db"]);
    assert_eq!(warned, [ROOMS_NO_NAME]);
    // the verdict falls when the run ends: a test's warning comes
    // after the ones raised at once
    let (_, warned) = run(&d, &["/rooms/*[!::name] | ::nosuch", "two.db"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/rooms): no property 'nosuch' on /rooms; \
             its properties: id, floor. Spell it another way: qua man UNKNOWN_PROPERTY",
            ROOMS_NO_NAME,
        ]
    );
    // a source that declares nothing is judged by the run (ruling
    // 2026-11-25; it stayed quiet): the name is nowhere
    let (out, warned) = run(&d, &["/people/*[::nosuch] | ::name", "people.json"]);
    assert_eq!(out, "");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::nosuch): none of the 2 nodes read has the property \
          'nosuch'; their properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // and a flag some object answers is the question the test put
    let (out, warned) = run(&d, &["/people/*[::age] | ::name", "people.json"]);
    assert_eq!(out, "Ada");
    assert_eq!(warned, [] as [&str; 0]);
    let _ = std::fs::remove_dir_all(&d);
}

/// A source that declares nothing is judged by the run (the user's
/// ruling of 2026-11-25: a mistyped property on a schemaless
/// document is warned of "when no node read in the run has the
/// property"): `/books/*::titel` on a JSON document printed five
/// nulls and nothing else, where a CSV names its columns. The same
/// code, once a name, when the run has ended, with the count of the
/// nodes read and the properties they have. A name some node has is
/// the data's: its nulls on the others say nothing.
#[test]
fn a_document_is_judged_by_the_run() {
    let d = fixture("evidence");
    std::fs::write(
        d.join("books.json"),
        r#"{"books": [
  {"title": "Emma", "author": {"name": "Austen"}, "year": 1815, "tags": ["novel"]},
  {"title": "Iliad", "author": {"name": "Homer"}, "year": null},
  {"title": "Inferno", "author": {"name": "Dante"}, "pages": 400}]}"#,
    )
    .unwrap();
    // the mistyped name: told once, with the count and what is there
    let (out, warned) = run(&d, &["/books/*::titel", "books.json"]);
    assert_eq!(out, "null\nnull\nnull");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::titel): none of the 3 nodes read has the property \
          'titel'; their properties: title, author, year, tags, pages. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // in a condition, and as a flag: the same verdict
    let (out, warned) = run(&d, &[r#"/books/*[::titel = "Emma"]::year"#, "books.json"]);
    assert_eq!(out, "");
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(warned[0].contains("none of the 3 nodes read has the property 'titel'"));
    let (out, warned) = run(&d, &["/books/*[::titel] @| count", "books.json"]);
    assert_eq!(out, "0");
    assert_eq!(warned.len(), 1, "{warned:?}");
    // the count is of nodes, each once, however often it is read
    // (spec: "the number of the nodes read"; it was of reads: two
    // fields of one record made "6 nodes" of the three, and a read of
    // the whole context on each thread "9 nodes")
    for q in [
        "/books/* | %(a = ::titel; b = ::titel) @| count",
        "/books/* | (sum(@@_::titel)) @| count",
        "/books/* | (::titel) | (^/books/*::titel @| count) @| count",
    ] {
        let (out, warned) = run(&d, &[q, "books.json"]);
        assert_eq!(out, "3", "{q}");
        assert_eq!(warned.len(), 1, "{q}: {warned:?}");
        assert!(
            warned[0].contains("none of the 3 nodes read has the property 'titel'"),
            "{q}: {warned:?}"
        );
    }
    // one node read
    let (_, warned) = run(&d, &["/books/*[1]::titel", "books.json"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::titel): the one node read has no property 'titel'; \
          its properties: title, author, year, tags. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // a name some node has is the data's, in whatever order the
    // nodes come: `pages` is on the last alone, `tags` on the first
    for q in [
        "/books/*::pages",
        "/books/*::tags",
        "/books/*[::pages] @| count",
        "/books/*[!::tags] @| count",
    ] {
        let (_, warned) = run(&d, &[q, "books.json"]);
        assert!(warned.is_empty(), "{q}: {warned:?}");
    }
    // a property that is there and null, and a key that holds an
    // object (it reads null as a property and is walked as a step):
    // the name is there
    let (out, warned) = run(&d, &["/books/*[2]::year", "books.json"]);
    assert_eq!(out, "null");
    assert!(warned.is_empty(), "{warned:?}");
    let (out, warned) = run(&d, &["/books/*::author", "books.json"]);
    assert_eq!(out, "null\nnull\nnull");
    assert!(warned.is_empty(), "{warned:?}");
    // two names, one of them mistyped: one warning
    let (_, warned) = run(&d, &["/books/* | %(::title; y = ::yaer)", "books.json"]);
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(warned[0].contains("(::yaer)"), "{warned:?}");
    // the names listed are the nodes' own listing, and the sentence
    // stops before an empty one (finding 2026-11-25): an object with
    // no key, and a reading that answers its properties from the
    // node and lists none (the text level; a reference entry was
    // told "its properties: none" after three were read off it)
    std::fs::write(d.join("empty.json"), r#"{"items":[{},{}]}"#).unwrap();
    let (_, warned) = run(&d, &["/items/* | ::x", "empty.json"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::x): none of the 2 nodes read has the property 'x'. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    std::fs::write(
        d.join("note.md"),
        "# Emma\n\nEmma Woodhouse, handsome, clever, and rich.\n",
    )
    .unwrap();
    let (_, warned) = run(&d, &["//paragraph | ::nosuch", "text:note.md"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::nosuch): the one node read has no property 'nosuch'. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    // the sources that declare their columns answer as they did
    let (_, warned) = run(&d, &["/row | ::agee", "people.csv"]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (/row): no property 'agee' on /row; its properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
}

/// A count of a path's nodes reads none of them, and what a read
/// tells of a NAME it told only where the value was made (finding
/// 2026-11-25): `/books/*::titel @| count` answered the count in
/// silence where `/books/* | ::titel @| count` warned; over a table
/// both spellings were `SELECT COUNT(*)`, and the scan of the glued
/// one was as quiet; `:nosuch @| count` answered where its piped
/// form is refused. The count stays what it was, and the name is
/// told of as the read tells it.
#[test]
fn a_count_tells_of_the_name_it_did_not_read() {
    let d = fixture("counted");
    std::fs::write(
        d.join("books.json"),
        r#"{"books": [
  {"title": "Emma", "author": {"name": "Austen"}, "year": 1815, "tags": ["novel"]},
  {"title": "Iliad", "author": {"name": "Homer"}, "year": null},
  {"title": "Inferno", "author": {"name": "Dante"}, "pages": 400}]}"#,
    )
    .unwrap();
    // a document: the glued count as the piped one
    for q in ["/books/*::titel @| count", "/books/* | ::titel @| count"] {
        let (out, warned) = run(&d, &[q, "books.json"]);
        assert_eq!(out, "3", "{q}");
        assert_eq!(
            warned,
            [
                "warning UNKNOWN_PROPERTY (::titel): none of the 3 nodes read has the property \
              'titel'; their properties: title, author, year, tags, pages. Spell it another way: qua man UNKNOWN_PROPERTY"
            ],
            "{q}"
        );
    }
    // a name that is there, on the first node or on the last alone,
    // or under a key that holds an object: quiet
    for q in [
        "/books/*::title @| count",
        "/books/*::pages @| count",
        "/books/*::author @| count",
        "//*::title @| count",
    ] {
        let (_, warned) = run(&d, &[q, "books.json"]);
        assert!(warned.is_empty(), "{q}: {warned:?}");
    }
    // a table: pushed and scanned, glued and piped, a value and a
    // record
    for q in [
        "/employees/*::nosuch @| count",
        "/employees/* | ::nosuch @| count",
        "/employees/* | %(::name; ::nosuch) @| count",
    ] {
        for flag in [&[][..], &["--no-pushdown"][..]] {
            let mut args = flag.to_vec();
            args.extend([q, "org.db"]);
            let (out, warned) = run(&d, &args);
            assert_eq!(out, "3", "{args:?}");
            assert_eq!(warned, [NO_PROPERTY], "{args:?}");
        }
    }
    // a column the table has is counted by the database as before
    for q in [
        "/employees/*::name @| count",
        "/employees/* | ::title @| count",
    ] {
        let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
            .current_dir(&d)
            .args(["--info", q, "org.db"])
            .stdin(std::process::Stdio::null())
            .output()
            .unwrap();
        let err = String::from_utf8_lossy(&out.stderr);
        assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "3", "{q}");
        assert!(
            err.contains("pushdown: SELECT COUNT(*) FROM employees"),
            "{q}: {err}"
        );
        assert!(!err.contains("UNKNOWN_PROPERTY"), "{q}: {err}");
    }
    // a core field the engine does not have is refused under a
    // count as it is under the pipe
    for q in ["/books/*:nosuch @| count", "/books/* | :nosuch @| count"] {
        let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
            .current_dir(&d)
            .args([q, "books.json"])
            .stdin(std::process::Stdio::null())
            .output()
            .unwrap();
        let err = String::from_utf8_lossy(&out.stderr);
        assert!(!out.status.success(), "{q}");
        assert!(err.contains("':nosuch' is no core field"), "{q}: {err}");
    }
    let (out, _) = run(&d, &["/books/*:name @| count", "books.json"]);
    assert_eq!(out, "3");
    let _ = std::fs::remove_dir_all(&d);
}

/// A read whose absence the query answers for itself is no evidence
/// of a slip on a source that declares nothing (the user's ruling of
/// 2026-11-25): the left of `??` and an interpolation's defaulted
/// hole. An optional key is one that every node read may lack, and
/// such a query had no quiet spelling. A declared source still tells
/// of a name it lacks, and a flag predicate is evidence as any other
/// read is.
#[test]
fn a_read_under_a_default_is_no_evidence() {
    let d = fixture("default");
    for q in [
        r#"/people/* | ::mentor ?? "none""#,
        r#"/people/* | "${::mentor:-none}""#,
    ] {
        let (out, warned) = run(&d, &[q, "people.json"]);
        assert_eq!(out, "none\nnone", "{q}");
        assert!(warned.is_empty(), "{q}: {warned:?}");
    }
    // in a condition as well
    let (out, warned) = run(
        &d,
        &[r#"/people/*[(::mentor ?? "x") = "x"]::name"#, "people.json"],
    );
    assert_eq!(out, "Ada\nBo");
    assert!(warned.is_empty(), "{warned:?}");
    // the bare read, the flag, and a read beside the defaulted one
    // are evidence still; the default's own right side is a read
    // like any other
    const NO_MENTOR: &str = "warning UNKNOWN_PROPERTY (::mentor): none of the 2 nodes read has \
                             the property 'mentor'; their properties: name, age. Spell it another way: qua man UNKNOWN_PROPERTY";
    for q in [
        "/people/* | ::mentor",
        "/people/*[::mentor] @| count",
        r#"/people/* | %(a = ::mentor ?? "none"; b = ::mentor)"#,
        "/people/* | (::mentro ?? ::mentor)",
    ] {
        let (_, warned) = run(&d, &[q, "people.json"]);
        assert_eq!(warned, [NO_MENTOR], "{q}");
    }
    // a table says what it has, default or none
    for q in [
        r#"/employees/* | ::nosuch ?? "x""#,
        r#"/employees/* | "${::nosuch:-none}""#,
    ] {
        let (_, warned) = run(&d, &[q, "org.db"]);
        assert_eq!(warned, [NO_PROPERTY], "{q}");
    }
    let _ = std::fs::remove_dir_all(&d);
}

/// On the text level the property names are the level's own
/// vocabulary, which no document can extend, so a node without one
/// of them is data and the name is never told of (the user's ruling
/// of 2026-11-25): a paragraph has no lemma, a reference entry may
/// give no year. Any other name is judged by the run.
#[test]
fn the_text_levels_vocabulary_is_never_told_of() {
    let d = fixture("text-vocabulary");
    std::fs::write(
        d.join("note.md"),
        "# Emma\n\nEmma Woodhouse, handsome, clever, and rich.\n",
    )
    .unwrap();
    for q in [
        "//paragraph | ::lemma",
        "//paragraph[::lemma] @| count",
        "//paragraph::hypograph @| count",
        "//paragraph | ::author",
    ] {
        let (_, warned) = run(&d, &[q, "text:note.md"]);
        assert!(warned.is_empty(), "{q}: {warned:?}");
    }
    // a name the level does not have, and a reference entry's campus
    // on a node that is no entry
    for (q, name) in [
        ("//paragraph | ::nosuch", "nosuch"),
        ("//paragraph | ::year", "year"),
    ] {
        let (_, warned) = run(&d, &[q, "text:note.md"]);
        assert_eq!(
            warned,
            [format!(
                "warning UNKNOWN_PROPERTY (::{name}): the one node read has no property '{name}'. Spell it another way: qua man UNKNOWN_PROPERTY"
            )],
            "{q}"
        );
    }
    // an entry: its campi and their aliases are its vocabulary, and
    // one that gives no address or no identifier is data
    std::fs::write(
        d.join("refs.md"),
        "# Sources\n\nThe frame is Austen [1]; the war is Homer [2].\n\n## References\n\n\
         1. Austen, Jane. Emma. London: John Murray, 1815.\n\
         2. Homer. The Iliad. Translated by Samuel Butler. London: Longmans, 1898.\n",
    )
    .unwrap();
    let target = "text:refs.md?references=cmos";
    let (out, warned) = run(&d, &["//bib::year", target]);
    assert_eq!(out, "1815\n1898");
    assert!(warned.is_empty(), "{warned:?}");
    for q in [
        "//bib | ::url",
        "//bib | ::doi",
        "//bib[2] | ::urldate",
        "//bib | ::ephemeris",
    ] {
        let (_, warned) = run(&d, &[q, target]);
        assert!(warned.is_empty(), "{q}: {warned:?}");
    }
    // (the entry's own fields are listed since its record lists them)
    let (_, warned) = run(&d, &["//bib[2] | ::nosuch", target]);
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_PROPERTY (::nosuch): the one node read has no property 'nosuch'; \
             its properties: onym, auctor, titulus, locus, officina, annus. Spell it another way: qua man UNKNOWN_PROPERTY"
        ]
    );
    let _ = std::fs::remove_dir_all(&d);
}

/// A staff and its departments: a label that leaves one table and
/// reaches the other.
fn staff(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-ways-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let db = rusqlite::Connection::open(dir.join("staff.db")).unwrap();
    db.execute_batch(
        "CREATE TABLE departments (id INTEGER PRIMARY KEY, name TEXT);
         INSERT INTO departments VALUES (1,'Tragedy'),(2,'Comedy');
         CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT,
                                 department_id INTEGER REFERENCES departments(id),
                                 manager_id INTEGER REFERENCES employees(id));
         INSERT INTO employees VALUES (1,'Sophocles',1,NULL),(2,'Euripides',1,1),
                                      (3,'Aristophanes',2,NULL);",
    )
    .unwrap();
    dir
}

const LEAVES_NONE: &str = "warning UNKNOWN_LABEL (/departments): no edge labelled 'department' \
                           leaves /departments: the label reaches it, '<-department'; the labels \
                           there: <-department";
const REACHES_NONE: &str = "warning UNKNOWN_LABEL (/employees): no edge labelled 'department' \
                            reaches /employees: the label leaves it, '->department'; the labels \
                            there: ->manager, <-manager, ->department";

/// A label the surface has, borne only by edges that go the other
/// way than the step asks, is told (the user's ruling of 2026-11-29,
/// relayed by lang): the step answered nothing in silence, the
/// label being one of the container's. It is told where the step
/// walks no edge in the whole run; a step that walks one from
/// another node asked nothing wrong of this one.
#[test]
fn a_label_that_goes_the_other_way_is_told() {
    let d = staff("ways");
    let ask = |q: &str| run(&d, &[q, "staff.db"]);
    assert_eq!(
        ask("/departments/*[1]->department"),
        (String::new(), vec![LEAVES_NONE.to_string()])
    );
    assert_eq!(
        ask("/employees/*[1]<-department"),
        (String::new(), vec![REACHES_NONE.to_string()])
    );
    // the way it goes, and both ways, are quiet
    for q in [
        "/departments/*[1]<-department | ::name",
        "/departments/*[1]--department | ::name",
    ] {
        assert_eq!(ask(q), ("Sophocles\nEuripides".to_string(), vec![]), "{q}");
    }
    // a second step of a chain answers nothing, and says why; so
    // does a repetition from the wrong table, and a bracket's test
    for q in [
        "/employees/*->department->department",
        "/departments/*[1](->department)+",
        "/departments/* | [->department]",
    ] {
        assert_eq!(
            ask(q),
            (String::new(), vec![LEAVES_NONE.to_string()]),
            "{q}"
        );
    }
    // quiet where the step walks an edge from another node: a union
    // of the two tables, a repetition's last round, the bracket's
    // test over both
    for (q, answer) in [
        ("/(employees || departments)/*->department @| count", "3"),
        ("/employees/*[1](->department)+ | ::name", "Tragedy"),
        (
            "/(employees || departments)/* | [->department] @| count",
            "3",
        ),
    ] {
        assert_eq!(ask(q), (answer.to_string(), vec![]), "{q}");
    }
}

/// `@| edges` is told of a label as a step is (the same ruling): the
/// table came back empty in silence. The context is one question: a
/// container that bears the label the way asked answers for all of
/// it, the edges among its nodes leaving some and reaching others.
#[test]
fn the_edges_command_is_told_of_a_label_it_does_not_find() {
    let d = staff("edges");
    let ask = |q: &str| run(&d, &[q, "staff.db"]);
    const EMPTY: &str = "#(@(source; target; label; directed))";
    const NO_SUCH: &str = "warning UNKNOWN_LABEL (/employees): no edge of /employees is labelled \
                           'nosuch'; the labels there: ->manager, <-manager, ->department";
    for q in [
        "/employees/* @| edges(->nosuch)",
        "/employees/* @| edges(--nosuch)",
    ] {
        assert_eq!(
            ask(q),
            (EMPTY.to_string(), vec![NO_SUCH.to_string()]),
            "{q}"
        );
    }
    // a label that goes the other way
    assert_eq!(
        ask("/departments/* @| edges(->department)"),
        (EMPTY.to_string(), vec![LEAVES_NONE.to_string()])
    );
    assert_eq!(
        ask("/employees/* @| edges(<-department)"),
        (EMPTY.to_string(), vec![REACHES_NONE.to_string()])
    );
    // the library's commands collect by the same step
    assert_eq!(
        ask("/departments/* @| graph:pagerank(->department) @| count"),
        ("2".to_string(), vec![LEAVES_NONE.to_string()])
    );
    // quiet: the label is the employees' and leaves them, though no
    // department is among the nodes; both tables together; and the
    // navop wildcard, whose name may be a child's
    for (q, answer) in [
        ("/employees/* @| edges(->department) | count", "0"),
        (
            "/(employees || departments)/* @| edges(->department) | count",
            "3",
        ),
        ("/employees/* @| edges(~nosuch) | count", "0"),
    ] {
        assert_eq!(ask(q), (answer.to_string(), vec![]), "{q}");
    }
    // a label neither table bears is told of the first of them
    let (out, warned) = ask("/(employees || departments)/* @| edges(->nosuch) | count");
    assert_eq!(out, "0");
    assert_eq!(
        warned,
        [
            "warning UNKNOWN_LABEL (/departments): no edge of /departments is labelled 'nosuch'; \
          the labels there: <-department"
        ]
    );
}