|
| 1 | +"""Phase 7.0 — capability-only exposures end-to-end. |
| 2 | +
|
| 3 | +A device registers via /ws/expose without being paired to any AI. |
| 4 | +Any registered publisher's bearer key can invoke its capabilities. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import socket |
| 9 | +import threading |
| 10 | +import time |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +try: |
| 15 | + import fastapi # noqa |
| 16 | + import uvicorn # noqa |
| 17 | + import httpx # noqa |
| 18 | + DEPS_AVAILABLE = True |
| 19 | +except ImportError: |
| 20 | + DEPS_AVAILABLE = False |
| 21 | + |
| 22 | +if DEPS_AVAILABLE: |
| 23 | + from zhub.server import create_app |
| 24 | +from zhub import publish, expose |
| 25 | + |
| 26 | + |
| 27 | +def _free_port() -> int: |
| 28 | + with socket.socket() as s: |
| 29 | + s.bind(("", 0)) |
| 30 | + return s.getsockname()[1] |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def hub(tmp_path): |
| 35 | + if not DEPS_AVAILABLE: |
| 36 | + pytest.skip("fastapi/uvicorn/httpx not installed") |
| 37 | + port = _free_port() |
| 38 | + db_path = str(tmp_path / "exp.db") |
| 39 | + app = create_app(db_path=db_path) |
| 40 | + |
| 41 | + def run(): |
| 42 | + config = uvicorn.Config(app, host="127.0.0.1", port=port, |
| 43 | + log_level="warning") |
| 44 | + asyncio.run(uvicorn.Server(config).serve()) |
| 45 | + |
| 46 | + threading.Thread(target=run, daemon=True).start() |
| 47 | + for _ in range(30): |
| 48 | + try: |
| 49 | + with socket.create_connection(("127.0.0.1", port), timeout=0.1): |
| 50 | + break |
| 51 | + except OSError: |
| 52 | + time.sleep(0.1) |
| 53 | + yield port |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_register_exposure_returns_id_and_key(hub): |
| 58 | + """expose() registers the device and the hub returns an exposure_id |
| 59 | + + dx_ device key.""" |
| 60 | + exp = expose( |
| 61 | + name="weather-sensor", |
| 62 | + capabilities={ |
| 63 | + "weather_lookup": ( |
| 64 | + {"type": "object", |
| 65 | + "properties": {"city": {"type": "string"}}, |
| 66 | + "required": ["city"]}, |
| 67 | + lambda args: {"temp": 22, "city": args["city"]}, |
| 68 | + ), |
| 69 | + }, |
| 70 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 71 | + public=True, |
| 72 | + ) |
| 73 | + for _ in range(50): |
| 74 | + if exp.exposure_id and exp.device_key: |
| 75 | + break |
| 76 | + await asyncio.sleep(0.1) |
| 77 | + assert exp.exposure_id.startswith("ex_"), f"got {exp.exposure_id!r}" |
| 78 | + assert exp.device_key.startswith("dx_"), f"got {exp.device_key!r}" |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_get_exposures_lists_public_only(hub): |
| 83 | + e_pub = expose( |
| 84 | + name="weather-public", |
| 85 | + capabilities={"weather_lookup": ({"type": "object"}, lambda a: {})}, |
| 86 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 87 | + public=True, |
| 88 | + ) |
| 89 | + e_priv = expose( |
| 90 | + name="secret-camera", |
| 91 | + capabilities={"take_photo": ({"type": "object"}, lambda a: {})}, |
| 92 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 93 | + public=False, |
| 94 | + ) |
| 95 | + for _ in range(50): |
| 96 | + if e_pub.exposure_id and e_priv.exposure_id: |
| 97 | + break |
| 98 | + await asyncio.sleep(0.1) |
| 99 | + |
| 100 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 101 | + listing = (await c.get(f"http://127.0.0.1:{hub}/exposures")).json() |
| 102 | + |
| 103 | + names = {e["name"] for e in listing} |
| 104 | + assert "weather-public" in names |
| 105 | + assert "secret-camera" not in names |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_invoke_exposure_via_publisher_key(hub): |
| 110 | + """A registered publisher's zk_ key authorizes invoking any exposure.""" |
| 111 | + captured: dict = {} |
| 112 | + e = expose( |
| 113 | + name="weather", |
| 114 | + capabilities={ |
| 115 | + "weather_lookup": ( |
| 116 | + {"type": "object", "required": ["city"], |
| 117 | + "properties": {"city": {"type": "string"}}}, |
| 118 | + lambda a: {"city": a["city"], "temp": 18, |
| 119 | + "_captured": captured.update(a) or True}, |
| 120 | + ), |
| 121 | + }, |
| 122 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 123 | + public=True, |
| 124 | + ) |
| 125 | + for _ in range(50): |
| 126 | + if e.exposure_id: |
| 127 | + break |
| 128 | + await asyncio.sleep(0.1) |
| 129 | + |
| 130 | + pub = publish( |
| 131 | + name="some-ai", |
| 132 | + description="any AI", |
| 133 | + chat_handler=lambda m, o: "ok", |
| 134 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 135 | + ) |
| 136 | + for _ in range(50): |
| 137 | + if pub.api_key: |
| 138 | + break |
| 139 | + await asyncio.sleep(0.1) |
| 140 | + |
| 141 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 142 | + resp = await c.post( |
| 143 | + f"http://127.0.0.1:{hub}/exposures/{e.exposure_id}/invoke", |
| 144 | + json={"capability": "weather_lookup", "args": {"city": "Mississauga"}}, |
| 145 | + headers={"Authorization": f"Bearer {pub.api_key}"}, |
| 146 | + ) |
| 147 | + assert resp.status_code == 200, resp.text |
| 148 | + body = resp.json() |
| 149 | + assert body["ok"] is True |
| 150 | + assert body["result"]["city"] == "Mississauga" |
| 151 | + assert body["result"]["temp"] == 18 |
| 152 | + assert captured == {"city": "Mississauga"} |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.asyncio |
| 156 | +async def test_invoke_rejects_without_publisher_key(hub): |
| 157 | + e = expose( |
| 158 | + name="x", |
| 159 | + capabilities={"do_x": ({"type": "object"}, lambda a: {"ok": True})}, |
| 160 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 161 | + public=True, |
| 162 | + ) |
| 163 | + for _ in range(50): |
| 164 | + if e.exposure_id: |
| 165 | + break |
| 166 | + await asyncio.sleep(0.1) |
| 167 | + |
| 168 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 169 | + # No bearer |
| 170 | + r = await c.post( |
| 171 | + f"http://127.0.0.1:{hub}/exposures/{e.exposure_id}/invoke", |
| 172 | + json={"capability": "do_x", "args": {}}, |
| 173 | + ) |
| 174 | + assert r.status_code == 401 |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.asyncio |
| 178 | +async def test_invoke_404_for_unknown_exposure(hub): |
| 179 | + pub = publish( |
| 180 | + name="auth-source", |
| 181 | + description="x", |
| 182 | + chat_handler=lambda m, o: "ok", |
| 183 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 184 | + ) |
| 185 | + for _ in range(50): |
| 186 | + if pub.api_key: |
| 187 | + break |
| 188 | + await asyncio.sleep(0.1) |
| 189 | + |
| 190 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 191 | + r = await c.post( |
| 192 | + f"http://127.0.0.1:{hub}/exposures/ex_nope/invoke", |
| 193 | + json={"capability": "x", "args": {}}, |
| 194 | + headers={"Authorization": f"Bearer {pub.api_key}"}, |
| 195 | + ) |
| 196 | + assert r.status_code == 404 |
| 197 | + |
| 198 | + |
| 199 | +@pytest.mark.asyncio |
| 200 | +async def test_invoke_validates_args_against_schema(hub): |
| 201 | + e = expose( |
| 202 | + name="strict", |
| 203 | + capabilities={ |
| 204 | + "strict_op": ( |
| 205 | + {"type": "object", "required": ["needed"], |
| 206 | + "properties": {"needed": {"type": "string"}}}, |
| 207 | + lambda a: {"got": a}, |
| 208 | + ), |
| 209 | + }, |
| 210 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 211 | + public=True, |
| 212 | + ) |
| 213 | + for _ in range(50): |
| 214 | + if e.exposure_id: |
| 215 | + break |
| 216 | + await asyncio.sleep(0.1) |
| 217 | + |
| 218 | + pub = publish( |
| 219 | + name="caller", |
| 220 | + description="x", |
| 221 | + chat_handler=lambda m, o: "ok", |
| 222 | + hub_url=f"ws://127.0.0.1:{hub}", |
| 223 | + ) |
| 224 | + for _ in range(50): |
| 225 | + if pub.api_key: |
| 226 | + break |
| 227 | + await asyncio.sleep(0.1) |
| 228 | + |
| 229 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 230 | + r = await c.post( |
| 231 | + f"http://127.0.0.1:{hub}/exposures/{e.exposure_id}/invoke", |
| 232 | + json={"capability": "strict_op", "args": {}}, |
| 233 | + headers={"Authorization": f"Bearer {pub.api_key}"}, |
| 234 | + ) |
| 235 | + assert r.status_code == 400 |
| 236 | + assert "needed" in r.text.lower() |
0 commit comments