"""The shared JSON service adapter must preserve admission and error handling across extension routes with different scopes and body limits.""" import json import unittest import urllib.error from urllib.parse import urlencode from harness import Albedo, Provider, text class ExtensionServiceTests(unittest.TestCase): def test_json_services_keep_authentication_limits_and_route_validation(self): provider = Provider(lambda _: text("unused")) self.addCleanup(provider.close) with Albedo(provider) as app: workspace = urlencode({"workspace": str(app.workspace)}) session = app.session() routes = [ (f"/extensions/work/items?{workspace}", 65_536, "items"), ("/extensions/paperclips/items", 65_536, "items"), (f"/extensions/links/groups?{workspace}", 65_536, "page"), (f"/extensions/schedule/jobs?session_id={session}", 65_536, "items"), (f"/extensions/run/sessions/{session}/jobs", 1024, "page"), ] for route, body_limit, field in routes: with self.subTest(route=route): with app.api( route, headers={"Accept": "application/json"} ) as response: self.assertIn(field, json.load(response)) for headers, status in [ ({"Authorization": "Bearer invalid"}, 401), ({"Accept": "text/event-stream"}, 406), ({"Accept": "text/event-stream, application/json;q=0.5"}, 406), ]: with self.subTest(headers=headers): with self.assertRaises(urllib.error.HTTPError) as caught: app.api(route, headers=headers) self.assertEqual(caught.exception.code, status) separator = "&" if "?" in route else "?" with self.assertRaises(urllib.error.HTTPError) as caught: app.api(route + separator + "unknown=1") self.assertEqual(caught.exception.code, 400) self.assertEqual( json.load(caught.exception)["code"], "invalid_request" ) with self.assertRaises(urllib.error.HTTPError) as caught: app.api(route, {"title": "x" * body_limit}, method="POST") self.assertEqual(caught.exception.code, 413) def test_skills_agents_and_search_negotiate_json(self): provider = Provider(lambda _: text("unused")) self.addCleanup(provider.close) with Albedo(provider) as app: session = app.session() routes = [ ( f"/extensions/skills/loads?session_id={session}", None, "items", 65_536, ), ("/extensions/web-search/providers", None, "items", 4096), ( f"/extensions/agents/sessions/{session}/messages", {"seq": 0, "through": None, "offset": 0, "limit": 100}, "content", 65_536, ), ] for route, body, field, body_limit in routes: with self.subTest(route=route): with app.api( route, body, headers={"Accept": "application/json"} ) as response: self.assertIn(field, json.load(response)) for accept in [ "text/event-stream", "text/event-stream, application/json;q=0.5", ]: with self.subTest(accept=accept): with self.assertRaises(urllib.error.HTTPError) as caught: app.api(route, body, headers={"Accept": accept}) self.assertEqual(caught.exception.code, 406) self.assertEqual( json.load(caught.exception)["code"], "not_acceptable" ) with self.assertRaises(urllib.error.HTTPError) as caught: app.api( route, body, headers={"Authorization": "Bearer invalid"} ) self.assertEqual(caught.exception.code, 401) with self.assertRaises(urllib.error.HTTPError) as caught: app.api(route, {"oversized": "x" * body_limit}, method="POST") self.assertEqual(caught.exception.code, 413)