use kernel::install::reference::{
hugging_face_repo, is_hugging_face_link, is_ollama_link, normalized, normalized_tag,
ollama_direct_tag, ollama_install_tag, ollama_tag,
};
use kernel::install::{InstallError, InstallProviderId};
#[test]
fn recognizes_hugging_face_and_ollama_links() {
assert!(is_hugging_face_link("https://huggingface.co/org/model"));
assert!(is_hugging_face_link("hf.co/org/model"));
assert!(!is_hugging_face_link("org/model"));
assert!(is_ollama_link("ollama.com/library/llama3"));
assert!(is_ollama_link("registry.ollama.ai/llama3"));
assert!(!is_ollama_link("llama3"));
}
#[test]
fn extracts_a_hugging_face_repo_from_bare_and_link_forms() {
assert_eq!(
hugging_face_repo("meta-llama/Llama-3"),
Some("meta-llama/Llama-3".to_owned())
);
assert_eq!(
hugging_face_repo("hf.co/org/model"),
Some("org/model".to_owned())
);
assert_eq!(
hugging_face_repo("https://huggingface.co/org/model"),
Some("org/model".to_owned())
);
assert_eq!(
hugging_face_repo("org/model/?tab=files"),
Some("org/model".to_owned())
);
assert_eq!(
hugging_face_repo("huggingface.co/org/model/tree/main"),
Some("org/model".to_owned())
);
}
#[test]
fn rejects_non_hugging_face_repos() {
assert_eq!(hugging_face_repo("org"), None);
assert_eq!(hugging_face_repo("datasets/foo"), None);
assert_eq!(hugging_face_repo("spaces/foo"), None);
assert_eq!(hugging_face_repo("org/model:tag"), None);
assert_eq!(hugging_face_repo("org/model/extra"), None);
assert_eq!(hugging_face_repo("huggingface.co/org/model/nonsense"), None);
assert_eq!(hugging_face_repo(" "), None);
}
#[test]
fn extracts_ollama_tags_with_namespace_rules() {
assert_eq!(ollama_tag("llama3"), Some("llama3".to_owned()));
assert_eq!(ollama_tag("llama3:8b"), Some("llama3:8b".to_owned()));
assert_eq!(ollama_tag("org/model"), None);
assert_eq!(
ollama_tag("org/model:tag"),
Some("org/model:tag".to_owned())
);
assert_eq!(
ollama_install_tag("org/model"),
Some("org/model".to_owned())
);
}
#[test]
fn extracts_ollama_tags_from_links() {
assert_eq!(
ollama_tag("ollama.com/library/llama3"),
Some("llama3".to_owned())
);
assert_eq!(
ollama_tag("registry.ollama.ai/mistral/7b"),
Some("mistral/7b".to_owned())
);
assert_eq!(ollama_tag("library/llama3"), Some("llama3".to_owned()));
}
#[test]
fn ollama_direct_tag_only_for_explicit_or_linked_inputs() {
assert_eq!(ollama_direct_tag("llama3:8b"), Some("llama3:8b".to_owned()));
assert_eq!(ollama_direct_tag("llama3"), None);
assert_eq!(
ollama_direct_tag("ollama.com/library/llama3"),
Some("llama3".to_owned())
);
assert_eq!(ollama_direct_tag("org/model"), None);
}
#[test]
fn normalizes_tags_and_by_provider() {
assert_eq!(normalized_tag("llama3"), "llama3:latest");
assert_eq!(normalized_tag("Llama3:8B"), "llama3:8b");
assert_eq!(
normalized(&InstallProviderId::ollama(), "Llama3"),
"llama3:latest"
);
assert_eq!(
normalized(&InstallProviderId::huggingface(), "Org/Model"),
"org/model"
);
}
#[test]
fn hugging_face_repo_edge_cases() {
assert_eq!(
hugging_face_repo("org/model#readme"),
Some("org/model".to_owned())
);
assert_eq!(hugging_face_repo("org//model"), None);
assert_eq!(
hugging_face_repo("www.huggingface.co/org/model"),
Some("org/model".to_owned())
);
assert_eq!(
hugging_face_repo("HF.CO/org/model"),
Some("org/model".to_owned())
);
assert_eq!(
hugging_face_repo("www.huggingface.co/org/model/tree/main"),
Some("org/model".to_owned())
);
assert_eq!(hugging_face_repo("a b/c"), None);
}
#[test]
fn ollama_tag_link_and_shape_edge_cases() {
assert_eq!(
ollama_tag("ollama.com/library/llama3/blobs"),
Some("llama3".to_owned())
);
assert_eq!(
ollama_tag("www.ollama.com/library/mistral"),
Some("mistral".to_owned())
);
assert_eq!(ollama_tag("model:"), None);
assert_eq!(ollama_tag("a b"), None);
assert_eq!(
ollama_install_tag("registry.ollama.ai/mistral/7b"),
Some("mistral/7b".to_owned())
);
assert_eq!(
ollama_direct_tag("org/model:tag"),
Some("org/model:tag".to_owned())
);
}
#[test]
fn install_errors_render_the_expected_messages() {
assert_eq!(
InstallError::ProviderUnknown(InstallProviderId::huggingface()).to_string(),
"No install provider is registered as huggingface."
);
assert_eq!(
InstallError::ReferenceInvalid("weird".to_owned()).to_string(),
"weird is not a reference this provider understands."
);
let disk = InstallError::InsufficientDisk {
required_bytes: 8_000_000_000,
available_bytes: 1_000_000_000,
}
.to_string();
assert!(disk.starts_with("Not enough free disk space:"));
assert!(disk.contains("is available."));
}
#[test]
fn provider_id_round_trips() {
assert_eq!(InstallProviderId::ollama().as_str(), "ollama");
assert_eq!(InstallProviderId::huggingface().as_str(), "huggingface");
assert_eq!(InstallProviderId::ollama().to_string(), "ollama");
}