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
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
//! 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::tower::TowerModel;

/// The pass under test, over a declaration written the way the derive took one.
fn parse(input: &syn::DeriveInput) -> syn::Result<TowerModel> {
  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) -> TowerModel {
  parse(&syn::parse2(input).expect("the declaration parses")).expect("the declaration is a tower")
}

/// 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 WRITTEN type as the model carries it — the assertion that a word was read AND that it was
/// there to read.
fn wrote(ty: Option<&syn::Type>) -> String {
  spelled(ty.expect("the word was written"))
}

/// THE WHOLE GRAMMAR in one declaration: the full-form question, fields as steps, an answer apiece,
/// and the face word on the step that needs it.
#[test]
fn a_tower_declares_its_question_and_its_steps_are_its_fields() {
  let model = accepted(quote::quote! {
    #[ingraph(question(type = "String"), crate = "other::ingraph")]
    pub struct ClipText {
      #[ingraph(answer = "Vec<String>")]
      tokenize: Tokenize,
      #[ingraph(answer = "Vec<f32>", async)]
      encode: ClipTextTower,
    }
  });

  assert_eq!(model.name, "ClipText");
  assert_eq!(wrote(model.question.as_ref()), "String");
  let krate = &model.krate;
  assert_eq!(
    quote::quote!(#krate).to_string().replace(' ', ""),
    "other::ingraph"
  );

  assert_eq!(model.steps.len(), 2);
  assert_eq!(model.steps[0].field, "tokenize");
  assert_eq!(spelled(&model.steps[0].ty), "Tokenize");
  assert_eq!(wrote(model.steps[0].answer.as_ref()), "Vec<String>");
  assert!(!model.steps[0].awaited);
  assert_eq!(model.steps[1].field, "encode");
  assert!(model.steps[1].awaited);
  assert!(model.awaited(), "the OR law reads the steps' own words");
}

/// The SUGAR fills the same seat as the full form, at BOTH of the tower's ends.
#[test]
fn each_end_words_sugar_is_its_full_form() {
  let sugar = accepted(quote::quote! {
    #[ingraph(question = "crate::ask::Text", answer = "Vec<f32>")]
    pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
  });
  assert_eq!(wrote(sugar.question.as_ref()), "crate::ask::Text");
  assert_eq!(wrote(sugar.answer.as_ref()), "Vec<f32>");

  let full = accepted(quote::quote! {
    #[ingraph(question(type = "crate::ask::Text"), answer(type = "Vec<f32>"))]
    pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
  });
  assert_eq!(wrote(full.question.as_ref()), "crate::ask::Text");
  assert_eq!(wrote(full.answer.as_ref()), "Vec<f32>");
}

/// **THE HEAD PIN IS OPTIONAL, and its absence is the whole road** — no projection stands in for it,
/// because a declaration that stated no intent has none to check.
#[test]
fn a_tower_may_write_no_head_answer() {
  let model = accepted(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "Vec<f32>")] one: One }
  });
  assert!(model.answer.is_none());
}

/// **THE TWO `answer`s ARE TWO WORDS.** One is at the tower and one is beside a step, and a
/// one-step tower writing both fills two seats rather than one.
#[test]
fn the_head_answer_and_a_steps_answer_are_separate_seats() {
  let model = accepted(quote::quote! {
    #[ingraph(answer = "Vec<f32>")]
    pub struct T { #[ingraph(answer = "Vec<f64>")] one: One }
  });
  assert_eq!(wrote(model.answer.as_ref()), "Vec<f32>");
  assert_eq!(wrote(model.steps[0].answer.as_ref()), "Vec<f64>");
}

/// A tower whose every step is bare is wholly blocking — the OR law's other end, read off the model.
#[test]
fn a_tower_with_no_face_word_is_wholly_blocking() {
  let model = accepted(quote::quote! {
    #[ingraph(question = "String")]
    pub struct Rewrite {
      #[ingraph(answer = "String")] fold: Fold,
      #[ingraph(answer = "String")] expand: Expand,
    }
  });
  assert!(!model.awaited());
}

/// The declaration's VISIBILITY is carried, the walk taking it.
#[test]
fn the_declarations_visibility_is_carried() {
  let model = accepted(quote::quote! {
    #[ingraph(question = "String")]
    pub(crate) struct T { #[ingraph(answer = "String")] one: One }
  });
  let vis = &model.vis;
  assert_eq!(
    quote::quote!(#vis).to_string().replace(' ', ""),
    "pub(crate)"
  );
}

/// **THE WHOLLY CANONICAL TOWER**: no question, no answer, no attribute of any kind — the shape a
/// declaration takes when every one of its steps has already said what it is for.
#[test]
fn a_tower_may_write_neither_type_word() {
  let model = accepted(quote::quote! {
    pub struct Words {
      tokenize: Tokenize,
      #[ingraph(async)]
      encode: Encode,
    }
  });
  assert!(model.question.is_none(), "absence is a road, not a gap");
  assert_eq!(model.steps.len(), 2);
  assert!(model.steps.iter().all(|step| step.answer.is_none()));
  assert!(model.steps[1].awaited, "the face word still selects");
}

/// **THE WORDS ARE INDEPENDENT**: written on the tower and omitted on a step, or the other way
/// round, and one step writing its answer beside another that does not.
#[test]
fn each_type_word_is_omitted_on_its_own() {
  let half = accepted(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T {
      one: One,
      two: Two,
    }
  });
  assert_eq!(wrote(half.question.as_ref()), "String");
  assert!(half.steps.iter().all(|step| step.answer.is_none()));

  let mixed = accepted(quote::quote! {
    pub struct T {
      #[ingraph(answer = "Vec<String>")] one: One,
      two: Two,
    }
  });
  assert!(mixed.question.is_none());
  assert_eq!(wrote(mixed.steps[0].answer.as_ref()), "Vec<String>");
  assert!(mixed.steps[1].answer.is_none());
}

/// A TOWER WITH NO STEP has no chain to state.
#[test]
fn an_empty_tower_is_refused() {
  for input in [
    quote::quote! {
      #[ingraph(question = "String")]
      pub struct T {}
    },
    quote::quote! {
      #[ingraph(question = "String")]
      pub struct T;
    },
  ] {
    let refusal = refused(input);
    assert!(refusal.contains("a tower declares no step"), "{refusal}");
  }
}

/// A POSITIONAL field has no name, and a step's name is its field's.
#[test]
fn a_tuple_struct_is_refused() {
  let refusal = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T(One, Two);
  });
  assert!(refusal.contains("NAMED fields"), "{refusal}");
}

/// VARIANTS are a choice between things rather than a chain of them.
#[test]
fn an_enum_is_refused() {
  let refusal = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub enum T { A, B }
  });
  assert!(refusal.contains("supports structs only"), "{refusal}");
}

/// A GENERIC tower would prove its connectivity for nobody.
#[test]
fn a_generic_tower_is_refused() {
  for input in [
    quote::quote! {
      #[ingraph(question = "String")]
      pub struct T<S> { #[ingraph(answer = "String")] one: S }
    },
    quote::quote! {
      #[ingraph(question = "String")]
      pub struct T where One: Sized { #[ingraph(answer = "String")] one: One }
    },
  ] {
    let refusal = refused(input);
    assert!(refusal.contains("no generic parameter"), "{refusal}");
  }
}

/// `async` is a BARE word — the body is the step author's.
#[test]
fn a_configured_face_word_is_refused() {
  for word in [quote::quote!(async = "true"), quote::quote!(async(spawn))] {
    let refusal = refused(quote::quote! {
      #[ingraph(question = "String")]
      pub struct T { #[ingraph(answer = "String", #word)] one: One }
    });
    assert!(refusal.contains("`async` is a bare word here"), "{refusal}");
  }
}

/// One seat, one filling — at both grains, and in either spelling of the question.
#[test]
fn a_second_word_in_one_seat_is_refused() {
  let question = refused(quote::quote! {
    #[ingraph(question = "String", question(type = "usize"))]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    question.contains("`question` is declared more than once"),
    "{question}"
  );

  let answer = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "String", answer = "usize")] one: One }
  });
  assert!(
    answer.contains("`answer` is declared more than once on this step"),
    "{answer}"
  );

  let pin = refused(quote::quote! {
    #[ingraph(answer = "String", answer(type = "usize"))]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    pin.contains("`answer` is declared more than once at the tower"),
    "{pin}"
  );

  let face = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "String", async, async)] one: One }
  });
  assert!(
    face.contains("`async` is declared more than once"),
    "{face}"
  );
}

/// An END WORD that names no type, in each of the three shapes that name none — at both ends.
#[test]
fn an_end_word_naming_no_type_is_refused() {
  for (word, head) in [
    ("question", quote::quote!(question)),
    ("question", quote::quote!(question())),
    ("question", quote::quote!(question(crate = "x"))),
    ("answer", quote::quote!(answer)),
    ("answer", quote::quote!(answer())),
    ("answer", quote::quote!(answer(crate = "x"))),
  ] {
    let refusal = refused(quote::quote! {
      #[ingraph(#head)]
      pub struct T { #[ingraph(answer = "String")] one: One }
    });
    assert!(
      refusal.contains(&format!("`{word}` names no type"))
        || refusal.contains(&format!("unknown entry in `{word}")),
      "{refusal}"
    );
  }
}

/// The head pin's own sentences say it DECIDES nothing, which is what keeps a reader from writing it
/// as an override and expecting the tower's answer to follow.
#[test]
fn the_head_pins_sentences_say_it_checks_rather_than_decides() {
  let empty = refused(quote::quote! {
    #[ingraph(answer)]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    empty.contains("whatever its last step arrives at"),
    "{empty}"
  );

  let inner = refused(quote::quote! {
    #[ingraph(answer(rename = "x"))]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(inner.contains("unknown entry in `answer(…)`"), "{inner}");
  assert!(inner.contains("DECIDES nothing"), "{inner}");
}

/// The two positions' own vocabularies, each refused with the words it takes.
#[test]
fn an_unknown_entry_is_refused_with_this_positions_words() {
  let tower = refused(quote::quote! {
    #[ingraph(question = "String", steps = "one")]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    tower.contains("unknown entry in `#[ingraph::tower(…)]`"),
    "{tower}"
  );

  let step = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "String", question = "usize")] one: One }
  });
  assert!(
    step.contains("unknown entry in `#[ingraph(…)]` on a step"),
    "{step}"
  );
}

/// **THE RETIREMENT.** `tower(arm(chain(…)))` was written on an ELECTION; both of its inner words
/// are refused BY NAME, and the sentence says where the form went.
#[test]
fn the_retired_inline_form_is_refused_by_name() {
  for word in [quote::quote!(arm(text)), quote::quote!(chain(a, b))] {
    let refusal = refused(quote::quote! {
      #[ingraph(question = "String", #word)]
      pub struct T { #[ingraph(answer = "String")] one: One }
    });
    assert!(refusal.contains("RETIRED inline tower form"), "{refusal}");
    assert!(refusal.contains("search recipe"), "{refusal}");
  }
}

/// The retired form's NAMING word, refused with the reason fields-are-steps deleted it.
#[test]
fn the_retired_naming_word_is_refused_by_name() {
  let refusal = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "String", name = "first")] one: One }
  });
  assert!(refusal.contains("retired inline form's word"), "{refusal}");
  assert!(
    refusal.contains("`one` already IS this step's name"),
    "{refusal}"
  );
}

/// **THE RESERVED SEAT.** An options word on a step is refused with the LAW rather than as an
/// unknown: the seat is the SERVER's, and the emitted `<Tower>StepOptions` carries the row already.
#[test]
fn an_options_word_on_a_step_is_refused_with_the_seat_law() {
  let refusal = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = "String", options(limit = 5))] one: One }
  });
  assert!(refusal.contains("`Serve::Options`"), "{refusal}");
  assert!(
    refusal.contains("carries one row per step already"),
    "{refusal}"
  );
  assert!(
    refusal.contains("seats `()` takes no knobs at all"),
    "{refusal}"
  );
  assert!(refusal.contains("naming knobs nothing reads"), "{refusal}");
  assert!(refusal.contains("`one`"), "{refusal}");
  assert!(
    !refusal.contains("unknown entry"),
    "refused with the LAW, not as an unknown: {refusal}"
  );
}

/// A VALUE that is not a quoted type, at every grain that takes one — and one that is a string
/// holding something no type could be.
#[test]
fn an_unquoted_type_is_refused() {
  let question = refused(quote::quote! {
    #[ingraph(question = String)]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(question.contains("quoted"), "{question}");

  let answer = refused(quote::quote! {
    #[ingraph(question = "String")]
    pub struct T { #[ingraph(answer = String)] one: One }
  });
  assert!(answer.contains("quoted"), "{answer}");

  let pin = refused(quote::quote! {
    #[ingraph(answer = Vec<f32>)]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    pin.contains("`answer` names the TYPE the whole tower is pinned to answer"),
    "{pin}"
  );

  let unparsable = refused(quote::quote! {
    #[ingraph(answer = "not a type at all")]
    pub struct T { #[ingraph(answer = "String")] one: One }
  });
  assert!(
    unparsable.contains("`answer` names the TYPE the whole tower is pinned to answer"),
    "{unparsable}"
  );
}

/// EVERY rule the pass can still see is reported, not the first — one compile, the whole
/// declaration.
#[test]
fn one_compile_reports_every_rule_it_can_still_see() {
  let refusal = refused(quote::quote! {
    #[ingraph(question)]
    pub struct T<S> {
      #[ingraph(name = "first")] one: One,
      #[ingraph(async = "true")] two: S,
    }
  });
  assert!(refusal.contains("`question` names no type"), "{refusal}");
  assert!(
    refusal.contains("`one` already IS this step's name"),
    "{refusal}"
  );
  assert!(refusal.contains("`async` is a bare word here"), "{refusal}");
  assert!(refusal.contains("no generic parameter"), "{refusal}");
}

// ── THE FAN SEGMENTS ──────────────────────────────────────────────────────────────────────────
//
// The amendment's two words: what each one puts in the model, the stage order between them, and the
// face word a segment has no server for.

/// The fan a step opened, or a panic naming the step that was supposed to have one.
fn fan(model: &TowerModel, at: usize) -> &ingraph_codegen::tower::Fan {
  model.steps[at]
    .fan
    .as_ref()
    .expect("this step opened a segment")
}

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

/// **② REACHES THE MODEL**, in both of its spellings, and neither omits anything.
#[test]
fn a_widening_names_the_expander_it_defaults_to() {
  for written in [
    quote::quote! {
      pub struct T { #[ingraph(expand = "app::Synonyms", answer = "String")] widen: Widen }
    },
    quote::quote! {
      pub struct T { #[ingraph(expand(type = "app::Synonyms"), answer = "String")] widen: Widen }
    },
  ] {
    let model = accepted(written);
    let ingraph_codegen::tower::Fan::Expand { expander } = fan(&model, 0) else {
      panic!("the step opened a WIDENING");
    };
    assert_eq!(path(expander), "app::Synonyms");
    assert_eq!(fan(&model, 0).word(), "expand");
    assert_eq!(
      fan(&model, 0).stage(),
      ingraph_codegen::tower::Stage::Expand
    );
    assert!(model.fanned());
  }
}

/// **④ REACHES THE MODEL** with BOTH halves of its wiring, shipped-bare and declaring-crate-quoted.
#[test]
fn a_crossing_names_its_router_and_the_slot_it_is_wired_to() {
  let model = accepted(quote::quote! {
    pub struct T {
      #[ingraph(route(router = script_weighted_fan, slot = "app::BodySlot"), answer = "String")]
      fan: Fan
    }
  });
  let ingraph_codegen::tower::Fan::Route { router, slot } = fan(&model, 0) else {
    panic!("the step opened a CROSSING");
  };
  assert_eq!(router.as_written(), "script_weighted_fan");
  assert_eq!(path(slot), "app::BodySlot");
  assert_eq!(fan(&model, 0).word(), "route");
  assert_eq!(fan(&model, 0).stage(), ingraph_codegen::tower::Stage::Route);

  let declared = accepted(quote::quote! {
    pub struct T {
      #[ingraph(route(slot = "app::BodySlot", router = "app::MyRouter"))]
      fan: Fan
    }
  });
  let ingraph_codegen::tower::Fan::Route { router, .. } = fan(&declared, 0) else {
    panic!("the step opened a CROSSING");
  };
  assert!(
    matches!(router, ingraph_codegen::tower::Router::Declared(_)),
    "a quoted router is the declaring crate's",
  );
  assert_eq!(router.as_written().replace(' ', ""), "app::MyRouter");
}

/// EVERY SHIPPED BARE WORD binds, and the roster a refusal prints is the one that binds.
#[test]
fn every_shipped_router_has_its_bare_word() {
  for word in ingraph_codegen::tower::Router::SHIPPED {
    let written: proc_macro2::TokenStream =
      format!("pub struct T {{ #[ingraph(route(router = {word}, slot = \"app::S\"))] fan: Fan }}")
        .parse()
        .expect("the declaration parses");
    let model = accepted(written);
    let ingraph_codegen::tower::Fan::Route { router, .. } = fan(&model, 0) else {
      panic!("the step opened a CROSSING");
    };
    assert_eq!(router.as_written(), word);
  }
}

/// **A TOWER THAT WRITES NO FAN WORD OPENS NO SEGMENT** — the degenerate case, read off the model.
#[test]
fn a_tower_with_no_fan_word_is_the_degenerate_case() {
  let model = accepted(quote::quote! {
    #[ingraph(question = "String")]
    pub struct ClipText {
      #[ingraph(answer = "Vec<String>")] tokenize: Tokenize,
      #[ingraph(answer = "Vec<f32>", async)] encode: Encode,
    }
  });
  assert!(!model.fanned());
  assert_eq!(model.fans().count(), 0);
  assert!(model.steps.iter().all(|step| step.fan.is_none()));
}

/// **THE SUGAR THE CROSSING DOES NOT HAVE**, refused by name pointing at the full form.
#[test]
fn a_crossing_has_no_short_spelling() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(route = script_weighted_fan)] fan: Fan }
  });
  assert!(refusal.contains("has no short spelling"), "{refusal}");
  assert!(refusal.contains("EQUALITY"), "{refusal}");
  assert!(
    !refusal.contains("unknown"),
    "refused with the LAW, not as an unknown: {refusal}"
  );
}

/// Both halves of the wiring are DEMANDED, one sentence apiece.
#[test]
fn a_crossing_names_both_halves_or_is_refused() {
  let bare = refused(quote::quote! {
    pub struct T { #[ingraph(route)] fan: Fan }
  });
  assert!(bare.contains("`route` names no property"), "{bare}");

  let empty = refused(quote::quote! {
    pub struct T { #[ingraph(route())] fan: Fan }
  });
  assert!(empty.contains("`route` names no property"), "{empty}");

  let no_router = refused(quote::quote! {
    pub struct T { #[ingraph(route(slot = "app::S"))] fan: Fan }
  });
  assert!(no_router.contains("names no `router`"), "{no_router}");

  let no_slot = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = fan_all))] fan: Fan }
  });
  assert!(no_slot.contains("names no `slot`"), "{no_slot}");
}

/// **MINT BARE, REFER QUOTED** — an unshipped bare word names the roster AND the quoted escape.
#[test]
fn an_unshipped_router_names_the_roster_and_the_escape() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = script_wieghted_fan, slot = "app::S"))] fan: Fan }
  });
  assert!(
    refusal.contains("is not a router this framework ships"),
    "{refusal}"
  );
  assert!(refusal.contains("`script_weighted_fan`"), "{refusal}");
  assert!(refusal.contains("arrives QUOTED"), "{refusal}");
  assert!(
    refusal.contains("did you mean `script_weighted_fan`"),
    "the near-miss list is the shipped roster: {refusal}"
  );
}

/// A property this entry does not know is refused AS AN UNKNOWN, with the seat's own vocabulary.
#[test]
fn an_unknown_crossing_property_names_this_positions_words() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = fan_all, slt = "app::S"))] fan: Fan }
  });
  assert!(
    refusal.contains("unknown property in `route(…)`"),
    "{refusal}"
  );
  assert!(refusal.contains("did you mean `slot`"), "{refusal}");
  assert!(
    !refusal.contains("names no `slot`"),
    "an absence check must not run over a word the reader could not place: {refusal}"
  );
}

/// The three second-declaration refusals, one per grain.
#[test]
fn one_of_each_word_and_one_of_each_property() {
  let expand = refused(quote::quote! {
    pub struct T { #[ingraph(expand = "app::A", expand = "app::B")] widen: Widen }
  });
  assert!(
    expand.contains("`expand` is declared more than once on this step"),
    "{expand}"
  );

  let route = refused(quote::quote! {
    pub struct T {
      #[ingraph(route(router = fan_all, slot = "app::S"), route(router = fan_all, slot = "app::S"))]
      fan: Fan
    }
  });
  assert!(
    route.contains("`route` is declared more than once on `fan`"),
    "{route}"
  );

  let property = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = fan_all, router = fan_all, slot = "app::S"))] fan: Fan }
  });
  assert!(
    property.contains("`router` is declared more than once in `route(…)`"),
    "{property}"
  );
}

/// **ONE STEP OPENS ONE SEGMENT** — two segment words on one field is refused with the stage law,
/// and the refusal names the words that were actually written.
#[test]
fn a_step_opens_one_segment() {
  let refusal = refused(quote::quote! {
    pub struct T {
      #[ingraph(expand = "app::A", route(router = fan_all, slot = "app::S"))]
      both: Both
    }
  });
  assert!(
    refusal.contains("`both` writes `expand` and `route`"),
    "{refusal}"
  );

  // ⟂ JOINS THE LAW rather than earning a second one, and the pair it makes is named the same way.
  let enriched = refused(quote::quote! {
    pub struct T {
      #[ingraph(annotate = "app::Tag", expand = "app::A")]
      both: Both
    }
  });
  assert!(
    enriched.contains("`both` writes `annotate` and `expand`"),
    "{enriched}"
  );

  // THREE WORDS IS ONE MISTAKE — the pairs they make are not reported as three.
  let all = refused(quote::quote! {
    pub struct T {
      #[ingraph(annotate = "app::Tag", expand = "app::A", route(router = fan_all, slot = "app::S"))]
      every: Every
    }
  });
  assert_eq!(
    all.matches("opens ONE segment").count(),
    1,
    "one law, said once, however many words a step wrote: {all}"
  );
  assert!(
    all.contains("`every` writes `annotate` and `expand` and `route`"),
    "{all}"
  );
}

/// **ONE SEGMENT OF EACH KIND PER TOWER**, and the refusal names the step that already opened it.
#[test]
fn a_tower_opens_one_segment_of_each_kind() {
  let widened = refused(quote::quote! {
    pub struct T {
      #[ingraph(expand = "app::A")] first: One,
      #[ingraph(expand = "app::B")] second: Two,
    }
  });
  assert!(
    widened.contains("`expand` is already open at `first`"),
    "{widened}"
  );
  assert!(widened.contains("ONE question"), "{widened}");

  let crossed = refused(quote::quote! {
    pub struct T {
      #[ingraph(route(router = fan_all, slot = "app::S"))] first: One,
      #[ingraph(route(router = fan_all, slot = "app::S"))] second: Two,
    }
  });
  assert!(
    crossed.contains("`route` is already open at `first`"),
    "{crossed}"
  );
  assert!(crossed.contains("ONE lane fan"), "{crossed}");
}

/// **② BEFORE ④**, refused BY NAME and never silently reordered.
#[test]
fn the_stages_run_in_the_order_the_pipeline_has() {
  let refusal = refused(quote::quote! {
    pub struct T {
      #[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
      #[ingraph(expand = "app::A")] widen: Widen,
    }
  });
  assert!(
    refusal.contains("`route` on `fan` comes before `expand` on `widen`"),
    "{refusal}"
  );
  assert!(refusal.contains("Move the step"), "{refusal}");

  let ordered = accepted(quote::quote! {
    pub struct T {
      #[ingraph(expand = "app::A")] widen: Widen,
      #[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
    }
  });
  assert_eq!(ordered.fans().count(), 2);
  assert_eq!(
    ordered.fans().map(|(at, ..)| at).collect::<Vec<_>>(),
    vec![0, 1],
    "the position IS the step's own index",
  );
}

/// **THE EXPANDER HAS NO BARE WORD**, and the refusal says why rather than offering a near miss.
#[test]
fn a_widening_names_a_type_and_never_a_word() {
  let bare = refused(quote::quote! {
    pub struct T { #[ingraph(expand)] widen: Widen }
  });
  assert!(bare.contains("`expand` names no type"), "{bare}");
  assert!(bare.contains("ships no expander"), "{bare}");

  let unquoted = refused(quote::quote! {
    pub struct T { #[ingraph(expand = synonyms)] widen: Widen }
  });
  assert!(unquoted.contains("quoted"), "{unquoted}");

  let inner = refused(quote::quote! {
    pub struct T { #[ingraph(expand(typ = "app::A"))] widen: Widen }
  });
  assert!(inner.contains("unknown entry in `expand(…)`"), "{inner}");
  assert!(inner.contains("did you mean `type`"), "{inner}");
}

/// **THE FALSE ABSENCE, CURED AT THE READER ALL THREE TYPE WORDS SHARE.**
///
/// A block whose only entry the reader could not place has not said *no type*: the word it could
/// not read may have been the very one it wanted. So one typo earns one sentence, at every position
/// this reader serves — the tower's two ENDS included, which is where the defect lived before the
/// fan words made the reader shared.
#[test]
fn a_type_word_claims_no_absence_it_could_not_check() {
  for written in [
    quote::quote! {
      #[ingraph(question(typ = "String"))]
      pub struct T { #[ingraph(answer = "String")] one: One }
    },
    quote::quote! {
      #[ingraph(answer(typ = "String"))]
      pub struct T { #[ingraph(answer = "String")] one: One }
    },
    quote::quote! {
      pub struct T { #[ingraph(expand(typ = "app::A"))] widen: Widen }
    },
  ] {
    let refusal = refused(written);
    assert!(refusal.contains("unknown entry"), "{refusal}");
    assert!(!refusal.contains("names no type"), "{refusal}");
  }
}

/// A `slot` that is not a quoted path is refused with the law rather than by syn.
#[test]
fn a_slot_is_a_quoted_path() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = fan_all, slot = BodySlot))] fan: Fan }
  });
  assert!(refusal.contains("`slot = \"path::to::Slot\"`"), "{refusal}");
  assert!(refusal.contains("QUOTED"), "{refusal}");
}

/// A `router` carrying a tail is told the RULE rather than syn's punctuation sentence.
#[test]
fn a_router_word_is_the_whole_of_what_it_carries() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(route(router = fan_all(2), slot = "app::S"))] fan: Fan }
  });
  assert!(
    refusal.contains("is a shipped router's BARE word or the declaring crate's own QUOTED path"),
    "{refusal}"
  );
}

/// The step's near-miss list GREW with the vocabulary, so a typo of a stage word is corrected.
#[test]
fn a_steps_vocabulary_names_the_two_fan_words() {
  let refusal = refused(quote::quote! {
    pub struct T { #[ingraph(rout(router = fan_all, slot = "app::S"))] fan: Fan }
  });
  assert!(refusal.contains("did you mean `route`"), "{refusal}");

  let widen = refused(quote::quote! {
    pub struct T { #[ingraph(expnd = "app::A")] widen: Widen }
  });
  assert!(widen.contains("did you mean `expand`"), "{widen}");

  let annotated = refused(quote::quote! {
    pub struct T { #[ingraph(anotate = "app::Tag")] detect: Detect }
  });
  assert!(annotated.contains("did you mean `annotate`"), "{annotated}");
}

/// **`async` BESIDE A FAN WORD IS REFUSED**, at both segments and for each one's own reason.
///
/// The refusal the fan WALK made checkable: a segment's field holds the segment's SEAT, and a seat
/// answers no `serve` call for a face to be about. It names which segment, so a reader is told why
/// the face is not theirs to choose rather than only that it is not.
#[test]
fn a_fan_segment_wears_no_face() {
  let widening = refused(quote::quote! {
    pub struct T { #[ingraph(expand = "app::Synonyms", async)] widen: Widen }
  });
  assert!(widening.contains("opens the WIDENING"), "{widening}");
  assert!(widening.contains("through the lift bridge"), "{widening}");
  assert!(widening.contains("Take the word off"), "{widening}");

  let crossing = refused(quote::quote! {
    pub struct T {
      #[ingraph(route(router = fan_all, slot = "app::S"), async)] fan: Fan
    }
  });
  assert!(crossing.contains("opens the CROSSING"), "{crossing}");
  assert!(
    crossing.contains("one face and it is the blocking one"),
    "{crossing}"
  );

  // AND A STEP THAT OPENS NO SEGMENT still wears whichever face it wrote.
  let plain = accepted(quote::quote! {
    pub struct T { #[ingraph(async)] encode: Encode }
  });
  assert!(plain.steps[0].awaited);
  assert!(plain.steps[0].fan.is_none());
}

/// ⟂ **THE ANNOTATION WORD**, in both of its spellings — one property, so neither omits anything.
#[test]
fn an_annotation_names_the_type_it_writes_into_the_question() {
  let sugar = accepted(quote::quote! {
    pub struct T { #[ingraph(annotate = "app::LanguageTag")] detect: Detect }
  });
  assert_eq!(
    wrote(sugar.steps[0].annotation.as_ref()),
    "app::LanguageTag"
  );
  assert_eq!(
    sugar.steps[0].stage(),
    Some(ingraph_codegen::tower::Stage::Annotate)
  );
  assert!(
    sugar.steps[0].fan.is_none(),
    "an annotation opens no FAN — the model's two seats say two different things"
  );
  assert_eq!(sugar.annotate_at(), Some(0));

  let full = accepted(quote::quote! {
    pub struct T { #[ingraph(annotate(type = "app::LanguageTag"))] detect: Detect }
  });
  assert_eq!(wrote(full.steps[0].annotation.as_ref()), "app::LanguageTag");

  // A tower that writes no annotation word answers `None` at every seat and at the roster.
  let plain = accepted(quote::quote! {
    pub struct T { encode: Encode }
  });
  assert!(plain.steps[0].annotation.is_none());
  assert!(plain.steps[0].stage().is_none());
  assert!(plain.annotate_at().is_none());
}

/// ⟂ **THE FOUR WAYS TO GET A ONE-TYPE WORD WRONG**, at the position that just grew one.
#[test]
fn an_annotation_that_names_no_type_is_refused() {
  let bare = refused(quote::quote! {
    pub struct T { #[ingraph(annotate)] detect: Detect }
  });
  assert!(bare.contains("`annotate` names no type"), "{bare}");
  assert!(
    bare.contains("the declaring crate's own type"),
    "no bare vocabulary here, and the sentence says why: {bare}"
  );

  let unquoted = refused(quote::quote! {
    pub struct T { #[ingraph(annotate = LanguageTag)] detect: Detect }
  });
  assert!(unquoted.contains("quoted"), "{unquoted}");

  let twice = refused(quote::quote! {
    pub struct T { #[ingraph(annotate = "app::A", annotate(type = "app::B"))] detect: Detect }
  });
  assert!(
    twice.contains("`annotate` is declared more than once"),
    "the sugar and the full form fill ONE seat: {twice}"
  );

  let inner = refused(quote::quote! {
    pub struct T { #[ingraph(annotate(typ = "app::A"))] detect: Detect }
  });
  assert!(inner.contains("unknown entry in `annotate(…)`"), "{inner}");
  assert!(inner.contains("did you mean `type`"), "{inner}");
}

/// ⟂ **`async` IS THE STAGE'S OWN WORD**, because the field holds a SERVER and not a seat.
///
/// The one place the annotation stage and the two fan words part company at the face: an LID reached
/// through a `Door` onto a graph resident wears the awaited face, and the word that says so is the
/// same one every other step writes.
#[test]
fn an_annotation_step_wears_the_face_its_server_does() {
  let awaited = accepted(quote::quote! {
    pub struct T { #[ingraph(annotate = "app::LanguageTag", async)] detect: Detect }
  });
  assert!(awaited.steps[0].awaited);
  assert!(awaited.steps[0].annotation.is_some());

  let blocking = accepted(quote::quote! {
    pub struct T { #[ingraph(annotate = "app::LanguageTag")] detect: Detect }
  });
  assert!(!blocking.steps[0].awaited);
}

/// ⟂ **`answer` DECIDES NOTHING HERE**, and is refused by name rather than read as a second
/// spelling of the question one line above.
#[test]
fn an_annotation_step_answers_the_question_it_was_given() {
  let refusal = refused(quote::quote! {
    pub struct T {
      #[ingraph(annotate = "app::LanguageTag", answer = "app::Ask")] detect: Detect
    }
  });
  assert!(
    refusal.contains("`answer` decides nothing on `detect`"),
    "{refusal}"
  );
  assert!(
    refusal.contains("hands back the question it was given"),
    "{refusal}"
  );

  // WRITTEN THE OTHER WAY ROUND is the same one mistake — the seat is read after the whole walk.
  let reversed = refused(quote::quote! {
    pub struct T {
      #[ingraph(answer = "app::Ask", annotate = "app::LanguageTag")] detect: Detect
    }
  });
  assert!(
    reversed.contains("`answer` decides nothing on `detect`"),
    "{reversed}"
  );
}

/// ⟂ **RUNS BEFORE ② AND ④**, and out of order is refused NAMING BOTH STEPS with the annotation's
/// own reason rather than the widening's.
#[test]
fn the_annotation_stage_runs_before_every_other() {
  let after_widening = refused(quote::quote! {
    pub struct T {
      #[ingraph(expand = "app::A")] widen: Widen,
      #[ingraph(annotate = "app::Tag")] detect: Detect,
    }
  });
  assert!(
    after_widening.contains("`expand` on `widen` comes before `annotate` on `detect`"),
    "{after_widening}"
  );
  assert!(
    after_widening.contains("the stages after it READ what it wrote"),
    "the reason belongs to the stage that was written late: {after_widening}"
  );

  let after_crossing = refused(quote::quote! {
    pub struct T {
      #[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
      #[ingraph(annotate = "app::Tag")] detect: Detect,
    }
  });
  assert!(
    after_crossing.contains("`route` on `fan` comes before `annotate` on `detect`"),
    "{after_crossing}"
  );

  // THE ORDER WRITTEN RIGHT is three stages in one tower, and the roster is in declaration order.
  let ordered = accepted(quote::quote! {
    pub struct T {
      #[ingraph(annotate = "app::Tag")] detect: Detect,
      #[ingraph(expand = "app::A")] widen: Widen,
      #[ingraph(route(router = fan_all, slot = "app::S"))] fan: Fan,
    }
  });
  assert_eq!(ordered.annotate_at(), Some(0));
  assert_eq!(ordered.expand_at(), Some(1));
  assert_eq!(ordered.route_at(), Some(2));
}

/// ⟂ **ONE ANNOTATION STAGE PER TOWER**, and its refusal states the reason a SECOND one is not
/// merely unlawful but dead.
#[test]
fn a_tower_annotates_at_one_stage() {
  let refusal = refused(quote::quote! {
    pub struct T {
      #[ingraph(annotate = "app::Tag")] first: One,
      #[ingraph(annotate = "app::Intent")] second: Two,
    }
  });
  assert!(
    refusal.contains("`annotate` is already open at `first`"),
    "{refusal}"
  );
  assert!(refusal.contains("which is nowhere"), "{refusal}");
}