cargo-rahti 0.0.21

Create and maintain Rahti projects: cargo rahti new, cargo rahti upgrade.
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
//! The scaffold: what it writes, and what it records having written.

use super::*;

/// A directory that removes itself, so a failed assertion does not leave a
/// project behind for the next run to trip over.
struct Tree(PathBuf);

impl Tree {
    fn new(name: &str) -> Self {
        let path = std::env::temp_dir().join(format!("cargo-rahti-{name}-{}", std::process::id()));
        let _ = fs::remove_dir_all(&path);
        fs::create_dir_all(&path).expect("a temporary directory");
        Tree(path)
    }

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

impl Drop for Tree {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

fn scaffold(tree: &Tree, tailwind: bool) -> Ledger {
    scaffold_with(tree, tailwind, None)
}

fn scaffold_with(tree: &Tree, tailwind: bool, db: Option<Backend>) -> Ledger {
    scaffold_full(tree, tailwind, db, false)
}

fn scaffold_full(tree: &Tree, tailwind: bool, db: Option<Backend>, ws: bool) -> Ledger {
    scaffold_features(tree, tailwind, db, ws, false)
}

fn scaffold_features(
    tree: &Tree,
    tailwind: bool,
    db: Option<Backend>,
    ws: bool,
    mcp: bool,
) -> Ledger {
    let options = Options {
        name: "demo".to_string(),
        tailwind,
        db,
        ws,
        mcp,
        local: None,
    };
    let mut ledger = Ledger::new();
    write_project(&tree.0, &options, tailwind, db, ws, mcp, &mut ledger)
        .expect("a written project");
    write_config(&tree.0, tailwind, db, ws, mcp, &ledger).expect("a written config");
    ledger
}

// -------------------------------------------------------------------- names

/// The name becomes a cargo package and a directory, so what one of those
/// would refuse later is refused here, where the message can say why.
#[test]
fn a_usable_name_is_accepted() {
    assert!(check_name("my-app").is_ok());
    assert!(check_name("my_app").is_ok());
    assert!(check_name("app2").is_ok());
}

#[test]
fn an_unusable_name_is_refused() {
    assert!(check_name("").is_err());
    assert!(
        check_name("my app").is_err(),
        "a space is not a package name"
    );
    assert!(
        check_name("my/app").is_err(),
        "a separator would escape the directory"
    );
    assert!(
        check_name("2fast").is_err(),
        "cargo will not take a leading digit"
    );
}

// ------------------------------------------------------------------ options

/// A flag adds a feature. There is no flag that removes one, so the whole of
/// the command line is what to add.
#[test]
fn the_engine_flag_is_read() {
    assert!(Options::parse(&["app", "--tailwind"]).unwrap().tailwind);
}

/// Leaving the flag out is how you say no — and the only way, which is the
/// point. An interactive run still asks; nothing else does.
#[test]
fn no_flag_is_the_way_to_say_no() {
    let options = Options::parse(&["app"]).unwrap();
    assert!(!options.tailwind);
    assert!(options.db.is_none());
    assert!(!options.mcp);
}

#[test]
fn the_mcp_flag_is_read() {
    assert!(Options::parse(&["app", "--mcp"]).unwrap().mcp);
}

#[test]
fn a_missing_name_is_reported() {
    assert!(Options::parse(&["--tailwind"]).is_err());
}

#[test]
fn an_unknown_option_is_reported() {
    assert!(Options::parse(&["app", "--turbo"]).is_err());
}

#[test]
fn local_takes_the_path_after_it() {
    let options = Options::parse(&["app", "--local", "/checkout"]).unwrap();
    assert_eq!(options.local.as_deref(), Some("/checkout"));
    assert!(
        Options::parse(&["app", "--local"]).is_err(),
        "the path is not optional"
    );
}

// ------------------------------------------------------------------- ledger

/// Every hash recorded has to describe the bytes actually on disk, because a
/// later comparison has nothing else to go on.
#[test]
fn the_ledger_matches_what_was_written() {
    let tree = Tree::new("ledger");
    let ledger = scaffold(&tree, false);

    assert!(!ledger.is_empty());
    for (path, hash) in &ledger {
        let bytes = fs::read(tree.0.join(path)).unwrap_or_else(|e| panic!("{path}: {e}"));
        assert_eq!(
            &sha256::hex(&bytes),
            hash,
            "{path} does not match its ledger entry"
        );
    }
}

/// The build owns the compiled stylesheet from the first compile onward, so a
/// hash taken at scaffold time would be wrong before anyone could read it —
/// and an upgrade would take that for an edit the author made.
#[test]
fn a_file_the_build_owns_is_not_in_the_ledger() {
    let tree = Tree::new("untracked");
    let ledger = scaffold(&tree, true);

    assert!(!ledger.contains_key("public/css/styles.css"));
    assert!(
        tree.0.join("public/css/styles.css").is_file(),
        "it still has to exist for the first build to overwrite"
    );
}

#[test]
fn the_config_records_the_ledger_and_the_choice() {
    let tree = Tree::new("config");
    scaffold(&tree, false);

    let config = tree.read("rahti.config.json");
    assert!(config.contains("\"schema\": 1"), "{config}");
    assert!(config.contains("\"createdWith\""), "{config}");
    assert!(config.contains("\"engine\": \"plain\""), "{config}");
    assert!(
        config.contains("\"src/app/page.rs\""),
        "the ledger is written:\n{config}"
    );
}

// ---------------------------------------------------------------------- MCP

#[test]
fn mcp_is_absent_until_the_project_opts_in() {
    let tree = Tree::new("no-mcp");
    let ledger = scaffold(&tree, false);

    assert!(!ledger.contains_key(".mcp.json"));
    assert!(!tree.0.join(".mcp.json").exists());
    assert!(!tree.read("rahti.config.json").contains("\"mcp\""));
}

#[test]
fn an_mcp_project_gets_a_recorded_client_configuration() {
    let tree = Tree::new("mcp");
    let ledger = scaffold_features(&tree, false, None, false, true);

    assert!(ledger.contains_key(".mcp.json"));
    let client = tree.read(".mcp.json");
    assert!(client.contains("\"command\": \"rahti-mcp\""), "{client}");
    assert!(client.contains("\"--project\", \".\""), "{client}");
    assert!(tree.read("rahti.config.json").contains("\"mcp\": true"));
}

// ------------------------------------------------------------------ engines

/// A project that chose plain CSS should carry no trace of the other choice —
/// that is the whole promise of the question being asked.
#[test]
fn a_plain_project_carries_nothing_of_tailwind() {
    let tree = Tree::new("plain");
    let ledger = scaffold(&tree, false);

    assert!(!ledger.contains_key("public/js/tailwind-merge.mjs"));
    assert!(!tree.0.join("public/js/tailwind-merge.mjs").exists());

    let main_js = tree.read("public/js/main.js");
    assert!(!main_js.contains("twMerge"), "{main_js}");

    let globals = tree.read("src/app/globals.css");
    assert!(!globals.contains("tailwindcss"), "{globals}");

    let config = tree.read("rahti.config.json");
    assert!(
        !config.contains("\"download\""),
        "no compiler to fetch:\n{config}"
    );
}

#[test]
fn a_tailwind_project_carries_the_helper_and_the_directives() {
    let tree = Tree::new("tw");
    let ledger = scaffold(&tree, true);

    assert!(ledger.contains_key("public/js/tailwind-merge.mjs"));
    assert!(tree.read("public/js/main.js").contains("twMerge"));
    assert!(
        tree.read("src/app/globals.css")
            .contains("@import \"tailwindcss\"")
    );
    assert!(
        tree.read("rahti.config.json")
            .contains("\"engine\": \"tailwind\"")
    );
}

/// The layout's single parent element is `<html>`, which is no place for a
/// `pp-component`: the client runtime would treat the whole document as a
/// component and re-render it, taking `<head>` and `<body>` with it — a blank
/// page from a freshly scaffolded project. `html!` handles that itself by
/// naming the `<template>` it injects around the slot, so the scaffolded file
/// writes neither.
#[test]
fn the_layout_leaves_the_component_marker_to_the_macro() {
    let tree = Tree::new("layout");
    scaffold(&tree, true);

    let layout = tree.read("src/app/layout.rs");
    let markup: String = layout
        .lines()
        .filter(|line| !line.trim_start().starts_with("//"))
        .collect::<Vec<_>>()
        .join("\n");

    assert!(
        !markup.contains("pp-component"),
        "the marker is injected, not written:\n{layout}"
    );
    assert!(
        markup.contains("<slot />"),
        "the slot is where it goes:\n{layout}"
    );
}

/// Both engines produce the same project but for the files that must differ.
#[test]
fn the_engines_differ_only_where_they_must() {
    let plain = Tree::new("same-plain");
    let tailwind = Tree::new("same-tw");
    let a = scaffold(&plain, false);
    let b = scaffold(&tailwind, true);

    let differ = [
        "src/app/globals.css",
        "src/app/page.rs",
        "public/js/main.js",
    ];
    for path in a.keys().filter(|p| b.contains_key(*p)) {
        let same = a[path] == b[path];
        if differ.contains(&path.as_str()) {
            assert!(!same, "{path} should differ between engines");
        } else {
            assert!(same, "{path} should be identical between engines");
        }
    }
}

// ----------------------------------------------------------------- database

#[test]
fn the_database_flag_is_read() {
    let db = |args: &[&str]| Options::parse(args).unwrap().db;

    assert_eq!(db(&["app", "--db", "postgres"]), Some(Backend::Postgres));
    assert_eq!(db(&["app", "--db", "mysql"]), Some(Backend::MySql));
    assert_eq!(db(&["app", "--db", "sqlite"]), Some(Backend::Sqlite));
}

/// The backend that needs no server running is the one worth having as the
/// short form.
#[test]
fn a_bare_db_flag_is_sqlite() {
    assert_eq!(
        Options::parse(&["app", "--db"]).unwrap().db,
        Some(Backend::Sqlite)
    );

    let both = Options::parse(&["app", "--db", "--tailwind"]).unwrap();
    assert_eq!(
        both.db,
        Some(Backend::Sqlite),
        "a flag after --db is a flag"
    );
    assert!(both.tailwind, "and it still counts as itself");
}

#[test]
fn a_misspelled_backend_is_refused() {
    assert!(Options::parse(&["app", "--db", "postgress"]).is_err());
}

/// A project with no database is a project with no database: no directories,
/// no dependency, no mention of it anywhere.
#[test]
fn a_project_without_a_database_carries_nothing_of_one() {
    let tree = Tree::new("no-db");
    let ledger = scaffold_with(&tree, true, None);

    for path in ledger.keys() {
        assert!(!path.starts_with("src/models"), "{path}");
        assert!(!path.starts_with("src/migrations"), "{path}");
        assert!(path != "src/db.rs", "{path}");
    }

    assert!(!tree.read("Cargo.toml").contains("sea-orm"));
    assert!(!tree.read("src/main.rs").contains("mod db;"));
    assert!(!tree.read("rahti.config.json").contains("\"db\""));

    // `.env` still exists, and it is not a database file: AUTH_SECRET lives
    // there whether or not the project has a database, and a `.env` that only
    // appeared once you chose SQLite would be a trap.
    let env = tree.read(".env");
    assert!(!env.contains("DATABASE_URL"), "{env}");
    assert!(env.contains("AUTH_SECRET="), "{env}");
    assert!(tree.read(".gitignore").contains("/.env"));
}

#[test]
fn a_database_project_gets_the_files_that_wire_one_up() {
    let tree = Tree::new("with-db");
    scaffold_with(&tree, true, Some(Backend::Sqlite));

    // The worked example everything else is copied from.
    assert!(
        tree.read("src/models/todo.rs")
            .contains("DeriveEntityModel")
    );
    assert!(
        tree.read("src/migrations/m20260101_000001_create_todo.rs")
            .contains("create_table")
    );
    // The connection, reached the only way a stateless handler can reach one.
    assert!(tree.read("src/db.rs").contains("OnceLock"));

    // Wired from `src/lib.rs`, not `src/main.rs`: startup is shared with the
    // optional native package, which has no `main` to call.
    let lib = tree.read("src/lib.rs");
    assert!(lib.contains("db::connect()"), "{lib}");
    assert!(lib.contains("pub mod models;"), "{lib}");
    assert!(lib.contains("initialize_application"), "{lib}");

    // And the binary is the thin half that only knows an address.
    let main = tree.read("src/main.rs");
    assert!(main.contains("initialize_application()"), "{main}");
    assert!(!main.contains("db::connect"), "{main}");
}

/// `src/lib.rs` must not call into a file the scaffold did not write.
///
/// The failure this catches happened: the template called
/// `auth::settings()`, and `cargo rahti new` does not write an `src/auth.rs`
/// — a project adds one when it wants private routes. Every scaffolded
/// project failed to compile, and nothing in the suite noticed, because no
/// test compiles a generated project.
#[test]
fn the_library_only_names_modules_it_declares() {
    for (label, db) in [("mods-none", None), ("mods-db", Some(Backend::Sqlite))] {
        let tree = Tree::new(label);
        scaffold_with(&tree, false, db);
        let lib = tree.read("src/lib.rs");

        // What it declares.
        let declared: Vec<&str> = lib
            .lines()
            .filter_map(|line| {
                let line = line.trim();
                let rest = line
                    .strip_prefix("pub mod ")
                    .or_else(|| line.strip_prefix("mod "))?;
                rest.strip_suffix(';')
            })
            .collect();

        // What it calls. A path at the start of a statement is either a module
        // of this crate or a dependency, and the dependencies are known.
        const CRATES: &[&str] = &["rahti", "std", "axum", "tokio", "serde", "self", "crate"];

        for line in lib.lines() {
            let code = line.trim();
            if code.starts_with("//") || code.starts_with("///") {
                continue;
            }
            let Some((head, _)) = code.split_once("::") else {
                continue;
            };
            let ident: String = head
                .chars()
                .rev()
                .take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
                .collect::<Vec<_>>()
                .into_iter()
                .rev()
                .collect();

            // A module path is lowercase; anything capitalised is a type or an
            // enum, and `StartupError::new` needs no `mod`.
            let lowercase = ident.starts_with(|c: char| c.is_ascii_lowercase());
            if ident.is_empty() || !lowercase || CRATES.contains(&ident.as_str()) {
                continue;
            }
            assert!(
                declared.contains(&ident.as_str()),
                "{label}: src/lib.rs calls `{ident}::…` and declares no `mod {ident};` — \
                 the generated project would not compile.\nline: {code}"
            );
        }

        // And every module it declares is a file somebody writes: the
        // scaffold, or `rahti-build` on the first cargo build.
        const GENERATED: &[&str] = &["components", "routes", "models", "migrations"];

        for module in declared {
            if GENERATED.contains(&module) {
                continue;
            }
            let file = tree.0.join(format!("src/{module}.rs"));
            let dir = tree.0.join(format!("src/{module}/mod.rs"));
            assert!(
                file.exists() || dir.exists(),
                "{label}: src/lib.rs declares `mod {module};`, and neither the scaffold nor \
                 rahti-build writes it"
            );
        }
    }
}

/// The binary calls into the library by the name cargo gave it.
#[test]
fn the_binary_names_the_library_target() {
    let tree = Tree::new("lib-name");
    scaffold(&tree, false);

    // Cargo turns a hyphen in a package name into an underscore in the crate
    // name, and the manifest writes both out so neither is a guess.
    let manifest = tree.read("Cargo.toml");
    assert!(manifest.contains("[lib]"), "{manifest}");
    assert!(manifest.contains("path = \"src/lib.rs\""), "{manifest}");
    // Native packaging is opt-in, and this is what keeps it that way for a
    // project that never asks: a web build cannot resolve a Tauri dependency
    // it has excluded.
    assert!(manifest.contains("exclude = [\"native\"]"), "{manifest}");

    assert!(
        tree.read("src/main.rs").contains("initialize_application"),
        "the binary does not call into the library"
    );
}

/// The feature is the one thing about a backend that is not interchangeable,
/// so the manifest has to name the right one.
#[test]
fn the_manifest_names_the_backend_feature() {
    for (backend, feature) in [
        (Backend::Sqlite, "sqlx-sqlite"),
        (Backend::Postgres, "sqlx-postgres"),
        (Backend::MySql, "sqlx-mysql"),
    ] {
        let tree = Tree::new(&format!("feature-{feature}"));
        scaffold_with(&tree, true, Some(backend));

        let manifest = tree.read("Cargo.toml");
        assert!(manifest.contains(feature), "{backend:?}:\n{manifest}");
        assert!(manifest.contains("sea-orm-migration"), "{manifest}");
    }
}

/// The config records which backend, and deliberately not how to reach it.
#[test]
fn the_config_records_the_backend_and_not_the_credential() {
    let tree = Tree::new("db-config");
    scaffold_with(&tree, true, Some(Backend::Postgres));

    let config = tree.read("rahti.config.json");
    assert!(config.contains("\"backend\": \"postgres\""), "{config}");
    assert!(config.contains("\"models\": \"src/models\""), "{config}");
    assert!(
        !config.contains("DATABASE_URL") && !config.contains("password"),
        "a connection string is a credential and this file is committed:\n{config}"
    );
}

/// `.env` holds the credentials, so it is written once and never tracked —
/// tracking it would have `upgrade` compare a hash against somebody's
/// password, and would make it a file the scaffold believes it may rewrite.
///
/// `.env.example` is untracked for its own reason: it carries this project's
/// generated cookie name, and an upgrade that regenerated it would silently
/// disagree with the `.env` beside it.
#[test]
fn neither_env_file_is_tracked() {
    let tree = Tree::new("db-env");
    let ledger = scaffold_with(&tree, true, Some(Backend::Sqlite));

    assert!(tree.read(".env").contains("DATABASE_URL="));
    assert!(
        !ledger.contains_key(".env"),
        "the credential is not the scaffold's"
    );
    assert!(
        !ledger.contains_key(".env.example"),
        "regenerating it would change the project's cookie name"
    );
    assert!(tree.0.join(".env.example").exists(), "still written once");
    assert!(tree.read(".gitignore").contains("/.env"));
}

// ------------------------------------------------------- generated secrets

/// The whole point of generating them: two projects made on one machine do not
/// share a signing key or a cookie name.
#[test]
fn two_projects_get_different_secrets() {
    let one = Tree::new("secret-one");
    let other = Tree::new("secret-two");
    scaffold_with(&one, true, Some(Backend::Sqlite));
    scaffold_with(&other, true, Some(Backend::Sqlite));

    let value = |tree: &Tree, key: &str| {
        tree.read(".env")
            .lines()
            .find_map(|line| line.strip_prefix(&format!("{key}=")))
            .map(|v| v.trim_matches('"').to_string())
            .unwrap_or_else(|| panic!("{key} is not in .env"))
    };

    assert_ne!(value(&one, "AUTH_SECRET"), value(&other, "AUTH_SECRET"));
    assert_ne!(
        value(&one, "AUTH_COOKIE_NAME"),
        value(&other, "AUTH_COOKIE_NAME")
    );
}

/// The real key goes in the ignored file. The committed example gets the
/// placeholder the runtime refuses to start a release build on — a secret in
/// git is not a secret, and the example has to say so rather than look
/// configured.
#[test]
fn the_example_carries_a_placeholder_and_not_the_key() {
    let tree = Tree::new("secret-example");
    scaffold_with(&tree, true, Some(Backend::Sqlite));

    let env = tree.read(".env");
    let example = tree.read(".env.example");

    let secret = env
        .lines()
        .find_map(|line| line.strip_prefix("AUTH_SECRET="))
        .expect("a secret")
        .trim_matches('"')
        .to_string();

    assert!(secret.len() >= 32, "a real key: {secret}");
    assert_ne!(secret, t::SECRET_PLACEHOLDER);
    assert!(
        !example.contains(&secret),
        "the committed file must not carry the key:\n{example}"
    );
    assert!(
        example.contains(&format!("AUTH_SECRET=\"{}\"", t::SECRET_PLACEHOLDER)),
        "{example}"
    );
}

/// The cookie name is not a credential — it is the project's identity under a
/// shared parent domain — so both files carry the same one. A clone that read
/// a different name out of the example would write a second cookie.
#[test]
fn both_env_files_agree_on_the_cookie_name() {
    let tree = Tree::new("cookie-name");
    scaffold_with(&tree, true, Some(Backend::Sqlite));

    let name_in = |contents: &str| {
        contents
            .lines()
            .find_map(|line| line.strip_prefix("AUTH_COOKIE_NAME="))
            .map(|v| v.trim_matches('"').to_string())
            .expect("a cookie name")
    };

    let real = name_in(&tree.read(".env"));
    assert_eq!(real, name_in(&tree.read(".env.example")));
    // A legal cookie name without quoting: hex, and long enough not to collide.
    assert_eq!(real.len(), 16, "{real}");
    assert!(real.chars().all(|c| c.is_ascii_hexdigit()), "{real}");
}

/// All three, under the names `rahti::auth` reads.
#[test]
fn the_env_file_names_the_three_auth_variables() {
    let tree = Tree::new("auth-vars");
    scaffold_with(&tree, true, None);
    let env = tree.read(".env");

    for key in ["AUTH_SECRET", "AUTH_COOKIE_NAME", "SESSION_LIFETIME_HOURS"] {
        assert!(env.contains(&format!("{key}=")), "{key} missing:\n{env}");
    }
}

/// Adding a database changes the files it has to change and no others.
#[test]
fn a_database_changes_only_what_it_must() {
    let without = Tree::new("diff-none");
    let with = Tree::new("diff-db");
    let a = scaffold_with(&without, true, None);
    let b = scaffold_with(&with, true, Some(Backend::Sqlite));

    // The guide is one of them: a database project's `AGENTS.md` carries the
    // database rules and the `database.md` table row.
    //
    // `src/lib.rs` rather than `src/main.rs`, because that is where startup
    // is: the binary only knows an address, and an address does not change
    // when a database appears.
    let differ = ["Cargo.toml", "src/lib.rs", ".gitignore", "AGENTS.md"];
    for path in a.keys().filter(|p| b.contains_key(*p)) {
        let same = a[path] == b[path];
        if differ.contains(&path.as_str()) {
            assert!(!same, "{path} should differ once there is a database");
        } else {
            assert!(same, "{path} should be identical either way");
        }
    }
}

// ------------------------------------------------------------ documentation

/// Rahti is not in anyone's training data, so a project that leaves the
/// scaffold without its documentation gives the next coding agent nothing to
/// read. The guide and the convention documents are scaffold files like any
/// other: written, ledgered, and refreshed by `upgrade` until edited.
#[test]
fn every_project_carries_its_own_documentation() {
    let tree = Tree::new("docs");
    let ledger = scaffold(&tree, false);

    assert!(ledger.contains_key("AGENTS.md"));
    assert!(ledger.contains_key("CLAUDE.md"));
    assert_eq!(tree.read("CLAUDE.md"), "@AGENTS.md\n");

    for (doc, _) in t::CORE_DOCS {
        let path = format!("docs/conventions/{doc}");
        assert!(ledger.contains_key(&path), "{path} is not in the ledger");
        assert!(tree.0.join(&path).is_file(), "{path} was not written");
    }
}

/// The guide must only ever name documents the project actually has: a table
/// row pointing at a file the scaffold did not write sends the reader
/// searching for something their checkout does not contain.
#[test]
fn the_guide_names_only_documents_the_project_has() {
    let bare = Tree::new("guide-bare");
    scaffold(&bare, false);

    let guide = bare.read("AGENTS.md");
    assert!(!guide.contains("database.md"), "{guide}");
    assert!(!guide.contains("websockets.md"), "{guide}");
    assert!(!bare.0.join("docs/conventions/database.md").exists());
    assert!(!bare.0.join("docs/conventions/websockets.md").exists());

    let full = Tree::new("guide-full");
    let ledger = scaffold_full(&full, false, Some(Backend::Sqlite), true);

    let guide = full.read("AGENTS.md");
    assert!(guide.contains("docs/conventions/database.md"), "{guide}");
    assert!(guide.contains("docs/conventions/websockets.md"), "{guide}");
    assert!(ledger.contains_key("docs/conventions/database.md"));
    assert!(ledger.contains_key("docs/conventions/websockets.md"));
}

/// The copies under `assets/docs/` exist because a published crate cannot
/// read a file outside its own directory. This holds each one byte-identical
/// to its canonical original in `docs/conventions/`, the same promise the
/// client runtime keeps between its two copies.
#[test]
fn shipped_docs_match_the_canonical_conventions() {
    let canonical = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/conventions");

    let mut shipped: Vec<(&str, &str)> = t::CORE_DOCS.to_vec();
    shipped.push(("database.md", t::DOC_DATABASE));
    shipped.push(("websockets.md", t::DOC_WEBSOCKETS));

    for (doc, embedded) in shipped {
        let original = fs::read_to_string(canonical.join(doc))
            .unwrap_or_else(|e| panic!("docs/conventions/{doc}: {e}"));
        assert_eq!(
            original.replace("\r\n", "\n"),
            embedded.replace("\r\n", "\n"),
            "assets/docs/{doc} has drifted from docs/conventions/{doc} — \
             copy the canonical file over it"
        );
    }
}

// --------------------------------------------------------------- websockets

#[test]
fn ws_is_off_unless_asked_for() {
    assert!(!Options::parse(&["app"]).unwrap().ws);
    assert!(Options::parse(&["app", "--ws"]).unwrap().ws);
}

/// The feature has to land inside the dependency table whichever form the
/// dependency takes — a published version, or a `--local` path.
#[test]
fn ws_puts_the_feature_on_the_rahti_dependency() {
    let with = t::cargo_toml("demo", "\"0.0.7\"", None, true);
    assert!(
        with.contains("rahti = { version = \"0.0.7\", features = [\"ws\"] }"),
        "{with}"
    );

    let local = t::cargo_toml("demo", "{ path = \"/checkout/crates/rahti\" }", None, true);
    assert!(
        local.contains("rahti = { path = \"/checkout/crates/rahti\", features = [\"ws\"] }"),
        "{local}"
    );
    // The build dependency has no `ws` feature to name.
    assert!(
        local.contains("rahti-build = { path = \"/checkout/crates/rahti-build\" }"),
        "{local}"
    );

    let without = t::cargo_toml("demo", "\"0.0.7\"", None, false);
    assert!(without.contains("rahti = \"0.0.7\""), "{without}");
    assert!(!without.contains("ws"), "{without}");
}

/// `upgrade` reads the key back to regenerate the same project, so the
/// choice has to be in the config.
#[test]
fn ws_is_recorded_in_the_config() {
    let tree = Tree::new("ws-config");
    scaffold_full(&tree, false, None, true);
    let config = fs::read_to_string(tree.0.join("rahti.config.json")).expect("a config");
    assert!(config.contains("\"ws\": true"), "{config}");

    let tree = Tree::new("ws-config-off");
    scaffold_full(&tree, false, None, false);
    let config = fs::read_to_string(tree.0.join("rahti.config.json")).expect("a config");
    assert!(!config.contains("\"ws\""), "{config}");
}