use gonidium::{DiagCode, Severity, parse_effective_config_toml};

#[test]
fn parse_repl_section_into_repl_config() {
    let raw = r#"
[repl]
opt_enabled = false
print_simplified_expr = false
header_locals_limit = 9
"#;

    let cfg = parse_effective_config_toml(raw).expect("parse config");
    assert!(!cfg.repl.opt_enabled);
    assert!(!cfg.repl.print_simplified_expr);
    assert_eq!(cfg.repl.header_locals_limit, 9);
}

#[test]
fn parse_diagnostics_grouped_short_codes() {
    let raw = r#"
[diagnostics]
allow = ["L301"]
warning = ["L201"]
error = ["L102"]
"#;

    let cfg = parse_effective_config_toml(raw).expect("parse config");
    assert_eq!(
        cfg.diag.severity(DiagCode::LowerImplicitPromotion),
        Severity::Allow
    );
    assert_eq!(
        cfg.diag.severity(DiagCode::LowerFloatPrecisionLoss),
        Severity::Warning
    );
    assert_eq!(
        cfg.diag.severity(DiagCode::LowerTypeMismatch),
        Severity::Error
    );
}

#[test]
fn unknown_diagnostic_short_code_is_rejected() {
    let raw = r#"
[diagnostics]
warning = ["X999"]
"#;

    let err = parse_effective_config_toml(raw).expect_err("should reject unknown short code");
    assert!(err.contains("unknown diagnostic code"));
}

/// P004/P005 are not valid short codes; the correct ones are P101/P102.
#[test]
fn old_p004_p005_short_codes_are_rejected() {
    for bad_code in ["P004", "P005"] {
        let raw = format!("[diagnostics]\nwarning = [\"{bad_code}\"]\n");
        let err = parse_effective_config_toml(&raw)
            .expect_err("should reject old/wrong diagnostic short code");
        assert!(
            err.contains("unknown diagnostic code"),
            "expected rejection of {bad_code}: {err}"
        );
    }
}

/// P101, P102, and C201 must round-trip through the config parser.
#[test]
fn parse_diagnostics_parse_and_compose_short_codes() {
    let raw = r#"
[diagnostics]
allow = ["P101", "P102", "C201"]
"#;

    let cfg = parse_effective_config_toml(raw).expect("parse config");
    assert_eq!(
        cfg.diag.severity(DiagCode::ParseUndefinedVar),
        Severity::Allow
    );
    assert_eq!(
        cfg.diag.severity(DiagCode::ParseReservedIdentifier),
        Severity::Allow
    );
    assert_eq!(
        cfg.diag.severity(DiagCode::ComposeSymbolReuse),
        Severity::Allow
    );
}
