ocre-cli 0.1.0

Command-line tool for Ocre: create, generate, migrate, run and deploy apps.
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
//! Generator infrastructure: --pretend/--force/--skip, generation records and
//! `ocre destroy`, `ocre g controller|resource|override|generator`, app
//! generators, and the field types they share.

#[path = "../support/mod.rs"]
mod support;

use std::{fs, path::Path};

use serde_json::{Value, json};
use support::{Sandbox, text};

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

fn ok(sandbox: &Sandbox, args: &[&str], root: &Path) -> Value {
    let (report, ok) = sandbox.json(args, root);
    assert!(ok, "{args:?}: {report}");
    report
}

fn fails(sandbox: &Sandbox, args: &[&str], root: &Path) -> Value {
    let (report, ok) = sandbox.json(args, root);
    assert!(!ok, "{args:?} should fail: {report}");
    report
}

fn records(root: &Path) -> Vec<String> {
    let mut names: Vec<String> = fs::read_dir(root.join(".ocre/generated"))
        .map(|dir| dir.map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect())
        .unwrap_or_default();
    names.sort();
    names
}

// ---------- --pretend, --force, --skip ----------

#[test]
fn pretend_reports_without_writing_anything() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let lib = read(&root, "src/lib.rs");
    let report = ok(&sandbox, &["g", "--pretend", "controller", "Pages", "about"], &root);
    assert_eq!(report["pretend"], true);
    assert_eq!(report["created"], json!(["src/pages.rs", "templates/pages/about.html"]));
    assert_eq!(report["updated"], json!(["src/lib.rs"]));
    assert!(!root.join("src/pages.rs").exists() && !root.join(".ocre").exists());
    assert_eq!(read(&root, "src/lib.rs"), lib);
    let output = sandbox.ocre(&["g", "controller", "Pages", "about", "--pretend"], &root);
    assert!(text(&output).0.contains("(--pretend: nothing was written)"), "{}", text(&output).0);
}

#[test]
fn existing_files_fail_unless_forced_or_skipped() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    ok(&sandbox, &["g", "controller", "Pages", "about"], &root);
    fs::write(root.join("templates/pages/about.html"), "mine").unwrap();
    let report = fails(&sandbox, &["g", "controller", "Pages", "about", "contact"], &root);
    assert_eq!(report["error"], "src/pages.rs already exists");
    assert!(report["hint"].as_str().unwrap().contains("--skip"));

    let report = ok(&sandbox, &["g", "controller", "Pages", "about", "contact", "--skip"], &root);
    assert_eq!(report["skipped"], json!(["src/pages.rs", "templates/pages/about.html"]));
    assert_eq!(report["created"], json!(["templates/pages/contact.html"]));
    assert_eq!(read(&root, "templates/pages/about.html"), "mine");

    let report = ok(&sandbox, &["g", "controller", "Pages", "about", "contact", "--force"], &root);
    assert_eq!(
        report["updated"],
        json!(["src/pages.rs", "templates/pages/about.html"]),
        "unchanged files are not rewritten"
    );
    assert!(read(&root, "src/pages.rs").contains("fn contact()"));
    assert!(read(&root, "templates/pages/about.html").contains("<h1>About</h1>"));
    let lib = read(&root, "src/lib.rs");
    assert_eq!(lib.matches("mod pages;").count(), 1, "markers are not inserted twice: {lib}");
    assert_eq!(lib.matches(".merge(pages::routes())").count(), 1);
    let (stdout, _) = text(&sandbox.ocre(&["g", "controller", "Pages", "about", "--skip"], &root));
    assert!(stdout.starts_with("  skip    src/pages.rs\n  skip    templates/pages/about.html\n\nNext:"), "{stdout}");

    let (_, stderr) = text(&sandbox.ocre(&["g", "controller", "Pages", "--force", "--skip"], &root));
    assert!(stderr.contains("cannot be used with"), "{stderr}");
}

// ---------- records and ocre destroy ----------

#[test]
fn every_run_is_recorded_and_destroy_takes_it_back() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let lib = read(&root, "src/lib.rs");
    ok(&sandbox, &["g", "model", "Author", "name:string"], &root);
    ok(&sandbox, &["g", "scaffold", "BlogPost", "title:string", "author:references"], &root);
    assert_eq!(records(&root), ["0001_model_author.json", "0002_scaffold_blogpost.json"]);
    let record: Value = serde_json::from_str(&read(&root, ".ocre/generated/0002_scaffold_blogpost.json")).unwrap();
    assert_eq!(record["command"], "ocre g scaffold BlogPost title:string author:references");
    assert_eq!((&record["generator"], &record["name"]), (&json!("scaffold"), &json!("BlogPost")));
    assert_eq!(record["created"][0]["path"], "migrations/0002_create_blog_posts.sql");
    assert_eq!(record["created"][0]["sha256"].as_str().unwrap().len(), 64);
    let author = &record["updated"][0];
    assert_eq!(author["path"], "src/models/author.rs");
    assert_eq!(author["hunks"][0]["after"], "    // ocre:associations");

    let report = ok(&sandbox, &["destroy", "scaffold", "blog-post", "--pretend"], &root);
    assert_eq!(report["pretend"], true);
    assert!(root.join("src/blog_posts.rs").exists());

    let report = ok(&sandbox, &["d", "scaffold", "blog_post"], &root);
    assert_eq!(
        report["updated"],
        json!(["src/models/author.rs", "src/models/mod.rs", "tests/factories/mod.rs", "src/lib.rs"])
    );
    assert!(report["removed"].as_array().unwrap().contains(&json!("templates/blog_posts/index.html")));
    assert_eq!(report["removed"].as_array().unwrap().last().unwrap(), ".ocre/generated/0002_scaffold_blogpost.json");
    assert!(report["next"][0].as_str().unwrap().contains("migrations/0002_create_blog_posts.sql"));
    assert!(!root.join("templates/blog_posts").exists(), "empty directories go too");
    assert!(!read(&root, "src/models/author.rs").contains("blog_posts"));

    ok(&sandbox, &["destroy", "model"], &root);
    assert_eq!(read(&root, "src/lib.rs"), lib, "lib.rs is back to what `ocre new` wrote");
    assert!(!root.join("src/models").exists() && !root.join(".ocre").exists());
}

#[test]
fn destroy_refuses_changed_files_unless_forced() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    ok(&sandbox, &["g", "controller", "Pages", "about"], &root);
    ok(&sandbox, &["g", "controller", "Help"], &root);
    fs::write(root.join("templates/pages/about.html"), "edited").unwrap();
    let lib = read(&root, "src/lib.rs").replace(".merge(pages::routes())", ".merge(pages::routes()) // edited");
    fs::write(root.join("src/lib.rs"), lib).unwrap();
    let report = fails(&sandbox, &["destroy", "controller", "Pages"], &root);
    assert_eq!(
        report["error"],
        "cannot destroy `ocre g controller Pages about`: templates/pages/about.html changed since it was generated; src/lib.rs: the generated `.merge(pages::routes())` changed"
    );
    assert!(root.join("src/pages.rs").exists());

    let report = ok(&sandbox, &["destroy", "controller", "Pages", "--force"], &root);
    assert_eq!(report["skipped"], json!(["src/lib.rs"]));
    assert!(!root.join("templates/pages").exists());
    let lib = read(&root, "src/lib.rs");
    assert!(!lib.contains("mod pages;") && lib.contains("// edited") && lib.contains("mod help;"), "{lib}");

    fs::remove_file(root.join("src/lib.rs")).unwrap();
    let report = fails(&sandbox, &["destroy", "controller"], &root);
    assert!(report["error"].as_str().unwrap().ends_with("src/lib.rs no longer exists"), "{report}");
    fs::remove_file(root.join("src/help.rs")).unwrap();
    ok(&sandbox, &["destroy", "controller", "--force"], &root);
}

#[test]
fn destroy_keeps_cloudflare_config_changes() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    ok(&sandbox, &["g", "cache"], &root);
    let (cargo, config) = (read(&root, "Cargo.toml"), read(&root, "cloudflare.config.ts"));
    assert!(config.contains("CACHE: bindings.kv(),"), "{config}");
    let report = ok(&sandbox, &["destroy", "cache"], &root);
    assert_eq!(report["skipped"], json!(["cloudflare.config.ts"]));
    assert_eq!((read(&root, "Cargo.toml"), read(&root, "cloudflare.config.ts")), (cargo, config));
}

#[test]
fn destroy_names_what_it_can_undo() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = fails(&sandbox, &["destroy", "scaffold", "Post"], &root);
    assert_eq!(report["error"], "no recorded `ocre g scaffold Post` run to destroy");
    assert!(report["hint"].as_str().unwrap().contains("this app has none"));
    ok(&sandbox, &["g", "controller", "Pages"], &root);
    let report = fails(&sandbox, &["destroy", "controller", "Help"], &root);
    assert_eq!(report["hint"], "recorded runs: ocre g controller Pages");
    fs::write(root.join(".ocre/generated/0002_broken.json"), "{").unwrap();
    fs::write(root.join(".ocre/generated/notes.txt"), "").unwrap();
    fs::write(root.join(".ocre/generated/0003_notes.txt"), "").unwrap();
    let report = fails(&sandbox, &["destroy", "controller", "Pages"], &root);
    assert!(
        report["error"]
            .as_str()
            .unwrap()
            .starts_with(".ocre/generated/0002_broken.json is not a valid generation record")
    );
}

#[test]
fn a_forced_overwrite_is_restored_by_destroy() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    fs::create_dir_all(root.join("templates/pages")).unwrap();
    fs::write(root.join("templates/pages/index.html"), "old page\n").unwrap();
    ok(&sandbox, &["g", "controller", "Pages", "--force"], &root);
    assert!(read(&root, "templates/pages/index.html").contains("<h1>Index</h1>"));
    let (stdout, _) = text(&sandbox.ocre(&["destroy", "controller", "Pages"], &root));
    assert!(
        stdout.starts_with("  update  templates/pages/index.html\n  update  src/lib.rs\n  remove  src/pages.rs\n"),
        "{stdout}"
    );
    assert_eq!(read(&root, "templates/pages/index.html"), "old page\n");
}

#[test]
fn the_blog_starter_records_its_scaffold() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &["--starter", "blog"]);
    let record: Value = serde_json::from_str(&read(&root, ".ocre/generated/0001_scaffold_post.json")).unwrap();
    assert_eq!(record["command"], "ocre g scaffold Post title:string body:text published:boolean");
    ok(&sandbox, &["destroy", "scaffold", "Post"], &root);
    assert!(!root.join("src/posts.rs").exists());
}

// ---------- ocre g controller ----------

#[test]
fn controller_generates_html_pages_with_paths() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = ok(&sandbox, &["g", "controller", "PagesController", "index", "AboutUs"], &root);
    assert_eq!(
        report["created"],
        json!(["src/pages.rs", "templates/pages/index.html", "templates/pages/about_us.html"])
    );
    assert_eq!(report["next"], json!(["ocre dev", "open http://localhost:8787/pages"]));
    let code = read(&root, "src/pages.rs");
    assert!(
        code.starts_with("//! Pages pages (HTML). Generated by `ocre g controller PagesController index about_us`.")
    );
    assert!(code.contains(".route(\"/pages\", get(index))\n        .route(\"/pages/about_us\", get(about_us))\n}"));
    assert!(code.contains("    pub fn index() -> &'static str {\n        \"/pages\"\n    }\n\n    pub fn about_us()"));
    assert!(code.contains("async fn about_us() -> Result<Html<String>> {\n    render(&AboutUsView)\n}\n"));
    assert!(!code.contains("CurrentUser"));
    assert!(read(&root, "templates/pages/about_us.html").contains("{% block title %}About us{% endblock %}"));
    let lib = read(&root, "src/lib.rs");
    assert!(lib.contains("mod pages;") && lib.contains(".merge(pages::routes())"));

    let report = ok(&sandbox, &["g", "controller", "Reports", "--api"], &root);
    assert_eq!(report["created"], json!(["src/reports_api.rs"]));
    assert_eq!(report["next"][1], "curl http://localhost:8787/api/reports");
    let code = read(&root, "src/reports_api.rs");
    assert!(code.contains("//! GET /api/reports\n"));
    assert!(code.contains("Ok(Json(IndexResponse { message: \"Edit index in src/reports_api.rs\" }))"));
}

#[test]
fn controller_actions_need_valid_names() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    for (args, error) in [
        (&["g", "controller", "fn"][..], "invalid controller name `fn`"),
        (&["g", "controller", "Stats_api"], "invalid controller name `Stats_api`"),
        (&["g", "controller", "Pages", "routes"], "invalid action name `routes`"),
        (&["g", "controller", "Pages", "9lives"], "invalid action name `9lives`"),
        (&["g", "controller", "Pages", "about", "About"], "action `about` is listed twice"),
    ] {
        assert_eq!(fails(&sandbox, args, &root)["error"], error);
    }
}

#[test]
fn controller_auth_needs_the_auth_generator() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = fails(&sandbox, &["g", "controller", "Dashboard", "--auth"], &root);
    assert_eq!(report["error"], "--auth needs src/auth.rs, which `ocre g auth` creates");
    ok(&sandbox, &["g", "auth"], &root);
    ok(&sandbox, &["g", "controller", "Dashboard", "--auth"], &root);
    let code = read(&root, "src/dashboard.rs");
    assert!(code.contains("use crate::auth::CurrentUser;"));
    assert!(code.contains("async fn index(CurrentUser(_user): CurrentUser) -> Result<Html<String>> {"));
    ok(&sandbox, &["g", "controller", "Stats", "--api", "--auth"], &root);
    let code = read(&root, "src/stats_api.rs");
    assert!(code.contains("use crate::auth_api::BearerUser;"));
    assert!(code.contains("async fn index(BearerUser(_user): BearerUser) -> ApiResult<Json<IndexResponse>> {"));
}

#[test]
fn controller_in_an_api_app_is_json() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("api", &["--api"]);
    let report = ok(&sandbox, &["g", "controller", "Health", "check"], &root);
    assert_eq!(report["created"], json!(["src/health_api.rs"]));
    assert!(read(&root, "src/health_api.rs").contains(".route(\"/api/health/check\", get(check))"));
    let report = fails(&sandbox, &["g", "controller", "Admin", "--auth"], &root);
    assert_eq!(report["error"], "--auth needs src/auth_api.rs, which `ocre g auth` creates");
}

// ---------- ocre g resource ----------

#[test]
fn resource_generates_model_index_and_show() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = ok(
        &sandbox,
        &["g", "resource", "Product", "name:string", "photo:attachment?", "sku:uuid?", "manual:attachment"],
        &root,
    );
    assert!(report["created"].as_array().unwrap().contains(&json!("src/models/product.rs")));
    assert_eq!(report["next"][2], "open http://localhost:8787/products");
    let code = read(&root, "src/products.rs");
    assert!(code.contains(".route(\"/products/{id}\", get(show))"));
    assert!(code.contains("render(&ShowView { product: product::find(&ctx, id).await?.or_404()? })"));
    let show = read(&root, "templates/products/show.html");
    assert!(show.contains("  <dt>Name</dt><dd>{{ product.name }}</dd>\n"));
    assert!(show.contains("<dd>{% if let Some(file) = product.photo() %}{{ file.filename }}{% endif %}</dd>"));
    assert!(show.contains("<dd>{% if let Some(value) = product.sku %}{{ value }}{% endif %}</dd>"));
    assert!(show.contains("<dd>{{ product.manual_filename }}</dd>"));
    assert!(
        read(&root, "templates/products/index.html")
            .contains("<a href=\"{{ paths::show(product.id) }}\">Product {{ product.id }}</a>")
    );

    let report = ok(&sandbox, &["g", "resource", "Order", "total:decimal", "--api"], &root);
    assert!(report["created"].as_array().unwrap().contains(&json!("src/orders_api.rs")));
    assert_eq!(report["next"][2], "curl http://localhost:8787/api/orders");
    assert!(read(&root, "src/orders_api.rs").contains("Ok((page.links(\"/api/orders\", orders.len()), Json(orders)))"));
}

// ---------- template overrides ----------

#[test]
fn override_lists_copies_and_uses_templates() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = ok(&sandbox, &["g", "override"], &root);
    let templates = report["templates"].as_array().unwrap();
    assert!(templates.contains(&json!({ "path": "scaffold/index.html", "overridden": false })));
    let report = ok(&sandbox, &["g", "override", "scaffold/", "controller/view.html"], &root);
    assert_eq!(report["created"].as_array().unwrap().len(), 7);
    assert!(
        ok(&sandbox, &["g", "override"], &root)["templates"]
            .as_array()
            .unwrap()
            .contains(&json!({ "path": "controller/view.html", "overridden": true }))
    );
    let output = sandbox.ocre(&["g", "override"], &root);
    assert!(text(&output).0.contains("  scaffold/show.html  (overridden in .ocre/templates/)\n"));

    fs::write(
        root.join(".ocre/templates/controller/view.html"),
        "<p><%= action.human | upper %> in <%= module %></p>\n",
    )
    .unwrap();
    ok(&sandbox, &["g", "controller", "Pages", "about"], &root);
    assert_eq!(read(&root, "templates/pages/about.html"), "<p>ABOUT in pages</p>\n");

    let index = read(&root, ".ocre/templates/scaffold/index.html").replace("<h1>", "<h1 class=\"title\">");
    fs::write(root.join(".ocre/templates/scaffold/index.html"), index).unwrap();
    ok(&sandbox, &["g", "scaffold", "Post", "title:string"], &root);
    assert!(read(&root, "templates/posts/index.html").contains("<h1 class=\"title\">Posts</h1>"));

    fs::write(root.join(".ocre/templates/scaffold/show.html"), "<%= missing %>").unwrap();
    let report = fails(&sandbox, &["g", "scaffold", "Tag", "name:string"], &root);
    assert!(
        report["error"]
            .as_str()
            .unwrap()
            .starts_with(".ocre/templates/scaffold/show.html failed to render: undefined value"),
        "{report}"
    );
    assert_eq!(
        report["hint"],
        "fix .ocre/templates/scaffold/show.html, or delete it to use the built-in template again"
    );

    let report = fails(&sandbox, &["g", "override", "mailer"], &root);
    assert_eq!(report["error"], "no generator template `mailer`");
}

// ---------- app generators ----------

#[test]
fn app_generators_render_files_and_insert_lines() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = ok(&sandbox, &["g", "generator", "Service"], &root);
    assert_eq!(
        report["created"],
        json!([".ocre/generators/service/generator.toml", ".ocre/generators/service/src/services/<%= singular %>.rs"])
    );
    let report = ok(&sandbox, &["g", "service", "Billing", "amount:decimal", "note:text?", "--pretend"], &root);
    assert_eq!(report["created"], json!(["src/services/billing.rs"]));
    ok(&sandbox, &["g", "service", "Billing", "amount:decimal", "note:text?"], &root);
    assert_eq!(
        read(&root, "src/services/billing.rs"),
        "//! Billing. Generated by `ocre g service Billing`.\n\npub struct Billing {\n    pub amount: String,\n    pub note: Option<String>,\n}\n"
    );

    fs::write(
        root.join(".ocre/generators/service/generator.toml"),
        "description = \"x\"\n[[insert]]\nfile = \"src/lib.rs\"\nafter = \"// ocre:modules\"\nline = \"mod <%= plural %>; // <%= args | join(',') %> <%= options.api %> <%= options.dry_run %>\"\n",
    )
    .unwrap();
    fs::write(root.join(".ocre/generators/service/src/services/<%= singular %>.rs"), "// <%= fields | length %>\n")
        .unwrap();
    let report = ok(&sandbox, &["g", "service", "Invoice", "a", "b", "--api=v2", "--dry-run"], &root);
    assert_eq!(report["updated"], json!(["src/lib.rs"]));
    assert_eq!(read(&root, "src/services/invoice.rs"), "// 0\n");
    assert!(read(&root, "src/lib.rs").contains("mod invoices; // a,b v2 true\n"));
    ok(&sandbox, &["destroy", "service", "Invoice"], &root);
    assert!(!read(&root, "src/lib.rs").contains("mod invoices;"));
}

#[test]
fn app_generators_report_their_mistakes() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let report = fails(&sandbox, &["g", "service", "X"], &root);
    assert_eq!(report["error"], "unknown generator `service`");
    ok(&sandbox, &["g", "generator", "service"], &root);
    let dir = root.join(".ocre/generators/service");
    assert_eq!(fails(&sandbox, &["g", "service"], &root)["error"], "`ocre g service` needs a name");
    fs::write(dir.join("generator.toml"), "descriptin = 1\n").unwrap();
    assert!(
        fails(&sandbox, &["g", "service", "X"], &root)["error"]
            .as_str()
            .unwrap()
            .starts_with(".ocre/generators/service/generator.toml is invalid")
    );
    fs::write(dir.join("generator.toml"), "[[insert]]\nfile = \"src/missing.rs\"\nafter = \"// m\"\nline = \"x\"\n")
        .unwrap();
    assert_eq!(fails(&sandbox, &["g", "service", "X"], &root)["error"], "src/missing.rs does not exist");
    fs::write(dir.join("generator.toml"), "[[insert]]\nfile = \"src/lib.rs\"\nafter = \"// m\"\nline = \"x\"\n")
        .unwrap();
    assert_eq!(fails(&sandbox, &["g", "service", "X"], &root)["error"], "src/lib.rs is missing the `// m` marker");
    fs::remove_file(dir.join("generator.toml")).unwrap();
    fs::write(dir.join("src/services/<%= singular %>.rs"), "<% if %>").unwrap();
    let report = fails(&sandbox, &["g", "service", "X"], &root);
    assert!(
        report["error"]
            .as_str()
            .unwrap()
            .starts_with(".ocre/generators/service/src/services/<%= singular %>.rs failed to render"),
        "{report}"
    );
    assert_eq!(report["hint"], "fix .ocre/generators/service/src/services/<%= singular %>.rs");
    let report = fails(&sandbox, &["g", "generator", "service"], &root);
    assert_eq!(report["error"], ".ocre/generators/service/src/services/<%= singular %>.rs already exists");
    assert!(!root.join(".ocre/generators/service/generator.toml").exists(), "nothing written on failure");
}

// ---------- field types ----------

#[test]
fn field_type_aliases_and_new_types() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    ok(&sandbox, &["g", "model", "Author", "name:string"], &root);
    ok(
        &sandbox,
        &[
            "g",
            "scaffold",
            "Book",
            "pages:small_int",
            "views:big_int",
            "stock:int",
            "weight:double",
            "price:decimal",
            "opens:time?",
            "isbn:uuid^",
            "done:bool",
            "at:date_time",
            "meta:jsonb?",
            "author:references:writer_id?",
        ],
        &root,
    );
    let sql = read(&root, "migrations/0002_create_books.sql");
    for column in [
        "pages INTEGER NOT NULL",
        "views INTEGER NOT NULL",
        "weight REAL NOT NULL",
        "price TEXT NOT NULL",
        "opens TEXT,",
        "isbn TEXT NOT NULL",
        "done INTEGER NOT NULL DEFAULT 0",
        "meta TEXT CHECK (json_valid(meta))",
        "writer_id INTEGER REFERENCES authors(id)",
        "CREATE UNIQUE INDEX index_books_on_isbn ON books (isbn);",
    ] {
        assert!(sql.contains(column), "{column} in {sql}");
    }
    let model = read(&root, "src/models/book.rs");
    for check in ["v.decimal(\"price\", &self.price);", "v.time(\"opens\", opens);", "v.uuid(\"isbn\", &self.isbn);"] {
        assert!(model.contains(check), "{check} in {model}");
    }
    assert!(model.contains("pub async fn writer(&self, ctx: &Ctx)"), "{model}");
    let form = read(&root, "templates/books/_form.html");
    assert!(form.contains("<label>Opens <input type=\"time\" name=\"opens\" value=\"{{ form.opens }}\"></label>"));
    assert!(form.contains("<input inputmode=\"decimal\" name=\"price\" value=\"{{ form.price }}\" required>"));
    assert!(form.contains("<label>Writer <input type=\"number\""), "{form}");
    assert!(read(&root, "src/models/author.rs").contains("books"));
}

#[test]
fn field_type_arguments_are_checked() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    ok(&sandbox, &["g", "model", "Author", "name:string"], &root);
    for (field, error) in [
        ("author:references:writer", "invalid foreign key column `writer` for `author`"),
        ("author:references:Writer_id?", "invalid foreign key column `Writer_id` for `author`"),
        ("title:string:long", "type `string` of `title` takes no `:long`"),
        ("size:tiny", "unknown field type `tiny` for `size`"),
        ("done:boolean?", "boolean field `done` cannot be optional"),
        ("cover:attachment^", "attachment `cover` cannot be unique"),
        ("data:json^", "json field `data` cannot be unique"),
        ("edit:attachment", "attachment name `edit` clashes with a scaffold route"),
        ("status:enum:draft,draft", "invalid values `draft,draft` for enum `status`"),
        ("status:enum:draft,published^", "enum `status` cannot be unique"),
        ("status:enum", "enum `status` has no values"),
    ] {
        let report = fails(&sandbox, &["g", "model", "Book", field], &root);
        assert_eq!(report["error"], error, "{field}");
    }
    let report = fails(&sandbox, &["g", "model", "Book", "size:tiny"], &root);
    assert!(report["hint"].as_str().unwrap().contains("decimal, boolean (bool), date, time"));
}

#[test]
fn model_factories_bring_the_test_dependency_to_older_apps() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    // An app made before generators wrote tests: no [dev-dependencies] at all.
    let cargo = read(&root, "Cargo.toml");
    let start = cargo.find("\n[dev-dependencies]").unwrap();
    let end = cargo[start + 1..].find("\n[").map_or(cargo.len(), |at| start + 1 + at);
    let old = format!("{}{}", &cargo[..start], &cargo[end..]);
    fs::write(root.join("Cargo.toml"), &old).unwrap();
    ok(&sandbox, &["g", "model", "Tag", "label:string"], &root);
    let cargo = read(&root, "Cargo.toml");
    let dev = &cargo[cargo.find("[dev-dependencies]").expect("section added")..];
    assert!(dev.lines().nth(1).unwrap().ends_with("features = [\"testing\"] }"), "{cargo}");
    ok(&sandbox, &["g", "model", "Label", "name:string"], &root);
    assert_eq!(read(&root, "Cargo.toml").matches("\"testing\"").count(), 1, "added once");

    // [dev-dependencies] present, without Ocre: the line goes under it.
    fs::write(root.join("Cargo.toml"), format!("{old}\n[dev-dependencies]\npretty_assertions = \"1\"\n")).unwrap();
    ok(&sandbox, &["g", "model", "Badge", "name:string"], &root);
    assert!(read(&root, "Cargo.toml").contains("[dev-dependencies]\nocre = {"));
}

#[test]
fn model_factories_need_a_one_line_ocre_dependency_and_the_marker() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let cargo = read(&root, "Cargo.toml");
    let broken: String = cargo.lines().filter(|l| !l.starts_with("ocre = ")).map(|l| format!("{l}\n")).collect();
    fs::write(root.join("Cargo.toml"), broken).unwrap();
    let report = fails(&sandbox, &["g", "model", "Tag", "label:string"], &root);
    assert_eq!(report["error"], "Cargo.toml has no one-line `ocre = { ... }` dependency");

    fs::write(root.join("Cargo.toml"), cargo).unwrap();
    fs::create_dir_all(root.join("tests/factories")).unwrap();
    fs::write(root.join("tests/factories/mod.rs"), "pub mod tag;\n").unwrap();
    let report = fails(&sandbox, &["g", "model", "Tag", "label:string"], &root);
    assert_eq!(report["error"], "tests/factories/mod.rs is missing the `// ocre:factories` marker");
    assert!(!root.join("src/models/tag.rs").exists(), "nothing written");
}

#[test]
fn data_loaders_compile_a_json_file_into_the_worker() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let (report, ok) = sandbox.json(&["g", "data", "countries"], &root);
    assert!(ok, "{report}");
    assert_eq!(
        report["created"],
        serde_json::json!(["src/data/mod.rs", "data/countries.json", "src/data/countries.rs"])
    );
    assert!(fs::read_to_string(root.join("src/lib.rs")).unwrap().contains("mod data;"));
    let loader = fs::read_to_string(root.join("src/data/countries.rs")).unwrap();
    assert!(loader.contains("include_str!(\"../../data/countries.json\")"), "{loader}");
    let (report, ok) = sandbox.json(&["g", "data", "currencies"], &root);
    assert!(ok, "{report}");
    assert_eq!(report["updated"], serde_json::json!(["src/data/mod.rs"]));
    assert!(
        fs::read_to_string(root.join("src/data/mod.rs")).unwrap().contains("pub mod currencies;\npub mod countries;")
    );

    let (report, ok) = sandbox.json(&["g", "data", "Bad-Name"], &root);
    assert!(!ok);
    assert_eq!(report["error"], "invalid data name `Bad-Name`");
    fs::write(root.join("src/data/mod.rs"), "").unwrap();
    assert_eq!(
        sandbox.json(&["g", "data", "rates"], &root).0["error"],
        "src/data/mod.rs is missing the `// ocre:data` marker"
    );
    fs::remove_file(root.join("src/data/mod.rs")).unwrap();
    fs::write(root.join("src/lib.rs"), "").unwrap();
    assert_eq!(
        sandbox.json(&["g", "data", "rates"], &root).0["error"],
        "src/lib.rs is missing the `// ocre:modules` marker"
    );
}

#[test]
fn generated_rust_is_formatted_by_rustfmt_when_available() {
    let mut sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    sandbox.remove_tool("rustfmt");
    let (report, ok) = sandbox.json(&["g", "scaffold", "Post", "title:string", "body:text?"], &root);
    assert!(ok, "{report}");
    for file in ["src/models/post.rs", "src/posts.rs", "src/lib.rs", "tests/posts.rs"] {
        let status = std::process::Command::new("rustfmt")
            .args(["--edition", "2024", "--check", file])
            .current_dir(&root)
            .status()
            .unwrap();
        assert!(status.success(), "{file} is not rustfmt-clean");
    }
    // A rustfmt that fails, or none at all: the text is written as generated.
    sandbox.script("rustfmt", "#!/bin/sh\nexit 1\n");
    assert!(sandbox.json(&["g", "model", "Tag", "name:string"], &root).1);
    sandbox.remove_tool("rustfmt");
    sandbox.isolate_path();
    assert!(sandbox.json(&["g", "model", "Label", "name:string"], &root).1);
    assert!(fs::read_to_string(root.join("src/models/label.rs")).unwrap().contains("pub fn query() -> Query<Label>"));
}

#[test]
fn system_tests_add_playwright_once() {
    let sandbox = Sandbox::new();
    let root = sandbox.new_app("shop", &[]);
    let (report, ok) = sandbox.json(&["g", "system_test", "signing_up"], &root);
    assert!(ok, "{report}");
    assert_eq!(report["created"], json!(["playwright.config.ts", "tests/system/signing_up.spec.ts"]));
    assert_eq!(report["updated"], json!(["package.json", ".gitignore"]));
    assert!(
        fs::read_to_string(root.join("package.json"))
            .unwrap()
            .contains("\"devDependencies\": {\n    \"@playwright/test\": \"1.63.0\",\n")
    );
    assert!(fs::read_to_string(root.join("tests/system/signing_up.spec.ts")).unwrap().contains("test(\"Signing up\""));
    let (report, ok) = sandbox.json(&["g", "system-test", "checkout"], &root);
    assert!(ok, "{report}");
    assert_eq!(report["created"], json!(["tests/system/checkout.spec.ts"]), "config and package once");

    assert_eq!(sandbox.json(&["g", "system_test", "Bad"], &root).0["error"], "invalid system test name `Bad`");
    assert_eq!(
        sandbox.json(&["g", "system_test", "checkout"], &root).0["error"],
        "tests/system/checkout.spec.ts already exists"
    );
    let other = sandbox.new_app("other", &[]);
    fs::write(other.join("package.json"), "{}\n").unwrap();
    assert_eq!(
        sandbox.json(&["g", "system_test", "x"], &other).0["error"],
        "package.json has no `\"devDependencies\": {` block"
    );
    fs::write(other.join("package.json"), "{\n  \"devDependencies\": {\n  }\n}\n").unwrap();
    fs::write(other.join(".gitignore"), "target/\ntest-results/\n").unwrap();
    let (report, _) = sandbox.json(&["g", "system_test", "x"], &other);
    assert_eq!(report["updated"], json!(["package.json"]), "test-results/ already ignored");
}