Cotton code
74 lines · 2.6 KB · RustRaw
//! The registry is TOTAL and every spelling is unique.//!//! A `match` on `ProviderKey` is exhaustive by the compiler, so what a test can//! still lose is the LIST: a fifth variant that nobody added to//! [`PROVIDER_KEYS`] compiles, and then the Keys page draws three fields, the//! server resolves three keys and the fourth is silently off. `total` is the//! guard for exactly that.use std::collections::BTreeSet;use super::*;/// Every variant is in the list. Written as a match so adding a variant is a/// compile error here until somebody says where it belongs.#[test]fn every_provider_key_is_in_the_list() {    for key in [        ProviderKey::Anthropic,        ProviderKey::OpenAi,        ProviderKey::AssemblyAi,        ProviderKey::Brave,    ] {        assert!(            PROVIDER_KEYS.contains(&key),            "{key:?} is declared but is not in PROVIDER_KEYS, so nothing draws or resolves it"        );    }    assert_eq!(PROVIDER_KEYS.len(), 4);}#[test]fn the_three_spellings_are_each_unique_and_never_blank() {    let names: BTreeSet<&str> = PROVIDER_KEYS.iter().map(|key| key.name()).collect();    let services: BTreeSet<&str> = PROVIDER_KEYS.iter().map(|key| key.service()).collect();    let env: BTreeSet<&str> = PROVIDER_KEYS.iter().map(|key| key.env_name()).collect();    assert_eq!(names.len(), PROVIDER_KEYS.len());    assert_eq!(services.len(), PROVIDER_KEYS.len());    assert_eq!(env.len(), PROVIDER_KEYS.len());    for key in PROVIDER_KEYS {        assert!(!key.label().is_empty());        assert!(!key.rests().is_empty());    }}/// The `api.` prefix is what keeps a provider key apart from a login somebody/// called "Anthropic".#[test]fn every_name_is_prefixed_so_it_cannot_collide_with_a_login() {    for key in PROVIDER_KEYS {        assert!(            key.name().starts_with("api."),            "{} is not prefixed",            key.name()        );    }}#[test]fn a_name_and_a_service_both_find_their_key() {    for key in PROVIDER_KEYS {        assert_eq!(ProviderKey::from_name(key.name()), Some(key));        assert_eq!(ProviderKey::from_service(key.service()), Some(key));    }    assert_eq!(ProviderKey::from_name("api.nobody"), None);    assert_eq!(ProviderKey::from_name("anthropic"), None);    assert_eq!(ProviderKey::from_service("document"), None);}/// The one string this module inherited rather than chose. Renaming it would/// orphan the row the owner already stored.#[test]fn the_anthropic_service_is_the_string_that_was_already_in_the_file() {    assert_eq!(ProviderKey::Anthropic.service(), "anthropic");}