ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
//! Every rule this pass states, each put to a declaration that breaks it — and the shapes it
//! deliberately does NOT refuse.
//!
//! The refusals are asserted here rather than only as fixtures, because a `trybuild` file pins the
//! MESSAGE a user sees and this pins the rule: a pass that stopped checking would still print
//! nothing, and an empty error list is what both look like from the outside.

use ingraph_codegen::{index::Dedup, search::SearchModel};

/// The pass under test, over a declaration written the way the derive took one.
fn parse(input: &syn::DeriveInput) -> syn::Result<SearchModel> {
  let (host, arguments) = crate::host::split(input);
  super::parse(&host, &arguments)
}

/// Parse a declaration, expecting it to be accepted.
fn accepted(input: proc_macro2::TokenStream) -> SearchModel {
  parse(&syn::parse2(input).expect("the declaration parses")).expect("the declaration is a recipe")
}

/// Parse a declaration, expecting the refusal it earns.
fn refused(input: proc_macro2::TokenStream) -> String {
  parse(&syn::parse2(input).expect("the declaration parses"))
    .expect_err("this declaration breaks a rule")
    .into_iter()
    .map(|error| error.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// A type as the model carries it, with the spacing taken out.
fn spelled(ty: &syn::Type) -> String {
  quote::quote!(#ty).to_string().replace(' ', "")
}

/// A path as the model carries it, with the spacing taken out.
fn path(path: &syn::Path) -> String {
  quote::quote!(#path).to_string().replace(' ', "")
}

/// **THE LOCKED DECLARATION**, read seat by seat: the head's broadcast, its join and its number, and
/// two members carrying every word a member takes.
#[test]
fn a_recipe_declares_its_broadcast_and_its_members_are_its_fields() {
  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")), dedup(hash), limit = 20)]
    pub struct TravelSearch {
      #[ingraph(weight = 1.0, depth = 50, tower(zh(type = "crate::ZhQuery", shared)))]
      subs: Subs,
      #[ingraph(weight = 2.0, depth = 100,
        tower(text(type = "crate::ClipText", shared, floor = "crate::floors::SEM_TEXT")),
        tower(image(type = "crate::ClipImage", shared)))]
      shots: Shots,
    }
  });

  assert_eq!(model.name, "TravelSearch");
  assert_eq!(model.dedup, Dedup::Hash);
  assert_eq!(model.limit.expect("the head named one").get(), 20);

  assert_eq!(model.seats.len(), 1);
  assert_eq!(model.seats[0].name, "text");
  assert_eq!(spelled(&model.seats[0].ty), "String");

  assert_eq!(model.members.len(), 2);
  let subs = &model.members[0];
  assert_eq!(subs.field, "subs");
  assert_eq!(spelled(&subs.ty), "Subs");
  assert_eq!(subs.weight, Some(1.0));
  assert_eq!(subs.depth.expect("the member named one").get(), 50);
  assert!(subs.floor.is_none(), "no member floor was written");
  assert_eq!(subs.arms.len(), 1);
  assert_eq!(subs.arms[0].name, "zh");
  assert_eq!(spelled(&subs.arms[0].tower), "crate::ZhQuery");

  let shots = &model.members[1];
  assert_eq!(shots.weight, Some(2.0));
  assert_eq!(shots.arms.len(), 2);
  assert_eq!(shots.arms[0].name, "text");
  assert_eq!(
    path(shots.arms[0].floor.as_ref().expect("the arm floored it")),
    "crate::floors::SEM_TEXT"
  );
  assert_eq!(shots.arms[1].name, "image");
  assert!(model.bound(), "both members bound a tower");
}

/// A recipe of PLAIN members is the whole grammar omitted — no head, no words, no arms.
#[test]
fn every_word_is_omissible_and_absence_is_never_a_default() {
  let model = accepted(quote::quote! {
    pub struct Plain {
      subs: Subs,
      shots: Shots,
    }
  });

  assert!(model.seats.is_empty());
  assert_eq!(
    model.dedup,
    Dedup::Hash,
    "the absent word is the root's own"
  );
  assert!(
    model.limit.is_none(),
    "a deferral, never the framework's 20"
  );
  assert!(!model.bound());
  for member in &model.members {
    assert!(member.weight.is_none());
    assert!(member.depth.is_none());
    assert!(member.floor.is_none());
    assert!(member.arms.is_empty());
  }
}

/// SEVERAL SEATS in one block, each named once and typed once.
#[test]
fn a_broadcast_may_carry_several_seats() {
  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String"), image(type = "Vec<u8>")))]
    pub struct Two {
      #[ingraph(tower(text(type = "T")), tower(image(type = "I")))]
      shots: Shots,
    }
  });

  assert_eq!(model.seats.len(), 2);
  assert_eq!(model.seats[0].name, "text");
  assert_eq!(model.seats[1].name, "image");
  assert_eq!(spelled(&model.seats[1].ty), "Vec<u8>");
}

/// A SEAT FEEDS THE ARMS THAT SHARE ITS NAME, and two members' arms of one name is the shape a
/// broadcast is for.
#[test]
fn one_seat_reaches_the_arm_of_its_name_on_every_member() {
  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String"), image(type = "Vec<u8>")))]
    pub struct Two {
      #[ingraph(tower(text(type = "A", shared)))]
      subs: Subs,
      #[ingraph(tower(text(type = "B", shared)), tower(image(type = "C", shared)))]
      shots: Shots,
    }
  });

  let bound = |member: usize, arm: usize| {
    model.members[member].arms[arm]
      .shared
      .as_ref()
      .expect("the arm wrote `shared`")
      .seat
      .to_string()
  };
  assert_eq!(bound(0, 0), "text");
  assert_eq!(bound(1, 0), "text");
  assert_eq!(bound(1, 1), "image");
}

/// **THE SOLE-SEAT DEFAULT** — one broadcast has one thing a `shared` could mean, so an arm of
/// another name reads it without naming it. The resource axis's own law, one layer up.
#[test]
fn a_lone_seat_feeds_every_arm_that_reads_the_broadcast() {
  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")))]
    pub struct Travel {
      #[ingraph(tower(zh(type = "crate::ZhQuery", shared)))]
      subs: Subs,
    }
  });

  let binding = model.members[0].arms[0]
    .shared
    .as_ref()
    .expect("the arm wrote `shared`");
  assert_eq!(binding.seat, "text", "the sole seat, not the arm's name");
}

/// The four spellings, each filling one seat, and the two that carry a HOOK carrying the path.
#[test]
fn the_shared_word_chooses_the_crossing() {
  use ingraph_codegen::search::SharedBridge;

  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")))]
    pub struct Four {
      #[ingraph(
        tower(a(type = "A", shared)),
        tower(b(type = "B", shared(try))),
        tower(c(type = "C", shared(with = "crate::hooks::widen"))),
        tower(d(type = "D", shared(try_with = "crate::hooks::narrow"))))]
      shots: Shots,
    }
  });

  let bridge = |arm: usize| {
    model.members[0].arms[arm]
      .shared
      .as_ref()
      .expect("the arm wrote `shared`")
      .bridge
      .clone()
  };
  assert!(matches!(bridge(0), SharedBridge::Total));
  assert!(matches!(bridge(1), SharedBridge::Fallible));
  match bridge(2) {
    SharedBridge::Hook(hook) => assert_eq!(path(&hook), "crate::hooks::widen"),
    other => panic!("`with` names a hook, not {}", other.word()),
  }
  match bridge(3) {
    SharedBridge::FallibleHook(hook) => assert_eq!(path(&hook), "crate::hooks::narrow"),
    other => panic!("`try_with` names a hook, not {}", other.word()),
  }
}

/// An arm that wrote NO `shared` is asked its own question — a road, never a gap.
#[test]
fn an_arm_with_no_shared_word_reads_no_broadcast() {
  let model = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")))]
    pub struct Own {
      #[ingraph(tower(text(type = "T")))]
      shots: Shots,
    }
  });

  assert!(model.members[0].arms[0].shared.is_none());
}

/// The FLOOR is a const path at both grains it is written at.
#[test]
fn a_floor_is_a_const_path_at_the_member_and_at_the_arm() {
  let model = accepted(quote::quote! {
    pub struct Floored {
      #[ingraph(floor = "crate::floors::MEMBER", tower(text(type = "T", floor = "crate::floors::ARM")))]
      shots: Shots,
    }
  });

  assert_eq!(
    path(model.members[0].floor.as_ref().expect("written")),
    "crate::floors::MEMBER"
  );
  assert_eq!(
    path(model.members[0].arms[0].floor.as_ref().expect("written")),
    "crate::floors::ARM"
  );
}

/// The two spellings of a list of arms are one declaration — `tower(…)` is a block, and a block may
/// be written more than once.
#[test]
fn arms_may_be_listed_in_one_block_or_in_several() {
  let together = accepted(quote::quote! {
    pub struct T { #[ingraph(tower(a(type = "A"), b(type = "B")))] shots: Shots }
  });
  let apart = accepted(quote::quote! {
    pub struct T { #[ingraph(tower(a(type = "A")), tower(b(type = "B")))] shots: Shots }
  });

  for model in [together, apart] {
    let names: Vec<String> = model.members[0]
      .arms
      .iter()
      .map(|arm| arm.name.to_string())
      .collect();
    assert_eq!(names, ["a", "b"]);
  }
}

/// The JOIN vocabulary is the framework's, at the grain the fused doors moved to.
#[test]
fn the_dedup_word_is_the_frameworks_own_at_this_grain() {
  let eq = accepted(quote::quote! {
    #[ingraph(dedup(eq))]
    pub struct T { shots: Shots }
  });
  assert_eq!(eq.dedup, Dedup::Eq);

  assert!(
    refused(quote::quote! {
      #[ingraph(dedup(bloom))]
      pub struct T { shots: Shots }
    })
    .contains("is not a dedup mechanism")
  );
}

/// `crate = "…"` is read here as it is everywhere in the family.
#[test]
fn the_crate_word_names_where_ingraph_lives() {
  let model = accepted(quote::quote! {
    #[ingraph(crate = "other::ingraph")]
    pub struct T { shots: Shots }
  });
  assert_eq!(path(&model.krate), "other::ingraph");
}

// ── the refusals ──────────────────────────────────────────────────────────────────────────────

/// A SECOND declaration of one seat, at every position that has one.
#[test]
fn nothing_is_declared_twice() {
  let table = [
    (
      quote::quote! {
        #[ingraph(shared(text(type = "A")), shared(image(type = "B")))]
        pub struct T { shots: Shots }
      },
      "`shared(…)` is declared more than once",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text(type = "A"), text(type = "B")))]
        pub struct T { shots: Shots }
      },
      "`text` is declared more than once in `shared(…)`",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text(type = "A", type = "B")))]
        pub struct T { shots: Shots }
      },
      "`type` is declared more than once in `text(…)`",
    ),
    (
      quote::quote! {
        #[ingraph(dedup(hash), dedup(eq))]
        pub struct T { shots: Shots }
      },
      "`dedup` is declared more than once",
    ),
    (
      quote::quote! {
        #[ingraph(limit = 10, limit = 20)]
        pub struct T { shots: Shots }
      },
      "`limit` is declared more than once in this recipe's head",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(weight = 1.0, weight = 2.0)] shots: Shots }
      },
      "`weight` is declared more than once on `shots`",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(depth = 10, depth = 20)] shots: Shots }
      },
      "`depth` is declared more than once in `shots`",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(floor = "crate::A", floor = "crate::B")] shots: Shots }
      },
      "`floor` is declared more than once in `shots`",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(tower(a(type = "A", type = "B")))] shots: Shots }
      },
      "`type` is declared more than once in `a(…)` on `shots`",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text(type = "S")))]
        pub struct T { #[ingraph(tower(a(type = "A", shared, shared(try))))] shots: Shots }
      },
      "`shared` is declared more than once in `a(…)` on `shots`",
    ),
  ];

  for (written, sentence) in table {
    let refusal = refused(written);
    assert!(refusal.contains(sentence), "{sentence}\n got: {refusal}");
  }
}

/// TWO ARMS OF ONE NAME on one member — and the sentence says what IS allowed beside it.
#[test]
fn one_member_may_not_bind_two_arms_of_one_name() {
  let refusal = refused(quote::quote! {
    pub struct T {
      #[ingraph(tower(text(type = "A")), tower(text(type = "B")))]
      shots: Shots,
    }
  });
  assert!(
    refusal.contains("`text` is bound more than once on `shots`"),
    "{refusal}"
  );
  assert!(
    refusal.contains("Two MEMBERS may each carry an arm called `text`"),
    "{refusal}"
  );
}

/// A WORD THAT DECIDED NOTHING is refused at every position that takes a value.
#[test]
fn a_word_that_named_nothing_is_refused() {
  let table = [
    (
      quote::quote! {
        #[ingraph(shared(text))]
        pub struct T { shots: Shots }
      },
      "`text` names no type",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text()))]
        pub struct T { shots: Shots }
      },
      "`text` names no type",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(tower(a))] shots: Shots }
      },
      "`a` names no tower",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(tower(a(floor = "crate::F")))] shots: Shots }
      },
      "`a` names no tower",
    ),
  ];

  for (written, sentence) in table {
    let refusal = refused(written);
    assert!(refusal.contains(sentence), "{sentence}\n got: {refusal}");
  }
}

/// **ONE WORD, TWO POSITIONS, TWO SHAPES** — and a bare `shared` at the HEAD is told the difference.
///
/// The bare word is legal, and legal at an ARM, which is exactly why the head cannot read one as an
/// omission: it is the mistake this grammar invites.
#[test]
fn a_bare_shared_at_the_head_is_told_which_position_it_is_at() {
  for written in [
    quote::quote! {
      #[ingraph(shared)]
      pub struct T { shots: Shots }
    },
    quote::quote! {
      #[ingraph(shared())]
      pub struct T { shots: Shots }
    },
  ] {
    let refusal = refused(written);
    assert!(
      refusal.contains("`shared` at the RECIPE names the seats"),
      "{refusal}"
    );
    assert!(refusal.contains("The BARE word is an ARM's"), "{refusal}");
  }
}

/// AN EMPTY `tower(…)` is a word that decided nothing, not a member declaring it has no arms.
#[test]
fn an_empty_tower_block_is_a_word_that_decided_nothing() {
  for written in [
    quote::quote! { pub struct T { #[ingraph(tower)] shots: Shots } },
    quote::quote! { pub struct T { #[ingraph(tower())] shots: Shots } },
  ] {
    let refusal = refused(written);
    assert!(
      refusal.contains("`tower` names the arms `shots` binds"),
      "{refusal}"
    );
  }
}

/// **THE BROADCAST AN ARM READS HAS TO EXIST** — and the two answers differ by what the recipe
/// declared.
#[test]
fn a_shared_arm_names_a_seat_the_recipe_declares() {
  let none = refused(quote::quote! {
    pub struct T { #[ingraph(tower(text(type = "A", shared)))] shots: Shots }
  });
  assert!(
    none.contains("`text` reads a broadcast this recipe does not declare"),
    "{none}"
  );

  let several = refused(quote::quote! {
    #[ingraph(shared(text(type = "A"), image(type = "B")))]
    pub struct T { #[ingraph(tower(zh(type = "Z", shared)))] shots: Shots }
  });
  assert!(
    several.contains("`zh` reads a broadcast seat this recipe does not declare"),
    "{several}"
  );
  assert!(several.contains("`text` or `image`"), "{several}");
}

/// A FLOOR IS A CONST, and the refusal carries the reason a literal cannot be one.
#[test]
fn a_floor_is_never_a_literal() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(floor = 0.7)] shots: Shots }
  });
  assert!(refusal.contains("`floor` names a CONST"), "{refusal}");
  assert!(
    refusal.contains("open type this framework never sees"),
    "{refusal}"
  );
}

/// **THE TWO-CLOCK WALL** — a per-leg floor is refused by name, with where it does live.
#[test]
fn a_per_leg_floor_has_no_recipe_side_home() {
  for written in [
    quote::quote! {
      pub struct T { #[ingraph(floor(dense = "crate::F"))] shots: Shots }
    },
    quote::quote! {
      pub struct T { #[ingraph(tower(a(type = "A", floor(dense = "crate::F"))))] shots: Shots }
    },
  ] {
    let refusal = refused(written);
    assert!(
      refusal.contains("`floor(…)` would name a floor per LEG"),
      "{refusal}"
    );
    assert!(
      refusal.contains("A per-leg floor is a REQUEST's"),
      "{refusal}"
    );
  }
}

/// A WEIGHT IS A RATIO — the SPELLING is refused, and the value is not.
#[test]
fn a_weight_is_a_plain_decimal() {
  for written in [
    quote::quote! { pub struct T { #[ingraph(weight = 2)] shots: Shots } },
    quote::quote! { pub struct T { #[ingraph(weight = "2.0")] shots: Shots } },
  ] {
    let refusal = refused(written);
    assert!(refusal.contains("`weight` is a RATIO"), "{refusal}");
  }
}

/// A NEGATIVE coefficient is ACCEPTED, because the request grain accepts one.
///
/// `Route::with_weight` takes any `f64`, so a declaration refusing what a request may send would be
/// this tier inventing a law the tier above it does not have. MEASURED rather than assumed: syn
/// reads the minus as part of the literal, so the value arrives here whole.
#[test]
fn a_negative_coefficient_is_the_authors_to_intend() {
  let model = accepted(quote::quote! {
    pub struct T { #[ingraph(weight = -1.0)] shots: Shots }
  });
  assert_eq!(model.members[0].weight, Some(-1.0));
}

/// A COEFFICIENT WITH NO FINITE VALUE is refused — it multiplies every term this member contributes.
#[test]
fn a_coefficient_has_a_finite_value() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(weight = 1e400)] shots: Shots }
  });
  assert!(
    refusal.contains("is past what a coefficient can be"),
    "{refusal}"
  );
}

/// The NUMBERS are the family's own, so their zero and their width are refused in its own words.
#[test]
fn a_zero_is_not_an_answer_anybody_asked_for() {
  assert!(
    refused(quote::quote! {
      #[ingraph(limit = 0)]
      pub struct T { shots: Shots }
    })
    .contains("`limit = 0` asks for an answer with no rows"),
  );
  assert!(
    refused(quote::quote! {
      pub struct T { #[ingraph(depth = 0)] shots: Shots }
    })
    .contains("`depth = 0` asks for an answer with no rows"),
  );
}

/// `depth` AT THE HEAD is refused by name, with the position it belongs to.
#[test]
fn depth_is_a_members_number_and_the_head_is_told_so() {
  let refusal = refused(quote::quote! {
    #[ingraph(depth = 50)]
    pub struct T { shots: Shots }
  });
  assert!(
    refusal.contains("`depth` is a MEMBER's number here"),
    "{refusal}"
  );
  assert!(
    refusal.contains("What the head takes is `limit`"),
    "{refusal}"
  );
}

/// An UNKNOWN word is refused at every level, and each level's sentence names that level's words.
#[test]
fn an_unknown_word_is_refused_where_it_was_written() {
  let table = [
    (
      quote::quote! {
        #[ingraph(fuse(rrf))]
        pub struct T { shots: Shots }
      },
      "unknown entry in `#[ingraph::search(…)]`",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text(type = "S", rename = "x")))]
        pub struct T { shots: Shots }
      },
      "unknown entry in `text(…)`",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(ask)] shots: Shots }
      },
      "unknown entry in `#[ingraph(…)]` on a member",
    ),
    (
      quote::quote! {
        pub struct T { #[ingraph(tower(a(type = "A", chain)))] shots: Shots }
      },
      "unknown entry in `a(…)` on `shots`",
    ),
    (
      quote::quote! {
        #[ingraph(shared(text(type = "S")))]
        pub struct T { #[ingraph(tower(a(type = "A", shared(maybe))))] shots: Shots }
      },
      "unknown entry in `shared(…)` in `a(…)` on `shots`",
    ),
  ];

  for (written, sentence) in table {
    let refusal = refused(written);
    assert!(refusal.contains(sentence), "{sentence}\n got: {refusal}");
  }
}

/// The NEAR MISS is this position's own vocabulary and never a neighbouring scope's.
#[test]
fn a_near_miss_suggests_this_positions_own_word() {
  let refusal = refused(quote::quote! {
    #[ingraph(limt = 20)]
    pub struct T { shots: Shots }
  });
  assert!(refusal.contains("did you mean `limit`"), "{refusal}");
}

/// `shared(try)` CARRIES NOTHING — it names which conversion, and the conversion is the author's.
#[test]
fn the_try_word_is_bare() {
  let refusal = refused(quote::quote! {
    #[ingraph(shared(text(type = "S")))]
    pub struct T { #[ingraph(tower(a(type = "A", shared(try = "x"))))] shots: Shots }
  });
  assert!(refusal.contains("`try` is a bare word here"), "{refusal}");
}

/// TWO CROSSINGS in one `shared(…)` is asking for neither.
#[test]
fn one_arm_crosses_one_way() {
  let refusal = refused(quote::quote! {
    #[ingraph(shared(text(type = "S")))]
    pub struct T {
      #[ingraph(tower(a(type = "A", shared(try, with = "crate::hooks::widen"))))]
      shots: Shots,
    }
  });
  assert!(
    refusal.contains("`shared(…)` names ONE crossing"),
    "{refusal}"
  );
}

/// THE SHAPES, each refused with what a recipe's fields are FOR.
#[test]
fn a_recipe_is_a_struct_with_named_fields_and_no_parameter() {
  let table = [
    (
      quote::quote! { pub struct Empty {} },
      "a recipe lists no member",
    ),
    (
      quote::quote! { pub struct Unit; },
      "a recipe lists no member",
    ),
    (
      quote::quote! { pub struct Positional(Subs, Shots); },
      "a recipe must be a struct with NAMED fields",
    ),
    (
      quote::quote! { pub enum Choice { Subs, Shots } },
      "`#[ingraph::search]` supports structs only",
    ),
    (
      quote::quote! { pub struct Generic<T> { shots: T } },
      "a search recipe takes no generic parameter",
    ),
    (
      quote::quote! { pub struct Bounded where Shots: Sized { shots: Shots } },
      "a search recipe takes no generic parameter",
    ),
  ];

  for (written, sentence) in table {
    let refusal = refused(written);
    assert!(refusal.contains(sentence), "{sentence}\n got: {refusal}");
  }
}

/// EVERY rule the pass can still see is reported, so one compile fixes a whole declaration.
#[test]
fn one_compile_reports_the_whole_declaration() {
  let refusal = refused(quote::quote! {
    #[ingraph(limit = 0, dedup(bloom))]
    pub struct Broken {
      #[ingraph(weight = 2, depth = 0)]
      shots: Shots,
    }
  });

  for sentence in [
    "`limit = 0`",
    "is not a dedup mechanism",
    "`weight` is a RATIO",
    "`depth = 0`",
  ] {
    assert!(refusal.contains(sentence), "{sentence}\n got: {refusal}");
  }
}

/// **THE BACKEND WORD REACHES THE MODEL AS THE BACKEND IT NAMES**, and a recipe that wrote none says
/// so — the presence law, which is what keeps the wire seams off a Rust-only recipe.
#[test]
fn the_backend_word_is_carried_as_the_backend_it_names() {
  let dressed = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")), graphql(async_graphql))]
    pub struct TravelSearch { subs: Subs }
  });
  assert_eq!(
    dressed.graphql,
    Some(ingraph_codegen::model::GraphqlKind::AsyncGraphql),
  );
  assert!(dressed.mounts());

  let plain = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")))]
    pub struct TravelSearch { subs: Subs }
  });
  assert!(plain.graphql.is_none(), "no block is *no wire face*");
  assert!(!plain.mounts());
  // THE EXCLUSION: everything the RUST surface is built from survives the absent word.
  assert_eq!(plain.members.len(), 1);
  assert_eq!(plain.seats.len(), 1);
}

/// **A `graphql` THAT NAMES NO BACKEND IS REFUSED**, with what the word is for said beside it.
#[test]
fn a_backend_block_that_names_nothing_is_refused() {
  for written in [
    quote::quote! {
      #[ingraph(shared(text(type = "String")), graphql)]
      pub struct TravelSearch { subs: Subs }
    },
    quote::quote! {
      #[ingraph(shared(text(type = "String")), graphql())]
      pub struct TravelSearch { subs: Subs }
    },
  ] {
    let refusal = refused(written);
    assert!(refusal.contains("names no backend"), "{refusal}");
    assert!(refusal.contains("async_graphql"), "{refusal}");
  }
}

/// **AN UNKNOWN BACKEND IS REFUSED BY NAME**, with the vocabulary this framework renders through.
#[test]
fn an_unknown_backend_is_refused_with_the_vocabulary() {
  let refusal = refused(quote::quote! {
    #[ingraph(shared(text(type = "String")), graphql(juniper))]
    pub struct TravelSearch { subs: Subs }
  });
  assert!(refusal.contains("unknown backend"), "{refusal}");
  assert!(refusal.contains("async_graphql"), "{refusal}");
}

/// **A SECOND `graphql(…)` BLOCK IS REFUSED** — one block holds every backend, on the family's own
/// second-declaration law.
#[test]
fn a_second_backend_block_is_refused() {
  let refusal = refused(quote::quote! {
    #[ingraph(
      shared(text(type = "String")),
      graphql(async_graphql),
      graphql(async_graphql)
    )]
    pub struct TravelSearch { subs: Subs }
  });
  assert!(refusal.contains("declared more than once"), "{refusal}");
}

/// **A PUBLISHED MEMBER'S QUESTION HAS ONE PRODUCER PER SEAT** — the wire road's second condition,
/// which arrived the day a resolver started running a bound tower and was re-scoped the day several
/// seats became a choice.
///
/// Four shapes, and only one is refused: two arms of one member on the SAME seat (two answers, one
/// question), against two arms on TWO seats (exactly one of which arrives, so nothing is ambiguous),
/// two arms where only one reads the broadcast, and the same colliding pair on an UNDRESSED recipe.
#[test]
fn a_published_member_binds_at_most_one_reading_arm_per_seat() {
  let refusal = refused(quote::quote! {
    #[ingraph(shared(text(type = "String")), graphql(async_graphql))]
    pub struct TravelSearch {
      #[ingraph(tower(zh(type = "crate::Zh", shared), en(type = "crate::En", shared)))]
      subs: Subs,
    }
  });
  assert!(
    refusal.contains("produced by ONE bound tower per seat"),
    "{refusal}",
  );
  assert!(refusal.contains("`zh`"), "{refusal}");
  assert!(refusal.contains("`en`"), "{refusal}");
  // AND THE SEAT IS NAMED, which is the grain the law moved to.
  assert!(refusal.contains("`text` broadcast"), "{refusal}");

  // TWO SEATS, TWO ARMS, ONE MEMBER — the shape the choice made legal: a request fills one seat, so
  // exactly one of these arms ever produces this member's question.
  let modes = accepted(quote::quote! {
    #[ingraph(
      shared(text(type = "String"), code(type = "crate::Sku")),
      graphql(async_graphql)
    )]
    pub struct TravelSearch {
      #[ingraph(tower(text(type = "crate::Zh", shared), code(type = "crate::En", shared)))]
      subs: Subs,
    }
  });
  assert_eq!(modes.members[0].arms.len(), 2);
  assert!(modes.mounts());

  // ONE PRODUCER, one arm that reads nothing — accepted, and both arms kept.
  let one = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")), graphql(async_graphql))]
    pub struct TravelSearch {
      #[ingraph(tower(zh(type = "crate::Zh", shared), en(type = "crate::En")))]
      subs: Subs,
    }
  });
  assert_eq!(one.members[0].arms.len(), 2);

  // AND THE RUST DOOR TAKES BOTH READERS, which is what makes this the WORD's condition.
  let undressed = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String")))]
    pub struct TravelSearch {
      #[ingraph(tower(zh(type = "crate::Zh", shared), en(type = "crate::En", shared)))]
      subs: Subs,
    }
  });
  assert_eq!(undressed.members[0].arms.len(), 2);
}

/// **A DRESSED RECIPE BROADCASTS INTO AT LEAST ONE SEAT** — the one count still refused, and the
/// several-seats arm retired: a choice is a road now, not an unanswerable question.
#[test]
fn a_dressed_recipe_needs_at_least_one_broadcast_seat() {
  let none = refused(quote::quote! {
    #[ingraph(graphql(async_graphql))]
    pub struct TravelSearch { subs: Subs }
  });
  assert!(none.contains("declares no `shared(…)` seat"), "{none}");

  // SEVERAL IS THE ROAD — one unbound member reaches every seat, so the recipe publishes its choice.
  let several = accepted(quote::quote! {
    #[ingraph(
      shared(text(type = "String"), point(type = "Vec<f32>")),
      graphql(async_graphql)
    )]
    pub struct TravelSearch { subs: Subs }
  });
  assert_eq!(several.seats.len(), 2);
  assert!(several.mounts());

  // AND THE RUST DOOR IS UNTOUCHED BY THE SEATLESS SHAPE, which is what makes this the WORD's
  // condition rather than the recipe's.
  let undressed = accepted(quote::quote! {
    pub struct TravelSearch { subs: Subs }
  });
  assert_eq!(undressed.members.len(), 1);
  assert!(!undressed.mounts());
}

/// **EVERY DECLARED SEAT REACHES A MEMBER** — the declared-word-without-an-instance law at the seat
/// grain, and the guarantee that an arriving variant never fuses nothing.
///
/// Four shapes: a seat every member's arms bought around (refused, naming the seat), the same
/// declaration with one member left unbound (accepted — an unbound member takes whichever seat
/// arrived), a seat bought by one member's arm while another member buys elsewhere (accepted), and
/// the refused shape UNDRESSED (accepted, because the law is the schema's).
#[test]
fn a_seat_no_member_reaches_is_refused_by_name() {
  let refusal = refused(quote::quote! {
    #[ingraph(
      shared(text(type = "String"), code(type = "crate::Sku")),
      graphql(async_graphql)
    )]
    pub struct TravelSearch {
      #[ingraph(tower(text(type = "crate::Zh", shared)))]
      subs: Subs,
      #[ingraph(tower(text(type = "crate::Clip", shared)))]
      shots: Shots,
    }
  });
  assert!(
    refusal.contains("`code` is a seat no member reaches"),
    "{refusal}"
  );
  assert!(refusal.contains("shared(…)"), "{refusal}");

  // AN UNBOUND MEMBER REACHES EVERY SEAT, which is the crossing it has always had.
  let unbound = accepted(quote::quote! {
    #[ingraph(
      shared(text(type = "String"), code(type = "crate::Sku")),
      graphql(async_graphql)
    )]
    pub struct TravelSearch {
      #[ingraph(tower(text(type = "crate::Zh", shared)))]
      subs: Subs,
      shots: Shots,
    }
  });
  assert!(unbound.mounts());

  // ONE MEMBER PER SEAT — the shape the whole road exists for.
  let split = accepted(quote::quote! {
    #[ingraph(
      shared(text(type = "String"), code(type = "crate::Sku")),
      graphql(async_graphql)
    )]
    pub struct TravelSearch {
      #[ingraph(tower(text(type = "crate::Zh", shared)))]
      subs: Subs,
      #[ingraph(tower(code(type = "crate::Codes", shared)))]
      shots: Shots,
    }
  });
  assert!(split.mounts());

  // AND THE RUST DOOR KEEPS THE UNREACHED SEAT: the law is what the `graphql(…)` word buys.
  let undressed = accepted(quote::quote! {
    #[ingraph(shared(text(type = "String"), code(type = "crate::Sku")))]
    pub struct TravelSearch {
      #[ingraph(tower(text(type = "crate::Zh", shared)))]
      subs: Subs,
      #[ingraph(tower(text(type = "crate::Clip", shared)))]
      shots: Shots,
    }
  });
  assert_eq!(undressed.seats.len(), 2);
  assert!(!undressed.mounts());
}

/// **THE UNKNOWN-WORD ROSTER OFFERS THE BACKEND WORD**, so a reader who wrote it wrong is told it
/// exists.
#[test]
fn the_head_roster_names_the_backend_word() {
  let refusal = refused(quote::quote! {
    #[ingraph(graphq(async_graphql))]
    pub struct TravelSearch { subs: Subs }
  });
  assert!(refusal.contains("graphql(<backend>)"), "{refusal}");
  assert!(refusal.contains("did you mean"), "{refusal}");
}