| 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(): |
| 96 | popen = mock.MagicMock() |
| 97 | with ( |
| 98 | mock.patch("lib.env.x_extras_enabled", return_value=True), |
| 99 | mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"), |
| 100 | mock.patch.object(bcl.env, "get_config", return_value={}), |
| 101 | mock.patch("os.makedirs"), |
| 102 | mock.patch("subprocess.Popen", popen), |
| 103 | ): |
| 104 | rc = bcl.main(["--exec"]) |
| 105 | assert rc == 0 |
| 106 | popen.assert_called_once() |
| 107 | args, kwargs = popen.call_args |
| 108 | assert args[0] == ["/usr/local/bin/box-chrome", "--new-window", "https://x.com/login"] |
| 109 | assert kwargs["env"]["SAND_CHROME_REMOTE_DEBUG_PORT"] == "18800" |
| 110 | |
| 111 | |
| 112 | def test_default_run_prints_but_does_not_spawn(): |
| 113 | with ( |
| 114 | mock.patch("lib.env.x_extras_enabled", return_value=True), |
| 115 | mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"), |
| 116 | mock.patch.object(bcl.env, "get_config", return_value={}), |
| 117 | mock.patch("subprocess.Popen", side_effect=AssertionError("no --exec = no spawn")), |
| 118 | ): |
| 119 | assert bcl.main([]) == 0 |
| 120 | |
| 121 | |
| 122 | # --- Official-only host (LAST30DAYS_HOST=grok-bot) -------------------------- |
| 123 | |
| 124 | GROK_BOT_CONFIG = {"LAST30DAYS_HOST": "grok-bot"} |
| 125 | |
| 126 | |
| 127 | def test_grok_bot_host_gets_no_launch_command_even_when_extras_apply(): |
| 128 | """U6: an official-only host gets the same no-launch recipe as a MacBook |
| 129 | even when the extras signals (Linux / box-chrome on PATH) are present.""" |
| 130 | with ( |
| 131 | mock.patch("lib.env.x_extras_enabled", return_value=True), |
| 132 | mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"), |
| 133 | ): |
| 134 | recipe = bcl.build_recipe(dict(GROK_BOT_CONFIG)) |
| 135 | assert recipe["applies"] is False |
| 136 | assert recipe["command"] is None |
| 137 | assert recipe["env"] is None |
| 138 | rendered = bcl.render_recipe(recipe).lower() |
| 139 | assert "no launch needed" in rendered |
| 140 | # R4: nothing in the Grok Bot output names the cookie machinery. |
| 141 | for banned in ("cookie", "cdp", "bird", "auth_token", "ct0", "keychain"): |
| 142 | assert banned not in rendered, f"{banned!r} leaked into Grok Bot output" |
| 143 | |
| 144 | |
| 145 | def test_main_exec_never_spawns_on_grok_bot_host(): |
| 146 | with ( |
| 147 | mock.patch("lib.env.x_extras_enabled", return_value=True), |
| 148 | mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"), |
| 149 | mock.patch.object(bcl.env, "get_config", return_value=dict(GROK_BOT_CONFIG)), |
| 150 | mock.patch("os.makedirs", side_effect=AssertionError("no profile dir on Grok Bot")), |
| 151 | mock.patch("subprocess.Popen", side_effect=AssertionError("no spawn on Grok Bot")), |
| 152 | ): |
| 153 | assert bcl.main(["--exec"]) == 0 |
| 154 | |
| 155 | |
| 156 | def test_linux_and_mac_mini_recipe_unchanged_without_host_signal(): |
| 157 | """R14/R15: without LAST30DAYS_HOST the extras recipe is what it was.""" |
| 158 | with ( |
| 159 | mock.patch("lib.env.x_extras_enabled", return_value=True), |
| 160 | mock.patch("shutil.which", return_value="/usr/local/bin/box-chrome"), |
| 161 | ): |
| 162 | recipe = bcl.build_recipe({}) |
| 163 | assert recipe["applies"] is True |
| 164 | assert recipe["command"] == ["/usr/local/bin/box-chrome", "--new-window", "https://x.com/login"] |
| 165 | |
| 166 | |
| 167 | # --- SKILL.md recipe contract --------------------------------------------- |
| 168 | |
| 169 | |
| 170 | def _skill(): |
| 171 | return SKILL.read_text() |
| 172 | |
| 173 | |
| 174 | def test_skill_md_references_helper_in_flows_and_repair(): |
| 175 | text = _skill() |
| 176 | # Auto Modal flow, Non-Modal Prose flow, and the repair section each point |
| 177 | # at the helper. |
| 178 | assert text.count("box_chrome_login.py") >= 3 |
| 179 | |
| 180 | |
| 181 | def test_skill_md_documents_18800_launch_and_macbook_skip(): |
| 182 | text = _skill() |
| 183 | assert "SAND_CHROME_REMOTE_DEBUG_PORT=18800" in text |
| 184 | assert "X on Linux / Mac mini (repair)" in text |
| 185 | # The repair recipe is rescoped off Grok Bot (R15): that host has its own |
| 186 | # official-only flow and never launches box-chrome. |
| 187 | assert "X on Linux / Grok Bot / Mac mini" not in text |
| 188 | # MacBook must be told to skip the box-chrome path. |
| 189 | assert "MacBook SKIPS" in text or "MacBook does NOT" in text |
| 190 | |
| 191 | |
| 192 | def test_skill_md_pins_browser_cdp_url_not_raw_cookies(): |
| 193 | text = _skill() |
| 194 | assert "BROWSER_CDP_URL=http://127.0.0.1:18800" in text |
| 195 | # The recipe must forbid writing the raw cookie pair to .env. |
| 196 | assert "never `AUTH_TOKEN`/`CT0`" in text or "Do NOT write `AUTH_TOKEN`" in text |
| 197 |