usesuper::*;fn fixture_path() -> std::path::PathBuf { cotton_store::manifest_dir().join("tests/fixtures/film_sidecars.json")}fn fixture() -> Value { serde_json::from_str(&std::fs::read_to_string(fixture_path()).expect("the shared fixture")).expect("the fixture is JSON")}fn names(list: &Value) -> Vec<String> { list.as_array().expect("a list").iter().map(|n| n.as_str().expect("a name").to_string()).collect()}/// RULE: the phone and the Mac look for one list of sidecars: the shared/// fixture's `lists` is this file's, and the film-nfo test holds the film/// importer to the same. Regenerate with/// COTTON_WRITE_FILM_SIDECARS=1.#[test]fn the_shared_lists_are_these() {letmut fixture = fixture();if std::env::var_os("COTTON_WRITE_FILM_SIDECARS").is_some() { fixture["lists"] = lists(); std::fs::write(fixture_path(), serde_json::to_string_pretty(&fixture).unwrap() + "\n").expect("the fixture is written"); } assert_eq!(fixture["lists"], lists(), "regenerate with COTTON_WRITE_FILM_SIDECARS=1, and change the film importer's NFO writer alike");}/// RULE: every case of the shared fixture answers here as written: what lies/// beside a film, an episode and a series, what an NFO says, and what it/// fills of a row.#[test]fn the_shared_fixture_reads_alike() {let fixture = fixture();for case in fixture["sidecars"].as_array().unwrap() {let found = sidecars(case["video"].as_str().unwrap(), &names(&case["siblings"]), case["episode"].as_bool().unwrap()); assert_eq!(serde_json::to_value(&found).unwrap(), case["expect"], "beside {}", case["video"]); }for case in fixture["series"].as_array().unwrap() { assert_eq!(serde_json::to_value(series_sidecars(&names(&case["siblings"]))).unwrap(), case["expect"], "a series' folder {}", case["siblings"]); }letmut read = std::collections::BTreeMap::new();for case in fixture["nfo"].as_array().unwrap() {let nfo = read_nfo(case["text"].as_str().unwrap()); assert_eq!(serde_json::to_value(&nfo).unwrap(), case["expect"], "the NFO “{}”", case["name"]); read.insert(case["name"].as_str().unwrap().to_string(), nfo); }for case in fixture["patch"].as_array().unwrap() {let nfo = read[case["nfo"].as_str().unwrap()].clone().expect("an NFO");letmut patch = nfo_patch(&nfo, case["table"].as_str().unwrap(), case["row"].as_object().unwrap());// filled_from is JSON text in the row; the fixture says it as an object.ifletSome(from) = patch.get("filled_from").and_then(Value::as_str) {let parsed: Value = serde_json::from_str(from).unwrap(); patch.insert("filled_from".into(), parsed); } assert_eq!(Value::Object(patch.clone()), case["expect"], "the patch “{}”", case["name"]);// Ids come first: the order the row is told in.let keys: Vec<&String> = patch.keys().filter(|k| *k != "filled_from").collect();let ids = keys.iter().take_while(|k| k.ends_with("_id")).count(); assert!(keys[ids..].iter().all(|k| !k.ends_with("_id")), "ids first in “{}”: {keys:?}", case["name"]); }}/// RULE: a subtitle's dotted words name its language as an ISO 639-2 code, an/// English name or a BCP 47 tag; anything else leaves it unknown, never/// guessed.#[test]fn a_subtitles_language_is_read_never_guessed() { assert_eq!(language_of("eng").as_deref(), Some("en")); assert_eq!(language_of("Hindi").as_deref(), Some("hi")); assert_eq!(language_of("pt-BR").as_deref(), Some("pt-br")); assert_eq!(language_of("und"), None); assert_eq!(language_of("1080p"), None); assert_eq!(language_of("Some Group"), None);}// RULE (SUBTITLE FILE, 25 Nov 2026): a lone subtitle file the owner gives says its language by the dotted words before its extension, marks passed over, and only a language the rules know; a word of the title is none.#[test]fn a_given_subtitle_file_says_its_language_by_its_last_words() { assert_eq!(given_language("Night.Ferry.2019.en.srt").as_deref(), Some("en")); assert_eq!(given_language("/tmp/picked/x/Night Ferry.French.srt").as_deref(), Some("fr")); assert_eq!(given_language("Night.Ferry.eng.forced.srt").as_deref(), Some("en")); assert_eq!(given_language("Night.Ferry.pt-br.vtt").as_deref(), Some("pt-br")); assert_eq!(given_language("The.Matrix.srt"), None, "a title's word is no language"); assert_eq!(given_language("night-ferry.srt"), None); assert_eq!(given_language("Night.Ferry.sdh.srt"), None, "a mark alone says no language");}/// RULE: a reference in an NFO's field reads as its character, so a title/// with `&` keeps its ampersand (quick-xml hands it over as its own event).#[test]fn an_nfo_field_keeps_its_references() {let nfo = read_nfo("<movie><title>Fish & Chips – Café</title><uniqueid type=\"IMDB\" default=\"true\">tt0000001</uniqueid></movie>").expect("an NFO"); assert_eq!(nfo.title.as_deref(), Some("Fish & Chips \u{2013} Caf\u{e9}"));}