Spaces:
Sleeping

File size: 6,963 Bytes
d3f9990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""`serve_http.py`'s command line, which is `seahaven.http.main`'s.

The in-process cases run the script with `seahaven.http.server.serve` replaced
by a recorder, so they check what reaches the server without binding a port.
One case runs the script as a process and makes a request to it.
"""

import json
import queue
import re
import runpy
import subprocess
import sys
import threading
import urllib.request
from collections.abc import Iterator
from pathlib import Path
from typing import Any

import pytest

from seahaven_stripe_world import world
from seahaven_stripe_world.http_api import handle

server = pytest.importorskip("seahaven.http.server", exc_type=ImportError)

SCRIPT = Path(__file__).parents[2] / "serve_http.py"
VARIABLES = (
    "SEAHAVEN_FIXTURE",
    "SEAHAVEN_SEED",
    "SEAHAVEN_NOW",
    "SEAHAVEN_CLOCK_MODE",
    "SEAHAVEN_RESET_OPTIONS",
)
TEST_MODE = '{"startup": {"livemode": false}}'
# A bound on a hang rather than a measurement.
PROCESS_WAIT = 60.0
RUNNING = re.compile(r"Uvicorn running on http://127\.0\.0\.1:(\d+)")


@pytest.fixture(autouse=True)
def clean_environment(monkeypatch: pytest.MonkeyPatch) -> None:
    for variable in VARIABLES:
        monkeypatch.delenv(variable, raising=False)


@pytest.fixture
def served(monkeypatch: pytest.MonkeyPatch) -> list[dict[str, Any]]:
    """What each run of the script handed the server, keyword arguments by name."""
    calls: list[dict[str, Any]] = []

    def record(given_world: Any, given_handler: Any, **kwargs: Any) -> None:
        assert (given_world, given_handler) == (world, handle)
        calls.append(kwargs)

    monkeypatch.setattr(server, "serve", record)
    return calls


def run_script(monkeypatch: pytest.MonkeyPatch, *argv: str) -> None:
    monkeypatch.setattr(sys, "argv", [str(SCRIPT), *argv])
    runpy.run_path(str(SCRIPT), run_name="__main__")


def test_no_options_serve_a_blank_live_mode_instance_on_the_default_address(
    monkeypatch: pytest.MonkeyPatch, served: list[dict[str, Any]]
) -> None:
    run_script(monkeypatch)
    assert served == [
        {"host": "127.0.0.1", "port": 8000, "reset_options": {}, "max_instances": 100}
    ]


def test_the_address_and_the_instance_limit_are_options(
    monkeypatch: pytest.MonkeyPatch, served: list[dict[str, Any]]
) -> None:
    run_script(monkeypatch, "--host", "0.0.0.0", "--port", "9123", "--max-instances", "0")
    assert served == [{"host": "0.0.0.0", "port": 9123, "reset_options": {}, "max_instances": 0}]


def test_the_convenience_flags_are_reset_options(
    monkeypatch: pytest.MonkeyPatch, served: list[dict[str, Any]]
) -> None:
    run_script(
        monkeypatch,
        "--fixture",
        "empty",
        "--seed",
        "7",
        "--now",
        "2026-09-02T00:00:00.000Z",
        "--clock-mode",
        "tick",
    )
    assert served[0]["reset_options"] == {
        "fixture": "empty",
        "seed": 7,
        "now": "2026-09-02T00:00:00.000Z",
        "clock_mode": "tick",
    }


def test_test_mode_is_a_reset_option(
    monkeypatch: pytest.MonkeyPatch, served: list[dict[str, Any]]
) -> None:
    run_script(monkeypatch, "--reset-options", TEST_MODE)
    assert served[0]["reset_options"] == {"startup": {"livemode": False}}


def test_the_environment_variables_are_reset_options(
    monkeypatch: pytest.MonkeyPatch, served: list[dict[str, Any]]
) -> None:
    monkeypatch.setenv("SEAHAVEN_FIXTURE", "empty")
    monkeypatch.setenv("SEAHAVEN_SEED", "11")
    run_script(monkeypatch)
    monkeypatch.delenv("SEAHAVEN_FIXTURE")
    monkeypatch.delenv("SEAHAVEN_SEED")
    monkeypatch.setenv("SEAHAVEN_RESET_OPTIONS", TEST_MODE)
    run_script(monkeypatch)
    assert [call["reset_options"] for call in served] == [
        {"fixture": "empty", "seed": 11},
        {"startup": {"livemode": False}},
    ]


@pytest.mark.parametrize(
    ("argv", "refusal"),
    [
        (["--fixture", "empty", "--reset-options", TEST_MODE], "cannot be combined with"),
        (["--seed", "seven"], "seven"),
        (["--reset-options", '{"control_tools": true}'], "control_tools"),
        (["--max-instances", "-1"], "--max-instances"),
    ],
)
def test_a_refused_option_is_one_line_and_exit_1(
    monkeypatch: pytest.MonkeyPatch,
    served: list[dict[str, Any]],
    capsys: pytest.CaptureFixture[str],
    argv: list[str],
    refusal: str,
) -> None:
    with pytest.raises(SystemExit) as exited:
        run_script(monkeypatch, *argv)
    assert exited.value.code == 1
    error = capsys.readouterr().err
    assert refusal in error
    assert error.count("\n") == 1
    assert served == []


# --- the script, as a process ------------------------------------------------


@pytest.fixture
def script() -> Iterator[tuple[subprocess.Popen[str], queue.Queue[str]]]:
    """`python serve_http.py --port 0 --reset-options <test mode>`, and its stderr lines."""
    process = subprocess.Popen(
        [sys.executable, str(SCRIPT), "--port", "0", "--reset-options", TEST_MODE],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    assert process.stderr is not None
    errors: queue.Queue[str] = queue.Queue()
    stream = process.stderr

    def drain() -> None:
        for line in stream:
            errors.put(line)

    drainer = threading.Thread(target=drain, daemon=True)
    drainer.start()
    try:
        yield process, errors
    finally:
        if process.poll() is None:
            process.kill()
        process.wait(PROCESS_WAIT)
        drainer.join(PROCESS_WAIT)
        stream.close()
        if process.stdout is not None:
            process.stdout.close()


def test_the_script_serves_stripes_api_over_http(
    script: tuple[subprocess.Popen[str], queue.Queue[str]],
) -> None:
    process, errors = script
    assert process.stdout is not None
    assert process.stdout.readline().startswith("Serving seahaven_stripe_world at http://")
    port = _port(errors)
    request = urllib.request.Request(
        f"http://127.0.0.1:{port}/worlds/proc/v1/customers",
        data=b"email=proc%40example.test",
        headers={"Authorization": "Bearer sk_test_anything"},
        method="POST",
    )
    # No proxy: a proxy in the environment would not reach this machine's loopback.
    direct = urllib.request.build_opener(urllib.request.ProxyHandler({}))
    with direct.open(request, timeout=PROCESS_WAIT) as response:
        customer = json.load(response)
    assert customer["email"] == "proc@example.test"
    assert customer["livemode"] is False


def _port(errors: queue.Queue[str]) -> int:
    """The port uvicorn bound, from its startup line on stderr."""
    seen: list[str] = []
    while True:
        try:
            line = errors.get(timeout=PROCESS_WAIT)
        except queue.Empty:
            raise AssertionError(f"the server did not start: {''.join(seen)}") from None
        seen.append(line)
        if match := RUNNING.search(line):
            return int(match.group(1))