"""Local trusted-zone URL tests. No database.""" from __future__ import annotations import os import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) os.environ.setdefault("KANSHO_PROVIDER_KEY", "") os.environ.setdefault("KANSHO_DETECT_PROVIDER_KEY", "") Path(tempfile.gettempdir()).mkdir(parents=True, exist_ok=True) from entity_detect import ( DETECT_MAX_TOKENS, DETECT_MAX_TOKENS_LOCAL, DETECT_TIMEOUT, DETECT_TIMEOUT_LOCAL, detect_chat_limits, ) from providers import ProviderConfig, is_local_url def expect(ok: bool, message: str) -> None: if not ok: raise SystemExit(f"FAIL: {message}") print(f"OK {message}") def main() -> None: expect(is_local_url("http://127.0.0.1:11434/v1/chat/completions"), "loopback is local") expect(is_local_url("http://localhost:11434/v1/chat/completions"), "localhost is local") expect(is_local_url("http://192.168.2.144:11434/v1/chat/completions"), "RFC1918 Ollama host is local") expect(is_local_url("http://10.0.0.8:11434/v1/chat/completions"), "10/8 is local") expect(is_local_url("http://ollama:11434/v1/chat/completions"), "compose hostname ollama is local") expect(not is_local_url("https://openrouter.ai/api/v1/chat/completions"), "OpenRouter is not local") expect(not is_local_url("https://8.8.8.8/v1/chat/completions"), "public IP is not local") remote = ProviderConfig( role="detect", name="openrouter", mode="http", url="https://openrouter.ai/api/v1/chat/completions", model="openai/gpt-4.1-nano", key="x", local=False, zdr=True, no_train=True, ) local = ProviderConfig( role="detect", name="ollama", mode="http", url="http://192.168.2.144:11434/v1/chat/completions", model="phi3:mini", key="", local=True, zdr=True, no_train=True, ) expect(detect_chat_limits(remote) == (DETECT_TIMEOUT, DETECT_MAX_TOKENS), "remote detect keeps 90s/1024") expect( detect_chat_limits(local) == (DETECT_TIMEOUT_LOCAL, DETECT_MAX_TOKENS_LOCAL), "local detect waits 300s with a 256-token cap", ) print("local url tests passed.") if __name__ == "__main__": main()