puerto 0.23.0

Puerto — Rust full-stack DDD framework CLI. Scaffolds Clean Architecture projects with auto-wired DI, SQLx support, and zero manual wiring. Formerly puerto-framework.
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
// Phase 18 (ADR-0022): an entity refers to another by holding its typed id.
use super::*;

// ── 18.2: the manifest ──────────────────────────────────────────────────────

#[test]
fn should_parse_a_reference_as_a_uuid_naming_its_target() {
    let field = puerto_toml::parse_field_arg("cliente_id:ref:Cliente").unwrap();

    assert_eq!(field.name, "cliente_id");
    assert_eq!(field.field_type, "Uuid");
    assert_eq!(field.references.as_deref(), Some("Cliente"));
    assert!(field.value_object.is_none());
    assert!(!field.unique);
}

#[test]
fn should_parse_an_optional_reference_as_an_optional_uuid() {
    let field = puerto_toml::parse_field_arg("reviewer_id:ref:opt:Cliente").unwrap();

    assert_eq!(field.field_type, "Option<Uuid>");
    assert_eq!(field.references.as_deref(), Some("Cliente"));
}

#[test]
fn should_parse_a_unique_reference_as_one_to_one() {
    let field = puerto_toml::parse_field_arg("cliente_id:ref:Cliente!").unwrap();

    assert!(field.unique);
    assert_eq!(field.references.as_deref(), Some("Cliente"));
}

#[test]
fn should_reject_a_unique_optional_reference() {
    let err = puerto_toml::parse_field_arg("reviewer_id:ref:opt:Cliente!").unwrap_err();

    assert!(err.contains("unique"), "{err}");
}

#[test]
fn should_reject_a_reference_whose_target_is_not_pascal_case() {
    let err = puerto_toml::parse_field_arg("cliente_id:ref:cliente").unwrap_err();

    assert!(err.contains("cliente"), "{err}");
}

#[test]
fn should_keep_a_reference_through_puerto_toml() {
    // Arrange
    let dir = temp_dir("references_round_trip");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    fs::write(dir.join("puerto.toml"), "[proyecto]\nnombre = \"shop\"\n").unwrap();
    let field = puerto_toml::parse_field_arg("cliente_id:ref:Cliente").unwrap();

    // Act
    puerto_toml::add_entity(&dir, "Pedido", vec![], false, vec![field]).unwrap();

    // Assert
    let written = fs::read_to_string(dir.join("puerto.toml")).unwrap();
    assert!(written.contains("referencia = \"Cliente\""), "{written}");
    let config = puerto_toml::read(&dir).unwrap();
    assert_eq!(
        config.entity[0].fields[0].references.as_deref(),
        Some("Cliente")
    );

    cleanup(&dir);
}

/// A manifest with a CRUD `Cliente`, a simple `Saludo` and an `Pedido` holding `pedido_field`.
fn validate_pedido_with(name: &str, project_extra: &str, pedido_field: &str) -> Result<(), String> {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    fs::write(
        dir.join("puerto.toml"),
        format!(
            r#"[proyecto]
nombre = "shop"
{project_extra}
[[entidad]]
nombre = "Saludo"
casos_de_uso = ["obtener_saludo"]

[[entidad]]
nombre = "Cliente"
casos_de_uso = ["crear_cliente", "obtener_cliente", "listar_clientes", "actualizar_cliente", "borrar_cliente"]

[[entidad]]
nombre = "Pedido"
casos_de_uso = ["crear_pedido", "obtener_pedido", "listar_pedidos", "actualizar_pedido", "borrar_pedido"]

[[entidad.campos]]
{pedido_field}
"#
        ),
    )
    .unwrap();
    let result = validate::run_validate(&dir).map_err(|e| e.to_string());
    cleanup(&dir);
    result
}

#[test]
fn should_validate_a_reference_to_a_declared_crud_entity() {
    let result = validate_pedido_with(
        "references_validate_ok",
        "",
        "nombre = \"cliente_id\"\ntipo = \"Uuid\"\nreferencia = \"Cliente\"",
    );

    assert!(result.is_ok(), "{result:?}");
}

#[test]
fn should_refuse_a_reference_to_an_undeclared_entity() {
    let err = validate_pedido_with(
        "references_validate_undeclared",
        "",
        "nombre = \"proveedor_id\"\ntipo = \"Uuid\"\nreferencia = \"Proveedor\"",
    )
    .unwrap_err();

    assert!(
        err.contains("'proveedor_id'") && err.contains("Proveedor"),
        "{err}"
    );
}

#[test]
fn should_refuse_a_reference_to_the_entity_itself() {
    let err = validate_pedido_with(
        "references_validate_self",
        "",
        "nombre = \"parent_id\"\ntipo = \"Option<Uuid>\"\nreferencia = \"Pedido\"",
    )
    .unwrap_err();

    assert!(
        err.contains("'parent_id'") && err.contains("itself"),
        "{err}"
    );
}

#[test]
fn should_refuse_a_reference_to_an_entity_without_crud() {
    let err = validate_pedido_with(
        "references_validate_simple",
        "",
        "nombre = \"saludo_id\"\ntipo = \"Uuid\"\nreferencia = \"Saludo\"",
    )
    .unwrap_err();

    assert!(err.contains("'saludo_id'") && err.contains("CRUD"), "{err}");
}

#[test]
fn should_refuse_a_reference_to_the_user_in_an_auth_project() {
    let err = validate_pedido_with(
        "references_validate_user",
        "auth = \"oidc\"",
        "nombre = \"owner_id\"\ntipo = \"Uuid\"\nreferencia = \"User\"",
    )
    .unwrap_err();

    assert!(err.contains("'owner_id'") && err.contains("User"), "{err}");
}

#[test]
fn should_refuse_a_reference_that_is_not_a_uuid() {
    let err = validate_pedido_with(
        "references_validate_type",
        "",
        "nombre = \"cliente_id\"\ntipo = \"i64\"\nreferencia = \"Cliente\"",
    )
    .unwrap_err();

    assert!(
        err.contains("'cliente_id'") && err.contains("Uuid"),
        "{err}"
    );
}

#[test]
fn should_refuse_a_reference_that_is_also_a_value_object() {
    let err = validate_pedido_with(
        "references_validate_vo",
        "",
        "nombre = \"cliente_id\"\ntipo = \"Uuid\"\nreferencia = \"Cliente\"\nobjeto_valor = \"ClienteRef\"",
    )
    .unwrap_err();

    assert!(
        err.contains("'cliente_id'") && err.contains("value_object"),
        "{err}"
    );
}

// ── 18.2: scaffold refuses before writing ───────────────────────────────────

fn pedido_fields() -> Vec<puerto_toml::Field> {
    vec![puerto_toml::parse_field_arg("cliente_id:ref:Cliente").unwrap()]
}

#[test]
fn should_refuse_to_scaffold_a_reference_to_an_undeclared_entity() {
    // Arrange
    let dir = temp_dir("references_scaffold_undeclared");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), false, &dir).unwrap();

    // Act
    let err = scaffold::run_scaffold("Pedido", &output, &pedido_fields(), OverwritePolicy::Ask)
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("Cliente"), "{err}");
    assert!(!output.join("business/src/domain/pedido").exists());

    cleanup(&dir);
}

#[test]
fn should_refuse_to_scaffold_a_reference_to_a_target_without_code() {
    // Arrange: Cliente is declared, but nothing was generated for it.
    let dir = temp_dir("references_scaffold_no_code");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), true, &dir).unwrap();
    puerto_toml::add_entity(
        &output,
        "Cliente",
        crate::generators::naming::crud_use_cases("cliente"),
        true,
        vec![],
    )
    .unwrap();

    // Act
    let err = scaffold::run_scaffold("Pedido", &output, &pedido_fields(), OverwritePolicy::Ask)
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("puerto generar scaffold Cliente"), "{err}");
    assert!(!output.join("business/src/domain/pedido").exists());

    cleanup(&dir);
}

#[test]
fn should_refuse_to_add_the_domain_of_a_reference_to_an_undeclared_entity() {
    // Arrange
    let dir = temp_dir("references_domain_undeclared");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), false, &dir).unwrap();

    // Act
    let err = scaffold::run_generate_domain("Pedido", &output, &pedido_fields())
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("Cliente"), "{err}");
    assert!(!output.join("business/src/domain/pedido").exists());

    cleanup(&dir);
}

#[test]
fn should_validate_a_reference_to_an_entity_named_user_outside_an_auth_project() {
    // Arrange: without auth, `Usuario` is an ordinary entity.
    let dir = temp_dir("references_validate_plain_user");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    fs::write(
        dir.join("puerto.toml"),
        r#"[proyecto]
nombre = "shop"

[[entidad]]
nombre = "Usuario"
casos_de_uso = ["crear_usuario", "obtener_usuario", "listar_usuarios", "actualizar_usuario", "borrar_usuario"]

[[entidad]]
nombre = "Pedido"
casos_de_uso = ["crear_pedido"]

[[entidad.campos]]
nombre = "owner_id"
tipo = "Uuid"
referencia = "Usuario"
"#,
    )
    .unwrap();

    // Act
    let result = validate::run_validate(&dir);

    // Assert
    assert!(result.is_ok(), "{result:?}");
    cleanup(&dir);
}

#[test]
fn should_scaffold_a_reference_to_a_target_with_code() {
    // Arrange
    let dir = temp_dir("references_scaffold_ok");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), false, &dir).unwrap();
    scaffold::run_scaffold("Cliente", &output, &[], OverwritePolicy::Ask).unwrap();

    // Act
    let result = scaffold::run_scaffold("Pedido", &output, &pedido_fields(), OverwritePolicy::Ask);

    // Assert
    assert!(result.is_ok(), "{result:?}");
    assert!(
        output
            .join("contextos/pedido/dominio/src/modelo.rs")
            .exists()
    );
    cleanup(&dir);
}

#[test]
fn should_refuse_to_scaffold_a_reference_to_a_target_whose_table_is_not_created() {
    // Arrange: the model is there, the migration that creates its table is not.
    let dir = temp_dir("references_scaffold_no_table");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), true, &dir).unwrap();
    scaffold::run_scaffold("Cliente", &output, &[], OverwritePolicy::Ask).unwrap();
    for entry in fs::read_dir(output.join("migraciones")).unwrap() {
        let path = entry.unwrap().path();
        if path.to_string_lossy().ends_with("_crear_tabla_cliente.sql") {
            fs::remove_file(path).unwrap();
        }
    }

    // Act
    let err = scaffold::run_scaffold("Pedido", &output, &pedido_fields(), OverwritePolicy::Ask)
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("puerto generar scaffold Cliente"), "{err}");
    cleanup(&dir);
}

// ── 18.3: generated backend ─────────────────────────────────────────────────

/// A project with a CRUD `Cliente` and an `Pedido` holding `cliente_id:ref:Cliente` and
/// `reviewer_id:ref:opt:Cliente`. `db`: `--db`; `events`: also `[proyecto] eventos = "db"`.
fn pedido_project(name: &str, db: bool, events: bool) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), db, &dir).unwrap();
    if events {
        let manifest = output.join("puerto.toml");
        let toml = fs::read_to_string(&manifest).unwrap();
        fs::write(
            &manifest,
            toml.replace("db = true", "db = true\neventos = \"db\""),
        )
        .unwrap();
    }
    scaffold::run_scaffold("Cliente", &output, &[], OverwritePolicy::Ask).unwrap();
    let fields = vec![
        puerto_toml::parse_field_arg("cliente_id:ref:Cliente").unwrap(),
        puerto_toml::parse_field_arg("reviewer_id:ref:opt:Cliente").unwrap(),
    ];
    scaffold::run_scaffold("Pedido", &output, &fields, OverwritePolicy::Ask).unwrap();
    (dir, output)
}

fn read(output: &Path, path: &str) -> String {
    fs::read_to_string(output.join(path)).unwrap_or_else(|e| panic!("{path}: {e}"))
}

#[test]
fn should_hold_the_target_id_type_in_the_domain() {
    let (dir, output) = pedido_project("references_domain", false, false);

    let model = read(&output, "contextos/pedido/dominio/src/modelo.rs");
    assert!(
        model.contains("use dominio_cliente::modelo::IdCliente;"),
        "{model}"
    );
    assert!(model.contains("pub cliente_id: IdCliente,"), "{model}");
    assert!(
        model.contains("pub reviewer_id: Option<IdCliente>,"),
        "{model}"
    );

    for uc in ["crear_pedido", "actualizar_pedido"] {
        let params = read(&output, &format!("contextos/pedido/aplicacion/src/{uc}.rs"));
        assert!(
            params.contains("use dominio_cliente::modelo::IdCliente;"),
            "{uc}"
        );
        assert!(
            params.contains("pub cliente_id: IdCliente,"),
            "{uc}: {params}"
        );
        assert!(
            params.contains("pub reviewer_id: Option<IdCliente>,"),
            "{uc}"
        );
        assert!(!params.contains("uuid::Uuid"), "{uc} imports Uuid");
    }

    let mother = read(&output, "contextos/pedido/dominio/src/pruebas/madres.rs");
    assert!(
        mother.contains("pub fn con_cliente_id(mut self, cliente_id: IdCliente)"),
        "{mother}"
    );
    assert!(
        mother.contains("unwrap_or_else(IdCliente::generar)"),
        "{mother}"
    );
    assert!(!mother.contains("uuid::Uuid"), "{mother}");

    let create = read(&output, "contextos/pedido/aplicacion/src/crear_pedido.rs");
    assert!(create.contains("cliente_id: props.cliente_id,"), "{create}");

    cleanup(&dir);
}

#[test]
fn should_turn_a_missing_reference_into_a_validation_error_on_the_field() {
    let (dir, output) = pedido_project("references_errors", false, false);

    let errors = read(&output, "contextos/pedido/dominio/src/errores.rs");
    assert!(
        errors.contains(
            "ErrorRepositorio::ReferenciaInexistente(campo) => {\n                Self::Validacion(format!(\"{campo}_no_encontrado\"))"
        ),
        "{errors}"
    );
    assert!(
        errors.contains("\"pedido.validacion.cliente_id_no_encontrado\""),
        "{errors}"
    );
    let cliente_errors = read(&output, "contextos/cliente/dominio/src/errores.rs");
    assert!(
        !cliente_errors.contains("MissingReference"),
        "{cliente_errors}"
    );

    cleanup(&dir);
}

#[test]
fn should_convert_a_reference_at_the_api_edge() {
    let (dir, output) = pedido_project("references_api", false, false);

    let dto = read(&output, "contextos/pedido/rest/src/dto.rs");
    assert!(dto.contains("pub cliente_id: Uuid,"), "{dto}");
    assert!(dto.contains("pub reviewer_id: Option<Uuid>,"), "{dto}");
    assert!(
        dto.contains("cliente_id: entidad.cliente_id.valor(),"),
        "{dto}"
    );
    assert!(
        dto.contains("reviewer_id: entidad.reviewer_id.map(|v| v.valor()),"),
        "{dto}"
    );

    let routes = read(&output, "contextos/pedido/rest/src/rutas.rs");
    assert!(
        routes.contains("use dominio_cliente::modelo::IdCliente;"),
        "{routes}"
    );
    assert!(
        routes.contains("cliente_id: IdCliente::from_uuid(cuerpo.cliente_id),"),
        "{routes}"
    );
    assert!(
        routes.contains("reviewer_id: cuerpo.reviewer_id.map(IdCliente::from_uuid),"),
        "{routes}"
    );
    assert!(
        routes.contains(
            "async fn deberia_responder_422_cuando_no_existe_la_referencia_de_cliente_id()"
        ),
        "{routes}"
    );

    cleanup(&dir);
}

#[test]
fn should_hold_a_uuid_in_the_row_and_the_target_id_in_the_entity() {
    let (dir, output) = pedido_project("references_row", true, false);

    let entity = read(&output, "contextos/pedido/infraestructura/src/fila.rs");
    assert!(
        entity.contains("use dominio_cliente::modelo::IdCliente;"),
        "{entity}"
    );
    assert!(entity.contains("pub cliente_id: Uuid,"), "{entity}");
    assert!(
        entity.contains("pub reviewer_id: Option<Uuid>,"),
        "{entity}"
    );
    assert!(
        entity.contains("cliente_id: IdCliente::from_uuid(fila.cliente_id),"),
        "{entity}"
    );
    assert!(
        entity.contains("reviewer_id: fila.reviewer_id.map(IdCliente::from_uuid),"),
        "{entity}"
    );
    assert!(
        entity.contains("cliente_id: entidad.cliente_id.valor(),"),
        "{entity}"
    );

    cleanup(&dir);
}

#[test]
fn should_create_a_foreign_key_and_an_index_per_reference() {
    let (dir, output) = pedido_project("references_migration", true, false);

    let migration = fs::read_dir(output.join("migraciones"))
        .unwrap()
        .map(|e| e.unwrap().path())
        .find(|p| p.to_string_lossy().ends_with("_crear_tabla_pedido.sql"))
        .map(|p| fs::read_to_string(p).unwrap())
        .unwrap();
    assert!(
        migration.contains(
            "cliente_id UUID NOT NULL CONSTRAINT pedidos_cliente_id_fkey REFERENCES clientes (id),"
        ),
        "{migration}"
    );
    assert!(
        migration.contains(
            "reviewer_id UUID CONSTRAINT pedidos_reviewer_id_fkey REFERENCES clientes (id),"
        ),
        "{migration}"
    );
    assert!(
        migration.contains("CREATE INDEX pedidos_cliente_id_idx ON pedidos (cliente_id);"),
        "{migration}"
    );
    assert!(
        migration.contains("CREATE INDEX pedidos_reviewer_id_idx ON pedidos (reviewer_id);"),
        "{migration}"
    );

    cleanup(&dir);
}

#[test]
fn should_index_a_unique_reference_only_once() {
    let sql = crate::generators::infrastructure::create_table_sql(
        "pedido",
        &[puerto_toml::parse_field_arg("cliente_id:ref:Cliente!").unwrap()],
    );

    assert!(
        sql.contains("CREATE UNIQUE INDEX pedidos_cliente_id_key"),
        "{sql}"
    );
    assert!(!sql.contains("pedidos_cliente_id_idx"), "{sql}");
}

#[test]
fn should_map_the_foreign_keys_where_the_rows_are_written() {
    let (dir, output) = pedido_project("references_pg_repo", true, false);

    let repo = read(
        &output,
        "contextos/pedido/infraestructura/src/repositorio.rs",
    );
    assert!(
        repo.contains("const REFERENCES: &[ClaveForanea] = &["),
        "{repo}"
    );
    assert!(
        repo.contains("restriccion: \"pedidos_cliente_id_fkey\","),
        "{repo}"
    );
    assert!(repo.contains("campo: \"reviewer_id\","), "{repo}");
    assert!(
        repo.contains("mapear_error_sqlx_con(&e, REFERENCES)"),
        "{repo}"
    );
    // The rows it seeds first get their cliente through Cliente's own seed (ADR-0023 § 5).
    let seed = read(
        &output,
        "contextos/pedido/infraestructura/src/pruebas/siembra.rs",
    );
    assert!(
        seed.contains("cliente_id: infra_cliente::pruebas::siembra::sembrar(pool).await.id,"),
        "{seed}"
    );
    let manifest = read(&output, "contextos/pedido/infraestructura/Cargo.toml");
    assert!(
        manifest.contains("pruebas = [\"dominio_pedido/pruebas\", \"dep:infra_cliente\", \"infra_cliente/pruebas\"]"),
        "{manifest}"
    );
    assert!(
        repo.contains("async fn deberia_rechazar_cliente_id_inexistente(pool: PgPool)"),
        "{repo}"
    );
    assert!(
        repo.contains("async fn deberia_rechazar_reviewer_id_inexistente(pool: PgPool)"),
        "{repo}"
    );
    assert!(
        repo.contains("ErrorRepositorio::ReferenciaInexistente(\"cliente_id\")"),
        "{repo}"
    );
    let cliente = read(
        &output,
        "contextos/cliente/infraestructura/src/pruebas/siembra.rs",
    );
    assert!(
        cliente.contains("pub async fn sembrar(pool: &PgPool) -> Cliente {"),
        "{cliente}"
    );
    let cliente_repo = read(
        &output,
        "contextos/cliente/infraestructura/src/repositorio.rs",
    );
    assert!(!cliente_repo.contains("REFERENCES"), "{cliente_repo}");

    cleanup(&dir);
}

#[test]
fn should_map_the_foreign_keys_in_the_unit_of_work_of_an_events_project() {
    let (dir, output) = pedido_project("references_pg_uow", true, true);

    let uow = read(
        &output,
        "contextos/pedido/infraestructura/src/unit_of_work.rs",
    );
    assert!(
        uow.contains("const REFERENCES: &[ClaveForanea] = &["),
        "{uow}"
    );
    assert!(
        uow.contains("mapear_error_sqlx_con(&e, REFERENCES)"),
        "{uow}"
    );
    assert!(
        uow.contains("async fn deberia_rechazar_cliente_id_inexistente(pool: PgPool)"),
        "{uow}"
    );
    let repo = read(
        &output,
        "contextos/pedido/infraestructura/src/repositorio.rs",
    );
    assert!(
        !repo.contains("REFERENCES"),
        "the repository only reads: {repo}"
    );

    cleanup(&dir);
}

// ── 18.3: end to end (`make test/db`) ──────────────────────────────────────

#[test]
#[ignore = "slow: needs Postgres — run via `make test/db`"]
fn postgres_references_project_answers_422_on_the_field_of_a_missing_reference() {
    use super::compile_matrix::REFERENCES_DB;
    use super::worker_e2e::{build_scenario_project, free_port, http, wait_until};

    const PROJECT: &str = "e2e-references";
    let Some(project) = build_scenario_project(
        &REFERENCES_DB,
        PROJECT,
        "puerto_matrix_references_e2e",
        &[],
        |_| {},
    ) else {
        eprintln!("PUERTO_TEST_DATABASE_URL not set — skipping (run `make test/db`)");
        return;
    };
    let port = free_port();
    let api = project.spawn(
        PROJECT,
        &project.dir.join("api.log"),
        &[("SERVICE_PORT", port.to_string())],
    );
    wait_until("the API to be ready", || {
        http(port, "GET", "/health/ready", "").is_some_and(|(s, _)| s == 200)
    });
    let pedido = |cliente: &str, reviewer: &str| {
        format!(r#"{{"clienteId":"{cliente}","reviewerId":{reviewer},"total":42}}"#)
    };
    let missing = "00000000-0000-4000-8000-000000000000";

    // A cliente nobody created: the foreign key refuses it, on its field.
    let (status, body) = http(port, "POST", "/api/pedidos", &pedido(missing, "null")).unwrap();
    assert_eq!(status, 422, "{body}");
    assert!(
        body.contains("\"codigo\":\"pedido.validacion.cliente_id_no_encontrado\""),
        "{body}"
    );

    // An existing one: created; an unknown reviewer is refused on its own field.
    let (status, body) = http(
        port,
        "POST",
        "/api/clientes",
        r#"{"name":"Ada","email":"ada@example.com"}"#,
    )
    .unwrap();
    assert_eq!(status, 201, "{body}");
    let cliente: serde_json::Value = serde_json::from_str(&body).unwrap();
    let cliente = cliente["id"].as_str().unwrap().to_string();
    let (status, body) = http(port, "POST", "/api/pedidos", &pedido(&cliente, "null")).unwrap();
    assert_eq!(status, 201, "{body}");
    let reviewer = format!("\"{missing}\"");
    let (status, body) = http(port, "POST", "/api/pedidos", &pedido(&cliente, &reviewer)).unwrap();
    assert_eq!(status, 422, "{body}");
    assert!(
        body.contains("\"codigo\":\"pedido.validacion.reviewer_id_no_encontrado\""),
        "{body}"
    );

    // The list filters by cliente (18.4): another cliente's pedido is left out, page and total.
    let (status, body) = http(
        port,
        "POST",
        "/api/clientes",
        r#"{"name":"Grace","email":"grace@example.com"}"#,
    )
    .unwrap();
    assert_eq!(status, 201, "{body}");
    let other: serde_json::Value = serde_json::from_str(&body).unwrap();
    let other = other["id"].as_str().unwrap().to_string();
    let (status, body) = http(port, "POST", "/api/pedidos", &pedido(&other, "null")).unwrap();
    assert_eq!(status, 201, "{body}");
    let (status, body) = http(
        port,
        "GET",
        &format!("/api/pedidos?clienteId={cliente}"),
        "",
    )
    .unwrap();
    assert_eq!(status, 200, "{body}");
    let page: serde_json::Value = serde_json::from_str(&body).unwrap();
    assert_eq!(page["total"], 1, "{body}");
    assert_eq!(
        page["elementos"][0]["clienteId"],
        cliente.as_str(),
        "{body}"
    );

    drop(api);
    cleanup(&project.dir);
}

#[test]
fn should_test_that_create_reports_a_missing_reference_on_its_field() {
    for (name, events) in [
        ("references_create_test", false),
        ("references_create_test_events", true),
    ] {
        let (dir, output) = pedido_project(name, events, events);

        let create = read(&output, "contextos/pedido/aplicacion/src/crear_pedido.rs");
        for field in ["cliente_id", "reviewer_id"] {
            assert!(
                create.contains(&format!(
                    "async fn deberia_devolver_un_error_de_validacion_cuando_{field}_no_existe()"
                )),
                "eventos = {events}: {create}"
            );
            assert!(create.contains(&format!(
                "ErrorRepositorio::ReferenciaInexistente(\"{field}\")"
            )));
            assert!(create.contains(&format!("\"pedido.validacion.{field}_no_encontrado\"")));
        }

        cleanup(&dir);
    }
}

// ── 18.4: list by reference ─────────────────────────────────────────────────

#[test]
fn should_filter_the_list_by_each_reference_in_the_domain() {
    let (dir, output) = pedido_project("references_filter_domain", false, false);

    let repository = read(&output, "contextos/pedido/dominio/src/repositorio.rs");
    assert!(
        repository.contains("use dominio_cliente::modelo::IdCliente;"),
        "{repository}"
    );
    assert!(
        repository.contains(
            "pub struct FiltroPedido {\n    pub cliente_id: Option<IdCliente>,\n    pub reviewer_id: Option<IdCliente>,\n}"
        ),
        "{repository}"
    );
    assert_eq!(
        repository
            .matches("async fn buscar_todos(&self, pagina: PeticionPagina, filtro: FiltroPedido)")
            .count(),
        2,
        "the port and its mock: {repository}"
    );
    let params = read(&output, "contextos/pedido/aplicacion/src/listar_pedidos.rs");
    assert!(
        params.contains("pub cliente_id: Option<IdCliente>,"),
        "{params}"
    );
    assert!(
        params.contains("pub reviewer_id: Option<IdCliente>,"),
        "{params}"
    );

    // An entity without references keeps today's port.
    let cliente = read(&output, "contextos/cliente/dominio/src/repositorio.rs");
    assert!(!cliente.contains("Filter"), "{cliente}");
    assert!(
        cliente.contains("async fn buscar_todos(&self, pagina: PeticionPagina)"),
        "{cliente}"
    );

    cleanup(&dir);
}

#[test]
fn should_pass_the_filter_from_the_use_case_to_the_repository() {
    let (dir, output) = pedido_project("references_filter_application", false, false);

    let list = read(&output, "contextos/pedido/aplicacion/src/listar_pedidos.rs");
    assert!(
        list.contains(
            "FiltroPedido {\n                    cliente_id: params.cliente_id,\n                    reviewer_id: params.reviewer_id,\n                }"
        ),
        "{list}"
    );
    assert!(
        list.contains("async fn deberia_pasar_el_filtro_al_repositorio()"),
        "{list}"
    );

    let repo = read(
        &output,
        "contextos/pedido/infraestructura/src/repositorio.rs",
    );
    assert!(
        repo.contains(".is_none_or(|id| fila.cliente_id == id)"),
        "{repo}"
    );
    assert!(
        repo.contains(".is_none_or(|id| fila.reviewer_id == Some(id))"),
        "{repo}"
    );
    assert!(
        repo.contains("async fn deberia_listar_solo_pedidos_con_ese_valor_de_cliente_id()"),
        "{repo}"
    );

    cleanup(&dir);
}

#[test]
fn should_take_each_reference_filter_as_a_camel_case_query_parameter() {
    let (dir, output) = pedido_project("references_filter_api", false, false);

    let routes = read(&output, "contextos/pedido/rest/src/rutas.rs");
    assert!(
        routes.contains("#[oai(name = \"clienteId\")] cliente_id: Query<Option<Uuid>>"),
        "{routes}"
    );
    assert!(
        routes.contains("cliente_id: cliente_id.0.map(IdCliente::from_uuid),"),
        "{routes}"
    );
    assert!(
        routes.contains("async fn deberia_pasar_el_filtro_por_reviewer_id_al_caso_de_uso()"),
        "{routes}"
    );
    assert!(routes.contains("?reviewerId={reviewer_id}"), "{routes}");

    cleanup(&dir);
}

#[test]
fn should_filter_the_page_and_its_total_in_postgres() {
    for (name, events) in [
        ("references_filter_pg", false),
        ("references_filter_pg_events", true),
    ] {
        let (dir, output) = pedido_project(name, true, events);

        let repo = read(
            &output,
            "contextos/pedido/infraestructura/src/repositorio.rs",
        );
        assert!(
            repo.contains(
                "FROM pedidos WHERE borrado = false AND ($3::uuid IS NULL OR cliente_id = $3) AND ($4::uuid IS NULL OR reviewer_id = $4)"
            ),
            "eventos = {events}: {repo}"
        );
        assert!(
            repo.contains(
                "FROM pedidos WHERE borrado = false AND ($1::uuid IS NULL OR cliente_id = $1) AND ($2::uuid IS NULL OR reviewer_id = $2)"
            ),
            "the COUNT, events = {events}: {repo}"
        );
        assert!(
            repo.contains("filtro.cliente_id.map(|id| id.valor())"),
            "{repo}"
        );
        for field in ["cliente_id", "reviewer_id"] {
            assert!(
                repo.contains(&format!(
                    "async fn deberia_listar_solo_pedidos_con_ese_valor_de_{field}(pool: PgPool)"
                )),
                "eventos = {events}: {repo}"
            );
        }

        cleanup(&dir);
    }
}

// ── 18.5: the frontend ──────────────────────────────────────────────────────

const PEDIDO_WEB: &str = "web/src/contextos/pedido";

#[test]
fn should_choose_a_reference_from_a_select_of_its_targets() {
    let (dir, output) = pedido_project("references_web_form", false, false);

    let view = read(
        &output,
        &format!("{PEDIDO_WEB}/vistas/formulario-pedido.tsx"),
    );
    assert!(
        view.contains("export interface OpcionesFormularioPedido {\n  clienteId: OpcionReferencia[];\n  reviewerId: OpcionReferencia[];\n}"),
        "{view}"
    );
    assert!(
        view.contains("<span className=\"fieldset-legend\">Cliente</span>"),
        "{view}"
    );
    assert!(
        view.contains("<option value=\"\">Elige una opción</option>"),
        "{view}"
    );
    assert!(view.contains("<option value=\"\">—</option>"), "{view}");
    assert!(
        view.contains("{opciones.clienteId.map((opcion) => ("),
        "{view}"
    );

    let container = read(
        &output,
        &format!("{PEDIDO_WEB}/contenedores/formulario-pedido.tsx"),
    );
    assert!(
        container.contains(
            r#"const clientes = useCarga(() => desenvolver(api.GET("/clientes", PRIMERA_PAGINA)), [api]);"#
        ),
        "{container}"
    );
    assert!(container.contains(
        "const PRIMERA_PAGINA = { params: { query: { limite: LIMITE_PAGINA_MAXIMO } } };"
    ));
    // One load per target entity, whatever the number of references to it.
    assert_eq!(
        container.matches(r#"api.GET("/clientes""#).count(),
        1,
        "{container}"
    );
    assert!(
        container.contains("clienteId: opcionesReferencia(clientes, (cliente) => cliente.nombre),"),
        "{container}"
    );

    let test = read(
        &output,
        &format!("{PEDIDO_WEB}/contenedores/formulario-pedido.test.tsx"),
    );
    assert!(test.contains("...referenciadosPor(peticion)"), "{test}");
    assert!(
        test.contains("MadreCliente.aleatorio({ id: dueno.clienteId ?? crypto.randomUUID() }),"),
        "{test}"
    );
    assert!(
        test.contains("test(\"debería ofrecer cada cliente en sus selects\""),
        "{test}"
    );

    let http = read(&output, "web/src/api/http.ts");
    assert!(
        http.contains("export const LIMITE_PAGINA_MAXIMO = 100;"),
        "{http}"
    );
    let forms = read(&output, "web/src/compartido/formularios.ts");
    assert!(
        forms.contains("export function opcionesReferencia<"),
        "{forms}"
    );

    cleanup(&dir);
}

#[test]
fn should_label_a_target_without_a_string_field_by_its_id() {
    let dir = temp_dir("references_web_label_id");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), false, &dir).unwrap();
    let weight = vec![puerto_toml::parse_field_arg("weight:i64").unwrap()];
    scaffold::run_scaffold("Pallet", &output, &weight, OverwritePolicy::Ask).unwrap();
    let parcel = vec![puerto_toml::parse_field_arg("pallet_id:ref:Pallet").unwrap()];
    scaffold::run_scaffold("Parcel", &output, &parcel, OverwritePolicy::Ask).unwrap();

    let container = read(
        &output,
        "web/src/contextos/parcel/contenedores/formulario-parcel.tsx",
    );
    assert!(container.contains("(pallet) => pallet.id)"), "{container}");

    cleanup(&dir);
}

/// A form with selects of two different targets loads both: the test of one target's options holds
/// only that target, and the fake API answers the other with a `404` (its select's alert), never a
/// crash.
#[test]
fn should_offer_one_targets_items_while_the_fake_api_answers_the_other_with_a_404() {
    let dir = temp_dir("references_web_two_targets");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("shop".into()), false, &dir).unwrap();
    scaffold::run_scaffold("Cliente", &output, &[], OverwritePolicy::Ask).unwrap();
    scaffold::run_scaffold("Pallet", &output, &[], OverwritePolicy::Ask).unwrap();
    let parcel = vec![
        puerto_toml::parse_field_arg("cliente_id:ref:Cliente").unwrap(),
        puerto_toml::parse_field_arg("pallet_id:ref:Pallet").unwrap(),
    ];
    scaffold::run_scaffold("Parcel", &output, &parcel, OverwritePolicy::Ask).unwrap();

    let test = read(
        &output,
        "web/src/contextos/parcel/contenedores/formulario-parcel.test.tsx",
    );
    let offer = |target: &str| {
        let start = test
            .find(&format!(
                "test(\"debería ofrecer cada {target} en sus selects"
            ))
            .unwrap_or_else(|| panic!("no offer test for {target}\n{test}"));
        let body = &test[start..];
        body[..body.find("});").unwrap()].to_string()
    };
    let cliente = offer("cliente");
    assert!(
        cliente.contains("const api = apiFalsa([coleccionCliente([cliente])]);"),
        "{cliente}"
    );
    let pallet = offer("pallet");
    assert!(
        pallet.contains("const api = apiFalsa([coleccionPallet([pallet])]);"),
        "{pallet}"
    );
    let fake = read(&output, "web/src/pruebas/api-falsa.ts");
    assert!(
        fake.contains(r#"if (objetivo === undefined) return responderError(404, "http.404");"#),
        "{fake}"
    );

    cleanup(&dir);
}

#[test]
fn should_link_a_reference_to_its_target_and_say_when_it_is_missing() {
    let (dir, output) = pedido_project("references_web_detail", false, false);

    let detail = read(&output, &format!("{PEDIDO_WEB}/vistas/detalle-pedido.tsx"));
    // canasta's detail showed a UUID: the link is named by the target's label (Phase 20.6),
    // which the container loads — the view only takes props (ADR-0025).
    assert!(
        detail.contains(
            r#"<EnlaceReferencia ruta="/clientes" id={pedido.clienteId} cargar={etiquetaCliente} />"#
        ),
        "{detail}"
    );
    assert!(!detail.contains("useDependencias"), "{detail}");
    let container = read(
        &output,
        &format!("{PEDIDO_WEB}/contenedores/detalle-pedido.tsx"),
    );
    assert!(
        container.contains(
            r#"const encontrado = await desenvolver(api.GET("/clientes/{id}", { params }));"#
        ),
        "{container}"
    );
    assert!(
        container.contains("    return encontrado.nombre;"),
        "{container}"
    );
    assert!(
        output
            .join("web/src/compartido/ui/enlace-referencia.tsx")
            .exists()
    );
    let detail_test = read(
        &output,
        &format!("{PEDIDO_WEB}/contenedores/detalle-pedido.test.tsx"),
    );
    assert!(
        detail_test.contains("test(\"debería enlazar Reviewer con su página y su etiqueta\""),
        "{detail_test}"
    );
    assert!(
        detail_test.contains(
            "expect(await screen.findByText(objetivo.nombre, { selector })).toBeTruthy();"
        ),
        "{detail_test}"
    );

    let messages = read(&output, "web/src/contextos/pedido/mensajes.ts");
    assert!(
        messages
            .contains("\"pedido.validacion.cliente_id_no_encontrado\": \"Cliente: no existe\","),
        "{messages}"
    );
    assert!(
        messages.contains("\"pedido.validacion.reviewer_id_no_encontrado\": \"reviewerId\","),
        "{messages}"
    );

    cleanup(&dir);
}

// ── 19.6 (ADR-0023 § 3): each crate names only what its layer may depend on ──────────

/// The project crates the `[dependencies]` of `manifest` name (dev-dependencies left out: the
/// test helpers of another crate are fine there).
fn project_dependencies(manifest: &str) -> Vec<String> {
    let deps = manifest
        .split("[dependencies]")
        .nth(1)
        .unwrap_or("")
        .split("\n[")
        .next()
        .unwrap_or("");
    deps.lines()
        .filter_map(|l| l.split_once(" = ").map(|(name, _)| name.trim().to_string()))
        .filter(|name| {
            [
                "dominio_",
                "aplicacion_",
                "infra_",
                "rest_",
                "nucleo",
                "cableado",
            ]
            .iter()
            .any(|p| name.starts_with(p))
        })
        .collect()
}

#[test]
fn should_let_each_crate_depend_only_on_what_its_layer_may_use() {
    let (dir, output) = pedido_project("references_dependency_rule", true, true);

    let deps = |c: &str, layer: &str| {
        project_dependencies(&read(&output, &format!("contextos/{c}/{layer}/Cargo.toml")))
    };
    assert_eq!(deps("pedido", "dominio"), ["dominio_cliente"]);
    assert_eq!(
        deps("pedido", "aplicacion"),
        ["dominio_pedido", "dominio_cliente"]
    );
    // `infra_cliente` is optional, for the seeds of the Postgres tests (`pruebas`).
    assert_eq!(
        deps("pedido", "infraestructura"),
        ["dominio_pedido", "dominio_cliente", "infra_cliente"]
    );
    assert!(
        read(&output, "contextos/pedido/infraestructura/Cargo.toml")
            .contains("infra_cliente = { workspace = true, optional = true }")
    );
    assert_eq!(
        deps("pedido", "rest"),
        ["aplicacion_pedido", "dominio_pedido", "dominio_cliente"]
    );
    assert!(deps("cliente", "dominio").is_empty());
    // Only the composition root knows the adapters; it knows no HTTP.
    let cableado = read(&output, "presentacion/cableado/Cargo.toml");
    assert!(
        cableado.contains("infra_pedido = { workspace = true }"),
        "{cableado}"
    );
    assert!(
        !cableado.contains("rest_") && !cableado.contains("poem"),
        "{cableado}"
    );

    cleanup(&dir);
}

#[test]
fn should_refuse_a_reference_that_closes_a_cycle_naming_it() {
    // Arrange: Pedido → Cliente already.
    let (dir, output) = pedido_project("references_cycle", false, false);
    let field = puerto_toml::parse_field_arg("last_pedido_id:ref:opt:Pedido").unwrap();

    // Act
    let err = scaffold::run_scaffold("Cliente", &output, &[field], OverwritePolicy::Force)
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("Cliente → Pedido → Cliente"), "{err}");
    assert!(
        !read(&output, "contextos/cliente/dominio/src/modelo.rs").contains("last_pedido_id"),
        "nothing is rewritten"
    );
    cleanup(&dir);
}