selfware 0.6.7

Your personal AI workshop — software you own, software that lasts
Documentation
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
use super::*;
use std::collections::HashMap;
use tempfile::TempDir;

// -- Template rendering -------------------------------------------------

#[test]
fn test_render_template_basic() {
    let mut vars = HashMap::new();
    vars.insert("name".into(), "hello-world".into());
    vars.insert("desc".into(), "A test project".into());

    let result = TemplateEngine::render_template("name={{name}}, desc={{desc}}", &vars);
    assert_eq!(result, "name=hello-world, desc=A test project");
}

#[test]
fn test_render_template_missing_placeholder_kept() {
    let vars = HashMap::new();
    let result = TemplateEngine::render_template("{{unknown}}", &vars);
    assert_eq!(result, "{{unknown}}");
}

#[test]
fn test_render_template_multiple_occurrences() {
    let mut vars = HashMap::new();
    vars.insert("x".into(), "42".into());
    let result = TemplateEngine::render_template("a={{x}} b={{x}}", &vars);
    assert_eq!(result, "a=42 b=42");
}

#[test]
fn test_render_template_empty_value() {
    let mut vars = HashMap::new();
    vars.insert("project_name".into(), "".into());
    let result = TemplateEngine::render_template("name={{project_name}}", &vars);
    assert_eq!(result, "name=");
}

#[test]
fn test_render_template_no_placeholders() {
    let vars = HashMap::new();
    let result = TemplateEngine::render_template("plain text", &vars);
    assert_eq!(result, "plain text");
}

// -- Rust scaffolding ---------------------------------------------------

#[test]
fn test_scaffold_rust_project() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        description: "Test Rust project".into(),
        framework: None,
        with_ci: true,
        with_tests: true,
        qa_profile: "standard".into(),
    };

    let files = engine
        .scaffold_project("rust", "my-app", dir.path(), &opts)
        .unwrap();

    // Verify Cargo.toml was created with correct name
    let cargo_path = dir.path().join("Cargo.toml");
    assert!(cargo_path.exists(), "Cargo.toml should exist");
    let cargo_content = std::fs::read_to_string(&cargo_path).unwrap();
    assert!(
        cargo_content.contains("name = \"my-app\""),
        "Cargo.toml should contain project name"
    );
    assert!(
        cargo_content.contains("Test Rust project"),
        "Cargo.toml should contain description"
    );

    // Verify src/main.rs
    assert!(dir.path().join("src/main.rs").exists());
    let main_content = std::fs::read_to_string(dir.path().join("src/main.rs")).unwrap();
    assert!(main_content.contains("my-app"));

    // Verify src/lib.rs
    assert!(dir.path().join("src/lib.rs").exists());

    // Verify tests/
    assert!(dir.path().join("tests/integration_test.rs").exists());

    // Verify CI workflow
    assert!(dir.path().join(".github/workflows/rust-qa.yml").exists());

    // All expected files present
    assert!(files.contains(&"Cargo.toml".to_string()));
    assert!(files.contains(&"src/main.rs".to_string()));
    assert!(files.contains(&"src/lib.rs".to_string()));
    assert!(files.contains(&"tests/integration_test.rs".to_string()));
    assert!(files.contains(&".github/workflows/rust-qa.yml".to_string()));
}

// -- Python scaffolding -------------------------------------------------

#[test]
fn test_scaffold_python_project() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        description: "Test Python project".into(),
        framework: None,
        with_ci: true,
        with_tests: true,
        qa_profile: "standard".into(),
    };

    let files = engine
        .scaffold_project("python", "my-api", dir.path(), &opts)
        .unwrap();

    // Verify pyproject.toml
    let pyproject_path = dir.path().join("pyproject.toml");
    assert!(pyproject_path.exists(), "pyproject.toml should exist");
    let pyproject_content = std::fs::read_to_string(&pyproject_path).unwrap();
    assert!(
        pyproject_content.contains("name = \"my-api\""),
        "pyproject.toml should contain project name"
    );

    // Verify module directory
    assert!(dir.path().join("src/my_api/__init__.py").exists());
    assert!(dir.path().join("src/my_api/cli.py").exists());

    // Verify tests
    assert!(dir.path().join("tests/__init__.py").exists());
    assert!(dir.path().join("tests/test_main.py").exists());

    // Verify CI
    assert!(dir.path().join(".github/workflows/python-qa.yml").exists());

    assert!(files.contains(&"pyproject.toml".to_string()));
    assert!(files.contains(&"src/my_api/__init__.py".to_string()));
}

// -- Node.js scaffolding ------------------------------------------------

#[test]
fn test_scaffold_nodejs_project() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        description: "Test Node project".into(),
        framework: None,
        with_ci: true,
        with_tests: true,
        qa_profile: "standard".into(),
    };

    let files = engine
        .scaffold_project("nodejs", "my-service", dir.path(), &opts)
        .unwrap();

    // Verify all 5 config files
    assert!(dir.path().join("package.json").exists());
    assert!(dir.path().join("tsconfig.json").exists());
    assert!(dir.path().join("eslint.config.mjs").exists());
    assert!(dir.path().join(".prettierrc").exists());
    assert!(dir.path().join("vitest.config.ts").exists());

    // Verify source and test
    assert!(dir.path().join("src/index.ts").exists());
    assert!(dir.path().join("tests/index.test.ts").exists());

    // Verify package.json has correct name
    let pkg_content = std::fs::read_to_string(dir.path().join("package.json")).unwrap();
    assert!(pkg_content.contains("\"name\": \"my-service\""));

    // CI
    assert!(dir.path().join(".github/workflows/nodejs-qa.yml").exists());

    // Check all 5 config files are in the list
    assert!(files.contains(&"package.json".to_string()));
    assert!(files.contains(&"tsconfig.json".to_string()));
    assert!(files.contains(&"eslint.config.mjs".to_string()));
    assert!(files.contains(&".prettierrc".to_string()));
    assert!(files.contains(&"vitest.config.ts".to_string()));
}

#[test]
fn test_scaffold_nodejs_aliases() {
    // All aliases should work
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions::default();

    for alias in &["nodejs", "node", "typescript", "node.js", "ts"] {
        let sub = dir.path().join(alias);
        std::fs::create_dir_all(&sub).unwrap();
        let result = engine.scaffold_project(alias, "test", &sub, &opts);
        assert!(
            result.is_ok(),
            "alias '{}' should succeed: {:?}",
            alias,
            result.err()
        );
    }
}

// -- QA schema ----------------------------------------------------------

#[test]
fn test_load_qa_schema_embedded() {
    let config = load_qa_schema(None).unwrap();
    assert_eq!(config.qa_profile.name, "standard");
    assert!(!config.qa_profile.stages.is_empty());
    assert!(!config.qa_profile.quality_gates.is_empty());
}

#[test]
fn test_load_qa_schema_from_disk() {
    let dir = TempDir::new().unwrap();
    let schema_path = dir.path().join("qa-schema.yaml");
    std::fs::write(&schema_path, QA_SCHEMA_YAML).unwrap();

    let config = load_qa_schema_profile(Some(&schema_path), "standard").unwrap();
    assert_eq!(config.qa_profile.name, "standard");
}

#[test]
fn test_load_qa_schema_profile_standard() {
    let config = load_qa_schema_profile(None, "standard").unwrap();
    assert_eq!(config.qa_profile.name, "standard");
}

#[test]
fn test_load_qa_schema_profile_strict() {
    let config = load_qa_schema_profile(None, "strict").unwrap();
    assert_eq!(config.qa_profile.name, "strict");
}

#[test]
fn test_load_qa_schema_profile_minimal() {
    let config = load_qa_schema_profile(None, "minimal").unwrap();
    assert_eq!(config.qa_profile.name, "minimal");
}

#[test]
fn test_load_qa_schema_profile_unknown() {
    let result = load_qa_schema_profile(None, "nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_qa_schema_to_weights() {
    let config = load_qa_schema(None).unwrap();
    let weights = qa_schema_to_weights(&config);
    // syntax weight = 0.10 * 100 = 10.0
    assert!((weights.syntax - 10.0).abs() < 0.01);
    // test weight = 0.30 * 100 = 30.0
    assert!((weights.test - 30.0).abs() < 0.01);
    assert!(weights.total() > 0.0);
}

// -- Embedded template validity -----------------------------------------

#[test]
fn test_embedded_rust_cargo_toml_is_valid_toml() {
    // Render with dummy vars first so placeholders don't break parsing
    let mut vars = HashMap::new();
    vars.insert("project_name".into(), "test_project".into());
    vars.insert("module_name".into(), "test_project".into());
    vars.insert("project_description".into(), "test".into());
    vars.insert("repository_url".into(), "".into());
    vars.insert("project_url".into(), "".into());
    vars.insert("keywords".into(), "test".into());
    vars.insert("categories".into(), "test".into());

    let rendered = TemplateEngine::render_template(RUST_CARGO_TOML, &vars);
    let parsed: Result<toml::Value, _> = toml::from_str(&rendered);
    assert!(
        parsed.is_ok(),
        "Rendered Cargo.toml should be valid TOML: {:?}",
        parsed.err()
    );
}

#[test]
fn test_embedded_nodejs_package_json_is_valid_json() {
    let mut vars = HashMap::new();
    vars.insert("project_name".into(), "test-project".into());
    vars.insert("project_description".into(), "test".into());
    vars.insert("repository_url".into(), "".into());
    vars.insert("project_url".into(), "".into());
    vars.insert("keywords".into(), "test".into());

    let rendered = TemplateEngine::render_template(NODEJS_PACKAGE_JSON, &vars);
    let parsed: Result<serde_json::Value, _> = serde_json::from_str(&rendered);
    assert!(
        parsed.is_ok(),
        "Rendered package.json should be valid JSON: {:?}",
        parsed.err()
    );
}

#[test]
fn test_embedded_prettierrc_is_valid_json() {
    let parsed: Result<serde_json::Value, _> = serde_json::from_str(NODEJS_PRETTIERRC);
    assert!(
        parsed.is_ok(),
        ".prettierrc should be valid JSON: {:?}",
        parsed.err()
    );
}

#[test]
fn test_embedded_tsconfig_is_parseable() {
    // tsconfig.json uses JSON-with-comments (JSONC) which TypeScript supports
    // but serde_json does not. Verify it at least contains the expected keys.
    assert!(NODEJS_TSCONFIG_JSON.contains("compilerOptions"));
    assert!(NODEJS_TSCONFIG_JSON.contains("\"strict\": true"));
    assert!(NODEJS_TSCONFIG_JSON.contains("\"outDir\""));
}

#[test]
fn test_embedded_qa_schema_is_valid_yaml() {
    // The schema is a multi-document YAML. Verify each document parses
    // individually via the iterator API.
    let mut count = 0;
    for document in serde_yaml::Deserializer::from_str(QA_SCHEMA_YAML) {
        let val = serde_yaml::Value::deserialize(document);
        assert!(
            val.is_ok(),
            "YAML document {} should parse: {:?}",
            count,
            val.err()
        );
        count += 1;
    }
    assert!(
        count >= 3,
        "Should have at least 3 YAML documents (standard, strict, minimal)"
    );
}

// -- CI workflow generation ---------------------------------------------

#[test]
fn test_ci_workflow_generation_rust() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        with_ci: true,
        ..Default::default()
    };

    let files = engine
        .scaffold_project("rust", "ci-test", dir.path(), &opts)
        .unwrap();
    assert!(files.contains(&".github/workflows/rust-qa.yml".to_string()));

    let wf_content =
        std::fs::read_to_string(dir.path().join(".github/workflows/rust-qa.yml")).unwrap();
    assert!(wf_content.contains("cargo check"));
    assert!(wf_content.contains("cargo clippy"));
}

#[test]
fn test_ci_workflow_generation_python() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        with_ci: true,
        ..Default::default()
    };

    let files = engine
        .scaffold_project("python", "ci-test", dir.path(), &opts)
        .unwrap();
    assert!(files.contains(&".github/workflows/python-qa.yml".to_string()));
}

#[test]
fn test_ci_workflow_generation_nodejs() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        with_ci: true,
        ..Default::default()
    };

    let files = engine
        .scaffold_project("nodejs", "ci-test", dir.path(), &opts)
        .unwrap();
    assert!(files.contains(&".github/workflows/nodejs-qa.yml".to_string()));
}

#[test]
fn test_no_ci_when_disabled() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        with_ci: false,
        ..Default::default()
    };

    let files = engine
        .scaffold_project("rust", "no-ci", dir.path(), &opts)
        .unwrap();
    assert!(!files.iter().any(|f| f.contains(".github")));
}

#[test]
fn test_no_tests_when_disabled() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions {
        with_tests: false,
        ..Default::default()
    };

    let files = engine
        .scaffold_project("rust", "no-tests", dir.path(), &opts)
        .unwrap();
    assert!(!files.iter().any(|f| f.contains("tests/")));
}

// -- Interview integration ----------------------------------------------

#[test]
fn test_scaffold_from_context_rust() {
    use crate::interview::{InterviewContext, ProjectType, TestingPreference};

    let dir = TempDir::new().unwrap();
    let project_dir = dir.path().join("my-rust-app");
    std::fs::create_dir_all(&project_dir).unwrap();

    let ctx = InterviewContext {
        language: Some("Rust".into()),
        framework: Some("axum".into()),
        project_type: Some(ProjectType::WebApi),
        testing_preference: Some(TestingPreference::TestsAfter),
        output_dir: None,
        scope: None,
        extra_notes: vec![],
        task: "Build a REST API".into(),
    };

    let files = scaffold_from_context(&ctx, &project_dir).unwrap();
    assert!(files.contains(&"Cargo.toml".to_string()));
    assert!(files.contains(&"src/main.rs".to_string()));
}

#[test]
fn test_scaffold_from_context_python() {
    use crate::interview::{InterviewContext, TestingPreference};

    let dir = TempDir::new().unwrap();
    let project_dir = dir.path().join("my-python-app");
    std::fs::create_dir_all(&project_dir).unwrap();

    let ctx = InterviewContext {
        language: Some("Python".into()),
        framework: None,
        project_type: None,
        testing_preference: Some(TestingPreference::Tdd),
        output_dir: None,
        scope: None,
        extra_notes: vec![],
        task: "A Python service".into(),
    };

    let files = scaffold_from_context(&ctx, &project_dir).unwrap();
    assert!(files.contains(&"pyproject.toml".to_string()));
}

#[test]
fn test_scaffold_from_context_no_tests() {
    use crate::interview::{InterviewContext, TestingPreference};

    let dir = TempDir::new().unwrap();
    let project_dir = dir.path().join("no-tests-app");
    std::fs::create_dir_all(&project_dir).unwrap();

    let ctx = InterviewContext {
        language: Some("Rust".into()),
        framework: None,
        project_type: None,
        testing_preference: Some(TestingPreference::None),
        output_dir: None,
        scope: None,
        extra_notes: vec![],
        task: "Quick script".into(),
    };

    let files = scaffold_from_context(&ctx, &project_dir).unwrap();
    assert!(!files.iter().any(|f| f.contains("tests/")));
}

#[test]
fn scaffold_from_context_writes_rust_project() {
    let dir = tempfile::tempdir().unwrap();
    let ctx = crate::interview::InterviewContext {
        language: Some("rust".into()),
        framework: None,
        project_type: None,
        testing_preference: Some(crate::interview::TestingPreference::Tdd),
        output_dir: None,
        scope: None,
        extra_notes: vec![],
        task: "test scaffold".into(),
    };
    let files = scaffold_from_context(&ctx, dir.path()).unwrap();
    assert!(!files.is_empty());
    assert!(files.iter().any(|f| f.ends_with("Cargo.toml")));
}

// -- Overwrite refusal ---------------------------------------------------

#[test]
fn test_scaffold_refuses_to_overwrite_existing_files() {
    let dir = TempDir::new().unwrap();
    let project_dir = dir.path().join("app");
    std::fs::create_dir_all(&project_dir).unwrap();
    // Pre-existing user file that a scaffold would silently clobber.
    std::fs::write(
        project_dir.join("Cargo.toml"),
        "[package]\nname = \"mine\"\n",
    )
    .unwrap();

    let engine = TemplateEngine::new();
    let opts = ScaffoldOptions::default();
    let result = engine.scaffold_project("rust", "app", &project_dir, &opts);

    let err = result.expect_err("scaffolding over existing files must fail");
    let msg = err.to_string();
    assert!(
        msg.contains("Cargo.toml"),
        "error must list the conflicting file(s): {}",
        msg
    );
    assert!(
        msg.contains("already exist") || msg.contains("refusing"),
        "error must explain the refusal: {}",
        msg
    );
    // Nothing else may be written: no half-scaffold left behind.
    assert!(
        !project_dir.join("src/main.rs").exists(),
        "no files should be written when the scaffold is refused"
    );
    // The existing file must be untouched.
    let content = std::fs::read_to_string(project_dir.join("Cargo.toml")).unwrap();
    assert!(content.contains("name = \"mine\""));
}

#[test]
fn test_scaffold_force_overwrites_existing_files() {
    let dir = TempDir::new().unwrap();
    let project_dir = dir.path().join("app");
    std::fs::create_dir_all(&project_dir).unwrap();
    std::fs::write(
        project_dir.join("Cargo.toml"),
        "[package]\nname = \"mine\"\n",
    )
    .unwrap();

    let engine = TemplateEngine::new();
    let opts = ScaffoldOptions::default();
    let files = engine
        .scaffold_project_force("rust", "app", &project_dir, &opts)
        .expect("force scaffold should succeed");
    assert!(files.contains(&"Cargo.toml".to_string()));
    // The forced scaffold actually replaced the file.
    let content = std::fs::read_to_string(project_dir.join("Cargo.toml")).unwrap();
    assert!(
        content.contains("name = \"app\""),
        "force should clobber, got: {}",
        content
    );
}

#[test]
fn test_scaffold_from_context_honors_output_dir_subdirectory() {
    use crate::interview::InterviewContext;

    let dir = TempDir::new().unwrap();
    let ctx = InterviewContext {
        language: Some("Rust".into()),
        framework: None,
        project_type: None,
        testing_preference: None,
        output_dir: Some("my-new-app".into()),
        scope: None,
        extra_notes: vec![],
        task: "test scaffold".into(),
    };

    let files = scaffold_from_context(&ctx, dir.path()).unwrap();
    assert!(files.contains(&"Cargo.toml".to_string()));
    // Files must land in the chosen subdirectory, NOT the caller's dir.
    assert!(
        dir.path().join("my-new-app/Cargo.toml").exists(),
        "scaffold should honor the interview's output-dir answer"
    );
    assert!(
        !dir.path().join("Cargo.toml").exists(),
        "nothing should be written into the caller's directory"
    );
}

#[test]
fn test_resolve_output_dir_rejects_path_traversal() {
    use crate::interview::InterviewContext;

    let base = std::path::Path::new("/tmp/scaffold-root");
    let ctx = |dir: &str| InterviewContext {
        language: None,
        framework: None,
        project_type: None,
        testing_preference: None,
        output_dir: Some(dir.to_string()),
        scope: None,
        extra_notes: vec![],
        task: String::new(),
    };

    for bad in ["../escape", "/abs/path", "a/b", "..", "x\\y"] {
        assert!(
            resolve_output_dir(&ctx(bad), base).is_err(),
            "'{}' must be rejected as an output-dir answer",
            bad
        );
    }
    // Plain names and the current-dir answers are accepted.
    assert_eq!(
        resolve_output_dir(&ctx("my-app"), base).unwrap(),
        base.join("my-app")
    );
    assert_eq!(resolve_output_dir(&ctx("."), base).unwrap(), base);
}

// -- Available templates ------------------------------------------------

#[test]
fn test_available_templates() {
    let templates = TemplateEngine::available_templates();
    assert_eq!(templates.len(), 3);
    assert!(templates.iter().any(|t| t.language == "rust"));
    assert!(templates.iter().any(|t| t.language == "python"));
    assert!(templates.iter().any(|t| t.language == "nodejs"));
}

// -- Unsupported language -----------------------------------------------

#[test]
fn test_scaffold_unsupported_language() {
    let dir = TempDir::new().unwrap();
    let engine = TemplateEngine::with_override_dir(None);
    let opts = ScaffoldOptions::default();

    let result = engine.scaffold_project("haskell", "test", dir.path(), &opts);
    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(err_msg.contains("Unsupported language"));
}

// -- Runtime override ---------------------------------------------------

#[test]
fn test_runtime_override() {
    let dir = TempDir::new().unwrap();
    let override_dir = dir.path().join("overrides");
    let rust_dir = override_dir.join("rust");
    std::fs::create_dir_all(&rust_dir).unwrap();

    // Write a custom Cargo.toml override
    std::fs::write(
        rust_dir.join("Cargo.toml"),
        "[package]\nname = \"{{project_name}}\"\nversion = \"99.0.0\"\nedition = \"2021\"\n",
    )
    .unwrap();

    let engine = TemplateEngine::with_override_dir(Some(override_dir));
    let opts = ScaffoldOptions::default();
    let project_dir = dir.path().join("project");
    std::fs::create_dir_all(&project_dir).unwrap();

    let _files = engine
        .scaffold_project("rust", "overridden", &project_dir, &opts)
        .unwrap();

    let cargo_content = std::fs::read_to_string(project_dir.join("Cargo.toml")).unwrap();
    assert!(
        cargo_content.contains("99.0.0"),
        "Should use the overridden template"
    );
}