ocre-cli 0.2.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
//! Test files the generators write: a factory per model
//! (`tests/factories/<model>.rs`, FactoryBot-style builders) and request
//! tests for scaffolds and APIs (`tests/<plural>.rs`). Request tests are
//! `#[ignore]`d in plain `cargo test`; `ocre test --e2e` runs them against
//! the app in workerd.

use std::fmt::Write as _;

use super::{
    Edits,
    fields::{Field, FieldType},
    insert_after_marker,
};
use crate::{
    names::{ModelNames, humanize},
    output::CliError,
};

pub(super) const FACTORIES_MARKER: &str = "// ocre:factories";

/// The attribute every request test carries.
const IGNORE: &str = "#[ignore = \"request test: run with `ocre test --e2e`\"]";

const FACTORIES_MOD: &str = r#"//! Test data, one module per model (FactoryBot-style): `post()` returns valid
//! attributes, unique per call; `insert()` writes them to the test database,
//! `form()` and `json()` give them as a form or an API sends them. A test
//! file uses them with `mod factories;`. `ocre g model` adds a module below.

// Each test file uses only some factories.
#![allow(dead_code)]

use ocre::serde_json::Value;

// ocre:factories

/// A column value as form text: strings as they are, `true`/`false`,
/// numbers, and an empty field for `NULL`.
pub fn form_text(value: Value) -> String {
    match value {
        Value::String(text) => text,
        Value::Null => String::new(),
        other => other.to_string(),
    }
}
"#;

/// Writes `tests/factories/<singular>.rs` and registers it in `tests/factories/mod.rs`.
pub(super) fn add_factory(
    edits: &mut Edits,
    names: &ModelNames,
    fields: &[Field],
    command: &str,
) -> Result<(), CliError> {
    ensure_test_dependency(edits)?;
    let registry = edits.read("tests/factories/mod.rs")?.unwrap_or_else(|| FACTORIES_MOD.to_owned());
    let registry = insert_after_marker(&registry, FACTORIES_MARKER, &format!("pub mod {};", names.singular))
        .ok_or_else(|| {
            CliError::new(format!("tests/factories/mod.rs is missing the `{FACTORIES_MARKER}` marker"))
                .hint(format!("put `{FACTORIES_MARKER}` on its own line where the `pub mod` lines go"))
        })?;
    edits.update("tests/factories/mod.rs", registry);
    let factory =
        factory_rs(names, fields, command, |target| edits.exists(&format!("tests/factories/{}.rs", target.singular)));
    edits.create(&format!("tests/factories/{}.rs", names.singular), factory)
}

/// Adds Ocre with its `testing` feature to `[dev-dependencies]` (apps created
/// before generators wrote tests lack it); unchanged when it is there, or
/// when the app has no Cargo.toml (cargo reports that itself).
fn ensure_test_dependency(edits: &mut Edits) -> Result<(), CliError> {
    let Some(cargo_toml) = edits.read("Cargo.toml")? else { return Ok(()) };
    if cargo_toml.lines().any(|line| line.starts_with("ocre = {") && line.contains("\"testing\"")) {
        return Ok(());
    }
    let ocre =
        cargo_toml.lines().find(|line| line.starts_with("ocre = {") && line.ends_with('}')).ok_or_else(|| {
            CliError::new("Cargo.toml has no one-line `ocre = { ... }` dependency")
                .hint("declare Ocre as `ocre = { ... }` on one line under [dependencies], then run the command again")
        })?;
    let line = format!("{}, features = [\"testing\"] }}", ocre.trim_end_matches('}').trim_end());
    let updated = match insert_after_marker(&cargo_toml, "[dev-dependencies]", &line) {
        Some(updated) => updated,
        None => format!("{}\n[dev-dependencies]\n{line}\n", cargo_toml.trim_end()),
    };
    edits.update("Cargo.toml", updated);
    Ok(())
}

/// A factory field: Rust type and default value (`n` is the sequence number).
struct FactoryField<'a> {
    field: &'a Field,
    ty: String,
    default: String,
    /// The default reads `n`.
    sequenced: bool,
}

impl<'a> FactoryField<'a> {
    /// `None` for columns the factory leaves to the database (lock versions) and attachments.
    fn new(field: &'a Field) -> Option<Self> {
        let name = &field.name;
        let (ty, default) = match field.ty {
            FieldType::Attachment | FieldType::LockVersion => return None,
            FieldType::String | FieldType::Text | FieldType::RichText => {
                ("String", format!("format!(\"{} {{n}}\")", humanize(name)))
            }
            // A polymorphic id: `with_parents` creates a record of the first type, like a reference.
            FieldType::Integer if field.polymorphic.is_some() => ("i64", "None".to_owned()),
            FieldType::Integer => ("i64", "n as i64".to_owned()),
            FieldType::Float => ("f64", "n as f64".to_owned()),
            FieldType::Decimal => ("String", "format!(\"{n}.99\")".to_owned()),
            FieldType::Boolean => ("bool", "false".to_owned()),
            FieldType::Date => ("String", "\"2026-01-01\".to_owned()".to_owned()),
            FieldType::Time => ("String", "\"12:00\".to_owned()".to_owned()),
            FieldType::DateTime => ("String", "\"2026-01-01 12:00:00\".to_owned()".to_owned()),
            FieldType::Uuid => ("String", "format!(\"00000000-0000-4000-8000-{n:012}\")".to_owned()),
            // Random: unique across test files sharing the test database.
            FieldType::PublicId => ("String", "ocre::token::public_id()".to_owned()),
            FieldType::Json => ("Value", "json!({})".to_owned()),
            FieldType::Enum => {
                let first = &field.enumeration.as_ref().expect("enum fields have values").values[0];
                ("String", format!("\"{first}\".to_owned()"))
            }
            FieldType::References => ("i64", "None".to_owned()),
        };
        let optional = field.optional || field.ty == FieldType::References || field.polymorphic.is_some();
        let sequenced = !field.optional && (default.contains("{n") || default.starts_with("n as"));
        Some(Self {
            field,
            ty: if optional { format!("Option<{ty}>") } else { ty.to_owned() },
            default: if field.optional { "None".to_owned() } else { default },
            sequenced,
        })
    }

    /// The value as the column stores it: JSON fields as their text.
    fn column_value(&self) -> String {
        let name = &self.field.name;
        match (self.field.ty, self.field.optional) {
            (FieldType::Json, false) => format!("Value::String(self.{name}.to_string())"),
            (FieldType::Json, true) => {
                format!("self.{name}.as_ref().map_or(Value::Null, |value| Value::String(value.to_string()))")
            }
            _ => format!("json!(self.{name})"),
        }
    }
}

/// `tests/factories/<singular>.rs`. `has_factory(target)` tells whether a
/// referenced model has a factory the parent can be created with.
fn factory_rs(
    names: &ModelNames,
    fields: &[Field],
    command: &str,
    has_factory: impl Fn(&ModelNames) -> bool,
) -> String {
    let ModelNames { model, singular, plural, human_plural, .. } = names;
    let factory_fields: Vec<FactoryField> = fields.iter().filter_map(FactoryField::new).collect();
    let (mut declarations, mut defaults, mut values, mut json_fields, mut parents) =
        (String::new(), String::new(), String::new(), String::new(), String::new());
    let mut creates_parents = false;
    for factory_field in &factory_fields {
        let field = factory_field.field;
        let name = &field.name;
        let parent = field.target.as_ref().or_else(|| field.polymorphic.as_ref().and_then(|targets| targets.first()));
        if let Some(target) = parent.filter(|_| !field.optional) {
            writeln!(declarations, "    /// `None`: `with_parents` sets it.").expect("writing to a String");
            if has_factory(target) && target.singular != *singular {
                creates_parents = true;
                write!(
                    parents,
                    "        if self.{name}.is_none() {{\n            self.{name} = Some(super::{}::{}().insert());\n        }}\n",
                    target.singular, target.singular
                )
            } else {
                writeln!(
                    parents,
                    "        assert!(self.{name}.is_some(), \"set {name}: tests/factories has no {} factory to create one\");",
                    target.singular
                )
            }
            .expect("writing to a String");
        }
        writeln!(declarations, "    pub {name}: {},", factory_field.ty).expect("writing to a String");
        writeln!(defaults, "        {name}: {},", factory_field.default).expect("writing to a String");
        writeln!(values, "            (\"{name}\", {}),", factory_field.column_value()).expect("writing to a String");
        // The API sets the public id itself.
        if field.ty != FieldType::PublicId {
            writeln!(json_fields, "            \"{name}\": self.{name},").expect("writing to a String");
        }
    }
    let mut attachment_values = String::new();
    for field in fields.iter().filter(|field| field.is_attachment() && !field.optional) {
        let name = &field.name;
        write!(
            attachment_values,
            "        // A placeholder for the required file (no object in R2).\n        values.extend([\n            \
             (\"{name}_key\", json!(format!(\"factories/{plural}/{{}}\", ocre::testing::sequence()))),\n            \
             (\"{name}_filename\", json!(\"file.txt\")),\n            (\"{name}_content_type\", json!(\"text/plain\")),\n            \
             (\"{name}_size\", json!(0)),\n        ]);\n"
        )
        .expect("writing to a String");
    }
    let sequence =
        if factory_fields.iter().any(|f| f.sequenced) { "    let n = ocre::testing::sequence();\n" } else { "" };
    let (values_binding, values_end) = if attachment_values.is_empty() {
        ("", String::new())
    } else {
        ("let mut values = ", format!(";\n{attachment_values}        values"))
    };
    let self_binding = if creates_parents { "mut self" } else { "self" };
    format!(
        r#"//! Test data for {human_plural}. Generated by `{command}`.
//!
//! `{singular}()` has valid attributes, unique per call; change any of them
//! with `{model} {{ field: value, ..{singular}() }}`.

use ocre::serde_json::{{Value, json}};

/// A {lower}'s attributes for tests.
#[derive(Debug, Clone)]
pub struct {model} {{
{declarations}}}

/// Valid attributes; text fields are unique per call.
pub fn {singular}() -> {model} {{
{sequence}    {model} {{
{defaults}    }}
}}

impl {model} {{
    /// Creates the rows it references with their factories, when unset.
    pub fn with_parents({self_binding}) -> Self {{
{parents}        self
    }}

    /// Column values for `ocre::testing::insert`, parents created first.
    pub fn values(self) -> Vec<(&'static str, Value)> {{
        {values_binding}self.with_parents().column_values(){values_end}
    }}

    fn column_values(self) -> Vec<(&'static str, Value)> {{
        vec![
{values}        ]
    }}

    /// Inserts the row into the test database (one wrangler call, about a second); returns its id.
    pub fn insert(self) -> i64 {{
        ocre::testing::insert("{plural}", &self.values())
    }}

    /// The fields as the new and edit forms post them.
    pub fn form(self) -> Vec<(&'static str, String)> {{
        self.with_parents().column_values().into_iter().map(|(name, value)| (name, super::form_text(value))).collect()
    }}

    /// The attributes as a JSON API receives them.
    pub fn json(self) -> Value {{
        self.with_parents().json_value()
    }}

    fn json_value(self) -> Value {{
        json!({{
{json_fields}        }})
    }}
}}
"#,
        lower = names.human_singular.to_lowercase(),
    )
}

/// The first required plain-text field (`title`): shown on pages, and
/// "can't be blank" when empty, so the tests assert on it.
fn text_field(fields: &[Field]) -> Option<&str> {
    fields
        .iter()
        .find(|field| matches!(field.ty, FieldType::String | FieldType::Text) && !field.optional)
        .map(|field| field.name.as_str())
}

/// `tests/<plural>.rs`: request tests of the scaffold's pages. Forms with
/// files are multipart, so models with attachments get the read and delete tests only.
pub(super) fn scaffold_tests(
    edits: &mut Edits,
    names: &ModelNames,
    fields: &[Field],
    command: &str,
    public_id: bool,
) -> Result<(), CliError> {
    let ModelNames { model, singular, plural, human_plural, human_singular } = names;
    let text = text_field(fields);
    // With a public id, URLs carry it: read it before the row is inserted.
    let (show_insert, insert) = if public_id {
        (
            "    let id = record.public_id.clone();\n    record.insert();".to_owned(),
            format!("let record = {singular}();\n    let id = record.public_id.clone();\n    record.insert();"),
        )
    } else {
        ("    let id = record.insert();".to_owned(), format!("let id = {singular}().insert();"))
    };
    // Clippy's `needless_update`: no `..factory()` when the text field is the only one.
    let rest = if fields.iter().filter_map(FactoryField::new).count() + usize::from(public_id) > 1 {
        format!(", ..{}()", names.singular)
    } else {
        String::new()
    };
    let files = fields.iter().any(Field::is_attachment);
    let mut tests = format!(
        r#"//! Request tests for the {lower_plural} pages. Generated by `{command}`.
//! They run against the app in workerd with `ocre test --e2e`; plain
//! `cargo test` skips them (`#[ignore]`).

mod factories;

use factories::{singular}::{singular};
use ocre::testing::Client;

#[test]
{IGNORE}
fn lists_{plural}() {{
    Client::new().get("/{plural}").assert_status(200).assert_contains("{human_plural}");
}}

#[test]
{IGNORE}
fn shows_a_{singular}() {{
    let record = {singular}();
{keep_text}{show_insert}
    Client::new().get(&format!("/{plural}/{{id}}")).assert_status(200){assert_text};
}}

#[test]
{IGNORE}
fn deletes_a_{singular}() {{
    {insert}
    let mut client = Client::new();
    client.post(&format!("/{plural}/{{id}}/delete"), &()).assert_redirect_to("/{plural}");
    assert_eq!(client.flash("notice").as_deref(), Some("{human_singular} was successfully destroyed."));
    client.get(&format!("/{plural}/{{id}}")).assert_status(404);
}}
"#,
        lower_plural = human_plural.to_lowercase(),
        keep_text = text.map(|name| format!("    let text = record.{name}.clone();\n")).unwrap_or_default(),
        assert_text = if text.is_some() { ".assert_contains(&text)" } else { "" },
    );
    if !files {
        write!(
            tests,
            r#"
#[test]
{IGNORE}
fn creates_a_{singular}() {{
    let mut client = Client::new();
    let created = client.post("/{plural}", &{singular}().form());
    let location = created.assert_status(303).location().unwrap_or_default().to_owned();
    assert!(location.starts_with("/{plural}/"), "redirects to the new {lower}: {{location}}");
    assert_eq!(client.flash("notice").as_deref(), Some("{human_singular} was successfully created."));
    client.follow_redirect(&created).assert_status(200).assert_contains("{human_singular} was successfully created.");
}}

#[test]
{IGNORE}
fn updates_a_{singular}() {{
    {insert}
    let changes = {singular}();
{keep_changed}    let mut client = Client::new();
    client.post(&format!("/{plural}/{{id}}"), &changes.form()).assert_redirect_to(&format!("/{plural}/{{id}}"));
    client.get(&format!("/{plural}/{{id}}")).assert_status(200){assert_changed};
}}
"#,
            lower = human_singular.to_lowercase(),
            keep_changed = text.map(|name| format!("    let text = changes.{name}.clone();\n")).unwrap_or_default(),
            assert_changed = if text.is_some() { ".assert_contains(&text)" } else { "" },
        )
        .expect("writing to a String");
        if let Some(name) = text {
            write!(
                tests,
                r#"
#[test]
{IGNORE}
fn rejects_an_invalid_{singular}() {{
    let invalid = factories::{singular}::{model} {{ {name}: String::new(){rest} }};
    Client::new().post("/{plural}", &invalid.form()).assert_status(422).assert_contains("can&#39;t be blank");
}}
"#
            )
            .expect("writing to a String");
        }
    }
    edits.create(&format!("tests/{plural}.rs"), tests)
}

/// `tests/api_<plural>.rs`: request tests of the JSON API (its attachments
/// are optional and uploaded separately, so JSON covers the rest).
pub(super) fn api_tests(
    edits: &mut Edits,
    names: &ModelNames,
    fields: &[Field],
    command: &str,
    public_id: bool,
) -> Result<(), CliError> {
    let ModelNames { model, singular, plural, human_plural, .. } = names;
    let text = text_field(fields);
    // Clippy's `needless_update`: no `..factory()` when the text field is the only one.
    let rest = if fields.iter().filter_map(FactoryField::new).count() + usize::from(public_id) > 1 {
        format!(", ..{}()", names.singular)
    } else {
        String::new()
    };
    let mut tests = format!(
        r#"//! Request tests for the {lower_plural} JSON API. Generated by `{command}`.
//! They run against the app in workerd with `ocre test --e2e`; plain
//! `cargo test` skips them (`#[ignore]`).

mod factories;

use factories::{singular}::{singular};
use ocre::{{serde_json::Value, testing::Client}};

#[test]
{IGNORE}
fn lists_{plural}() {{
    let list: Value = Client::new().get("/api/{plural}").assert_status(200).json();
    assert!(list.is_array(), "{{list}}");
}}

#[test]
{IGNORE}
fn shows_a_{singular}() {{
    let id = {singular}().insert();
    let shown: Value = Client::new().get(&format!("/api/{plural}/{{id}}")).assert_status(200).json();
    assert_eq!(shown["id"], id);
}}

#[test]
{IGNORE}
fn deletes_a_{singular}() {{
    let id = {singular}().insert();
    let mut client = Client::new();
    client.delete(&format!("/api/{plural}/{{id}}")).assert_status(204);
    client.get(&format!("/api/{plural}/{{id}}")).assert_status(404);
}}

#[test]
{IGNORE}
fn creates_a_{singular}() {{
    let mut client = Client::new();
    let created: Value = client.post_json("/api/{plural}", &{singular}().json()).assert_status(201).json();
    let id = created["id"].as_i64().expect("the new id");
    client.get(&format!("/api/{plural}/{{id}}")).assert_status(200);
}}

#[test]
{IGNORE}
fn updates_a_{singular}() {{
    let id = {singular}().insert();
    let changes = {singular}().json();
    let updated: Value =
        Client::new().patch_json(&format!("/api/{plural}/{{id}}"), &changes).assert_status(200).json();
    assert_eq!(updated["id"], id);
{assert_changed}}}
"#,
        lower_plural = human_plural.to_lowercase(),
        assert_changed =
            text.map(|name| format!("    assert_eq!(updated[\"{name}\"], changes[\"{name}\"]);\n")).unwrap_or_default(),
    );
    if let Some(name) = text {
        write!(
            tests,
            r#"
#[test]
{IGNORE}
fn rejects_an_invalid_{singular}() {{
    let invalid = factories::{singular}::{model} {{ {name}: String::new(){rest} }};
    Client::new().post_json("/api/{plural}", &invalid.json()).assert_status(422).assert_contains("{name}");
}}
"#
        )
        .expect("writing to a String");
    }
    if public_id {
        // URLs and JSON carry the public id: read it from the factory before inserting.
        tests = tests
            .replace(
                &format!("    let id = {singular}().insert();"),
                &format!("    let record = {singular}();\n    let id = record.public_id.clone();\n    record.insert();"),
            )
            .replace("assert_eq!(shown[\"id\"], id);", "assert_eq!(shown[\"public_id\"], id);\n    assert!(shown.get(\"id\").is_none(), \"the integer id stays internal\");")
            .replace("let id = created[\"id\"].as_i64().expect(\"the new id\");", "let id = created[\"public_id\"].as_str().expect(\"the new public id\");")
            .replace("assert_eq!(updated[\"id\"], id);", "assert_eq!(updated[\"public_id\"], id);");
    }
    edits.create(&format!("tests/api_{plural}.rs"), tests)
}