"""Session lifecycle with a tiny local static fixture (no network).""" from __future__ import annotations import time from pathlib import Path import pytest from backend.app.services.detect import detect_preview from backend.app.services.ports import PortPool from backend.app.services.runner import start_static_server, terminate_sandbox def test_detect_static(tmp_path: Path): (tmp_path / "index.html").write_text("hi", encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "static" assert plan.static_root == tmp_path def test_detect_unsupported_empty(tmp_path: Path): (tmp_path / "README.md").write_text("# nothing", encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "unsupported" @pytest.mark.asyncio async def test_static_server_and_port(tmp_path: Path): (tmp_path / "index.html").write_text("ok", encoding="utf-8") pool = PortPool(9200, 9210) port = pool.acquire() logs: list[str] = [] box = await start_static_server(tmp_path, port=port, log=logs.append) assert box.alive() terminate_sandbox(box) pool.release(port) # give process a moment time.sleep(0.2) def test_detect_ignores_cra_public_shell(tmp_path: Path): public = tmp_path / "public" public.mkdir() (public / "index.html").write_text("
", encoding="utf-8") (tmp_path / "package.json").write_text( '{"scripts":{"start":"react-scripts start"},"dependencies":{"react-scripts":"5.0.0"}}', encoding="utf-8", ) plan = detect_preview(tmp_path) assert plan.kind == "node" assert plan.static_root is None def test_detect_django_not_template_html(tmp_path: Path): tpl = tmp_path / "hello" / "templates" tpl.mkdir(parents=True) (tpl / "index.html").write_text("{% extends 'base.html' %}

nope

", encoding="utf-8") (tmp_path / "manage.py").write_text("#!/usr/bin/env python\n", encoding="utf-8") (tmp_path / "requirements.txt").write_text("django\n", encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "python" assert plan.meta.get("framework") == "django" def test_detect_skips_template_only_tree(tmp_path: Path): tpl = tmp_path / "hello" / "templates" tpl.mkdir(parents=True) (tpl / "index.html").write_text("{% extends 'base.html' %}", encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "unsupported" @pytest.mark.asyncio async def test_node_process_not_aborted_by_rlimit(tmp_path: Path): import shutil if not shutil.which("node"): pytest.skip("node not installed") from backend.app.services.runner import run_command logs: list[str] = [] rc = await run_command( ["node", "-e", "console.log('heap-ok')"], cwd=tmp_path, env={}, timeout=30, log=logs.append, ) assert rc == 0, logs def test_detect_rejects_nested_vite_shell(tmp_path: Path): """unsloth-style studio/frontend/index.html must not become a static preview.""" fe = tmp_path / "studio" / "frontend" fe.mkdir(parents=True) (fe / "index.html").write_text( "
" "", encoding="utf-8", ) (fe / "package.json").write_text('{"dependencies":{"vite":"5.0.0"}}', encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "unsupported" assert plan.static_root is None assert "studio/frontend/index.html" in plan.reason assert "white" in plan.reason.lower() def test_detect_keeps_real_nested_static(tmp_path: Path): site = tmp_path / "docs" site.mkdir() (site / "index.html").write_text("

Mozilla is cool

", encoding="utf-8") plan = detect_preview(tmp_path) assert plan.kind == "static" assert plan.static_root == site def test_detect_serves_built_vite_dist(tmp_path: Path): dist = tmp_path / "dist" dist.mkdir() (dist / "index.html").write_text( "
" "", encoding="utf-8", ) plan = detect_preview(tmp_path) assert plan.kind == "static" assert plan.static_root == dist def test_detect_root_vite_shell_without_package_json(tmp_path: Path): (tmp_path / "index.html").write_text( "
" "", encoding="utf-8", ) plan = detect_preview(tmp_path) assert plan.kind == "unsupported" assert "Unbuilt" in plan.reason