#!/usr/bin/env python3 from __future__ import annotations import json import shlex import shutil import subprocess import unittest from pathlib import Path SCRIPT_DIR = Path(__file__).resolve().parent SKILL_ROOT = SCRIPT_DIR.parent SHELL_ENTRYPOINT = SCRIPT_DIR / "clinic-shell.sh" class ClinicShellTiCDCTest(unittest.TestCase): maxDiff = None def invoke( self, command: str, *, body: dict | str = "{}", http_status: str = "200", curl_exit: int = 0, shell: str = "bash", ) -> subprocess.CompletedProcess: response_body = json.dumps(body) if isinstance(body, dict) else body script = f""" source {shlex.quote(str(SHELL_ENTRYPOINT))} CLINIC_ENDPOINT=https://clinic.test CLINIC_API_KEY=test-key _ensure_clinic_auth() {{ return 0; }} FAKE_RESPONSE_BODY={shlex.quote(response_body)} FAKE_HTTP_STATUS={shlex.quote(http_status)} FAKE_CURL_EXIT={curl_exit} REQUEST_FILE=$(mktemp) curl() {{ local output_file="" request_url="" while [ "$#" -gt 0 ]; do case "$1" in --fail-with-body) echo "unsupported curl option" >&2 return 2 ;; -o) output_file="$2" shift 2 ;; -w|-H) shift 2 ;; -sS) shift ;; *) request_url="$1" shift ;; esac done printf '%s' "$FAKE_RESPONSE_BODY" > "$output_file" printf '%s' "$request_url" > "$REQUEST_FILE" printf '%s' "$FAKE_HTTP_STATUS" return "$FAKE_CURL_EXIT" }} {command} command_status=$? printf '\n__REQUEST_URL__=%s\n' "$(command cat "$REQUEST_FILE")" command rm -f -- "$REQUEST_FILE" exit "$command_status" """ return subprocess.run( [shell, "-c", script], cwd=SKILL_ROOT, check=False, capture_output=True, text=True, ) @staticmethod def split_output(completed: subprocess.CompletedProcess) -> tuple[str, str]: body, marker, url = completed.stdout.partition("\n__REQUEST_URL__=") if not marker: raise AssertionError(completed.stdout + completed.stderr) return body, url.strip() def test_list_works_without_fail_with_body_support(self): completed = self.invoke( "clinic_ticdc_changefeeds_list 100 200", body={"changefeeds": []}, ) self.assertEqual(completed.returncode, 0, completed.stderr) body, url = self.split_output(completed) self.assertEqual(json.loads(body), {"changefeeds": []}) self.assertEqual( url, "https://clinic.test/data-proxy/api/v1/orgs/100/clusters/200/" "ticdc/changefeeds", ) def test_detail_accepts_and_encodes_opaque_changefeed_id(self): completed = self.invoke( "clinic_ticdc_changefeed_get 100 200 'cfd-one/two three'", body={"changefeed": {"id": "cfd-one/two three"}}, ) self.assertEqual(completed.returncode, 0, completed.stderr) body, url = self.split_output(completed) self.assertEqual( json.loads(body), {"changefeed": {"id": "cfd-one/two three"}}, ) self.assertEqual( url, "https://clinic.test/data-proxy/api/v1/orgs/100/clusters/200/" "ticdc/changefeeds/cfd-one%2Ftwo%20three", ) def test_http_error_preserves_body_and_returns_curl_status_22(self): completed = self.invoke( "clinic_ticdc_changefeeds_list 100 200", body={"code": "Unavailable", "message": "try again"}, http_status="503", ) self.assertEqual(completed.returncode, 22) body, _ = self.split_output(completed) self.assertEqual( json.loads(body), {"code": "Unavailable", "message": "try again"}, ) def test_empty_changefeed_id_fails_before_request(self): completed = self.invoke("clinic_ticdc_changefeed_get 100 200 ''") self.assertEqual(completed.returncode, 2) self.assertIn("must not be empty", completed.stderr) _, url = self.split_output(completed) self.assertEqual(url, "") def test_transport_error_is_preserved(self): completed = self.invoke( "clinic_ticdc_changefeeds_list 100 200", http_status="000", curl_exit=7, ) self.assertEqual(completed.returncode, 7) def test_public_helper_dependencies_are_exported(self): completed = self.invoke( "bash -c 'type _clinic_ticdc_positive_id >/dev/null && " "type _clinic_ticdc_get >/dev/null && " "type clinic_ticdc_changefeed_get >/dev/null'" ) self.assertEqual(completed.returncode, 0, completed.stderr) @unittest.skipUnless(shutil.which("zsh"), "zsh is not installed") def test_opaque_changefeed_id_works_in_zsh(self): completed = self.invoke( "clinic_ticdc_changefeed_get 100 200 cfd-example", body={"changefeed": {"id": "cfd-example"}}, shell="zsh", ) self.assertEqual(completed.returncode, 0, completed.stderr) _, url = self.split_output(completed) self.assertTrue(url.endswith("/ticdc/changefeeds/cfd-example")) if __name__ == "__main__": unittest.main()