diff --git a/strata_cli/launch/cmd.py b/strata_cli/launch/cmd.py index 940914a9..3b007a7d 100644 --- a/strata_cli/launch/cmd.py +++ b/strata_cli/launch/cmd.py @@ -605,15 +605,16 @@ def run_connect(a: argparse.Namespace) -> int: from strata_cli.launch import access, harness machine = a.output == "json" if not a.harness: - rows = [{"id": h.id, "title": h.title, "speaks": h.speaks, "status": h.status} for h in harness.HARNESSES] + rows = [{"id": h.id, "title": h.title, "speaks": h.speaks, "status": h.status, "vendor_tested": h.tested or None} for h in harness.HARNESSES] if machine: print(json.dumps({"schema_version": 1, "ok": True, "harnesses": rows})) else: - _say(f"{'App':<20}{'Works with':<22}Status") + _say(f"{'App':<20}{'Works with':<22}Vendor tested") for r in rows: _say(f"{r['id']:<20}{r['speaks']:<22}{r['status']}") _say("") - _say("Show one: strata connect opencode Write it for you: strata connect opencode --write") + _say("Vendor tested is the app version its maker tested the setup with, not a check of your server.") + _say("Check your server: strata connect opencode --check Show one: strata connect opencode Write it: strata connect opencode --write") return 0 h = harness.find(a.harness) if h is None: @@ -631,10 +632,30 @@ def run_connect(a: argparse.Namespace) -> int: out = harness.render(h, tgt) shown_key = key if a.show_key else None if machine: - print(json.dumps({"schema_version": 1, "ok": True, **{k: (harness.mask(v, key) if isinstance(v, str) else v) for k, v in out.items() - if k != "files"}, - "files": [{**f, "text": harness.mask(f["text"], key)} for f in out["files"]]})) - return 0 + payload = {"schema_version": 1, "ok": True, **{k: (harness.mask(v, key) if isinstance(v, str) else v) for k, v in out.items() + if k != "files"}, + "files": [{**f, "text": harness.mask(f["text"], key)} for f in out["files"]], + "vendor_tested": h.tested or None} + if not a.check: + print(json.dumps(payload)) + return 0 + # --check runs the same two checks as the plain-words output (one prompt, one tool call, the way this app sends them) and the answer + # says what they found: "ok" is true only when both worked, and the exit code is 1 when one failed or the server cannot be reached. + from strata_cli.launch import check + results = check.run_checks(h.id if not h.built_in else "opencode", tgt.url, key, tgt.model) + passed = all(r.ok for r in results) + payload["checks"] = [{"name": r.name, "ok": r.ok, "detail": r.detail, "seconds": round(r.seconds, 1)} for r in results] + payload["checked_server"] = tgt.url + payload["ok"] = passed + if not passed: + reached = any(r.ok for r in results) + payload["error"] = {"code": 1, "message": ("The server answered, but the app's prompt or tool call did not work: " + + "; ".join(f"{r.name}: {r.detail}" for r in results if not r.ok)) + if reached else + "The server could not be reached or did not answer the check: " + + "; ".join(f"{r.name}: {r.detail}" for r in results if not r.ok)} + print(json.dumps(payload)) + return 0 if passed else 1 _say(f"{h.title} ({h.speaks}; {h.status})") if not info.get("running") and not a.url: _say("[!] No running Strata server was found, so this uses the default address. Start one with: strata launch") diff --git a/tests/terminal/test_tuiqa_connect_cli.py b/tests/terminal/test_tuiqa_connect_cli.py new file mode 100644 index 00000000..120df27b --- /dev/null +++ b/tests/terminal/test_tuiqa_connect_cli.py @@ -0,0 +1,117 @@ +"""Tester report T07: `strata connect pi --check --output json` skipped the checks and said "ok": true with exit 0 even when nothing was +listening. It now runs the same two checks as the plain-words output, returns each result, and exits 1 when one failed or the server cannot be +reached.""" + +from __future__ import annotations + +import json +import socket +import threading +import unittest +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +from tests.terminal.launch_support import Home, run_cli + + +class Handler(BaseHTTPRequestHandler): + mode = "good" # good | no-text | no-tool + + def log_message(self, *a) -> None: + pass + + def do_POST(self) -> None: + body = json.loads(self.rfile.read(int(self.headers.get("Content-Length") or 0)) or b"{}") + wants_tool = bool(body.get("tools")) + message = {"role": "assistant", "content": "pong"} + if Handler.mode == "no-text" and not wants_tool: + message = {"role": "assistant", "content": None} + if wants_tool: + message = ({"role": "assistant", "content": "I cannot."} if Handler.mode == "no-tool" else + {"role": "assistant", "content": None, "tool_calls": [ + {"id": "1", "type": "function", "function": {"name": "get_weather", "arguments": json.dumps({"city": "Paris"})}}]}) + data = json.dumps({"choices": [{"message": message}]}).encode() + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + +class ConnectCheckJson(unittest.TestCase): + def setUp(self): + self.home = Home().__enter__() + self.addCleanup(self.home.__exit__, None, None, None) + self.server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + self.port = self.server.server_address[1] + threading.Thread(target=self.server.serve_forever, daemon=True).start() + self.addCleanup(self.server.shutdown) + Handler.mode = "good" + + def free_port(self) -> int: + with socket.socket() as s: + s.bind(("127.0.0.1", 0)) + return s.getsockname()[1] + + def run_json(self, port: int, *extra: str): + code, out, err = run_cli(["connect", "pi", "--url", f"http://127.0.0.1:{port}", "--context", "128k", "--check", "--output", "json", *extra]) + return code, json.loads(out), err + + def run_human(self, port: int): + return run_cli(["connect", "pi", "--url", f"http://127.0.0.1:{port}", "--context", "128k", "--check"]) + + def test_a_working_server_passes_both_checks_with_ok_true_and_exit_zero(self): + code, data, _ = self.run_json(self.port) + self.assertEqual(code, 0) + self.assertIs(data["ok"], True) + self.assertEqual([c["name"] for c in data["checks"]], ["prompt", "tool call"]) + self.assertTrue(all(c["ok"] for c in data["checks"])) + self.assertIn("get_weather", data["checks"][1]["detail"]) + self.assertEqual(data["checked_server"], f"http://127.0.0.1:{self.port}") + self.assertEqual(data["vendor_tested"], "pi 0.74.2") + self.assertIn("files", data) # the rendered setup is still there + + def test_a_server_that_answers_wrongly_is_false_with_exit_one_and_says_which_check(self): + for mode, failing in (("no-text", "prompt"), ("no-tool", "tool call")): + Handler.mode = mode + code, data, _ = self.run_json(self.port) + self.assertEqual(code, 1, mode) + self.assertIs(data["ok"], False) + bad = [c["name"] for c in data["checks"] if not c["ok"]] + self.assertEqual(bad, [failing], mode) + self.assertIn(failing, data["error"]["message"]) + self.assertIn("answered", data["error"]["message"]) + + def test_nothing_listening_is_false_with_exit_one_and_says_it_could_not_be_reached(self): + port = self.free_port() + code, data, _ = self.run_json(port) + self.assertEqual(code, 1) + self.assertIs(data["ok"], False) + self.assertEqual([c["ok"] for c in data["checks"]], [False, False]) + self.assertIn("could not be reached", data["error"]["message"]) + + def test_json_and_human_agree_on_the_exit_code(self): + port = self.free_port() + self.assertEqual(self.run_json(port)[0], self.run_human(port)[0]) + Handler.mode = "no-tool" + self.assertEqual(self.run_json(self.port)[0], self.run_human(self.port)[0]) + Handler.mode = "good" + self.assertEqual(self.run_json(self.port)[0], self.run_human(self.port)[0], 0) + + def test_without_check_json_is_unchanged_and_asks_nothing_of_the_server(self): + code, out, _ = run_cli(["connect", "pi", "--url", f"http://127.0.0.1:{self.free_port()}", "--output", "json"]) + data = json.loads(out) + self.assertEqual(code, 0) + self.assertTrue(data["ok"]) + self.assertNotIn("checks", data) + + def test_the_list_says_vendor_tested_and_not_a_check_of_your_server(self): + code, out, _ = run_cli(["connect"]) + self.assertIn("Vendor tested", out) + self.assertIn("not a check of your server", out) + code, out, _ = run_cli(["connect", "--output", "json"]) + rows = json.loads(out)["harnesses"] + self.assertEqual(next(r for r in rows if r["id"] == "pi")["vendor_tested"], "pi 0.74.2") + + +if __name__ == "__main__": + unittest.main()