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
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
//! Every entry this grammar takes, pinned to the model it builds — and every rule it states, put to
//! a declaration that breaks it.
//!
//! 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.
//!
//! The PARSE PINS are the other half, and the emission's arrival did not retire them. They were
//! written when nothing emitted, so the model was the only witness that a word had been read at all;
//! what they are now is the front end's OWN claim, held apart from the second grammar's. A carried
//! word travels UNINTERPRETED, so *it reached the model whole* and *the record pass then spent it*
//! are two facts with two owners, and reading the second off an expansion would leave the first
//! checked by nobody.
//!
//! What the emission added here is one pin and one line. `an_accepted_aggregate_mounts_a_record…`
//! replaced the staged endpoint's, 1 for 1, and the ruled line gained the execution word its clock
//! always owed — which is the census pin doing its job, the mistake having been invisible while the
//! poll words were carried unread.

use ingraph_codegen::aggregate::{AggregateModel, Measure};
use ingraph_codegen::prefix::EmissionPrefix;
use quote::ToTokens as _;

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

/// Parse a declaration, expecting the refusals it earns — each one on its own.
///
/// Beside [`refused`] rather than under it, because ONE MISTAKE, ONE ERROR is a claim about how MANY
/// sentences a declaration earned, and a joined string cannot be counted: a refusal is free to
/// contain a newline of its own.
fn refusals(input: proc_macro2::TokenStream) -> Vec<String> {
  parse(&syn::parse2(input).expect("the declaration parses"))
    .expect_err("this declaration breaks a rule")
    .into_iter()
    .map(|error| error.to_string())
    .collect()
}

/// Parse a declaration, expecting the refusals it earns — every one of them, joined.
fn refused(input: proc_macro2::TokenStream) -> String {
  refusals(input).join("\n")
}

/// The RULED LINE — the closed design's own example, spelled in this grammar's settled words.
fn ruled() -> proc_macro2::TokenStream {
  quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting),
      poll(recount, every = "10m", light, async(inline)),
      sqlite,
    )]
    pub struct CodecCensus {
      #[ingraph(group)]
      codec: Codec,
      #[ingraph(measure)]
      count: Count,
    }
  }
}

/// A token stream rendered without the spacing `to_string` puts in, for comparing what was carried.
fn tight(tokens: &proc_macro2::TokenStream) -> String {
  tokens.to_string().replace(' ', "")
}

/// **THE RULED LINE PARSES, and every word of it reaches the model.**
///
/// The single pin the whole grammar hangs off: one declaration, and each of the six head entries
/// read back where the emission will look for it.
#[test]
fn the_ruled_line_parses_and_every_entry_reaches_the_model() {
  let model = accepted(ruled());

  assert_eq!(model.aggregate, "CodecCensus");
  assert_eq!(
    model
      .upstream
      .to_token_stream()
      .to_string()
      .replace(' ', ""),
    "demux::Track",
    "the upstream travels as the path the declaration named"
  );
  assert_eq!(
    model.krate.to_token_stream().to_string().replace(' ', ""),
    "::ingraph"
  );
  assert!(model.krate_spelled.is_none(), "no `crate` was written");

  assert_eq!(model.groups.len(), 1);
  assert_eq!(model.groups[0].field, "codec");
  assert_eq!(model.groups[0].from, "codec");
  assert_eq!(model.groups[0].ty.to_token_stream().to_string(), "Codec");

  assert_eq!(model.measures.len(), 1);
  assert_eq!(model.measures[0].field, "count");
  assert!(matches!(model.measures[0].by, Measure::Counting));
  assert_eq!(model.measures[0].ty.to_token_stream().to_string(), "Count");

  assert_eq!(
    tight(&model.triggers),
    "poll(recount,every=\"10m\",light,async(inline))",
    "the clock is carried VERBATIM — its grammar is the record pass's, execution word and all"
  );
  assert_eq!(tight(&model.stores), "sqlite");
  assert!(model.graphql.is_none());
}

/// **THE ROW SHAPE IS GROUPS THEN MEASURES**, whichever order the declaration wrote its blocks in.
///
/// The key is a row's first columns everywhere else in this framework, and an aggregate's key is its
/// group columns — so the walk the emission mints a row from is the model's, not each reader's.
#[test]
fn the_row_is_its_groups_then_its_measures() {
  let model = accepted(quote::quote! {
    #[ingraph(
      measure(count = counting, bytes(fold = summing, from = "size")),
      group(codec = "codec", lang = "language"),
      upstream(node = "demux::Track"),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(measure)] count: Count,
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
      #[ingraph(group)] lang: Language,
    }
  });

  assert_eq!(
    model.columns().map(ToString::to_string).collect::<Vec<_>>(),
    ["codec", "lang", "count", "bytes"],
  );
  assert_eq!(
    model.groups[0].from, "codec",
    "a group entry carries the UPSTREAM's field name, not this declaration's"
  );
  assert_eq!(model.groups[1].from, "language");
}

/// **MINT BARE, REFER QUOTED** — the shipped folds and the open escape, in one declaration.
#[test]
fn a_measure_takes_a_shipped_word_bare_and_a_declared_fold_quoted() {
  let model = accepted(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting, bytes(fold = summing, from = "size"), median = "app::Median"),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      #[ingraph(measure)] bytes: u64,
      #[ingraph(measure)] median: f64,
    }
  });

  assert!(matches!(model.measures[0].by, Measure::Counting));
  assert!(matches!(model.measures[1].by, Measure::Summing));
  match &model.measures[2].by {
    Measure::Declared(path) => assert_eq!(
      path.to_token_stream().to_string().replace(' ', ""),
      "app::Median",
    ),
    other => panic!("a quoted fold is a declared one: {other:?}"),
  }
}

/// **THE THREE CARRIED WORDS TRAVEL WHOLE**, blocks and all — nothing here reads inside one.
#[test]
fn the_record_grammars_words_are_carried_verbatim() {
  let model = accepted(quote::quote! {
    #[ingraph(
      crate = "framework::ingraph",
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting),
      poll(recount, every = "10m", light),
      poll(nightly, every = "1d", light),
      sqlite,
      postgres(schema = "media"),
      table = "codec_census",
      rename = "CodecCensusRow",
      graphql(async_graphql),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert_eq!(
    tight(&model.triggers),
    "poll(recount,every=\"10m\",light),poll(nightly,every=\"1d\",light)",
    "both clocks are carried, comma-joined, in written order"
  );
  assert_eq!(
    tight(&model.stores),
    "sqlite,postgres(schema=\"media\"),table=\"codec_census\",rename=\"CodecCensusRow\"",
    "the store words and their two common entries travel together and unread"
  );
  assert_eq!(
    tight(model.graphql.as_ref().expect("a wire face was named")),
    "graphql(async_graphql)",
  );
  assert_eq!(
    model.krate.to_token_stream().to_string().replace(' ', ""),
    "framework::ingraph",
  );
  assert_eq!(
    model
      .krate_spelled
      .as_ref()
      .expect("the literal is kept beside the path")
      .value(),
    "framework::ingraph",
    "both halves are carried so the record pass is handed the string that was written",
  );
}

/// **THE PROJECTION WORDS ARE READ AND CARRIED**, on both roles.
#[test]
fn a_column_carries_the_projection_its_marker_declared() {
  let model = accepted(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure, copy)] count: Count,
    }
  });

  assert!(matches!(
    model.groups[0].projection,
    ingraph_codegen::model::Projection::Borrowed
  ));
  assert!(matches!(
    model.measures[0].projection,
    ingraph_codegen::model::Projection::Copied
  ));
}

/// **THE MOUNT** — an accepted declaration becomes a RECORD, keyed by its group projection.
///
/// The pin that replaced the staged endpoint when the emission landed. What it reads is the one
/// thing the mount DECIDES rather than carries: an aggregate's author writes every column and no
/// key, so `key = lineage ⊕ id` is minted over the group columns — a one-column group being a ROOT,
/// whose key is the id alone and which has no lineage type beside it.
///
/// The carried halves are read back on the same model, which is what proves they reached the second
/// grammar rather than being parsed and dropped: the store word is the record's own `persist`, and
/// the clock is a `poll(…)` entry with the name the declaration minted.
#[test]
fn an_accepted_aggregate_mounts_a_record_keyed_by_its_group_projection() {
  let input: syn::DeriveInput = syn::parse2(ruled()).expect("the declaration parses");
  let (host, arguments) = crate::host::split(&input);
  let model = super::parse(&host, &arguments).expect("the declaration is an aggregate");
  let mounted = super::mount(&host, model).expect("and it mounts a record");

  assert_eq!(
    mounted.record.kind,
    ingraph_codegen::model::NodeKind::Aggregate
  );
  assert_eq!(mounted.record.key.id.name.to_string(), "codec");
  assert!(
    mounted.record.lineage.is_none(),
    "one group column is a ROOT record: the key is the id alone",
  );
  assert!(
    mounted.record.persist.is_some(),
    "the carried store word reached the record grammar",
  );
  assert_eq!(
    mounted
      .record
      .polls
      .as_ref()
      .map(|polls| polls.entries.len()),
    Some(1),
    "and so did the clock",
  );
}

/// **A WRONG DECLARATION HEARS WHAT IS WRONG WITH IT**, and it hears it BEFORE the mount.
///
/// The order the staged endpoint's three postures were ruled on, kept now that the emission has
/// arrived: this grammar's own refusals are reported first, so a declaration that is wrong here
/// never reaches the record pass to be told something else instead.
#[test]
fn a_refused_aggregate_is_not_told_it_is_early() {
  let refusals = refused(quote::quote! {
    #[ingraph(group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(refusals.contains("`upstream(…)`"), "{refusals}");
  assert!(!refusals.contains("cannot be emitted yet"), "{refusals}");
}

/// **THE TRIGGERLESS VERDICT** — a clock is MANDATORY, and the sentence says why silence is worse.
#[test]
fn an_aggregate_with_no_clock_is_refused_by_name() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), sqlite)]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(refusals.contains("needs a `poll(…)` clock"), "{refusals}");
  assert!(
    refusals.contains("stays empty and answers nothing forever"),
    "the refusal says what silence would have cost: {refusals}"
  );
  assert!(
    refusals.contains("notify edge used as an ALARM"),
    "the recorded freshness door is named rather than left out: {refusals}"
  );
}

/// **THE UPSTREAM IS MANDATORY AND SINGULAR**, and the second one names the seat that blocks it.
#[test]
fn an_aggregate_folds_exactly_one_upstream() {
  let missing = refused(quote::quote! {
    #[ingraph(group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    missing.contains("folds the rows of ONE upstream"),
    "{missing}"
  );
  assert!(
    missing.contains("reads its upstream's TABLE rather than its messages"),
    "{missing}"
  );

  let twice = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      upstream(node = "demux::Chapter"),
      group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    twice.contains("`upstream` is declared more than once"),
    "{twice}"
  );
  assert!(
    twice.contains("single-upstream by ruling rather than by omission"),
    "the blocked seat is named: {twice}"
  );
}

/// **A PROJECTION INSIDE `upstream(…)` IS REFUSED POINTING AT `group(…)`** — the one place this
/// grammar deliberately differs from a derived index's, said in the sentence.
#[test]
fn a_projection_inside_the_upstream_is_sent_to_the_group_block() {
  let refusals = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track", codec = "codec"),
      group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("names the EDGE on an aggregate"),
    "{refusals}"
  );
  assert!(
    refusals.contains("`group(codec = \"<upstream field>\")`"),
    "{refusals}"
  );
  assert!(
    refusals.contains("a measure's column is FOLDED into being"),
    "the sentence says WHY the projection is not total here: {refusals}"
  );
}

/// **ZERO GROUPS AND ZERO MEASURES ARE BOTH REFUSED**, whether the block is absent or empty.
#[test]
fn an_aggregate_groups_something_and_measures_something() {
  let absent = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      codec: Codec,
    }
  });
  assert!(
    absent.contains("needs at least one `group(…)` entry"),
    "{absent}"
  );
  assert!(
    absent.contains("needs at least one `measure(…)` entry"),
    "{absent}"
  );

  let empty = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(), measure(), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      codec: Codec,
    }
  });
  assert!(
    empty.contains("needs at least one `group(…)` entry"),
    "{empty}"
  );
  assert!(
    empty.contains("needs at least one `measure(…)` entry"),
    "{empty}"
  );
}

/// **ONE FIELD, ONE ENTRY** — in either block.
#[test]
fn a_field_is_named_once_in_a_block() {
  let refusals = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec", codec = "codec_name"),
      measure(count = counting, count(fold = summing, from = "size")),
      poll(p, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("`codec` is named more than once in `group(…)`"),
    "{refusals}"
  );
  assert!(
    refusals.contains("`count` is named more than once in `measure(…)`"),
    "{refusals}"
  );
}

/// **EVERY FIELD WEARS EXACTLY ONE ROLE** — and both halves of that are refusals.
#[test]
fn every_field_wears_exactly_one_role() {
  let none = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      note: String,
    }
  });
  assert!(
    none.contains("`note` wears neither `#[ingraph(group)]` nor `#[ingraph(measure)]`"),
    "{none}"
  );

  let both = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group, measure)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    both.contains("wears both `#[ingraph(group)]` and `#[ingraph(measure)]`"),
    "{both}"
  );

  let twice = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group, group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    twice.contains("`group` is declared more than once on `codec` — one marker, one field"),
    "{twice}"
  );
}

/// **A ROLE MARKER IS BARE**, its presence being the whole of what it declares.
#[test]
fn a_role_marker_carries_nothing() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group(rename = "c"))] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(refusals.contains("`group` is a BARE marker"), "{refusals}");
}

/// **THE HEAD AND THE FIELD ARE CHECKED AGAINST EACH OTHER**, in both directions.
///
/// The whole return on writing each word twice: a disagreement is caught here rather than becoming a
/// row whose shape depends on which half was read.
#[test]
fn a_head_entry_and_a_marker_that_disagree_are_refused() {
  let crossed = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec", count = "n"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    crossed.contains("`count` is named in `group(…)` and wears `#[ingraph(measure)]`"),
    "{crossed}"
  );

  let unnamed = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(group)] lang: Language,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    unnamed.contains("`lang` wears `#[ingraph(group)]` and `group(…)` has no entry for it"),
    "{unnamed}"
  );
  assert!(
    unnamed.contains("`lang = \"<upstream field>\"`"),
    "the missing half is spelled out: {unnamed}"
  );

  let stranger = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting, bytes(fold = summing, from = "size")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    stranger.contains("`bytes` is not one of `CodecCensus`'s"),
    "{stranger}"
  );
}

/// **THE BARE FOLD VOCABULARY IS CLOSED, and the quoted one is not** — with a near miss on the
/// closed half.
#[test]
fn an_unshipped_bare_fold_is_refused_with_the_open_half_named() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = countng), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("is no fold this framework ships"),
    "{refusals}"
  );
  assert!(refusals.contains("`counting` and `summing`"), "{refusals}");
  assert!(refusals.contains("\"path::to::Fold\""), "{refusals}");
  assert!(
    refusals.contains("did you mean `counting`?"),
    "the near miss reaches the closed half: {refusals}"
  );
}

/// **THE FULL FORM REACHES THE MODEL**, both properties, on the entry that wrote them.
///
/// The citizen spelling: every property of one measure column named, and the column the fold reads
/// carried beside the fold that reads it.
#[test]
fn a_measure_entrys_full_form_carries_the_fold_and_the_column_it_reads() {
  let model = accepted(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(
        bytes(fold = summing, from = "size"),
        median(fold = "app::Median", from = "duration_ms"),
      ),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
      #[ingraph(measure)] median: f64,
    }
  });

  assert!(matches!(model.measures[0].by, Measure::Summing));
  assert_eq!(
    model.measures[0]
      .from
      .as_ref()
      .expect("the full form named the column"),
    "size",
    "a measure entry carries the UPSTREAM's column name, not this declaration's field name"
  );
  match &model.measures[1].by {
    Measure::Declared(path) => assert_eq!(
      path.to_token_stream().to_string().replace(' ', ""),
      "app::Median",
    ),
    other => panic!("a quoted fold is a declared one: {other:?}"),
  }
  assert_eq!(
    model.measures[1]
      .from
      .as_ref()
      .expect("a declared fold may read a column too"),
    "duration_ms",
  );
}

/// **THE SUGAR IS THE ENTRY WHERE NOTHING IS LEFT OUT**, and both halves of the fold axis reach it.
///
/// `counting` reads no column, so the short spelling states its entry WHOLE. A fold the declaring
/// crate wrote is not this framework's to answer for, so both spellings are its author's to choose
/// between — and the column it reads is [`None`] in the short one, which is a fold over the group's
/// rows rather than a seat left empty.
#[test]
fn the_sugar_states_a_whole_entry_and_reads_no_column() {
  let model = accepted(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting, median = "app::Median"),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      #[ingraph(measure)] median: f64,
    }
  });

  assert!(matches!(model.measures[0].by, Measure::Counting));
  assert!(
    model.measures[0].from.is_none(),
    "a counted column reads no column of the rows it counts"
  );
  assert!(matches!(model.measures[1].by, Measure::Declared(_)));
  assert!(model.measures[1].from.is_none());
}

/// **THE TWO SPELLINGS MIX INSIDE ONE BLOCK**, which is where this seat departs from `delegate(…)`.
///
/// There the two are told apart by what the block OPENS with, so a block is written in one of them.
/// Here every entry opens with the field's own name and the fork is the single token after it, so
/// the choice is per ENTRY — and a declaration with a counted column and a summed one writes each in
/// the shortest spelling that omits nothing.
#[test]
fn one_measure_block_holds_both_spellings() {
  let model = accepted(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(count = counting, bytes(fold = summing, from = "size")),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert!(model.measures[0].from.is_none());
  assert_eq!(model.measures[1].from.as_ref().expect("named"), "size");
}

/// **A FOLD THAT READS A COLUMN IS REFUSED IN THE SUGAR**, pointing at the full form.
///
/// The `delegate(…)` seat's mixed-form tooth at this position: the short spelling is kept for the
/// entries it can state whole, and the moment an entry has a second property, every property it has
/// is named.
#[test]
fn a_fold_that_reads_a_column_is_refused_the_short_spelling() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes = summing), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert!(
    refusals.contains("`bytes = summing` leaves out the column `summing` folds"),
    "{refusals}"
  );
  assert!(
    refusals.contains("`bytes(fold = summing, from = \"<upstream column>\")`"),
    "the full spelling is written out: {refusals}"
  );
  assert!(
    refusals.contains("`<field> = counting` is a whole entry"),
    "the sentence says where the short spelling stays: {refusals}"
  );
}

/// **A SUMMED COLUMN IS NAMED AND NEVER DEFAULTED**, which is the ruling the sentence carries.
#[test]
fn a_full_form_whose_fold_reads_a_column_must_name_one() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing)), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert!(
    refusals.contains("`summing` folds an upstream COLUMN and `bytes(…)` names none"),
    "{refusals}"
  );
  assert!(
    refusals.contains("It is not filled in from `bytes`"),
    "the refused convenience is named rather than left to be assumed: {refusals}"
  );
  assert!(
    refusals.contains("would fold the wrong column wherever they did not"),
    "the sentence says what the default would have cost: {refusals}"
  );
}

/// **A FOLD THAT READS NO COLUMN IS REFUSED ONE.**
#[test]
fn a_fold_that_reads_no_column_is_refused_a_column() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count(fold = counting, from = "n")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("`from` names the upstream column a fold READS, and `counting` reads none"),
    "{refusals}"
  );
  assert!(
    refusals.contains("`count(fold = counting)` is the entry entire"),
    "{refusals}"
  );
}

/// **A DECLARED FOLD IS NEITHER**, so both spellings are the declaring crate's to choose between.
///
/// The framework knows what `counting` and `summing` read because it wrote them. A type it never saw
/// is a question it has no standing to answer, so neither refusal fires over one.
#[test]
fn a_declared_fold_is_refused_neither_spelling() {
  let with_column = accepted(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(median(fold = "app::Median", from = "ms")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] median: f64,
    }
  });
  assert_eq!(with_column.measures[0].from.as_ref().expect("named"), "ms");

  let without = accepted(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(median(fold = "app::Median")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] median: f64,
    }
  });
  assert!(without.measures[0].from.is_none());
}

/// **THE ENTRY GROUP IS THE EXTENSIBLE SEAT**, so an unknown word in it is a roster near miss.
///
/// And the near miss costs ONE error: a word the roster could not place is a word whose meaning is
/// unknown, so the absence it leaves behind — this entry names no column — is a claim about the same
/// typo rather than a second rule.
#[test]
fn an_unknown_property_names_the_entrys_roster_and_the_nearest_word() {
  let near = refusals(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, frm = "size")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });
  assert_eq!(near.len(), 1, "one mistake, one error: {near:?}");
  let near = near.join("\n");
  assert!(near.contains("unknown property in `bytes(…)`"), "{near}");
  assert!(
    near.contains("a measure entry knows `fold` and `from`"),
    "the roster is printed off the table the walk reads: {near}"
  );
  assert!(near.contains("did you mean `from`?"), "{near}");

  let far = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, hippopotamus = "size")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });
  assert!(
    far.contains("a measure entry knows `fold` and `from`"),
    "the sentence still teaches where nothing is near: {far}"
  );
  assert!(!far.contains("did you mean"), "{far}");
}

/// **A COLUMN WRITTEN AND REFUSED IS NOT ALSO A COLUMN MISSING** — the `spoken` device at the
/// property grain.
#[test]
fn a_column_that_could_not_be_read_is_never_reported_as_absent() {
  let refusals = refusals(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, from = 7)), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
  assert!(
    refusals[0].contains("`from = \"<upstream column>\"` names the upstream column"),
    "{refusals:?}"
  );
  assert!(
    !refusals[0].contains("names none"),
    "an author who wrote the column is never told they left it out: {refusals:?}"
  );
}

/// **AN ENTRY NAMES A FOLD**, whatever else it says — and a refused fold does not earn this too.
#[test]
fn a_full_form_that_names_no_fold_is_refused_once() {
  let none = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(from = "size")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });
  assert!(
    none.contains("`bytes(…)` names no fold, and a measure column is what a FOLD fills"),
    "{none}"
  );

  let refused_fold = refusals(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summin, from = "size")), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });
  assert_eq!(
    refused_fold.len(),
    1,
    "a fold written and refused BY NAME must not also be told it was never written: {refused_fold:?}"
  );
  assert!(
    refused_fold[0].contains("is no fold this framework ships"),
    "{refused_fold:?}"
  );
}

/// **AN EMPTY ENTRY SAYS NOTHING**, and the sentence names both spellings.
#[test]
fn an_empty_measure_entry_is_refused_with_both_spellings() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes()), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert!(
    refusals.contains("`bytes()` says nothing, which is not a measure"),
    "{refusals}"
  );
  assert!(refusals.contains("FULL FORM"), "{refusals}");
  assert!(
    refusals.contains("the sugar `bytes = <fold>`"),
    "{refusals}"
  );
}

/// **ONE ENTRY ANSWERS EACH PROPERTY ONCE**, at both words.
#[test]
fn a_property_written_twice_in_one_entry_is_refused() {
  let refusals = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"), group(codec = "codec"),
      measure(bytes(fold = summing, fold = counting, from = "size", from = "n")),
      poll(p, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] bytes: u64,
    }
  });

  assert!(
    refusals.contains("`fold` is declared more than once in `bytes(…)`"),
    "{refusals}"
  );
  assert!(
    refusals.contains("`from` is declared more than once in `bytes(…)`"),
    "{refusals}"
  );
  assert!(
    refusals.contains("a column of its own takes an entry of its own"),
    "the sentence says what a second answer is not: {refusals}"
  );
}

/// **THE BOOK SKETCH'S CALL SHAPE EARNS ONE ERROR, AND IT IS THE RIGHT ONE.**
///
/// The regression the fold seam's census measured and left recorded: `summing("duration_ms")` was
/// syn's bare *expected `,`* AND a second, FALSE refusal saying the declaration named no clock —
/// against a declaration that names one, the head parse having stopped inside the block before it
/// reached the word. Both halves are fixed here: the entry reader consumes the tail itself and
/// answers with the rule rather than the punctuation, and the head's `whole` flag is what would have
/// suppressed the cascade had it aborted anyway.
#[test]
fn the_sketchs_fold_call_earns_one_error_and_never_the_false_clock() {
  let refusals = refusals(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"),
      group(codec = "codec"),
      measure(total_ms = summing("duration_ms")),
      poll(recount, every = "10m", light),
      sqlite,
    )]
    pub struct TrackCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] total_ms: u64,
    }
  });

  assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
  assert!(
    refusals[0].contains("`total_ms = summing` leaves out the column `summing` folds"),
    "and it is the rule rather than the punctuation: {refusals:?}"
  );
  assert!(
    !refusals[0].contains("poll"),
    "the clock this declaration DOES carry is never refused as missing: {refusals:?}"
  );
}

/// **A `counting` COLUMN IS A CARDINALITY**, and the type is the citizen's fact rather than the
/// declaration's — so another one is refused HERE and not by an `E0308` inside an expansion.
///
/// The line the emission's arrival made drawable: what a fold ANSWERS is knowable at the
/// declaration exactly where this crate wrote the fold. `counting` is one of the two it wrote.
///
/// **BOTH SPELLINGS ARE READ, and the second is the 2026-08-27 ruling's own footprint.** `u32` is
/// the ordinary narrowing this refusal has always caught. `u64` is the one that used to be RIGHT —
/// it was the citizen's `Value` until `Count` was minted — so it is the near miss an author is
/// likeliest to write, and it earns the same sentence rather than a silent `E0308`.
#[test]
fn a_counting_column_of_another_width_is_refused_at_its_own_type() {
  let over = |ty: proc_macro2::TokenStream| {
    refusals(quote::quote! {
      #[ingraph(
        upstream(node = "demux::Track"),
        group(codec = "codec"),
        measure(count = counting),
        poll(recount, every = "10m", light, async(inline)),
        sqlite,
      )]
      pub struct CodecCensus {
        #[ingraph(group)] codec: Codec,
        #[ingraph(measure)] count: #ty,
      }
    })
  };

  for narrowing in [quote::quote!(u32), quote::quote!(u64)] {
    let spelled = narrowing.to_string();
    let refusals = over(narrowing);

    assert_eq!(
      refusals.len(),
      1,
      "one mistake, one error at `{spelled}`: {refusals:?}"
    );
    assert!(
      refusals[0].contains("which answers HOW MANY ROWS a group holds"),
      "the sentence names what the fold answers at `{spelled}`: {refusals:?}"
    );
    assert!(
      refusals[0].contains("carries every cardinality at `ingraph::Count`"),
      "and the TYPE it answers at, which is no longer a width at `{spelled}`: {refusals:?}"
    );
    assert!(
      refusals[0].contains("Write `count: ingraph::Count`"),
      "and it says what to write instead at `{spelled}`: {refusals:?}"
    );
  }
}

/// **THE OTHER SIDE OF THAT LINE STAYS SILENT** — a `summing` column, and a `counting` column whose
/// type this pass has only the NAME of.
///
/// Two refusals deliberately not written. `summing` answers the UPSTREAM COLUMN's type, which two
/// macro expansions cannot share, so its mismatch is the emitted call's `E0271` at the string its
/// author wrote. An ALIAS is the second: `type Tally = Count;` is a perfectly good `counting` column
/// and a pass that read its spelling would refuse it wrongly — the false refusal the elected-field
/// seat one kind over records as the reason its own check is a bound rather than an allowlist. The
/// citizen's own answer is a PATH rather than a primitive word, so `Count` itself takes this same
/// silent road: the acceptance costs the refused list nothing.
#[test]
fn a_width_this_pass_cannot_know_is_left_to_the_emitted_call() {
  let over = |measure: proc_macro2::TokenStream, ty: proc_macro2::TokenStream| {
    accepted(quote::quote! {
      #[ingraph(
        upstream(node = "demux::Track"),
        group(codec = "codec"),
        measure(#measure),
        poll(recount, every = "10m", light, async(inline)),
        sqlite,
      )]
      pub struct CodecCensus {
        #[ingraph(group)] codec: Codec,
        #[ingraph(measure)] count: #ty,
      }
    })
  };

  over(
    quote::quote!(count(fold = summing, from = "size")),
    quote::quote!(u32),
  );
  over(quote::quote!(count = counting), quote::quote!(Tally));
  over(
    quote::quote!(count = counting),
    quote::quote!(ingraph::Count),
  );
  over(
    quote::quote!(count = "app::Median"),
    quote::quote!(::core::primitive::f64),
  );
}

/// **A COLUMN MAY NOT BE CALLED `fold`**, because the expansion mints a method of that name.
///
/// The hole the emission opened, closed at the declaration. A `copy` column takes the BARE field
/// name for its accessor, so `fold` written as a column and `fold` written by the expansion are two
/// definitions of one name on one type — measured as `E0592`, reported at the attribute, inside an
/// expansion its author cannot edit. The refusal fires whatever projection the column wears: which
/// spellings collide is a fact about today's accessor family, and a rule that tracked it would have
/// to be re-derived every time that family grew.
#[test]
fn a_column_named_after_the_emissions_own_method_is_refused() {
  for projection in [quote::quote!(measure), quote::quote!(measure, copy)] {
    let refusals = refusals(quote::quote! {
      #[ingraph(
        upstream(node = "demux::Track"),
        group(codec = "codec"),
        measure(fold(fold = summing, from = "size")),
        poll(recount, every = "10m", light, async(inline)),
        sqlite,
      )]
      pub struct Colliding {
        #[ingraph(group)] codec: Codec,
        #[ingraph(#projection)] fold: u64,
      }
    });

    assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
    assert!(
      refusals[0].contains("is the method an aggregate's expansion MINTS"),
      "{refusals:?}"
    );
  }
}

/// **A HEAD THE READER COULD NOT FINISH CLAIMS NO ABSENCE**, which is the cascade fix in general.
///
/// The walk here stops at the missing comma after `upstream(…)`, so `group(…)`, `measure(…)` and
/// `poll(…)` are never read. Every refusal those three would earn by their ABSENCE is a claim about
/// tokens nobody looked at, and the declaration earns the reader's own failure and nothing else.
#[test]
fn a_head_that_could_not_be_read_to_the_end_earns_no_absence_refusals() {
  let refusals = refusals(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track") group(codec = "codec"),
      measure(count = counting),
      poll(recount, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
  for false_absence in [
    "needs a `poll(…)` clock",
    "needs at least one `group(…)` entry",
    "needs at least one `measure(…)` entry",
    "has no entry for it",
  ] {
    assert!(
      !refusals[0].contains(false_absence),
      "{false_absence} is a claim about tokens the reader never reached: {refusals:?}"
    );
  }
}

/// **THE FIELD'S OWN REFUSALS SURVIVE A TRUNCATED HEAD**, because they were never the head's to make.
///
/// The other half of the `whole` device, and the line it draws: *this marked field has no head
/// entry* is a claim about what the head does not say, and *this field wears no role* is a claim
/// about the field's own attribute, which was read in full whatever the head did.
#[test]
fn a_truncated_head_still_reports_what_the_fields_said() {
  let refusals = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track") group(codec = "codec"),
      measure(count = counting),
      poll(recount, every = "10m", light),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      note: String,
    }
  });

  assert!(
    refusals.contains("`note` wears neither `#[ingraph(group)]` nor `#[ingraph(measure)]`"),
    "{refusals}"
  );
  assert!(!refusals.contains("needs a `poll(…)` clock"), "{refusals}");
}

/// **AN AGGREGATE MINTS ITS OWN KEY**, so no field may claim to be its id.
#[test]
fn an_aggregate_takes_no_declared_id() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group, id)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("mints its key from its GROUP COLUMNS"),
    "{refusals}"
  );
}

/// **THE OTHER KINDS' ELECTION MARKERS ARE REFUSED BY NAME**, each sent to the kind that runs one.
#[test]
fn an_election_marker_is_sent_to_the_kind_that_elects() {
  for (word, owner) in [("knn", "vector"), ("searchable", "fulltext")] {
    let marker = syn::Ident::new(word, proc_macro2::Span::call_site());
    let refusals = refused(quote::quote! {
      #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
      pub struct CodecCensus {
        #[ingraph(group, #marker)] codec: Codec,
        #[ingraph(measure)] count: Count,
      }
    });

    assert!(
      refusals.contains(&format!("The marker is the `{owner}` kind's own")),
      "{refusals}"
    );
    assert!(
      refusals.contains("an aggregate FOLDS its upstream's rows"),
      "{refusals}"
    );
  }
}

/// **THE WORDS AN AGGREGATE DECIDES FOR ITSELF**, each answered with what it decided.
#[test]
fn the_record_words_an_aggregate_has_already_decided_are_refused_by_name() {
  for (word, clause) in [
    ("lineage", "the path is the GROUP columns"),
    ("key", "the key is the group columns"),
    (
      "subscribe",
      "reads its upstream's TABLE rather than its messages",
    ),
  ] {
    let entry = syn::Ident::new(word, proc_macro2::Span::call_site());
    let refusals = refused(quote::quote! {
      #[ingraph(
        upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
        poll(p, every = "10m", light), #entry = "x",
      )]
      pub struct CodecCensus {
        #[ingraph(group)] codec: Codec,
        #[ingraph(measure)] count: Count,
      }
    });

    assert!(refusals.contains(clause), "{word}: {refusals}");
  }
}

/// **THE ONE-STORAGE LAW**, at the three words that name another storage.
#[test]
fn another_storage_word_is_refused_with_the_one_storage_law() {
  for (word, held) in [
    ("fulltext", "full-text index"),
    ("vector", "embedding index"),
    ("blob", "artifact family"),
  ] {
    let entry = syn::Ident::new(word, proc_macro2::Span::call_site());
    let refusals = refused(quote::quote! {
      #[ingraph(
        upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
        poll(p, every = "10m", light), #entry(tantivy),
      )]
      pub struct CodecCensus {
        #[ingraph(group)] codec: Codec,
        #[ingraph(measure)] count: Count,
      }
    });

    assert!(
      refusals.contains(&format!("an aggregate owns no {held}")),
      "{refusals}"
    );
    assert!(
      refusals.contains("an aggregate's is the folded rows it keeps"),
      "the law's own clause for this kind: {refusals}"
    );
  }
}

/// **AN UNKNOWN HEAD ENTRY IS A NEAR MISS**, over the head's own words and no others.
#[test]
fn an_unknown_head_entry_names_the_grammar_and_the_nearest_word() {
  let refusals = refused(quote::quote! {
    #[ingraph(
      upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
      poll(p, every = "10m", light), measur(x = counting),
    )]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("unknown `ingraph` aggregate attribute"),
    "{refusals}"
  );
  assert!(refusals.contains("did you mean `measure`?"), "{refusals}");
}

/// **AN UNKNOWN FIELD ENTRY IS A NEAR MISS TOO**, over the field's two roles and the projections.
#[test]
fn an_unknown_field_entry_names_the_roles_and_the_nearest_word() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(grup)] codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  assert!(
    refusals.contains("a field of an aggregate takes its ROLE"),
    "{refusals}"
  );
  assert!(refusals.contains("did you mean `group`?"), "{refusals}");
}

/// **THE DECLARATION'S OWN SHAPE**, at the three ways it can be wrong.
#[test]
fn an_aggregate_is_a_concrete_struct_with_named_fields() {
  let generic = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus<T> {
      #[ingraph(group)] codec: T,
      #[ingraph(measure)] count: Count,
    }
  });
  assert!(
    generic.contains("generic aggregates are not supported"),
    "{generic}"
  );

  let positional = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus(Codec, u64);
  });
  assert!(
    positional.contains("must be a struct with named fields"),
    "{positional}"
  );

  let variant = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub enum CodecCensus { A }
  });
  assert!(
    variant.contains("`#[ingraph::aggregate]` supports structs only"),
    "{variant}"
  );

  let bare = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {}
  });
  assert!(
    bare.contains("an aggregate with no fields folds nothing"),
    "{bare}"
  );
}

/// **THE ONE-STORAGE LAW ON THE FIELD SIDE** — a declaration a derived index already rewrote.
#[test]
fn a_declaration_carrying_an_injected_key_is_a_second_storage() {
  let injected = ingraph_codegen::model::borrowed_key(&EmissionPrefix::default());
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
    pub struct CodecCensus {
      #[ingraph(group)] codec: Codec,
      #[ingraph(measure)] count: Count,
      #injected: Key,
    }
  });

  assert!(
    refusals.contains("a second storage macro is being asked to fold a row that has one"),
    "{refusals}"
  );
}

/// **THE WRONG-KIND COURTESY IS WIRED AT ALL FOUR POSITIONS** — both words, at a head and at a
/// field, in each of the two grammars that are not this one.
///
/// The sentence is pinned by its own goldens; what this pins is the WIRING. Take the arm out of
/// either grammar and the word falls through to that position's unknown-entry road — which on a
/// derived index's field is worse than an unknown word, every word that grammar does not own being
/// an ENGINE, so a `measure` would become an engine nothing binds.
#[test]
fn the_role_words_are_refused_by_name_on_every_other_kind() {
  let head = |source: proc_macro2::TokenStream, kind| {
    let input: syn::DeriveInput = syn::parse2(source).expect("the declaration parses");
    let (host, arguments) = crate::host::split(&input);
    let refused = match kind {
      ingraph_codegen::model::NodeKind::Record => crate::parse::parse(&host, &arguments, kind)
        .err()
        .map(|e| e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>()),
      _ => crate::index::parse(&host, &arguments, kind)
        .err()
        .map(|e| e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>()),
    };
    refused.expect("this declaration breaks a rule").join("\n")
  };

  let record = head(
    quote::quote! {
      #[ingraph(sqlite, group(codec = "codec"))]
      pub struct Rows {
        #[ingraph(id)] id: u128,
        #[ingraph(measure)] count: Count,
      }
    },
    ingraph_codegen::model::NodeKind::Record,
  );
  assert!(
    record.contains(
      "`group(…)` says which column a folded row is keyed by, and a record folds \
                     nothing"
    ),
    "{record}"
  );
  assert!(
    record.contains(
      "`measure` marks this column as the one that says how a group's rows are \
                     folded"
    ),
    "{record}"
  );
  assert!(
    record.contains("declare it with `#[ingraph::aggregate(…)]`"),
    "{record}"
  );

  let index = head(
    quote::quote! {
      #[ingraph(upstream(node = "Segment", body = "text"), dedup(hash), measure(count = counting))]
      pub struct Near {
        #[ingraph(searchable, group)] body: String,
      }
    },
    ingraph_codegen::model::NodeKind::Fulltext,
  );
  assert!(
    index.contains(
      "`measure(…)` says how a group's rows are folded into one column, and a \
                    full-text index folds nothing"
    ),
    "{index}"
  );
  assert!(
    index.contains(
      "`group` marks this column as the one that says which column a folded row is \
                    keyed by"
    ),
    "{index}"
  );
  assert!(
    !index.contains("reads as an ENGINE"),
    "a word this grammar can answer is never read as somebody's engine: {index}"
  );
}

/// **EVERY REFUSAL AT ONCE** — the accumulation this pass is built on, and the property that a
/// declaration wrong in several ways costs one compile rather than several.
#[test]
fn one_compile_reports_the_whole_declaration() {
  let refusals = refused(quote::quote! {
    #[ingraph(upstream(node = "demux::Track"))]
    pub struct CodecCensus {
      codec: Codec,
      #[ingraph(measure)] count: Count,
    }
  });

  for rule in [
    "wears neither",
    "needs at least one `group(…)` entry",
    "has no entry for it",
    "needs a `poll(…)` clock",
  ] {
    assert!(
      refusals.contains(rule),
      "{rule} is missing from: {refusals}"
    );
  }
}