返回 last30days-skill
test_box_chrome_login.py
根目录 / tests / test_box_chrome_login.py
1 """Extras-host box-chrome X login helper (skills/.../scripts/box_chrome_login.py).
2
3 Locks: a MacBook never gets a launch command and never spawns box-chrome (even
4 with --exec); an extras host with box-chrome on PATH documents the last30days
5 extras port 18800; and the SKILL.md recipe is present in the Auto flow and the
6 repair section with the MacBook-skip and no-.env-cookie rules. No cookie values
7 are ever produced (the helper reads none).
8 """
9
10 from pathlib import Path
11 from unittest import mock
12
13 import box_chrome_login as bcl
14 from lib import chrome_cdp
15
16 REPO = Path(__file__).resolve().parent.parent
17 SKILL = REPO / "skills" / "last30days" / "SKILL.md"
18
19
20 def test_extras_port_is_18800_and_matches_chrome_cdp():
21 assert bcl.EXTRAS_CDP_PORT == 18800
22 assert bcl.EXTRAS_CDP_PORT == chrome_cdp._BOX_CHROME_PORT
23
24
25 def test_macbook_gets_no_launch_command():
26 with mock.patch("lib.env.x_extras_enabled", return_value=False):
27 recipe = bcl.build_recipe({})
28 assert recipe["applies"] is False
29 assert recipe["command"] is None
30 assert recipe["env"] is None
31 rendered = bcl.render_recipe(recipe).lower()
32 assert "not an extras host" in rendered or "no box-chrome login" in rendered.replace("-", "")
33
34
35 def test_extras_with_box_chrome_documents_18800():
36 with (
37 mock.patch("lib.env.x_extras_enabled", return_value=True),
38 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
39 ):
40 recipe = bcl.build_recipe({})
41 assert recipe["applies"] is True
42 assert recipe["command"] == ["/usr/local/bin/box-chrome", "--new-window", "https://x.com/login"]
43 assert recipe["env"]["SAND_CHROME_REMOTE_DEBUG_PORT"] == "18800"
44 assert recipe["env"]["CHROME_USER_DATA_DIR"]
45 rendered = bcl.render_recipe(recipe)
46 assert "SAND_CHROME_REMOTE_DEBUG_PORT=18800" in rendered
47 assert "box-chrome" in rendered
48 # The pin guidance names the endpoint but never a cookie value.
49 assert "BROWSER_CDP_URL=http://127.0.0.1:18800" in rendered
50
51
52 def test_helper_launches_via_box_chrome_without_custom_class():
53 """Launch through the box-chrome wrapper (it sets --class=box-chrome); never
54 a raw chrome with a custom --class, which is what failed live."""
55 with (
56 mock.patch("lib.env.x_extras_enabled", return_value=True),
57 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
58 ):
59 recipe = bcl.build_recipe({})
60 assert recipe["command"][0].endswith("box-chrome")
61 assert not any(arg.startswith("--class") for arg in recipe["command"])
62 assert "google-chrome" not in " ".join(recipe["command"])
63
64
65 def test_extras_without_box_chrome_guides_pin():
66 with (
67 mock.patch("lib.env.x_extras_enabled", return_value=True),
68 mock.patch("shutil.which", return_value=None),
69 ):
70 recipe = bcl.build_recipe({})
71 assert recipe["applies"] is True
72 assert recipe["command"] is None
73 assert "BROWSER_CDP_URL" in recipe["note"]
74
75
76 def test_main_exec_never_spawns_on_macbook():
77 with (
78 mock.patch("lib.env.x_extras_enabled", return_value=False),
79 mock.patch.object(bcl.env, "get_config", return_value={}),
80 mock.patch("subprocess.Popen", side_effect=AssertionError("no spawn on MacBook")),
81 ):
82 assert bcl.main(["--exec"]) == 0
83
84
85 def test_main_exec_missing_box_chrome_does_not_spawn():
86 with (
87 mock.patch("lib.env.x_extras_enabled", return_value=True),
88 mock.patch("shutil.which", return_value=None),
89 mock.patch.object(bcl.env, "get_config", return_value={}),
90 mock.patch("subprocess.Popen", side_effect=AssertionError("no spawn without box-chrome")),
91 ):
92 assert bcl.main(["--exec"]) == 0
93
94
95 def test_main_exec_spawns_box_chrome_on_extras_with_18800(tmp_path):
96 popen = mock.MagicMock()
97 profile = tmp_path / "x-chrome"
98 with (
99 mock.patch("lib.env.x_extras_enabled", return_value=True),
100 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
101 mock.patch.object(bcl.env, "get_config", return_value={}),
102 mock.patch.object(bcl, "_default_profile_dir", return_value=str(profile)),
103 mock.patch("subprocess.Popen", popen),
104 ):
105 rc = bcl.main(["--exec"])
106 assert rc == 0
107 assert profile.is_dir()
108 popen.assert_called_once()
109 args, kwargs = popen.call_args
110 assert args[0] == ["/usr/local/bin/box-chrome", "--new-window", "https://x.com/login"]
111 assert kwargs["env"]["SAND_CHROME_REMOTE_DEBUG_PORT"] == "18800"
112 assert kwargs["env"]["CHROME_USER_DATA_DIR"] == str(profile)
113
114
115 def test_default_run_prints_but_does_not_spawn():
116 with (
117 mock.patch("lib.env.x_extras_enabled", return_value=True),
118 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
119 mock.patch.object(bcl.env, "get_config", return_value={}),
120 mock.patch("subprocess.Popen", side_effect=AssertionError("no --exec = no spawn")),
121 ):
122 assert bcl.main([]) == 0
123
124
125 # --- Official-only host (LAST30DAYS_HOST=grok-bot) --------------------------
126
127 GROK_BOT_CONFIG = {"LAST30DAYS_HOST": "grok-bot"}
128
129
130 def test_grok_bot_host_gets_no_launch_command_even_when_extras_apply():
131 """U6: an official-only host gets the same no-launch recipe as a MacBook
132 even when the extras signals (Linux / box-chrome on PATH) are present."""
133 with (
134 mock.patch("lib.env.x_extras_enabled", return_value=True),
135 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
136 ):
137 recipe = bcl.build_recipe(dict(GROK_BOT_CONFIG))
138 assert recipe["applies"] is False
139 assert recipe["command"] is None
140 assert recipe["env"] is None
141 rendered = bcl.render_recipe(recipe).lower()
142 assert "no launch needed" in rendered
143 # R4: nothing in the Grok Bot output names the cookie machinery.
144 for banned in ("cookie", "cdp", "bird", "auth_token", "ct0", "keychain"):
145 assert banned not in rendered, f"{banned!r} leaked into Grok Bot output"
146
147
148 def test_main_exec_never_spawns_on_grok_bot_host():
149 with (
150 mock.patch("lib.env.x_extras_enabled", return_value=True),
151 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
152 mock.patch.object(bcl.env, "get_config", return_value=dict(GROK_BOT_CONFIG)),
153 mock.patch("os.makedirs", side_effect=AssertionError("no profile dir on Grok Bot")),
154 mock.patch("subprocess.Popen", side_effect=AssertionError("no spawn on Grok Bot")),
155 ):
156 assert bcl.main(["--exec"]) == 0
157
158
159 def test_linux_and_mac_mini_recipe_unchanged_without_host_signal():
160 """R14/R15: without LAST30DAYS_HOST the extras recipe is what it was."""
161 with (
162 mock.patch("lib.env.x_extras_enabled", return_value=True),
163 mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"),
164 ):
165 recipe = bcl.build_recipe({})
166 assert recipe["applies"] is True
167 assert recipe["command"] == ["/usr/local/bin/box-chrome", "--new-window", "https://x.com/login"]
168
169
170 # --- SKILL.md recipe contract ---------------------------------------------
171
172
173 def _skill():
174 from tests.skill_contract import reference_text
175
176 return reference_text("setup-wizard")
177
178
179 def test_skill_md_references_helper_in_flows_and_repair():
180 text = _skill()
181 # Auto Modal flow, Non-Modal Prose flow, and the repair section each point
182 # at the helper.
183 assert text.count("box_chrome_login.py") >= 3
184
185
186 def test_skill_md_documents_18800_launch_and_macbook_skip():
187 text = _skill()
188 assert "SAND_CHROME_REMOTE_DEBUG_PORT=18800" in text
189 assert "X on Linux / Mac mini (repair)" in text
190 # The repair recipe is rescoped off Grok Bot (R15): that host has its own
191 # official-only flow and never launches box-chrome.
192 assert "X on Linux / Grok Bot / Mac mini" not in text
193 # MacBook must be told to skip the box-chrome path.
194 assert "MacBook SKIPS" in text or "MacBook does NOT" in text
195
196
197 def test_skill_md_pins_browser_cdp_url_not_raw_cookies():
198 text = _skill()
199 assert "BROWSER_CDP_URL=http://127.0.0.1:18800" in text
200 # The recipe must forbid writing the raw cookie pair to .env.
201 assert "never `AUTH_TOKEN`/`CT0`" in text or "Do NOT write `AUTH_TOKEN`" in text
202
202 lines PYTHON