from unittest import mock
from pathlib import Path
from domin8.tools.import_graph import build_import_graph
from domin8 import resources
def test_gpu_auto_install(monkeypatch, tmp_path):
repo_root = tmp_path
# Create minimal repo
(repo_root / "pkg").mkdir()
(repo_root / "pkg" / "mod.py").write_text("def public():\n return 1\n")
# Write optimization config enabling GPU auto and auto_install_deps
(repo_root / "config").mkdir()
(repo_root / "config" / "optimization.json").write_text('{"use_gpu_auto": true, "auto_install_deps": true, "preferred_gpu_backend": "torch"}')
# Simulate GPU present
monkeypatch.setattr(resources, 'detect_gpus', lambda: resources.GPUInfo(backend='nvidia-smi', device_count=1))
called = {'installed': False}
def fake_install(backend='torch'):
called['installed'] = True
return True
monkeypatch.setattr(resources, 'ensure_ml_deps_installed', fake_install)
# Simulate torch with cuda available
fake_torch = mock.MagicMock()
fake_torch.cuda.is_available.return_value = True
monkeypatch.setitem(__import__('sys').modules, 'torch', fake_torch)
g = build_import_graph(repo_root)
assert hasattr(g, 'gpu_info')
assert g.gpu_info.device_count == 1
assert called['installed']
assert g.gpu_enabled is True