Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import unittest | |
| import json | |
| from pathlib import Path | |
| from unittest.mock import patch, MagicMock | |
| from fastapi.testclient import TestClient | |
| # Add ROOT_DIR to path | |
| ROOT_DIR = Path(__file__).resolve().parent.parent.parent.parent | |
| sys.path.append(str(ROOT_DIR)) | |
| # Import app from main_routes | |
| try: | |
| from api.main_routes import app | |
| from api.utils.core import get_gpu_stats | |
| except ImportError: | |
| # Try different path for direct run | |
| sys.path.append(str(ROOT_DIR / "backend")) | |
| from api.main_routes import app | |
| from api.utils.core import get_gpu_stats | |
| class TestDashboardServer(unittest.TestCase): | |
| def setUp(self): | |
| self.client = TestClient(app) | |
| def test_get_stories(self): | |
| # We check if it returns a 200 and a list of stories | |
| response = self.client.get("/api/stories") | |
| self.assertEqual(response.status_code, 200) | |
| self.assertIn("stories", response.json()) | |
| def test_get_videos(self): | |
| response = self.client.get("/api/videos") | |
| self.assertEqual(response.status_code, 200) | |
| self.assertIn("videos", response.json()) | |
| def test_get_gpu_stats(self, mock_subprocess): | |
| # Sample output from nvidia-smi | |
| mock_subprocess.return_value = b"42, 60, 2048, 8192" | |
| stats = get_gpu_stats() | |
| self.assertIsNotNone(stats) | |
| self.assertEqual(stats["load"], "42%") | |
| self.assertEqual(stats["temp"], "60°C") | |
| self.assertEqual(stats["vram_used"], "2.0 GB") | |
| def test_apply_config_endpoint(self): | |
| # Need to mock the dotenv import inside apply_config | |
| with patch('dotenv.set_key') as mock_set_key: | |
| payload = { | |
| "vfx_profile": "horror", | |
| "audio_mode": "demonic", | |
| "sfx_mode": "intense", | |
| "image_gen_mode": "api", | |
| "voice_gen_mode": "api" | |
| } | |
| # We must pass the payload as a dict (json parameter) | |
| response = self.client.post("/api/config", json=payload) | |
| self.assertEqual(response.status_code, 200) | |
| self.assertEqual(response.json()["status"], "success") | |
| # We expect 5 calls to set_key for the 5 configuration items | |
| self.assertEqual(mock_set_key.call_count, 5) | |
| def test_get_system_stats(self): | |
| # Test if system stats returns required keys | |
| response = self.client.get("/api/system") | |
| self.assertEqual(response.status_code, 200) | |
| data = response.json() | |
| self.assertIn("local_ip", data) | |
| def test_get_story_content_success(self): | |
| # We test the new route with path specifier | |
| # Since we don't want to rely on real files, we'll mock read_story | |
| with patch('api.main_routes.read_story') as mock_read: | |
| mock_read.return_value = {"status": "success", "content": "# Test", "path": "General/Test.md"} | |
| response = self.client.get("/api/stories/content/General/Test.md") | |
| self.assertEqual(response.status_code, 200) | |
| self.assertEqual(response.json()["content"], "# Test") | |
| mock_read.assert_called_with("General/Test.md") | |
| if __name__ == "__main__": | |
| unittest.main() | |