import json import re import threading import urllib.error import urllib.request import pytest from geoql_env import http_server as H from geoql_env import tasks from geoql_env.engine import Result from geoql_env.tasks import Interpretation, Task TASK = Task("t", "Which cafes are here?", "real", "set", (Interpretation("A", "nwr(1); out; // gold", "x"),), family="containment", split="dev") class FakeEngine: def query(self, world, text): ids = {"gold": (1, 2), "half": (1,)}[re.search(r"// (\w+)", text).group(1)] return Result("ok", elements=[{"type": "node", "id": i, "tags": {"amenity": "cafe"}} for i in ids]) @pytest.fixture def base(monkeypatch): monkeypatch.setattr(tasks, "load_all", lambda: [TASK]) server = H.Server(("127.0.0.1", 0), engine=FakeEngine()) threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True).start() yield f"http://127.0.0.1:{server.server_port}" server.shutdown() server.server_close() def call(base, path, body=None, raw=None): data = raw if raw is not None else None if body is None else json.dumps(body).encode() request = urllib.request.Request(base + path, data=data, method="GET" if data is None else "POST") try: with urllib.request.urlopen(request) as response: return response.status, json.load(response) except urllib.error.HTTPError as ex: return ex.code, json.load(ex) def test_episode_over_http(base): assert call(base, "/health") == (200, {"status": "ok"}) assert call(base, "/tasks") == (200, {"tasks": [{"id": "t", "split": "dev", "family": "containment", "world": "real", "answer": "set"}]}) status, reset = call(base, "/reset", {"task": "t"}) assert status == 200 and reset["info"]["task"] == "t" and len(reset["observation"]["tools"]) == 4 episode = reset["episode_id"] status, step = call(base, "/step", {"episode_id": episode, "action": {"tool": "execute", "arguments": {"query": "nwr(1); out; // half"}}}) assert status == 200 and step["observation"].startswith("Tagged objects: 1") assert (step["reward"], step["terminated"], step["truncated"]) == (0.0, False, False) assert call(base, f"/state?episode_id={episode}")[1]["used"] == 1 status, step = call(base, "/step", {"episode_id": episode, "action": {"tool": "submit", "arguments": {"query": "nwr(1); out; // gold"}}}) assert (status, step["reward"], step["terminated"], step["info"]["passed"]) == (200, 1.0, True, True) assert call(base, "/step", {"episode_id": episode, "action": {"tool": "submit", "arguments": {}}}) == ( 409, {"error": "the episode is over"}) status, single = call(base, "/reset", {"split": "dev", "seed": 1, "mode": "single_shot"}) assert status == 200 and single["observation"]["tools"] == [] and single["episode_id"] != episode @pytest.mark.parametrize("path,body,raw,status,error", [ ("/reset", {"task": "nope"}, None, 404, "unknown task 'nope'"), ("/reset", {"task": "t", "mode": "chat"}, None, 400, "mode must be one of agentic, single_shot"), ("/reset", {"split": "test"}, None, 400, "no tasks in split 'test'"), ("/reset", None, b"[1]", 400, "the request body must be a JSON object"), ("/reset", None, b"{", 400, None), ("/step", {"episode_id": "nope", "action": {}}, None, 404, "unknown episode 'nope'"), ("/state?episode_id=nope", None, None, 404, "unknown episode 'nope'"), ("/submit", {}, None, 404, "no route POST /submit"), ]) def test_errors(base, path, body, raw, status, error): got_status, got = call(base, path, body, raw) assert got_status == status and (error is None or got == {"error": error}) def test_invalid_action(base): episode = call(base, "/reset", {"task": "t"})[1]["episode_id"] assert call(base, "/step", {"episode_id": episode, "action": "execute"}) == ( 400, {"error": 'an action is {"tool": name, "arguments": {...}}'})