Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(inference): add offline fetcher for context window tests

Steven Enamakel393c955a9011
diff --git a/crates/openhuman-core/src/inference/context_window.rs b/crates/openhuman-core/src/inference/context_window.rs
index 05bbbb0b4c..d88ccfa9bc 100644
--- a/crates/openhuman-core/src/inference/context_window.rs
+++ b/crates/openhuman-core/src/inference/context_window.rs
@@ -11,9 +11,9 @@ use std::sync::OnceLock;
use tinyinference_llm::model::context_window::{
ContextWindowResolver, ContextWindowRouteInput, ManagedDefaultWindow, ResolvedWindow,
};
-use tinyinference_llm::model::discover::{
- DiscoveryRequest, ModelLimitsCache, ModelListingFetcher, model_limits_cache,
-};
+use tinyinference_llm::model::discover::model_limits_cache;
+#[cfg(test)]
+use tinyinference_llm::model::discover::{ModelLimitsCache, ModelListingFetcher};
use tinyinference_local::profile::LocalProviderKind;
use crate::config::Config;
@@ -77,6 +77,20 @@ async fn resolve_context_window_inner(
) -> ResolvedWindow {
let local_kind = tinyinference_local::profile::kind_from_provider_string(provider);
let route = route_input(role, provider, model, config, extensions, local_kind);
+ #[cfg(test)]
+ {
+ return resolver()
+ .resolve_for_route_with_fetcher(
+ provider,
+ model,
+ local_kind,
+ route,
+ model_limits_cache(),
+ &OfflineFetcher,
+ )
+ .await;
+ }
+ #[cfg(not(test))]
resolver()
.resolve_for_route(
provider,
@@ -89,6 +103,25 @@ async fn resolve_context_window_inner(
.await
}
+/// Unit-test callers never reach a provider unless they explicitly inject a
+/// fetcher through `resolve_context_window_with`.
+#[cfg(test)]
+struct OfflineFetcher;
+
+#[cfg(test)]
+#[async_trait::async_trait]
+impl ModelListingFetcher for OfflineFetcher {
+ async fn get_json(
+ &self,
+ url: &str,
+ _headers: &[(String, String)],
+ ) -> tinyinference_llm::Result<serde_json::Value> {
+ Err(tinyinference_llm::Error::Catalog(format!(
+ "offline in unit tests: {url}"
+ )))
+ }
+}
+
/// Test seam that injects an offline listing fetcher while preserving the
/// production route-input construction.
#[cfg(test)]
@@ -186,6 +219,7 @@ fn static_route_facts(
/// Builds the host-configured model-listing client lazily, only when the
/// native resolver determines discovery can run.
+#[cfg(not(test))]
fn model_limits_client(config: &Config, provider: &str) -> reqwest::Client {
model_limits_client_with(
config,
diff --git a/crates/openhuman-core/src/inference/context_window_tests.rs b/crates/openhuman-core/src/inference/context_window_tests.rs
index 174f3a1bf2..77a89a2409 100644
--- a/crates/openhuman-core/src/inference/context_window_tests.rs
+++ b/crates/openhuman-core/src/inference/context_window_tests.rs
@@ -417,6 +417,7 @@ fn model_limits_client_uses_provider_ca_and_falls_back_to_proxy() {
move |service, timeout, connect_timeout| {
proxy_record.lock().unwrap().push((
"proxy".to_string(),
+ String::new(),
service.to_string(),
timeout,
connect_timeout,
@@ -434,7 +435,13 @@ fn model_limits_client_uses_provider_ca_and_falls_back_to_proxy() {
5,
3
),
- ("proxy".to_string(), MODEL_LIMITS_SERVICE.to_string(), 5, 3)
+ (
+ "proxy".to_string(),
+ String::new(),
+ MODEL_LIMITS_SERVICE.to_string(),
+ 5,
+ 3
+ )
]
);
}