#![allow(clippy::unwrap_used)]
use std::collections::HashMap;
use fraiseql_core::schema::SourceDefinition;
use fraiseql_functions::{FunctionModule, RuntimeType};
use super::{schedulable, source_host_config_from, sources_enabled_from};
use crate::server_config::SourcesConfig;
fn module(name: &str) -> FunctionModule {
FunctionModule::from_source(name.to_string(), String::new(), RuntimeType::Deno)
}
fn registry() -> HashMap<String, FunctionModule> {
HashMap::from([("pollOrders".to_string(), module("pollOrders"))])
}
#[test]
fn schedulable_keeps_only_enabled_backed_valid_sources() {
let sources = vec![
SourceDefinition::new("orders", "*/5 * * * *", "pollOrders"),
SourceDefinition::new("disabled", "*/5 * * * *", "pollOrders").disabled(),
SourceDefinition::new("native", "*/5 * * * *", "nativeThing"),
SourceDefinition::new("bad-cron", "not-a-cron", "pollOrders"),
];
let kept = schedulable(&sources, ®istry());
let names: Vec<&str> = kept.iter().map(|(source, _, _)| source.name.as_str()).collect();
assert_eq!(names, ["orders"], "only the enabled, backed, valid-cron source is scheduled");
assert_eq!(kept[0].2.expression, "*/5 * * * *");
}
#[test]
fn schedulable_is_empty_when_nothing_qualifies() {
let sources = vec![SourceDefinition::new("native", "*/5 * * * *", "unloaded")];
assert!(schedulable(&sources, ®istry()).is_empty());
}
#[test]
fn enabled_resolves_env_over_config() {
let on = SourcesConfig {
enabled: true,
allowed_domains: vec![],
log_payloads: false,
};
let off = SourcesConfig {
enabled: false,
allowed_domains: vec![],
log_payloads: false,
};
assert!(sources_enabled_from(&on, |_| None));
assert!(!sources_enabled_from(&off, |_| None));
assert!(!sources_enabled_from(&on, |_| Some("false".to_string())));
assert!(!sources_enabled_from(&on, |_| Some("OFF".to_string())));
assert!(sources_enabled_from(&off, |_| Some("true".to_string())));
assert!(sources_enabled_from(&off, |_| Some("1".to_string())));
}
#[test]
fn host_config_allowlist_resolves_env_over_config() {
let config = SourcesConfig {
enabled: true,
allowed_domains: vec!["from-toml.example".to_string()],
log_payloads: false,
};
let host = source_host_config_from(&config, |_| None);
assert_eq!(host.allowed_domains, vec!["from-toml.example".to_string()]);
let host = source_host_config_from(&config, |_| Some(" a.example, b.example ".to_string()));
assert_eq!(host.allowed_domains, vec!["a.example".to_string(), "b.example".to_string()]);
let empty = SourcesConfig::default();
assert!(source_host_config_from(&empty, |_| None).allowed_domains.is_empty());
}