Download tests/test_live_source_regressions.py from nikeshn/LibBee: direct link, hf CLI and curl.
- Browser
- Download file 8.4 kB
-
https://huggingface.co/spaces/nikeshn/LibBee/resolve/main/tests/test_live_source_regressions.py
- Command line
-
hf download hf://spaces/nikeshn/LibBee/tests/test_live_source_regressions.py
-
curl -L -o test_live_source_regressions.py https://huggingface.co/spaces/nikeshn/LibBee/resolve/main/tests/test_live_source_regressions.py
8.4 kB
| """Offline behavior tests for live-source formatting and fallback metadata. | |
| Run with: python -m unittest tests.test_live_source_regressions -v | |
| AST loading isolates these functions from optional provider/FAISS dependencies. | |
| """ | |
| import ast | |
| import asyncio | |
| import html | |
| import logging | |
| from pathlib import Path | |
| import re | |
| import time | |
| from datetime import datetime, timezone, timedelta | |
| from types import SimpleNamespace | |
| from typing import Any, Dict, List, Optional | |
| from urllib.parse import urlsplit | |
| import unittest | |
| ROOT = Path(__file__).resolve().parents[1] / "src" / "agentcore" | |
| def load_functions(filename, names): | |
| tree = ast.parse((ROOT / filename).read_text()) | |
| chosen = [node for node in tree.body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names] | |
| namespace = {"Any": Any, "Dict": Dict, "List": List, "Optional": Optional, | |
| "ChatMessage": Any, "html": html, "re": re, "asyncio": asyncio, | |
| "datetime": datetime, "timezone": timezone, "timedelta": timedelta, | |
| "urlsplit": urlsplit, "time": time, "logger": logging.getLogger("live-source-tests")} | |
| exec(compile(ast.Module(body=chosen, type_ignores=[]), filename, "exec"), namespace) | |
| return namespace | |
| class WebSourceTests(unittest.TestCase): | |
| def setUp(self): | |
| self.ns = load_functions("intents_general.py", { | |
| "_response_field", "_citation_source", "_render_web_text", "_parse_web_response", | |
| "_web_search_result", "_web_search_answer", | |
| }) | |
| def test_openai_citations_are_clickable_and_preserve_prose(self): | |
| prose = 'The library is open.\ue200cite\ue202turn0search0\ue201' | |
| response = {"status": "completed", "output": [ | |
| {"type": "web_search_call", "status": "completed"}, | |
| {"type": "message", "content": [{"type": "output_text", "text": prose, | |
| "annotations": [{"type": "url_citation", "url": "https://library.ku.ac.ae/hours?a=1&b=2", | |
| "title": 'Official <hours> "page"', "end_index": len(prose)}]}]}, | |
| ]} | |
| result = self.ns["_parse_web_response"](response, "gpt") | |
| self.assertTrue(result["live_search"]) | |
| self.assertEqual(len(result["sources"]), 1) | |
| self.assertIn('The library is open.', result["answer"]) | |
| self.assertIn('href="https://library.ku.ac.ae/hours?a=1&b=2"', result["answer"]) | |
| self.assertIn('Official <hours> "page"', result["answer"]) | |
| self.assertNotIn('\ue200cite', result["answer"]) | |
| def test_citations_without_completed_search_do_not_claim_live(self): | |
| response = {"output": [{"type": "message", "content": [{"type": "output_text", "text": "Claim", | |
| "annotations": [{"type": "url_citation", "url": "https://example.org", "end_index": 5}]}]}]} | |
| self.assertFalse(self.ns["_parse_web_response"](response, "gpt")["live_search"]) | |
| def test_anthropic_embedded_error_is_not_live_success(self): | |
| response = {"stop_reason": "end_turn", "content": [ | |
| {"type": "web_search_tool_result", "content": {"type": "web_search_tool_result_error", "error_code": "unavailable"}}, | |
| {"type": "text", "text": "General answer"}, | |
| ]} | |
| self.assertFalse(self.ns["_parse_web_response"](response, "claude")["live_search"]) | |
| def test_anthropic_block_citations_and_safe_text(self): | |
| response = SimpleNamespace(stop_reason="end_turn", content=[ | |
| SimpleNamespace(type="web_search_tool_result", content=[]), | |
| SimpleNamespace(type="text", text='<script>alert(1)</script>Details', citations=[ | |
| SimpleNamespace(type="web_search_result_location", url="https://library.ku.ac.ae", title="Library")]), | |
| ]) | |
| result = self.ns["_parse_web_response"](response, "claude") | |
| self.assertTrue(result["live_search"]) | |
| self.assertIn('<script>', result["answer"]) | |
| self.assertIn('>[1]</a>', result["answer"]) | |
| response.stop_reason = "pause_turn" | |
| self.assertFalse(self.ns["_parse_web_response"](response, "claude")["live_search"]) | |
| def test_unsafe_citation_urls_are_rejected(self): | |
| for url in ("javascript:alert(1)", "data:text/html,test", "https://user:password@example.org", "https://example.org\n/injected"): | |
| with self.subTest(url=url): | |
| self.assertIsNone(self.ns["_citation_source"]({"url": url})) | |
| def test_fallback_metadata_and_notice_do_not_claim_web_search(self): | |
| async def general(*args): | |
| return "Offline answer" | |
| self.ns.update({"get_settings": lambda: SimpleNamespace(openai_api_key="", anthropic_api_key=""), | |
| "_build_history_messages": lambda history: [], "_GUARDRAIL": "", | |
| "_llm_general_answer": general}) | |
| result = asyncio.run(self.ns["_web_search_result"]("current news", [], "gpt")) | |
| self.assertFalse(result["live_search"]) | |
| self.assertEqual(result["sources"], []) | |
| self.assertEqual(result["tools_used"], ["general_knowledge"]) | |
| self.assertIn("couldn’t verify current web information", result["answer"]) | |
| self.assertIn("Offline answer", result["answer"]) | |
| class LibCalTests(unittest.TestCase): | |
| def setUp(self): | |
| self.ns = load_functions("libcal.py", {"_parse_libcal_time", "_format_libcal_day", "_libcal_bool", | |
| "_format_event_datetime", "_libcal_fetch_events", "_libcal_fetch_hours"}) | |
| self.ns["_UAE_TIMEZONE"] = timezone(timedelta(hours=4)) | |
| def test_split_hours_preserve_every_interval_and_note(self): | |
| result = self.ns["_format_libcal_day"]({"status": "open", "note": "Ramadan schedule", "times": [ | |
| {"from": "8:00am", "to": "12:00pm"}, {"from": "4:00pm", "to": "10:00pm"}]}) | |
| self.assertEqual(result, "8:00 AM – 12:00 PM; 4:00 PM – 10:00 PM — Ramadan schedule") | |
| def test_malformed_interval_does_not_hide_valid_later_interval(self): | |
| result = self.ns["_format_libcal_day"]({"status": "open", "hours": [None, "bad", {"opens": "9am", "closes": "5pm"}]}) | |
| self.assertEqual(result, "9 AM – 5 PM") | |
| def test_closed_zero_and_24_hour_notes(self): | |
| self.assertEqual(self.ns["_format_libcal_day"]({"status": 0}), "Closed") | |
| self.assertEqual(self.ns["_format_libcal_day"]({"status": "24hours", "note": "Card access"}), "Open 24 hours — Card access") | |
| def test_event_utc_to_uae_and_overnight_end_date(self): | |
| result = self.ns["_format_event_datetime"]("2026-09-10T18:30:00Z", "2026-09-10T22:00:00Z", False) | |
| self.assertEqual(result, "Thu 10 Sep · 10:30 PM – Fri 11 Sep · 2:00 AM (UAE, UTC+4)") | |
| def test_all_day_preserves_calendar_date(self): | |
| self.assertEqual(self.ns["_format_event_datetime"]("2026-09-10", "2026-09-11", True), "Thu 10 Sep · All day") | |
| self.assertIn("9:00 AM", self.ns["_format_event_datetime"]("2026-09-10T09:00:00", "", False)) | |
| def test_string_false_flags_are_false(self): | |
| for value in ("0", "false", "False", "no", 0, False, ""): | |
| self.assertFalse(self.ns["_libcal_bool"](value)) | |
| self.assertTrue(self.ns["_libcal_bool"]("1")) | |
| def test_empty_event_result_is_cached(self): | |
| date = datetime.now(self.ns["_UAE_TIMEZONE"]).strftime("%Y-%m-%d") | |
| self.ns.update({"_libcal_events_cache": {f"{date}_30_10": {"data": [], "cached_at": time.time()}}, | |
| "_LIBCAL_EVENTS_TTL": 1800}) | |
| # No client exists in this namespace: returning [] proves this is a cache hit. | |
| self.assertEqual(asyncio.run(self.ns["_libcal_fetch_events"]()), []) | |
| def test_previous_day_hours_are_not_reused_after_midnight(self): | |
| attempts = [] | |
| def unavailable_client(**kwargs): | |
| attempts.append(kwargs) | |
| raise RuntimeError("simulated unavailable LibCal transport") | |
| self.ns.update({"_libcal_hours_cache": {"data": {"old": "schedule"}, "cached_at": time.time(), "date_from": "2000-01-01"}, | |
| "_LIBCAL_HOURS_TTL": 3600, "make_client": unavailable_client}) | |
| # Deliberately unavailable client forces the honest unavailable path on a stale day. | |
| self.assertIsNone(asyncio.run(self.ns["_libcal_fetch_hours"]())) | |
| self.assertEqual(len(attempts), 1) | |
| if __name__ == "__main__": | |
| unittest.main() | |