|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi.testclient import TestClient |
| 6 | + |
| 7 | +from eval_protocol.utils.vite_server import ViteServer |
| 8 | + |
| 9 | + |
| 10 | +class TestViteServer: |
| 11 | + """Test ViteServer class.""" |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def temp_build_dir_with_files(self): |
| 15 | + """Create a temporary build directory with index.html and assets/ directory.""" |
| 16 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 17 | + temp_path = Path(temp_dir) |
| 18 | + |
| 19 | + # Create index.html |
| 20 | + (temp_path / "index.html").write_text("<html><body>Test</body></html>") |
| 21 | + |
| 22 | + # Create assets directory and some files inside it |
| 23 | + assets_dir = temp_path / "assets" |
| 24 | + assets_dir.mkdir() |
| 25 | + (assets_dir / "app.js").write_text("console.log('test');") |
| 26 | + (assets_dir / "style.css").write_text("body { color: black; }") |
| 27 | + |
| 28 | + # Optionally, create a nested directory inside assets |
| 29 | + (assets_dir / "nested").mkdir() |
| 30 | + (assets_dir / "nested" / "file.txt").write_text("nested content") |
| 31 | + |
| 32 | + yield temp_path |
| 33 | + |
| 34 | + def test_initialization(self, temp_build_dir_with_files): |
| 35 | + """Test ViteServer initialization.""" |
| 36 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files), host="localhost", port=8000) |
| 37 | + |
| 38 | + assert vite_server.build_dir == temp_build_dir_with_files |
| 39 | + assert vite_server.host == "localhost" |
| 40 | + assert vite_server.port == 8000 |
| 41 | + assert vite_server.index_file == "index.html" |
| 42 | + assert vite_server.app is not None |
| 43 | + |
| 44 | + def test_initialization_invalid_build_dir(self): |
| 45 | + """Test ViteServer initialization with invalid build directory.""" |
| 46 | + with pytest.raises(FileNotFoundError): |
| 47 | + ViteServer(build_dir="nonexistent_dir") |
| 48 | + |
| 49 | + def test_initialization_invalid_index_file(self, temp_build_dir_with_files): |
| 50 | + """Test ViteServer initialization with invalid index file.""" |
| 51 | + # Remove the index.html file |
| 52 | + (temp_build_dir_with_files / "index.html").unlink() |
| 53 | + |
| 54 | + with pytest.raises(FileNotFoundError): |
| 55 | + ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 56 | + |
| 57 | + def test_html_injection_in_vite_server(self, temp_build_dir_with_files): |
| 58 | + """Test that ViteServer injects server configuration into HTML.""" |
| 59 | + # Create a more complex HTML file for testing injection |
| 60 | + index_html = """<!DOCTYPE html> |
| 61 | +<html lang="en"> |
| 62 | +<head> |
| 63 | + <meta charset="UTF-8"> |
| 64 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 65 | + <title>Test App</title> |
| 66 | + <link rel="stylesheet" href="/assets/style.css"> |
| 67 | +</head> |
| 68 | +<body> |
| 69 | + <div id="app">Test Application</div> |
| 70 | + <script src="/assets/app.js"></script> |
| 71 | +</body> |
| 72 | +</html>""" |
| 73 | + |
| 74 | + # Write the test HTML |
| 75 | + (temp_build_dir_with_files / "index.html").write_text(index_html) |
| 76 | + |
| 77 | + # Create ViteServer instance |
| 78 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files), host="localhost", port=8000) |
| 79 | + |
| 80 | + # Test the HTML injection method directly |
| 81 | + injected_html = vite_server._inject_config_into_html(index_html) |
| 82 | + |
| 83 | + # Verify server configuration is injected |
| 84 | + assert "window.SERVER_CONFIG" in injected_html |
| 85 | + assert 'host: "localhost"' in injected_html |
| 86 | + assert 'port: "8000"' in injected_html |
| 87 | + assert 'protocol: "ws"' in injected_html |
| 88 | + assert 'apiProtocol: "http"' in injected_html |
| 89 | + |
| 90 | + # Verify injection happens before closing </head> tag |
| 91 | + head_end_index = injected_html.find("</head>") |
| 92 | + config_script_index = injected_html.find("window.SERVER_CONFIG") |
| 93 | + assert config_script_index < head_end_index |
| 94 | + |
| 95 | + # Verify the original HTML structure is preserved |
| 96 | + assert '<div id="app">Test Application</div>' in injected_html |
| 97 | + assert '<script src="/assets/app.js"></script>' in injected_html |
| 98 | + |
| 99 | + # Test that the injected config is valid JavaScript |
| 100 | + assert "window.SERVER_CONFIG = {" in injected_html |
| 101 | + assert "};" in injected_html |
| 102 | + |
| 103 | + def test_html_injection_without_head_tag(self, temp_build_dir_with_files): |
| 104 | + """Test HTML injection when no </head> tag is present.""" |
| 105 | + # Create HTML without </head> tag |
| 106 | + simple_html = """<!DOCTYPE html> |
| 107 | +<html> |
| 108 | +<body> |
| 109 | + <h1>Simple App</h1> |
| 110 | + <p>No head tag</p> |
| 111 | +</body> |
| 112 | +</html>""" |
| 113 | + |
| 114 | + (temp_build_dir_with_files / "index.html").write_text(simple_html) |
| 115 | + |
| 116 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files), host="127.0.0.1", port=9000) |
| 117 | + |
| 118 | + injected_html = vite_server._inject_config_into_html(simple_html) |
| 119 | + |
| 120 | + # Verify config is injected at the beginnin |
| 121 | + assert injected_html.strip().startswith("<script>") |
| 122 | + assert "window.SERVER_CONFIG" in injected_html |
| 123 | + assert 'host: "127.0.0.1"' in injected_html |
| 124 | + assert 'port: "9000"' in injected_html |
| 125 | + |
| 126 | + # Verify original content is preserved |
| 127 | + assert "<h1>Simple App</h1>" in injected_html |
| 128 | + assert "<p>No head tag</p>" in injected_html |
| 129 | + |
| 130 | + def test_vite_server_endpoints_with_injection(self, temp_build_dir_with_files): |
| 131 | + """Test that ViteServer endpoints serve HTML with injected configuration.""" |
| 132 | + # Create test HTML |
| 133 | + test_html = """<!DOCTYPE html> |
| 134 | +<html> |
| 135 | +<head> |
| 136 | + <title>Test</title> |
| 137 | +</head> |
| 138 | +<body> |
| 139 | + <h1>Hello World</h1> |
| 140 | +</body> |
| 141 | +</html>""" |
| 142 | + |
| 143 | + (temp_build_dir_with_files / "index.html").write_text(test_html) |
| 144 | + |
| 145 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files), host="localhost", port=8000) |
| 146 | + |
| 147 | + client = TestClient(vite_server.app) |
| 148 | + |
| 149 | + # Test root endpoint returns HTML with injection |
| 150 | + response = client.get("/") |
| 151 | + assert response.status_code == 200 |
| 152 | + assert "window.SERVER_CONFIG" in response.text |
| 153 | + assert 'host: "localhost"' in response.text |
| 154 | + assert 'port: "8000"' in response.text |
| 155 | + |
| 156 | + # Test SPA routing also returns HTML with injection |
| 157 | + response = client.get("/some/route") |
| 158 | + assert response.status_code == 200 |
| 159 | + assert "window.SERVER_CONFIG" in response.text |
| 160 | + assert "Hello World" in response.text |
| 161 | + |
| 162 | + # Test health endpoint |
| 163 | + response = client.get("/health") |
| 164 | + assert response.status_code == 200 |
| 165 | + data = response.json() |
| 166 | + assert data["status"] == "ok" |
| 167 | + assert data["build_dir"] == str(temp_build_dir_with_files) |
| 168 | + |
| 169 | + def test_static_file_serving(self, temp_build_dir_with_files): |
| 170 | + """Test that static files are served correctly.""" |
| 171 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 172 | + client = TestClient(vite_server.app) |
| 173 | + |
| 174 | + # Test serving static files |
| 175 | + response = client.get("/assets/app.js") |
| 176 | + assert response.status_code == 200 |
| 177 | + assert "console.log('test')" in response.text |
| 178 | + |
| 179 | + response = client.get("/assets/style.css") |
| 180 | + assert response.status_code == 200 |
| 181 | + assert "color: black" in response.text |
| 182 | + |
| 183 | + response = client.get("/assets/nested/file.txt") |
| 184 | + assert response.status_code == 200 |
| 185 | + assert "nested content" in response.text |
| 186 | + |
| 187 | + def test_spa_routing(self, temp_build_dir_with_files): |
| 188 | + """Test SPA routing fallback.""" |
| 189 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 190 | + client = TestClient(vite_server.app) |
| 191 | + |
| 192 | + # Test that non-existent routes fall back to index.html |
| 193 | + response = client.get("/some/nonexistent/route") |
| 194 | + assert response.status_code == 200 |
| 195 | + assert "Test" in response.text |
| 196 | + |
| 197 | + def test_api_routes_not_affected(self, temp_build_dir_with_files): |
| 198 | + """Test that API routes are not affected by SPA routing.""" |
| 199 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 200 | + client = TestClient(vite_server.app) |
| 201 | + |
| 202 | + # Test that API routes return 404 (not index.html) |
| 203 | + response = client.get("/api/test") |
| 204 | + assert response.status_code == 404 |
| 205 | + |
| 206 | + def test_assets_routes_not_affected(self, temp_build_dir_with_files): |
| 207 | + """Test that asset routes are not affected by SPA routing.""" |
| 208 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 209 | + client = TestClient(vite_server.app) |
| 210 | + |
| 211 | + # Test that asset routes return 404 for non-existent files (not index.html) |
| 212 | + response = client.get("/assets/nonexistent.js") |
| 213 | + assert response.status_code == 404 |
| 214 | + |
| 215 | + def test_health_routes_not_affected(self, temp_build_dir_with_files): |
| 216 | + """Test that health routes are not affected by SPA routing.""" |
| 217 | + vite_server = ViteServer(build_dir=str(temp_build_dir_with_files)) |
| 218 | + client = TestClient(vite_server.app) |
| 219 | + |
| 220 | + # Test that health routes work correctly |
| 221 | + response = client.get("/health") |
| 222 | + assert response.status_code == 200 |
| 223 | + data = response.json() |
| 224 | + assert data["status"] == "ok" |
0 commit comments