from __future__ import annotations from types import SimpleNamespace from unittest.mock import AsyncMock, MagicMock import pytest from fastapi import HTTPException from app.core.config import Settings from app.features.admin import api as admin_api def test_enable_module_repo_sync_defaults_to_false(monkeypatch): monkeypatch.delenv("ENABLE_MODULE_REPO_SYNC", raising=False) s = Settings(_env_file=None) assert s.enable_module_repo_sync is False @pytest.mark.asyncio async def test_get_builder_config_reflects_setting(monkeypatch): monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=False)) res_off = await admin_api.get_builder_config(admin=MagicMock()) assert res_off == {"sync_repo_enabled": False} monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=True)) res_on = await admin_api.get_builder_config(admin=MagicMock()) assert res_on == {"sync_repo_enabled": True} @pytest.mark.asyncio async def test_sync_builder_module_to_repo_blocked_when_disabled(monkeypatch): monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=False)) service = SimpleNamespace(builder=SimpleNamespace(sync_content_repo=AsyncMock())) with pytest.raises(HTTPException) as exc_info: await admin_api.sync_builder_module_to_repo("m0042", admin=MagicMock(), service=service) assert exc_info.value.status_code == 403 service.builder.sync_content_repo.assert_not_awaited() @pytest.mark.asyncio async def test_sync_builder_module_to_repo_allowed_when_enabled(monkeypatch): monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=True)) service = SimpleNamespace( builder=SimpleNamespace(sync_content_repo=AsyncMock(return_value={"status": "synced"})) ) res = await admin_api.sync_builder_module_to_repo("m0042", admin=MagicMock(), service=service) assert res == {"status": "synced"} service.builder.sync_content_repo.assert_awaited_once_with("m0042")