#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed panic")]

use std::collections::BTreeMap;

use super::{Source, Warning};
use crate::{
    test::{ExpectFile, ExpectValue, Expectation},
    warning::{self, test},
};

/// Expectations for the result of calling `timezone::find_or_infer`.
#[derive(serde::Deserialize)]
pub(crate) struct FindOrInferExpect {
    /// The expected timezone.
    #[serde(default)]
    timezone: Expectation<String>,

    /// A list of expected warnings by `Warning::id()`.
    #[serde(default)]
    warnings: Expectation<BTreeMap<String, Vec<String>>>,
}

#[track_caller]
pub(crate) fn assert_find_or_infer_outcome(
    timezone: Source,
    expect: ExpectFile<FindOrInferExpect>,
    warnings: &warning::Set<Warning>,
) {
    let ExpectFile {
        value: expect,
        expect_file_name,
    } = expect;

    let Some(expect) = expect else {
        assert!(
                warnings.is_empty(),
                "There is no expectation file at `{expect_file_name}` but the timezone has warnings;\n{:?}",
                warnings.path_id_map()
            );
        return;
    };

    if let Expectation::Present(ExpectValue::Some(expected)) = &expect.timezone {
        assert_eq!(expected, &timezone.into_timezone().to_string());
    }

    test::assert_warnings(&expect_file_name, warnings, expect.warnings);
}
