File size: 2,608 Bytes
d686d47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import json
import zipfile

from conftest import complete
from ros_fixtures import INTERFACES, as_file_blocks, python_package

from api.ros_check import load_interfaces


def test_app_page_and_assets_are_served(run_env):
    client = run_env[0]
    page = client.get("/")
    assert page.status_code == 200 and "RosDiff" in page.text and "/static/app/app.js" in page.text
    assert client.get("/static/app/app.js").status_code == 200
    assert client.get("/static/app/app.css").status_code == 200


def test_history_generation_and_media(run_env):
    client, keys_, backend, services, gen_id = run_env
    h = {"X-API-Key": keys_["alice"]}
    hist = client.get("/me/history", headers=h).json()
    assert hist["tester"] == "alice" and hist["items"][0]["id"] == gen_id
    assert hist["items"][0]["kind"] == "simulate" and hist["items"][0]["title"].startswith("G1 near a slope")
    assert client.get("/me/history", headers={"X-API-Key": keys_["bob"]}).json()["items"] == []

    run_id = client.post("/simulate/run", json={"generation_id": gen_id}, headers=h).json()["run_id"]
    complete(backend, "job-1", run_id)
    job = client.get(f"/jobs/{run_id}", headers=h).json()
    assert job["media"]["video"].endswith(f"runs/{run_id}/video.mp4") and "video_webm" in job["media"]

    g = client.get(f"/generations/{gen_id}", headers=h).json()
    assert g["kind"] == "simulate" and g["valid"] and "<mujoco" in g["output"]
    assert [j["id"] for j in g["jobs"]] == [run_id] and "/report/" in g["jobs"][0]["report_url"]
    assert client.get(f"/generations/{gen_id}", headers={"X-API-Key": keys_["bob"]}).status_code == 404


def test_code_zip(run_env):
    client, keys_, backend, services, _ = run_env
    services.settings.data_dir.mkdir(parents=True, exist_ok=True)
    services.settings.interfaces_file.write_text(json.dumps(INTERFACES))
    load_interfaces.cache_clear()
    services.llm_factory().responses[:] = [as_file_blocks(python_package())]
    h = {"X-API-Key": keys_["alice"]}
    gen = client.post("/code/generate", json={"request": "Stop on a close LaserScan reading"}, headers=h).json()
    r = client.get(f"/code/{gen['id']}/zip", headers=h)
    assert r.status_code == 200 and r.headers["content-type"] == "application/zip"
    names = zipfile.ZipFile(io.BytesIO(r.content)).namelist()
    assert "safety_stop/package.xml" in names
    assert client.get(f"/code/{gen['id']}/zip", headers={"X-API-Key": keys_["bob"]}).status_code == 404
    g = client.get(f"/generations/{gen['id']}", headers=h).json()
    assert g["kind"] == "code" and "safety_stop/package.xml" in g["output"]