import os import tempfile from pathlib import Path from unittest.mock import MagicMock import pytest from pr.ads import AsyncDataSet from pr.web.app import create_app @pytest.fixture def temp_dir(): with tempfile.TemporaryDirectory() as tmpdir: yield tmpdir @pytest.fixture def mock_api_response(): return { "choices": [{"message": {"role": "assistant", "content": "Test response"}}], "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, } @pytest.fixture def mock_args(): args = MagicMock() args.message = None args.model = None args.api_url = None args.model_list_url = None args.interactive = False args.verbose = False args.no_syntax = False args.include_env = False args.context = None args.api_mode = False return args @pytest.fixture def sample_context_file(temp_dir): context_path = os.path.join(temp_dir, ".rcontext.txt") with open(context_path, "w") as f: f.write("Sample context content\n") return context_path @pytest.fixture async def client(aiohttp_client, monkeypatch): """Create a test client for the app.""" with tempfile.NamedTemporaryFile(delete=False, suffix=".db") as tmp: temp_db_file = tmp.name # Monkeypatch the db monkeypatch.setattr("pr.web.views.base.db", AsyncDataSet(temp_db_file, f"{temp_db_file}.sock")) with tempfile.TemporaryDirectory() as tmpdir: tmp_path = Path(tmpdir) static_dir = tmp_path / "static" templates_dir = tmp_path / "templates" repos_dir = tmp_path / "repos" static_dir.mkdir() templates_dir.mkdir() repos_dir.mkdir() # Monkeypatch the directories monkeypatch.setattr("pr.web.config.STATIC_DIR", static_dir) monkeypatch.setattr("pr.web.config.TEMPLATES_DIR", templates_dir) monkeypatch.setattr("pr.web.config.REPOS_DIR", repos_dir) monkeypatch.setattr("pr.web.config.REPOS_DIR", repos_dir) # Create minimal templates (templates_dir / "index.html").write_text("Index") (templates_dir / "login.html").write_text("Login") (templates_dir / "register.html").write_text("Register") (templates_dir / "dashboard.html").write_text("Dashboard") (templates_dir / "repos.html").write_text("Repos") (templates_dir / "api_keys.html").write_text("API Keys") (templates_dir / "repo.html").write_text("Repo") (templates_dir / "file.html").write_text("File") (templates_dir / "edit_file.html").write_text("Edit File") (templates_dir / "deploy.html").write_text("Deploy") app_instance = await create_app() client = await aiohttp_client(app_instance) yield client # Cleanup db os.unlink(temp_db_file) sock_file = f"{temp_db_file}.sock" if os.path.exists(sock_file): os.unlink(sock_file) @pytest.fixture async def authenticated_client(client): """Create a client with an authenticated user.""" # Register a user resp = await client.post( "/register", data={ "username": "testuser", "email": "test@example.com", "password": "password123", "confirm_password": "password123", }, allow_redirects=False, ) assert resp.status == 302 # Redirect to login # Login resp = await client.post( "/login", data={"username": "testuser", "password": "password123"}, allow_redirects=False ) assert resp.status == 302 # Redirect to dashboard return client