puerto 0.33.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, 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
use super::*;

// ── puerto generar dominio / application / repository / presentation ─────

#[test]
fn generate_domain_creates_domain_files_and_mother() {
    let dir = temp_dir("gen_domain");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();

    assert!(
        output
            .join("contextos/artilugio/dominio/src/modelo.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/dominio/src/errores.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/dominio/src/repositorio.rs")
            .exists()
    );
    // The use cases — contract and implementation — are the application crate's.
    assert!(!output.join("contextos/artilugio/aplicacion").exists());
    assert!(
        output
            .join("contextos/artilugio/dominio/src/pruebas/madres.rs")
            .exists()
    );

    let lib = fs::read_to_string(output.join("contextos/artilugio/dominio/src/lib.rs")).unwrap();
    assert!(lib.contains("pub mod modelo;"), "{lib}");
    assert!(lib.contains("pub mod madres;"), "{lib}");

    let toml = fs::read_to_string(output.join("puerto.toml")).unwrap();
    assert!(toml.contains("Artilugio"));

    cleanup(&dir);
}

// ── T-06: `generate domain` accepts typed fields ──────────────────────────

#[test]
fn generate_domain_writes_cli_fields_into_model_and_manifest() {
    let dir = temp_dir("gen_domain_fields");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    let fields = vec![
        puerto_toml::parse_field_arg("amount:i64").unwrap(),
        puerto_toml::parse_field_arg("status:Status:enum:Draft/Paid").unwrap(),
    ];
    scaffold::run_generate_domain("Factura", &output, &fields).unwrap();

    let model = fs::read_to_string(output.join("contextos/factura/dominio/src/modelo.rs")).unwrap();
    assert!(model.contains("pub amount: i64"), "got:\n{model}");
    assert!(model.contains("pub status: Status"), "got:\n{model}");

    // The manifest is what makes the *later* layered commands field-aware.
    let config = puerto_toml::read(&output).unwrap();
    let factura = config
        .entity
        .iter()
        .find(|e| e.name == "Factura")
        .expect("Factura in puerto.toml");
    let names: Vec<&str> = factura.fields.iter().map(|f| f.name.as_str()).collect();
    assert_eq!(names, vec!["amount", "status"]);

    cleanup(&dir);
}

#[test]
fn generate_domain_fields_reach_the_later_layers() {
    // The whole point of persisting fields in `generate domain`: application, repository and
    // presentation all read them back from puerto.toml. They used to be written as `vec![]`, so
    // the entire domain-first workflow could only ever produce the default `name: String`.
    let dir = temp_dir("gen_domain_fields_layers");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    let fields = vec![puerto_toml::parse_field_arg("amount:i64").unwrap()];
    scaffold::run_generate_domain("Factura", &output, &fields).unwrap();
    scaffold::run_generate_application("Factura", &output, OverwritePolicy::Ask).unwrap();
    scaffold::run_generate_repository("Factura", &output, OverwritePolicy::Ask).unwrap();
    scaffold::run_generate_presentation("Factura", &output, OverwritePolicy::Ask).unwrap();

    let impl_rs =
        fs::read_to_string(output.join("contextos/factura/aplicacion/src/crear_factura.rs"))
            .unwrap();
    assert!(impl_rs.contains("amount"), "got:\n{impl_rs}");

    let dto = fs::read_to_string(output.join("contextos/factura/rest/src/dto.rs")).unwrap();
    assert!(dto.contains("pub amount: i64"), "got:\n{dto}");

    cleanup(&dir);
}

#[test]
fn generate_domain_rejects_conflicting_value_object_types() {
    let dir = temp_dir("gen_domain_vo_conflict");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    let fields = vec![
        puerto_toml::parse_field_arg("a:Code:String").unwrap(),
        puerto_toml::parse_field_arg("b:Code:i64").unwrap(),
    ];
    let result = scaffold::run_generate_domain("Factura", &output, &fields);

    assert!(result.is_err());
    assert!(
        result.unwrap_err().to_string().contains("Code"),
        "error should name the conflicting value object"
    );

    cleanup(&dir);
}

#[test]
fn generate_application_creates_use_case_impls() {
    let dir = temp_dir("gen_application");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();
    scaffold::run_generate_application("Artilugio", &output, OverwritePolicy::Ask).unwrap();

    assert!(
        output
            .join("contextos/artilugio/aplicacion/src/crear_artilugio.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/aplicacion/src/obtener_artilugio.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/aplicacion/src/listar_artilugios.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/aplicacion/src/actualizar_artilugio.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/aplicacion/src/borrar_artilugio.rs")
            .exists()
    );

    let lib = fs::read_to_string(output.join("contextos/artilugio/aplicacion/src/lib.rs")).unwrap();
    assert!(lib.contains("pub mod crear_artilugio;"), "{lib}");

    cleanup(&dir);
}

#[test]
fn generate_application_errors_without_prior_domain() {
    let dir = temp_dir("gen_app_no_domain");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    let result = scaffold::run_generate_application("Ghost", &output, OverwritePolicy::Ask);
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("no está en puerto.toml")
    );

    cleanup(&dir);
}

#[test]
fn generate_repository_creates_infra_files() {
    let dir = temp_dir("gen_repository");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();
    scaffold::run_generate_repository("Artilugio", &output, OverwritePolicy::Ask).unwrap();

    assert!(
        output
            .join("contextos/artilugio/infraestructura/src/repositorio.rs")
            .exists()
    );
    let repo =
        fs::read_to_string(output.join("contextos/artilugio/infraestructura/src/repositorio.rs"))
            .unwrap();
    assert!(repo.contains("RepositorioArtilugioEnMemoria"));

    cleanup(&dir);
}

#[test]
fn generate_presentation_creates_all_files_and_regenerates_bootstrap() {
    let dir = temp_dir("gen_presentation");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();
    scaffold::run_generate_application("Artilugio", &output, OverwritePolicy::Ask).unwrap();
    scaffold::run_generate_repository("Artilugio", &output, OverwritePolicy::Ask).unwrap();
    scaffold::run_generate_presentation("Artilugio", &output, OverwritePolicy::Ask).unwrap();

    assert!(output.join("contextos/artilugio/rest/src/lib.rs").exists());
    assert!(
        output
            .join("contextos/artilugio/rest/src/rutas.rs")
            .exists()
    );
    assert!(output.join("contextos/artilugio/rest/src/dto.rs").exists());
    assert!(
        output
            .join("contextos/artilugio/rest/src/respuestas.rs")
            .exists()
    );
    assert!(
        output
            .join("contextos/artilugio/rest/src/errores.rs")
            .exists()
    );

    let bootstrap = wiring(&output);
    assert!(bootstrap.contains("ApiArtilugio"));

    cleanup(&dir);
}

#[test]
fn should_refuse_the_api_until_the_application_crate_exists() {
    // The routes hold the use cases' traits, which live in the application crate (ADR-0023 § 4):
    // written right after `domain`, the REST crate named a crate that does not exist and the
    // workspace stopped loading.
    let dir = temp_dir("gen_presentation_api_first");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();
    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();

    // Act
    let err = scaffold::run_generate_presentation("Artilugio", &output, OverwritePolicy::Ask)
        .unwrap_err()
        .to_string();

    // Assert
    assert!(err.contains("puerto generar aplicacion Artilugio"), "{err}");
    assert!(!output.join("contextos/artilugio/rest").exists());

    cleanup(&dir);
}

#[test]
fn should_write_the_api_but_not_the_wiring_before_the_repository() {
    // `bootstrap.rs` and `cableado` also name the repository adapter: rewritten without it, the
    // project stopped compiling while the command printed "All layers wired".
    let dir = temp_dir("gen_presentation_before_repo");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();
    scaffold::run_generate_domain("Artilugio", &output, &[]).unwrap();
    scaffold::run_generate_application("Artilugio", &output, OverwritePolicy::Ask).unwrap();
    let before = wiring(&output);

    // Act
    scaffold::run_generate_presentation("Artilugio", &output, OverwritePolicy::Ask).unwrap();

    // Assert
    assert!(
        output
            .join("contextos/artilugio/rest/src/rutas.rs")
            .exists()
    );
    assert_eq!(
        wiring(&output),
        before,
        "the wiring waits for the repository"
    );

    cleanup(&dir);
}

#[test]
fn generate_presentation_errors_without_prior_domain() {
    let dir = temp_dir("gen_pres_no_domain");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    let result = scaffold::run_generate_presentation("Ghost", &output, OverwritePolicy::Ask);
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("no está en puerto.toml")
    );

    cleanup(&dir);
}

#[test]
fn generate_scaffold_includes_object_mother() {
    let dir = temp_dir("scaffold_with_mother");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();

    scaffold::run("Artilugio", &output, false, &[], &[]).unwrap();

    assert!(
        output
            .join("contextos/artilugio/dominio/src/pruebas/madres.rs")
            .exists()
    );
    let mother =
        fs::read_to_string(output.join("contextos/artilugio/dominio/src/pruebas/madres.rs"))
            .unwrap();
    assert!(mother.contains("MadreArtilugio"));
    assert!(mother.contains("pub fn aleatorio()"));

    cleanup(&dir);
}

// ── Fase 33.1: the per-layer commands do not overwrite without --forzar ──

/// A scaffolded `Artilugio` with `// a mano` appended to one file of each layer.
fn scaffolded_with_hand_edits(name: &str) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();
    scaffold::run_scaffold("Artilugio", &output, &[], OverwritePolicy::Ask).unwrap();
    for file in [
        "contextos/artilugio/aplicacion/src/crear_artilugio.rs",
        "contextos/artilugio/infraestructura/src/repositorio.rs",
        "contextos/artilugio/rest/src/rutas.rs",
    ] {
        let path = output.join(file);
        let content = fs::read_to_string(&path).unwrap();
        fs::write(&path, format!("{content}\n// a mano\n")).unwrap();
    }
    (dir, output)
}

type LayerCommand = fn(&str, &Path, OverwritePolicy) -> Result<(), Box<dyn std::error::Error>>;

fn keeps_hand_edit(output: &Path, file: &str) -> bool {
    fs::read_to_string(output.join(file))
        .unwrap()
        .contains("// a mano")
}

#[test]
fn should_refuse_to_overwrite_a_layer_changed_by_hand_without_forzar() {
    // Arrange
    let (dir, output) = scaffolded_with_hand_edits("layer_guard_refuses");
    let cases: [(&str, LayerCommand); 3] = [
        (
            "contextos/artilugio/aplicacion/src/crear_artilugio.rs",
            scaffold::run_generate_application,
        ),
        (
            "contextos/artilugio/infraestructura/src/repositorio.rs",
            scaffold::run_generate_repository,
        ),
        (
            "contextos/artilugio/rest/src/rutas.rs",
            scaffold::run_generate_presentation,
        ),
    ];

    for (file, command) in cases {
        // Act
        let err = command("Artilugio", &output, OverwritePolicy::Ask)
            .unwrap_err()
            .to_string();

        // Assert
        assert!(err.contains("--forzar"), "{file}: {err}");
        assert!(keeps_hand_edit(&output, file), "{file} was overwritten");
    }

    cleanup(&dir);
}

#[test]
fn should_overwrite_a_layer_changed_by_hand_with_forzar() {
    // Arrange
    let (dir, output) = scaffolded_with_hand_edits("layer_guard_forced");

    // Act
    scaffold::run_generate_application("Artilugio", &output, OverwritePolicy::Force).unwrap();
    scaffold::run_generate_repository("Artilugio", &output, OverwritePolicy::Force).unwrap();
    scaffold::run_generate_presentation("Artilugio", &output, OverwritePolicy::Force).unwrap();

    // Assert
    for file in [
        "contextos/artilugio/aplicacion/src/crear_artilugio.rs",
        "contextos/artilugio/infraestructura/src/repositorio.rs",
        "contextos/artilugio/rest/src/rutas.rs",
    ] {
        assert!(!keeps_hand_edit(&output, file), "{file} was not rewritten");
    }

    cleanup(&dir);
}

#[test]
fn should_not_suggest_rewriting_the_layer_when_one_of_its_files_was_deleted() {
    // `validar` used to answer a deleted use case with `generar aplicacion`, which rewrote every
    // other use case of the layer.
    let dir = temp_dir("drift_deleted_use_case");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("myapp", &dir).unwrap();
    scaffold::run_scaffold("Artilugio", &output, &[], OverwritePolicy::Ask).unwrap();
    fs::remove_file(output.join("contextos/artilugio/aplicacion/src/borrar_artilugio.rs")).unwrap();

    // Act
    let config = puerto_toml::read(&output).unwrap();
    let lines = crate::generators::consistency::drift_lines(
        &crate::generators::consistency::missing_entity_files(&output, &config),
    );

    // Assert
    let line = lines.join("\n");
    assert!(!line.contains("generar aplicacion"), "{line}");
    assert!(line.contains("casos_de_uso"), "{line}");
    assert!(line.contains("puerto generar bootstrap"), "{line}");

    cleanup(&dir);
}