//! pd.2 (`prelude-decouple` milestone): pin that
//! `surface::load_workspace` injects the prelude into every workspace
//! and rejects user modules trying to take the reserved name.

use ailang_core::workspace::WorkspaceLoadError;
use ailang_surface::load_workspace;
use std::path::Path;

#[test]
fn surface_load_workspace_injects_prelude_into_every_workspace() {
    // Use any existing single-module example from `examples/`. The hello
    // example is a stable choice (used by other surface tests). Post-
    // form-a.1 the canonical sidecar is `.ail` (Form A); the `.ail.json`
    // for `hello` was deleted by form-a.1 T8.
    let entry = Path::new("../../examples/hello.ail");
    let ws = load_workspace(entry).expect("load");
    assert!(
        ws.modules.contains_key("prelude"),
        "prelude must appear in every workspace post-pd.2"
    );
}

#[test]
fn surface_load_workspace_rejects_user_module_named_prelude() {
    let dir = tempfile::tempdir().unwrap();
    // Create a JSON-form fixture for the user-side "prelude" module.
    // Surface's loader should fail with ReservedModuleName.
    let prelude_path = dir.path().join("prelude.ail.json");
    std::fs::write(
        &prelude_path,
        r#"{"schema":"ailang/v0","name":"prelude","imports":[],"defs":[]}"#,
    )
    .unwrap();
    // Single-file workspace where the entry IS named "prelude" trips
    // the reservation.
    let err = load_workspace(&prelude_path).expect_err("must reject");
    match err {
        WorkspaceLoadError::ReservedModuleName { name } => {
            assert_eq!(name, "prelude");
        }
        other => panic!("expected ReservedModuleName, got: {other:?}"),
    }
}
