Repository navigation
Expand file tree
/
Copy pathtest_dev_launch.py
More file actions
113 lines (98 loc) · 4.14 KB
/
Copy pathtest_dev_launch.py
File metadata and controls
113 lines (98 loc) · 4.14 KB
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
"""Development launch uses native authority without bypassing page bootstrap."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
@pytest.fixture
def dev_launch(monkeypatch):
from sqlrooms.agent import dev as module
response = SimpleNamespace(
raise_for_status=lambda: None, json=lambda: {"binding": "current"}
)
client = Mock()
client.__enter__ = Mock(return_value=client)
client.__exit__ = Mock(return_value=False)
client.get.return_value = response
monkeypatch.setattr(module.httpx, "Client", Mock(return_value=client))
monkeypatch.setattr(module.registry, "verify", Mock())
monkeypatch.setattr(
module.registry,
"request",
Mock(return_value={"url": "http://localhost:3100/#sqlrooms-ticket=once"}),
)
monkeypatch.setattr(module.webbrowser, "open_new_tab", Mock(return_value=True))
monkeypatch.setattr(module.sys.stderr, "isatty", lambda: False)
return module
def test_dev_launch_targets_exact_backend_and_keeps_ticket_out_of_logs(
dev_launch, monkeypatch, capsys
):
expected = {"instanceId": "current", "apiUrl": "http://127.0.0.1:4273"}
monkeypatch.setattr(
dev_launch.registry,
"records",
lambda **kwargs: [
{"instanceId": "other", "apiUrl": expected["apiUrl"]},
expected,
],
)
dev_launch.launch_dev(expected["apiUrl"])
dev_launch.registry.verify.assert_called_once_with(expected, settings=None)
dev_launch.registry.request.assert_called_once_with(
expected, "/api/auth/ticket", payload={}
)
dev_launch.webbrowser.open_new_tab.assert_called_once_with(
"http://localhost:3100/#sqlrooms-ticket=once"
)
assert "once" not in capsys.readouterr().err
@pytest.mark.parametrize(
"record",
[
{"instanceId": "old", "apiUrl": "http://127.0.0.1:4273"},
{"instanceId": "current", "apiUrl": "http://127.0.0.1:4274"},
],
)
def test_dev_launch_never_targets_other_binding_or_port(
dev_launch, monkeypatch, record
):
monkeypatch.setattr(dev_launch.registry, "records", lambda **kwargs: [record])
with pytest.raises(RuntimeError, match="native handoff"):
dev_launch.launch_dev("http://127.0.0.1:4273", timeout=0)
dev_launch.registry.request.assert_not_called()
dev_launch.webbrowser.open_new_tab.assert_not_called()
def test_no_open_browser_prints_only_to_interactive_terminal(
dev_launch, monkeypatch, capsys
):
record = {"instanceId": "current", "apiUrl": "http://127.0.0.1:4273"}
monkeypatch.setattr(dev_launch.registry, "records", lambda **kwargs: [record])
monkeypatch.setattr(dev_launch.sys.stderr, "isatty", lambda: True)
dev_launch.launch_dev(record["apiUrl"], open_browser=False)
assert "#sqlrooms-ticket=once" in capsys.readouterr().err
dev_launch.webbrowser.open_new_tab.assert_not_called()
def test_automatic_browser_and_terminal_receive_separate_tickets(
dev_launch, monkeypatch, capsys
):
record = {"instanceId": "current", "apiUrl": "http://127.0.0.1:4273"}
monkeypatch.setattr(dev_launch.registry, "records", lambda **kwargs: [record])
monkeypatch.setattr(dev_launch.sys.stderr, "isatty", lambda: True)
dev_launch.registry.request.side_effect = [
{"url": "http://localhost:3100/#sqlrooms-ticket=browser"},
{"url": "http://localhost:3100/#sqlrooms-ticket=terminal"},
]
dev_launch.launch_dev(record["apiUrl"])
dev_launch.webbrowser.open_new_tab.assert_called_once_with(
"http://localhost:3100/#sqlrooms-ticket=browser"
)
output = capsys.readouterr().err
assert "#sqlrooms-ticket=terminal" in output
assert "#sqlrooms-ticket=browser" not in output
def test_development_launch_rejects_other_process(dev_launch, monkeypatch):
module = dev_launch
monkeypatch.setattr(
module.registry,
"records",
lambda **kwargs: [
{"instanceId": "current", "apiUrl": "http://localhost:4273", "pid": 99}
],
)
with pytest.raises(RuntimeError, match="native handoff"):
module.launch_dev("http://localhost:4273", expected_pid=100, timeout=0)
module.registry.verify.assert_not_called()