capy-cli 0.10.0

The command-line interface for the Capy web framework.
Documentation
//! Generated frontend contracts and explicit project selection.
use capy_cli::{new_project, FrontendDriver, ProjectConfig};
use std::{fs, process::Command};

#[test]
fn default_project_has_a_capy_welcome_page_and_valid_react_frontend() {
    let temp = tempfile::tempdir().unwrap();
    let root = temp.path().join("blog");
    new_project(&root, "blog").unwrap();
    assert_eq!(
        ProjectConfig::load(&root).unwrap().frontend.driver,
        FrontendDriver::React
    );
    let package: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(root.join("frontend/package.json")).unwrap())
            .unwrap();
    assert_eq!(package["private"], true);
    assert_eq!(package["scripts"]["build"], "tsc -b && vite build");
    assert_eq!(package["dependencies"].as_object().unwrap().len(), 2);
    for file in [
        "src/main.tsx",
        "src/App.tsx",
        "src/lib/api.ts",
        "src/components/.gitkeep",
        "src/hooks/.gitkeep",
        "src/pages/.gitkeep",
        "public/capy.svg",
        "vite.config.ts",
        "tsconfig.json",
        "tsconfig.app.json",
        "tsconfig.node.json",
        "index.html",
        ".gitignore",
    ] {
        assert!(root.join("frontend").join(file).is_file(), "{file}");
    }
    for name in ["tsconfig.json", "tsconfig.app.json", "tsconfig.node.json"] {
        serde_json::from_str::<serde_json::Value>(
            &fs::read_to_string(root.join("frontend").join(name)).unwrap(),
        )
        .unwrap();
    }
    assert!(root.join("routes/web.rs").is_file());
    assert!(fs::read_to_string(root.join("src/main.rs"))
        .unwrap()
        .contains("application::application().run().await"));
    let main = fs::read_to_string(root.join("src/bootstrap.rs")).unwrap();
    assert!(main.contains(".api_routes("));
    assert!(main.contains(".frontend(\"frontend/dist\")"));
    let vite = fs::read_to_string(root.join("frontend/vite.config.ts")).unwrap();
    assert!(vite.contains("CAPY_BACKEND_URL"));
    assert!(!vite.contains("8000"));
    let page = fs::read_to_string(root.join("frontend/src/App.tsx")).unwrap();
    for content in [
        "Your Capy app is ready.",
        "A calmer way to build web apps in Rust.",
        "Let’s build something cool.",
        "Backend connected ✓",
        "Backend unavailable",
        "src=\"/capy.svg\" alt=\"Capy\"",
        "https://docs.rs/capy-framework",
        "href=\"/api/health\"",
        "api.get('/api/health'",
    ] {
        assert!(page.contains(content), "Missing welcome content: {content}");
    }
    assert_eq!(
        fs::read_to_string(root.join("frontend/public/capy.svg")).unwrap(),
        include_str!("../templates/frontend/public/capy.svg")
    );
    let dependencies = package["devDependencies"].as_object().unwrap();
    assert_eq!(dependencies["@capy/api-contract-tooling"], "workspace:*");
    assert!(root
        .join("frontend/tools/api-contracts/index.mjs")
        .is_file());
    let tooling: serde_json::Value = serde_json::from_str(
        &fs::read_to_string(root.join("frontend/tools/api-contracts/package.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(tooling["dependencies"]["openapi-typescript"], "7.13.0");
    assert_eq!(tooling["dependencies"]["typescript"], "5.9.3");
}

#[test]
fn cli_frontend_options_generate_the_requested_stack() {
    let temp = tempfile::tempdir().unwrap();
    for (name, flags, driver) in [
        ("default", vec![], FrontendDriver::React),
        ("react", vec!["--frontend", "react"], FrontendDriver::React),
        ("service", vec!["--api"], FrontendDriver::None),
        ("none", vec!["--frontend", "none"], FrontendDriver::None),
    ] {
        let result = Command::new(env!("CARGO_BIN_EXE_capy"))
            .args(["new", name])
            .args(flags)
            .current_dir(temp.path())
            .output()
            .unwrap();
        assert!(
            result.status.success(),
            "{}",
            String::from_utf8_lossy(&result.stderr)
        );
        let root = temp.path().join(name);
        assert_eq!(ProjectConfig::load(&root).unwrap().frontend.driver, driver);
        assert_eq!(
            root.join("frontend").exists(),
            driver == FrontendDriver::React
        );
        assert_eq!(
            root.join("routes/web.rs").exists(),
            driver == FrontendDriver::React
        );
        if driver == FrontendDriver::None {
            assert!(!fs::read_to_string(root.join("src/main.rs"))
                .unwrap()
                .contains(".frontend("));
            assert!(!fs::read_to_string(root.join("routes/mod.rs"))
                .unwrap()
                .contains("web"));
        }
    }
    for args in [["--frontend", "vue"], ["--frontend", "unknown"]] {
        assert!(!Command::new(env!("CARGO_BIN_EXE_capy"))
            .args(["new", "invalid"])
            .args(args)
            .current_dir(temp.path())
            .status()
            .unwrap()
            .success());
    }
    assert!(!Command::new(env!("CARGO_BIN_EXE_capy"))
        .args(["new", "invalid", "--api", "--frontend", "react"])
        .current_dir(temp.path())
        .status()
        .unwrap()
        .success());
    assert!(!temp.path().join("invalid").exists());
}

#[test]
fn metadata_is_explicit_and_legacy_projects_remain_backend_only() {
    let temp = tempfile::tempdir().unwrap();
    fs::create_dir(temp.path().join("frontend")).unwrap();
    assert_eq!(
        ProjectConfig::load(temp.path()).unwrap().frontend.driver,
        FrontendDriver::None
    );
    for invalid in [
        "",
        "[frontend]\ndriver = \"vue\"",
        "[frontend]\ndirver = \"react\"",
        "[frontend]\ndriver = \"none\"\nextra = true",
    ] {
        fs::write(temp.path().join("capy.toml"), invalid).unwrap();
        assert!(ProjectConfig::load(temp.path()).is_err(), "{invalid}");
    }
    fs::write(
        temp.path().join("capy.toml"),
        "[frontend]\ndriver = \"react\"",
    )
    .unwrap();
    let driver = ProjectConfig::load(temp.path()).unwrap().frontend.driver;
    assert!(driver
        .validate(temp.path())
        .unwrap_err()
        .to_string()
        .contains("frontend/package.json"));
}