File size: 4,815 Bytes
3c07134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20df900
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ba0cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""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("<html><body>hi</body></html>", 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("<html>ok</html>", 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("<html><body><div id='root'></div></body></html>", 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' %}<h1>nope</h1>", 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(
        "<html><head></head><body><div id=\"root\"></div>"
        "<script type=\"module\" src=\"/src/main.tsx\"></script></body></html>",
        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("<html><body><h1>Mozilla is cool</h1></body></html>", 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(
        "<html><body><div id=\"root\"></div>"
        "<script type=\"module\" src=\"/assets/index-abc123.js\"></script></body></html>",
        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(
        "<!doctype html><html><body><div id='root'></div>"
        "<script type='module' src='/src/main.jsx'></script></body></html>",
        encoding="utf-8",
    )
    plan = detect_preview(tmp_path)
    assert plan.kind == "unsupported"
    assert "Unbuilt" in plan.reason