Skip to content

Commit 4aed82d

Browse files
committed
test(cli): exercise standalone sign-in with and without a built page
Use the real aiohttp Application while replacing the runner and TCP site, so build_app can register its response hook without a partial fake. Resolve the UI from temporary candidates, cover both page states, and assert that revalidation is registered only when a built page is served. Correct the startup comment now that the coroutine is exercised in tests.
1 parent 9fdc3a2 commit 4aed82d

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎raven/cli/serve_commands.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,8 @@ async def _serve_main(port: int, open_browser: bool) -> None:
411411
from raven.rpc.transports.ws import WsGateway, build_app, pick_port
412412

413413
register_console_feature()
414-
# This coroutine binds a port and serves until it is killed, so no test
415-
# runs it; what it schedules is pinned by source in
416-
# tests/test_cli_serve_commands.py instead.
414+
# Startup tests replace this background warm-up to keep provider
415+
# initialization out of the serving probe.
417416
warm_up_in_background() # pragma: no cover
418417

419418
# Declared before anything can emit a span. The page runs on the terminal's

‎tests/test_cli_serve_commands.py‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -470,24 +470,8 @@ async def _teardown() -> None:
470470
build_error=None, agent_loop=None, deliverables=None, dispatcher=None, teardown=_teardown
471471
)
472472

473-
class _Application:
474-
"""The three things `build_app` touches on a real aiohttp application."""
475-
476-
def __init__(self) -> None:
477-
self.router = self
478-
self.on_shutdown: list = []
479-
480-
def add_get(self, *_args, **_kwargs) -> None:
481-
pass
482-
483-
def add_post(self, *_args, **_kwargs) -> None:
484-
pass
485-
486-
def add_static(self, *_args, **_kwargs) -> None:
487-
pass
488-
489473
class _Runner:
490-
"""`AppRunner`/`TCPSite`: the four awaitables `_serve_main` uses."""
474+
"""`AppRunner`/`TCPSite` without binding sockets."""
491475

492476
def __init__(self, *_args) -> None:
493477
pass
@@ -532,14 +516,21 @@ def test_a_standalone_serve_opens_through_the_same_path(self) -> None:
532516
assert "_open(auth_url)" in src
533517
assert "webbrowser.open" not in src
534518

535-
async def test_a_standalone_serve_prints_the_link_it_opens(self, monkeypatch, opened: list[str]) -> None:
519+
@pytest.mark.parametrize("built_page", [False, True], ids=["without-page", "with-page"])
520+
async def test_a_standalone_serve_prints_the_link_it_opens(
521+
self, two_candidates, built_page: bool, monkeypatch, opened: list[str]
522+
) -> None:
536523
"""The standalone path, run rather than read: the URL it hands the browser
537524
is one the gateway minted, and the launch that asked for a browser is the
538-
one that gets one."""
525+
one that gets one, with or without a built page."""
539526
from contextlib import suppress
540527

541528
from raven.cli import serve_commands as sc
542529

530+
packaged, _source = two_candidates
531+
if built_page:
532+
_build(packaged, "page")
533+
543534
printed: list[str] = []
544535
monkeypatch.setattr(sc.typer, "echo", printed.append)
545536

@@ -563,9 +554,14 @@ async def _nothing() -> None:
563554

564555
from aiohttp import web
565556

566-
monkeypatch.setattr(web, "AppRunner", TestTheSignInLink._Runner)
557+
apps: list[web.Application] = []
558+
559+
def _runner(app: web.Application) -> TestTheSignInLink._Runner:
560+
apps.append(app)
561+
return self._Runner()
562+
563+
monkeypatch.setattr(web, "AppRunner", _runner)
567564
monkeypatch.setattr(web, "TCPSite", TestTheSignInLink._Runner)
568-
monkeypatch.setattr(web, "Application", TestTheSignInLink._Application)
569565
monkeypatch.setattr(serve_commands, "_announce_updates", lambda *a, **k: _nothing())
570566
monkeypatch.setattr(serve_commands, "_write_serve_state", lambda *a, **k: None)
571567

@@ -575,6 +571,9 @@ async def _nothing() -> None:
575571
assert len(opened) == 1
576572
assert opened[0].startswith("http://127.0.0.1:18999/auth#")
577573
assert f"sign in with this link (it works once): {opened[0]}" in printed
574+
(app,) = apps
575+
assert isinstance(app, web.Application)
576+
assert any(hook.__name__ == "revalidate" for hook in app.on_response_prepare) is built_page
578577

579578

580579
class _FakeProc:

0 commit comments

Comments
 (0)