import asyncio
import multiprocessing as mp
from pathlib import Path

from httpx import ASGITransport, AsyncClient

from .util import with_app


@with_app
async def _run(app, message):
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://server"
    ) as client:
        response = await client.get("/")
        assert response.text == message


def run(profile, message):
    import os

    os.environ["ZAYT_PROFILE"] = profile
    return asyncio.run(_run(message))


def test_example(monkeypatch):
    path = Path(__file__).parents[2] / "examples" / "settings"
    monkeypatch.syspath_prepend(path)
    monkeypatch.chdir(path)

    with mp.Pool(processes=1) as pool:
        pool.apply(run, ["", "Hello, World!"])
        pool.apply(run, ["dev", "Hello, dev World!"])
