helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Tests of the schema decoder against what helm-schema 0.24.0 does with the same text (recorded
//! with the real binary): aliases, merges, nulls, binary strings and JSON syntax.

use super::parse_schema;

fn described(text: &str, json: bool) -> Result<String, String> {
    parse_schema(text, json).map(|schema| format!("{schema:?}"))
}

/// Texts helm-schema 0.24.0 rejects as YAML or JSON, with its message (recorded from the real
/// binary; `json` texts are `$ref`ed files).
const HARD_FAILURES: [(&str, bool, &str); 5] = [
    (
        "default: &a [*a]",
        false,
        "yaml: anchor 'a' value contains itself",
    ),
    (
        "x-a: &a {y: *a}",
        false,
        "yaml: anchor 'a' value contains itself",
    ),
    (
        "default: {<<: 3}",
        false,
        "yaml: map merge requires map or sequence of maps as the value",
    ),
    ("", true, "unexpected end of JSON input"),
    (
        "type: string",
        true,
        "invalid character 'y' looking for beginning of value",
    ),
];

#[test]
fn hard_yaml_and_json_errors_match_go() {
    for (text, json, expected) in HARD_FAILURES {
        assert_eq!(described(text, json), Err(expected.to_owned()), "{text}");
    }
}

/// Block texts with a substring of the decoded schema that Go produces for them.
const DECODED: [(&str, &str); 6] = [
    (
        "default: {n: 2, <<: {n: 1, m: 3}}",
        r#"default: Some(Object {"m": Number(3), "n": Number(2)})"#,
    ),
    (
        "default: {<<: [{a: 1}, {a: 9, b: 2}]}",
        r#"default: Some(Object {"a": Number(1), "b": Number(2)})"#,
    ),
    ("x-base: &b {type: string}\n<<: *b", r#"types: ["string"]"#),
    (
        "properties:\nanyOf: null\nenum: null\ndependencies: null",
        "properties: None",
    ),
    ("title: !!binary SGVsbG8=", r#"title: "Hello""#),
    (
        "default: !!binary SGVsbG8=",
        r#"default: Some(String("Hello"))"#,
    ),
];

#[test]
fn merges_nulls_and_binary_decode_as_in_go() {
    for (text, expected) in DECODED {
        let described = described(text, false).expect("decodes");

        assert!(described.contains(expected), "{text}: {described}");
    }
}