Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.0 KB
Raw Blame
"""Run the copied Dashboard Quickstart programs against a deployed Cloud."""

import asyncio
import json
import os
import re
import subprocess
import sys
from pathlib import Path

import pytest
from e2e_auth import auth_context
from network_fixtures import ready

EXAMPLES = Path(__file__).resolve().parents[3] / "target/e2e/quickstart"
COMMANDS = {
    "python": [sys.executable, "main.py"],
    "typescript": ["node", "index.mjs"],
    "go": ["./quickstart-go"],
    "rust": ["./quickstart-rust"],
    "c": ["./quickstart-c"],
    "cli": ["bash", "quickstart.sh"],
    "rest": ["bash", "quickstart-rest.sh"],
}
CREATED_BOX = re.compile(r"^Created Box: (\S+)$", re.MULTILINE)
PAGE = (
    b"<!doctype html><title>BoxLite preview</title><h1>Hello from BoxLite</h1>"
    b"<p>This page is running inside your Box.</p>"
)


def quickstart_interfaces():
    if os.environ.get("BOXLITE_E2E_QUICKSTART") != "1":
        return []
    interfaces = json.loads((EXAMPLES / "interfaces.json").read_text())
    assert set(interfaces) == COMMANDS.keys()
    return interfaces


@pytest.mark.e2e
@pytest.mark.parametrize("interface", quickstart_interfaces())
async def test_quickstart_example(rt, interface, tmp_path):
    ctx = auth_context()
    assert ctx.auth == "api-key"
    env = {
        **os.environ,
        "BOXLITE_API_KEY": ctx.token,
        "BOXLITE_REST_URL": ctx.url,
        "BOXLITE_HOME": str(tmp_path),
    }
    for key in ("BOXLITE_PROFILE", "BOXLITE_CONTEXT", "BOXLITE_REST_PATH_PREFIX"):
        env.pop(key, None)
    if cli := os.environ.get("BOXLITE_E2E_CLI"):
        (tmp_path / "boxlite").symlink_to(Path(cli).resolve())
        env["PATH"] = f"{tmp_path}{os.pathsep}{env['PATH']}"

    output = ""
    try:
        result = await asyncio.to_thread(
            subprocess.run,
            COMMANDS[interface],
            cwd=EXAMPLES,
            env=env,
            capture_output=True,
            text=True,
            timeout=120,
            check=False,
        )
        output = result.stdout.replace(ctx.token, "[redacted]")
        error = result.stderr.replace(ctx.token, "[redacted]")
        assert result.returncode == 0, (
            f"{interface} exited {result.returncode}\n{output}\n{error}"
        )
        if interface == "rest":
            assert re.search(r'"exit_code":\s*0\b', output)
        else:
            assert "Hello from BoxLite" in output.splitlines()
        created = CREATED_BOX.search(output)
        assert created is not None
        assert await rt.get_info(created[1]) is not None

        preview = re.search(r"^Preview URL: (https://\S+)$", output, re.MULTILINE)
        assert preview is not None
        await ready(preview[1], PAGE)
    except subprocess.TimeoutExpired as timeout:
        output = (timeout.stdout or b"").decode(errors="replace")
        pytest.fail(f"{interface} exceeded the {timeout.timeout}-second runtime limit")
    finally:
        if (created := CREATED_BOX.search(output)) and await rt.get_info(
            created[1]
        ) is not None:
            await rt.remove(created[1], force=True)