test_updates_api.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. """Integration tests for Updates API endpoints."""
  2. from pathlib import Path
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. import pytest
  5. from httpx import AsyncClient
  6. class TestUpdatesAPI:
  7. @pytest.mark.asyncio
  8. async def test_get_version(self, async_client: AsyncClient):
  9. response = await async_client.get("/api/v1/updates/version")
  10. assert response.status_code == 200
  11. @pytest.mark.asyncio
  12. async def test_apply_update_docker_rejection(self, async_client: AsyncClient):
  13. with (
  14. patch("backend.app.api.routes.updates._is_ha_addon", return_value=False),
  15. patch("backend.app.api.routes.updates._is_docker_environment", return_value=True),
  16. ):
  17. response = await async_client.post("/api/v1/updates/apply")
  18. result = response.json()
  19. assert result["success"] is False
  20. assert result["is_docker"] is True
  21. assert result.get("is_ha_addon") is not True
  22. # Docker message tells the user to docker compose, not HA.
  23. assert "Docker Compose" in result["message"]
  24. @pytest.mark.asyncio
  25. async def test_apply_update_ha_addon_rejection(self, async_client: AsyncClient):
  26. """HA addons are also Docker, so the route must check HA first and
  27. return the HA-specific message — otherwise users see "run docker
  28. compose" advice they can't follow."""
  29. with (
  30. patch("backend.app.api.routes.updates._is_ha_addon", return_value=True),
  31. patch("backend.app.api.routes.updates._is_docker_environment", return_value=True),
  32. ):
  33. response = await async_client.post("/api/v1/updates/apply")
  34. result = response.json()
  35. assert result["success"] is False
  36. assert result["is_ha_addon"] is True
  37. assert result["is_docker"] is True
  38. assert "Home Assistant" in result["message"]
  39. assert "Docker Compose" not in result["message"]
  40. @pytest.mark.asyncio
  41. async def test_apply_update_non_docker(self, async_client: AsyncClient):
  42. """Test non-Docker path - mock _perform_update + _discover_target_release
  43. to prevent side effects (network call to GitHub releases API + actual
  44. git/pip subprocesses)."""
  45. with (
  46. patch("backend.app.api.routes.updates._is_ha_addon", return_value=False),
  47. patch("backend.app.api.routes.updates._is_docker_environment", return_value=False),
  48. patch(
  49. "backend.app.api.routes.updates._discover_target_release",
  50. new_callable=AsyncMock,
  51. return_value="v9.9.9",
  52. ),
  53. patch("backend.app.api.routes.updates._perform_update", new_callable=AsyncMock),
  54. ):
  55. response = await async_client.post("/api/v1/updates/apply")
  56. assert response.json()["success"] is True
  57. def test_is_docker_with_dockerenv(self):
  58. from backend.app.api.routes.updates import _is_docker_environment
  59. with patch("os.path.exists", return_value=True):
  60. assert _is_docker_environment() is True
  61. def test_is_ha_addon_detects_supervisor_token(self):
  62. """HA Supervisor sets SUPERVISOR_TOKEN on every addon container.
  63. That env-var alone is the canonical HA-addon signal."""
  64. from backend.app.api.routes.updates import _is_ha_addon
  65. with patch.dict("os.environ", {"SUPERVISOR_TOKEN": "abc123"}, clear=False):
  66. assert _is_ha_addon() is True
  67. def test_is_ha_addon_false_outside_supervisor(self):
  68. from backend.app.api.routes.updates import _is_ha_addon
  69. with patch.dict("os.environ", {}, clear=True):
  70. assert _is_ha_addon() is False
  71. def test_is_ha_addon_empty_token_treated_as_unset(self):
  72. """An empty string is not a real token — guard against shells that
  73. export the variable empty."""
  74. from backend.app.api.routes.updates import _is_ha_addon
  75. with patch.dict("os.environ", {"SUPERVISOR_TOKEN": ""}, clear=False):
  76. assert _is_ha_addon() is False
  77. @pytest.mark.asyncio
  78. async def test_check_returns_ha_addon_flag_and_method(self, async_client: AsyncClient):
  79. """`/updates/check` must surface the deployment shape so the frontend
  80. can pick the right CTA. HA must take precedence over Docker because
  81. HA addons run *inside* a Docker container — checking docker first
  82. would mis-classify them."""
  83. import httpx as _httpx
  84. fake_release = {
  85. "tag_name": "v999.9.9",
  86. "name": "Far Future Release",
  87. "body": "",
  88. "html_url": "https://example.invalid/r",
  89. "published_at": "2099-01-01T00:00:00Z",
  90. }
  91. class _Resp:
  92. status_code = 200
  93. def raise_for_status(self):
  94. return None
  95. def json(self):
  96. return [fake_release]
  97. class _FakeClient:
  98. async def __aenter__(self):
  99. return self
  100. async def __aexit__(self, *_):
  101. return None
  102. async def get(self, *_, **__):
  103. return _Resp()
  104. with (
  105. patch.object(_httpx, "AsyncClient", _FakeClient),
  106. patch("backend.app.api.routes.updates._is_ha_addon", return_value=True),
  107. patch("backend.app.api.routes.updates._is_docker_environment", return_value=True),
  108. ):
  109. response = await async_client.get("/api/v1/updates/check")
  110. body = response.json()
  111. assert body["is_ha_addon"] is True
  112. assert body["update_method"] == "ha_addon"
  113. # is_docker is preserved alongside so older frontend bundles still
  114. # hit a managed-deployment branch (degrades to Docker UX) instead of
  115. # rendering the in-app Install button.
  116. assert body["is_docker"] is True
  117. @pytest.mark.asyncio
  118. async def test_check_docker_only_returns_docker_method(self, async_client: AsyncClient):
  119. import httpx as _httpx
  120. fake_release = {
  121. "tag_name": "v999.9.9",
  122. "name": "Far Future Release",
  123. "body": "",
  124. "html_url": "https://example.invalid/r",
  125. "published_at": "2099-01-01T00:00:00Z",
  126. }
  127. class _Resp:
  128. status_code = 200
  129. def raise_for_status(self):
  130. return None
  131. def json(self):
  132. return [fake_release]
  133. class _FakeClient:
  134. async def __aenter__(self):
  135. return self
  136. async def __aexit__(self, *_):
  137. return None
  138. async def get(self, *_, **__):
  139. return _Resp()
  140. with (
  141. patch.object(_httpx, "AsyncClient", _FakeClient),
  142. patch("backend.app.api.routes.updates._is_ha_addon", return_value=False),
  143. patch("backend.app.api.routes.updates._is_docker_environment", return_value=True),
  144. ):
  145. response = await async_client.get("/api/v1/updates/check")
  146. body = response.json()
  147. assert body["is_ha_addon"] is False
  148. assert body["is_docker"] is True
  149. assert body["update_method"] == "docker"
  150. @pytest.mark.asyncio
  151. async def test_check_backs_off_after_github_rate_limit(self, async_client: AsyncClient):
  152. """#1420: once GitHub returns 403 with X-RateLimit-Remaining=0, the
  153. next call must short-circuit on the backoff window instead of hitting
  154. api.github.com again. Otherwise the user's logs flood with rate-limit
  155. errors and Bambuddy keeps adding to whatever throttle GitHub applies."""
  156. import time
  157. import httpx as _httpx
  158. import backend.app.api.routes.updates as updates_module
  159. # Reset module-level backoff state between tests.
  160. updates_module._github_rate_limit_until = 0.0
  161. # Future reset time, ~10 minutes ahead — the backoff window we expect.
  162. future_reset = time.time() + 600
  163. class _RateLimitedResp:
  164. status_code = 403
  165. headers = {
  166. "X-RateLimit-Remaining": "0",
  167. "X-RateLimit-Reset": str(int(future_reset)),
  168. }
  169. text = "API rate limit exceeded"
  170. def raise_for_status(self):
  171. raise _httpx.HTTPStatusError("403", request=None, response=self)
  172. def json(self):
  173. return {"message": "API rate limit exceeded"}
  174. call_counter = {"n": 0}
  175. class _FakeClient:
  176. async def __aenter__(self):
  177. return self
  178. async def __aexit__(self, *_):
  179. return None
  180. async def get(self, *_, **__):
  181. call_counter["n"] += 1
  182. return _RateLimitedResp()
  183. try:
  184. with patch.object(_httpx, "AsyncClient", _FakeClient):
  185. first = await async_client.get("/api/v1/updates/check")
  186. second = await async_client.get("/api/v1/updates/check")
  187. finally:
  188. updates_module._github_rate_limit_until = 0.0
  189. # First request reached httpx; second short-circuited on the backoff.
  190. assert call_counter["n"] == 1
  191. first_body = first.json()
  192. second_body = second.json()
  193. assert "rate limit" in (first_body.get("error") or "").lower()
  194. assert "rate limit" in (second_body.get("error") or "").lower()
  195. # Backoff window roughly matches the X-RateLimit-Reset header.
  196. assert second_body.get("retry_after_seconds", 0) > 0
  197. def test_parse_version(self):
  198. from backend.app.api.routes.updates import parse_version
  199. assert parse_version("0.1.5")[:3] == (0, 1, 5)
  200. def test_is_newer_version(self):
  201. from backend.app.api.routes.updates import is_newer_version
  202. assert is_newer_version("0.1.5", "0.1.5b7") is True
  203. def test_parse_github_remote_recognises_ssh_https_and_dotgit(self):
  204. """`_parse_github_remote` must accept the four canonical forms `git
  205. remote -v` prints; anything else returns None so callers can treat
  206. it as 'reset to expected URL'."""
  207. from backend.app.api.routes.updates import _parse_github_remote
  208. assert _parse_github_remote("git@github.com:maziggy/bambuddy.git") == (
  209. "maziggy",
  210. "bambuddy",
  211. )
  212. assert _parse_github_remote("git@github.com:maziggy/bambuddy") == (
  213. "maziggy",
  214. "bambuddy",
  215. )
  216. assert _parse_github_remote("https://github.com/maziggy/bambuddy.git") == (
  217. "maziggy",
  218. "bambuddy",
  219. )
  220. assert _parse_github_remote("https://github.com/maziggy/bambuddy") == (
  221. "maziggy",
  222. "bambuddy",
  223. )
  224. # Non-GitHub host → None (we don't claim ownership over arbitrary
  225. # forge URLs).
  226. assert _parse_github_remote("git@gitlab.com:maziggy/bambuddy.git") is None
  227. # Empty / malformed → None.
  228. assert _parse_github_remote("") is None
  229. assert _parse_github_remote("not-a-url") is None
  230. assert _parse_github_remote("https://github.com/maziggy") is None # no /repo
  231. @pytest.mark.asyncio
  232. async def test_perform_update_preserves_ssh_origin_when_pointing_at_correct_repo(self, tmp_path):
  233. """Regression for the developer-checkout footgun: if origin already
  234. points at github.com/maziggy/bambuddy via SSH, the updater must
  235. leave it alone instead of clobbering it with HTTPS. Pre-fix, every
  236. Apply Update click rewrote `git@github.com:...` to `https://...`,
  237. breaking subsequent `git push` for any developer testing the
  238. upgrade flow against their own checkout."""
  239. from backend.app.api.routes import updates as updates_module
  240. app_dir = tmp_path / "app"
  241. data_dir = tmp_path / "app" / "data"
  242. app_dir.mkdir()
  243. data_dir.mkdir()
  244. (app_dir / "requirements.txt").write_text("fastapi\n")
  245. calls: list[dict] = []
  246. async def fake_create_subprocess_exec(*args, **kwargs):
  247. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  248. proc = MagicMock()
  249. # When the updater asks `git remote get-url origin`, return the
  250. # SSH URL. Every other subprocess returns successfully with no
  251. # output.
  252. if "get-url" in args and "origin" in args:
  253. proc.communicate = AsyncMock(return_value=(b"git@github.com:maziggy/bambuddy.git\n", b""))
  254. else:
  255. proc.communicate = AsyncMock(return_value=(b"", b""))
  256. proc.returncode = 0
  257. return proc
  258. with (
  259. patch.object(updates_module.settings, "base_dir", data_dir),
  260. patch.object(updates_module.settings, "app_dir", app_dir),
  261. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  262. patch.object(
  263. updates_module.asyncio,
  264. "create_subprocess_exec",
  265. side_effect=fake_create_subprocess_exec,
  266. ),
  267. ):
  268. await updates_module._perform_update("v0.2.4b1")
  269. # The updater MUST NOT have run `git remote set-url origin <https>`
  270. # because origin already pointed at the right repo over SSH.
  271. set_url_calls = [c for c in calls if "set-url" in c["args"] and "origin" in c["args"]]
  272. assert not set_url_calls, (
  273. "Updater clobbered an SSH origin pointing at the correct repo. "
  274. "Captured set-url calls: " + repr([c["args"] for c in set_url_calls])
  275. )
  276. @pytest.mark.asyncio
  277. async def test_perform_update_resets_origin_when_pointing_elsewhere(self, tmp_path):
  278. """Defensive: if origin points at a fork or unrelated repo (or is
  279. missing), the updater should still rewrite it to the canonical
  280. HTTPS URL so subsequent fetch / reset works against the right
  281. repo. This is the original behaviour that the SSH-preservation
  282. fix above must NOT regress."""
  283. from backend.app.api.routes import updates as updates_module
  284. from backend.app.core.config import GITHUB_REPO
  285. app_dir = tmp_path / "app"
  286. data_dir = tmp_path / "app" / "data"
  287. app_dir.mkdir()
  288. data_dir.mkdir()
  289. (app_dir / "requirements.txt").write_text("fastapi\n")
  290. calls: list[dict] = []
  291. async def fake_create_subprocess_exec(*args, **kwargs):
  292. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  293. proc = MagicMock()
  294. # origin is set to a fork — must be rewritten.
  295. if "get-url" in args and "origin" in args:
  296. proc.communicate = AsyncMock(return_value=(b"git@github.com:somefork/bambuddy.git\n", b""))
  297. else:
  298. proc.communicate = AsyncMock(return_value=(b"", b""))
  299. proc.returncode = 0
  300. return proc
  301. with (
  302. patch.object(updates_module.settings, "base_dir", data_dir),
  303. patch.object(updates_module.settings, "app_dir", app_dir),
  304. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  305. patch.object(
  306. updates_module.asyncio,
  307. "create_subprocess_exec",
  308. side_effect=fake_create_subprocess_exec,
  309. ),
  310. ):
  311. await updates_module._perform_update("v0.2.4b1")
  312. set_url_calls = [c for c in calls if "set-url" in c["args"] and "origin" in c["args"]]
  313. assert set_url_calls, "Updater must rewrite origin when it points at a fork."
  314. rewritten_to = set_url_calls[0]["args"][-1]
  315. assert rewritten_to == f"https://github.com/{GITHUB_REPO}.git", (
  316. f"Expected origin to be reset to canonical HTTPS URL; got: {rewritten_to}"
  317. )
  318. @pytest.mark.asyncio
  319. async def test_perform_update_resets_to_target_ref_not_hardcoded_main(self, tmp_path):
  320. """Regression for the hardcoded-`origin/main` limitation: the in-app
  321. updater must reset to the caller-supplied target ref (typically a
  322. release tag like `v0.2.4b1` discovered from the GitHub releases API)
  323. so beta releases that don't live on main can actually be installed.
  324. Pre-fix, `_perform_update` issued `git reset --hard origin/main`
  325. verbatim and silently no-op'd whenever the latest release wasn't on
  326. main — leaving a 0.2.3.x user clicking *Apply Update* stranded on
  327. 0.2.3.x. Also asserts the fetch step uses `--tags` so a tag ref is
  328. actually resolvable post-fetch."""
  329. from backend.app.api.routes import updates as updates_module
  330. app_dir = tmp_path / "app"
  331. data_dir = tmp_path / "app" / "data"
  332. app_dir.mkdir()
  333. data_dir.mkdir()
  334. (app_dir / "requirements.txt").write_text("fastapi\n")
  335. calls: list[dict] = []
  336. async def fake_create_subprocess_exec(*args, **kwargs):
  337. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  338. proc = MagicMock()
  339. if "get-url" in args and "origin" in args:
  340. proc.communicate = AsyncMock(return_value=(b"git@github.com:maziggy/bambuddy.git\n", b""))
  341. else:
  342. proc.communicate = AsyncMock(return_value=(b"", b""))
  343. proc.returncode = 0
  344. return proc
  345. with (
  346. patch.object(updates_module.settings, "base_dir", data_dir),
  347. patch.object(updates_module.settings, "app_dir", app_dir),
  348. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  349. patch.object(
  350. updates_module.asyncio,
  351. "create_subprocess_exec",
  352. side_effect=fake_create_subprocess_exec,
  353. ),
  354. ):
  355. await updates_module._perform_update("v0.2.4b1")
  356. # Reset target must be the caller-supplied ref, not "origin/main".
  357. reset_calls = [c for c in calls if "reset" in c["args"] and "--hard" in c["args"]]
  358. assert reset_calls, "git reset must be invoked"
  359. reset_target = reset_calls[0]["args"][-1]
  360. assert reset_target == "v0.2.4b1", (
  361. f"Expected reset target to be the caller-supplied ref 'v0.2.4b1'; "
  362. f"got {reset_target!r}. Regression to a hardcoded 'origin/main' "
  363. "would re-introduce the in-app-updater-can't-install-betas bug."
  364. )
  365. # Fetch must include --tags so v0.2.4b1 (a tag) is locally resolvable.
  366. fetch_calls = [c for c in calls if "fetch" in c["args"]]
  367. assert fetch_calls
  368. assert "--tags" in fetch_calls[0]["args"], (
  369. "Fetch must use --tags so release-tag refs (the production path "
  370. "for tag-based updates) are resolvable for the subsequent reset. "
  371. f"Captured fetch call: {fetch_calls[0]['args']}"
  372. )
  373. @pytest.mark.asyncio
  374. async def test_apply_update_passes_discovered_release_to_perform_update(self, async_client: AsyncClient):
  375. """End-to-end glue: the route handler calls `_discover_target_release`
  376. to pick the tag (respecting include_beta_updates), then schedules
  377. `_perform_update` with that tag — not with no arg, not with main."""
  378. from backend.app.api.routes import updates as updates_module
  379. captured_ref: list[str] = []
  380. async def fake_perform_update(target_ref):
  381. captured_ref.append(target_ref)
  382. async def fake_discover(_db):
  383. return "v0.2.4b1"
  384. with (
  385. patch.object(updates_module, "_is_ha_addon", return_value=False),
  386. patch.object(updates_module, "_is_docker_environment", return_value=False),
  387. patch.object(updates_module, "_perform_update", side_effect=fake_perform_update),
  388. patch.object(updates_module, "_discover_target_release", side_effect=fake_discover),
  389. ):
  390. response = await async_client.post("/api/v1/updates/apply")
  391. assert response.json()["success"] is True
  392. assert captured_ref == ["v0.2.4b1"], (
  393. f"apply_update must pass the discovered tag to _perform_update; captured invocations: {captured_ref}"
  394. )
  395. @pytest.mark.asyncio
  396. async def test_apply_update_returns_clear_error_when_no_release_resolves(self, async_client: AsyncClient):
  397. """If GitHub is unreachable or no release matches the user's channel,
  398. the route returns a useful error instead of silently kicking off an
  399. update that can't possibly land. Avoids the previous failure mode
  400. where in-app update appeared to succeed but did nothing."""
  401. from backend.app.api.routes import updates as updates_module
  402. async def fake_discover(_db):
  403. return None
  404. # The route guards against a concurrent update via the module-global
  405. # `_update_status` — reset it so a previous test that left the status
  406. # mid-flight doesn't short-circuit this one.
  407. updates_module._update_status = {"status": "idle", "progress": 0, "message": "", "error": None}
  408. with (
  409. patch.object(updates_module, "_is_ha_addon", return_value=False),
  410. patch.object(updates_module, "_is_docker_environment", return_value=False),
  411. patch.object(updates_module, "_discover_target_release", side_effect=fake_discover),
  412. ):
  413. response = await async_client.post("/api/v1/updates/apply")
  414. body = response.json()
  415. assert body["success"] is False
  416. assert "release" in body["message"].lower()
  417. @pytest.mark.asyncio
  418. async def test_perform_update_runs_pip_in_app_dir_not_data_dir(self, tmp_path):
  419. """Native install: `requirements.txt` lives at INSTALL_PATH (the source-
  420. code dir), NOT at DATA_DIR (where systemd sets DATA_DIR=INSTALL_PATH/data).
  421. Pre-fix, the updater ran `pip install -r requirements.txt` with
  422. `cwd=settings.base_dir`, which on a native install resolves to the data
  423. dir — `requirements.txt` isn't there and pip fails with `Could not open
  424. requirements file`. The fix: pip's cwd is `settings.app_dir` (the source
  425. tree) so it can actually find the file.
  426. This test mocks every subprocess so it can capture the cwd of each call
  427. and assert that the pip step runs in app_dir while git steps continue
  428. to run in base_dir (their existing behaviour — git walks up to find
  429. `.git` so that path keeps working)."""
  430. from backend.app.api.routes import updates as updates_module
  431. # Set up fake install layout: app_dir has requirements.txt, data_dir is
  432. # a sibling (mirroring `INSTALL_PATH=/opt/bambuddy`, `DATA_DIR=/opt/bambuddy/data`).
  433. app_dir = tmp_path / "app"
  434. data_dir = tmp_path / "app" / "data"
  435. app_dir.mkdir()
  436. data_dir.mkdir()
  437. (app_dir / "requirements.txt").write_text("fastapi\n")
  438. # Capture every subprocess call's cwd + the executable token.
  439. calls: list[dict] = []
  440. async def fake_create_subprocess_exec(*args, **kwargs):
  441. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  442. proc = MagicMock()
  443. proc.communicate = AsyncMock(return_value=(b"", b""))
  444. proc.returncode = 0
  445. return proc
  446. with (
  447. patch.object(updates_module.settings, "base_dir", data_dir),
  448. patch.object(updates_module.settings, "app_dir", app_dir),
  449. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  450. patch.object(
  451. updates_module.asyncio,
  452. "create_subprocess_exec",
  453. side_effect=fake_create_subprocess_exec,
  454. ),
  455. ):
  456. await updates_module._perform_update("v0.2.4b1")
  457. # Find the pip invocation (sys.executable + "-m" + "pip" + "install").
  458. pip_calls = [c for c in calls if "pip" in c["args"] and "install" in c["args"]]
  459. assert pip_calls, "pip install was never invoked. Captured: " + repr([c["args"] for c in calls])
  460. pip_cwd = pip_calls[0]["cwd"]
  461. assert pip_cwd == str(app_dir), (
  462. f"pip install must run in app_dir ({app_dir}) so it finds "
  463. f"requirements.txt; got cwd={pip_cwd}. Regression to base_dir "
  464. f"breaks every native-install upgrade."
  465. )
  466. # Sanity check: the requirements.txt that pip would read actually exists
  467. # at the captured cwd. If this fails the cwd is wrong even if it isn't
  468. # base_dir — useful diagnostic if someone refactors path handling.
  469. assert (Path(pip_cwd) / "requirements.txt").exists()