|
| 1 | +"""CLI smoke tests for the CodeAlive skill scripts.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from helpers import mock_codealive_server |
| 12 | + |
| 13 | + |
| 14 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 15 | +SKILL_ROOT = REPO_ROOT / "skills" / "codealive-context-engine" |
| 16 | + |
| 17 | + |
| 18 | +def _run(script_name: str, *args: str, env: dict[str, str]) -> subprocess.CompletedProcess[str]: |
| 19 | + script = SKILL_ROOT / "scripts" / script_name |
| 20 | + return subprocess.run( |
| 21 | + [sys.executable, str(script), *args], |
| 22 | + text=True, |
| 23 | + capture_output=True, |
| 24 | + env=env, |
| 25 | + check=False, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def test_datasources_search_fetch_and_chat_scripts_work_against_mock_backend(): |
| 30 | + def search_handler(_request): |
| 31 | + return 200, { |
| 32 | + "results": [ |
| 33 | + { |
| 34 | + "identifier": "org/repo::src/auth.py::AuthService", |
| 35 | + "kind": "Class", |
| 36 | + "description": "Handles auth", |
| 37 | + "location": {"path": "src/auth.py", "range": {"start": {"line": 10}, "end": {"line": 20}}}, |
| 38 | + "contentByteSize": 2048, |
| 39 | + } |
| 40 | + ] |
| 41 | + }, {} |
| 42 | + |
| 43 | + def fetch_handler(_request): |
| 44 | + return 200, { |
| 45 | + "artifacts": [ |
| 46 | + { |
| 47 | + "identifier": "org/repo::src/auth.py::AuthService", |
| 48 | + "content": "class AuthService:\n pass\n", |
| 49 | + "startLine": 10, |
| 50 | + "contentByteSize": 28, |
| 51 | + } |
| 52 | + ] |
| 53 | + }, {} |
| 54 | + |
| 55 | + def chat_handler(_request): |
| 56 | + return 200, { |
| 57 | + "id": "conv_123", |
| 58 | + "choices": [{"message": {"content": "Auth is handled in AuthService."}}], |
| 59 | + }, {} |
| 60 | + |
| 61 | + with mock_codealive_server( |
| 62 | + { |
| 63 | + ("GET", "/api/datasources/ready"): ( |
| 64 | + 200, |
| 65 | + [{"id": "repo-1", "name": "backend", "type": "Repository", "description": "Main backend"}], |
| 66 | + ), |
| 67 | + ("GET", "/api/search?Query=auth&Mode=auto&IncludeContent=false&DescriptionDetail=Short&Names=backend"): search_handler, |
| 68 | + ("POST", "/api/search/artifacts"): fetch_handler, |
| 69 | + ("POST", "/api/chat/completions"): chat_handler, |
| 70 | + } |
| 71 | + ) as (base_url, requests): |
| 72 | + env = { |
| 73 | + **os.environ, |
| 74 | + "CODEALIVE_API_KEY": "skill-test-key", |
| 75 | + "CODEALIVE_BASE_URL": f"{base_url}/api", |
| 76 | + } |
| 77 | + |
| 78 | + datasources = _run("datasources.py", "--json", env=env) |
| 79 | + search = _run("search.py", "auth", "backend", env=env) |
| 80 | + fetch = _run("fetch.py", "org/repo::src/auth.py::AuthService", env=env) |
| 81 | + chat = _run("chat.py", "How does auth work?", "backend", env=env) |
| 82 | + |
| 83 | + assert datasources.returncode == 0, datasources.stderr |
| 84 | + assert json.loads(datasources.stdout)[0]["name"] == "backend" |
| 85 | + |
| 86 | + assert search.returncode == 0, search.stderr |
| 87 | + assert "src/auth.py:10-20" in search.stdout |
| 88 | + assert "Handles auth" in search.stdout |
| 89 | + |
| 90 | + assert fetch.returncode == 0, fetch.stderr |
| 91 | + assert "AuthService" in fetch.stdout |
| 92 | + assert "10 | class AuthService:" in fetch.stdout |
| 93 | + |
| 94 | + assert chat.returncode == 0, chat.stderr |
| 95 | + assert "Auth is handled in AuthService." in chat.stdout |
| 96 | + assert "Conversation ID: conv_123" in chat.stdout |
| 97 | + |
| 98 | + assert [request["path"] for request in requests] == [ |
| 99 | + "/api/datasources/ready", |
| 100 | + "/api/search?Query=auth&Mode=auto&IncludeContent=false&DescriptionDetail=Short&Names=backend", |
| 101 | + "/api/search/artifacts", |
| 102 | + "/api/chat/completions", |
| 103 | + ] |
| 104 | + |
| 105 | + |
| 106 | +def test_check_auth_hook_normalizes_base_url_and_uses_repo_root_fallback(): |
| 107 | + script = REPO_ROOT / "hooks" / "scripts" / "check_auth.sh" |
| 108 | + env = { |
| 109 | + "PATH": "/usr/bin:/bin", |
| 110 | + "USER": "codealive-skills-test", |
| 111 | + "CODEALIVE_BASE_URL": "https://codealive.example.com/api", |
| 112 | + } |
| 113 | + |
| 114 | + result = subprocess.run( |
| 115 | + ["/bin/bash", str(script)], |
| 116 | + text=True, |
| 117 | + capture_output=True, |
| 118 | + env=env, |
| 119 | + check=False, |
| 120 | + ) |
| 121 | + |
| 122 | + assert result.returncode == 0 |
| 123 | + assert "https://codealive.example.com/settings/api-keys" in result.stdout |
| 124 | + assert str(REPO_ROOT / "skills" / "codealive-context-engine" / "setup.py") in result.stdout |
0 commit comments