Files
MyAiConfiguration/scripts/tests/test-semantic-retrieval.py
T
fraujulian 49be8edbec
CI / Capabilities and shell quality (push) Successful in 11s
CI / Python tests and compilation (push) Successful in 11s
CI / PowerShell tests on Ubuntu (push) Successful in 28s
CI / CI configuration security checks (push) Successful in 3s
CI / Build and inspect generated packages (push) Successful in 6s
CI / Qwen retrieval integration (push) Successful in 14m20s
CI / Bash integration and installation guards (push) Successful in 16s
CI / Installed client configuration smoke tests (push) Successful in 13s
feat(retrieval): preload Qwen models during setup
2026-09-18 15:47:42 +02:00

91 lines
4.1 KiB
Python

import hashlib
import importlib.util
import io
import json
from contextlib import redirect_stdout
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parent.parent.parent
SPEC = importlib.util.spec_from_file_location('semantic_retrieval', ROOT / 'scripts/lib/semantic-retrieval.py')
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
class SemanticRetrievalTests(unittest.TestCase):
def test_sync_does_not_register_mcp(self):
with tempfile.TemporaryDirectory() as directory:
home = Path(directory) / 'home'
output = io.StringIO()
with redirect_stdout(output):
MODULE.sync(ROOT, home, ['codex', 'claude'], True, True, False)
self.assertNotIn('mcp', output.getvalue().lower())
def test_runtime_prefers_cuda_packages_for_nvidia_gpu(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory)
python = MODULE.python_path(target / '.venv')
python.parent.mkdir(parents=True)
python.touch()
with patch.object(MODULE.shutil, 'which', return_value='nvidia-smi'), patch.object(MODULE.subprocess, 'run') as run:
MODULE.ensure_runtime(target, False)
self.assertEqual(run.call_args.args[0][-2:], ['--extra-index-url', MODULE.CUDA_INDEX])
def test_runtime_falls_back_to_cpu_packages_when_cuda_install_fails(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory)
python = MODULE.python_path(target / '.venv')
python.parent.mkdir(parents=True)
python.touch()
failure = MODULE.subprocess.CalledProcessError(1, 'pip')
with patch.object(MODULE.shutil, 'which', return_value='nvidia-smi'), patch.object(MODULE.subprocess, 'run', side_effect=[failure, None]) as run:
MODULE.ensure_runtime(target, False)
self.assertEqual(run.call_count, 2)
self.assertNotIn('--extra-index-url', run.call_args.args[0])
def test_sync_preloads_models_after_installing_runtime(self):
with tempfile.TemporaryDirectory() as directory:
home = Path(directory) / 'home'
target = home / '.my-ai-configuration/semantic-retrieval'
python = MODULE.python_path(target / '.venv')
with patch.object(MODULE, 'ensure_runtime', return_value=python), \
patch.object(MODULE.subprocess, 'run') as run:
MODULE.sync(ROOT, home, ['codex'], True, False, False)
self.assertEqual([[
str(python), str(target / 'server.py'), 'preload', '--model-cache', str(target / 'model-cache')
]], [call.args[0] for call in run.call_args_list])
def test_summary_dry_run_omits_details_without_writes(self):
with tempfile.TemporaryDirectory() as directory:
home = Path(directory) / 'home'
for enabled in (True, False):
output = io.StringIO()
with redirect_stdout(output):
MODULE.sync(ROOT, home, ['codex'], enabled, True, False, summary=True)
self.assertEqual(output.getvalue(), '')
self.assertFalse(home.exists())
def test_disable_removes_owned_runtime_without_cli_registration(self):
with tempfile.TemporaryDirectory() as directory:
home = Path(directory) / 'home'
target = home / '.my-ai-configuration/semantic-retrieval'
target.mkdir(parents=True)
for name in MODULE.FILES:
(target / name).write_text(name)
state_path = target.parent / 'semantic-retrieval.json'
state_path.write_text(json.dumps({'version': 1, 'clients': ['codex'], 'files': {
name: hashlib.sha256((target / name).read_bytes()).hexdigest() for name in MODULE.FILES}}))
MODULE.sync(ROOT, home, ['codex'], False, False, False)
self.assertFalse(target.exists())
self.assertFalse(state_path.exists())
if __name__ == '__main__':
unittest.main()