from __future__ import annotations import json import os import threading from pathlib import Path import pytest if os.getenv("STACKCHAIN_RUN_RELEASE_E2E") != "1": pytest.skip("packaged Human Gates journey runs only in its gated CI job", allow_module_level=True) pytest.importorskip("playwright.sync_api") from playwright.sync_api import expect, sync_playwright from fake_gitea import FakeGiteaServer from test_mobile_offline_issue_release import ACCESS_TOKEN, ROOT, release_server @pytest.mark.parametrize(("width", "height"), [(320, 568), (390, 844)]) def test_release_artifact_reopens_human_gates_with_one_fresh_mobile_snapshot( tmp_path: Path, width: int, height: int ): archives = sorted((ROOT / "dist").glob("stackchain-dashboard-*.tar.gz")) assert len(archives) == 1, "browser job must download exactly one assembled release archive" fake = FakeGiteaServer(("127.0.0.1", 0)) fake_thread = threading.Thread(target=fake.serve_forever, daemon=True) fake_thread.start() current = {"gate": "g1"} evidence_url = {"value": "/evidence/manifest"} list_requests: list[str] = [] browser_errors: list[str] = [] def gate(gate_id: str) -> dict: return { "id": gate_id, "title": "First candidate" if gate_id == "g1" else "Fresh candidate", "project": "stackchain/stackchain-dashboard", "candidate_hash": "a1" if gate_id == "g1" else "b2", "revision": 1, "priority": 5, "checks": [], "artifacts": [{"name": "Signed manifest", "url": evidence_url["value"]}], "links": [], "provenance": {}, "history": [], } try: with release_server( archives[0], tmp_path, f"http://127.0.0.1:{fake.server_port}" ) as origin, sync_playwright() as playwright: evidence_url["value"] = origin + "/evidence/manifest" browser = playwright.chromium.launch(args=["--ignore-certificate-errors"]) context = browser.new_context( viewport={"width": width, "height": height}, ignore_https_errors=True ) page = context.new_page() page.on("pageerror", lambda error: browser_errors.append(error.stack or str(error))) def human_gates_route(route): path = route.request.url.split("?", 1)[0] if route.request.method == "POST": payload = {"receipt_id": "receipt-1", "state": "released"} elif path.endswith("/api/v1/human-gates"): list_requests.append(current["gate"]) payload = {"pending_count": 1, "items": [gate(current["gate"])]} else: payload = gate(path.rsplit("/", 1)[-1]) route.fulfill(status=200, content_type="application/json", body=json.dumps(payload)) page.route("**/api/v1/human-gates**", human_gates_route) page.route("**/api/v1/human-gates/**", human_gates_route) page.goto(origin + "/", wait_until="networkidle") page.locator('input[name="device_label"]').fill("Human Gates release phone") page.locator('input[name="access_token"]').fill(ACCESS_TOKEN) page.locator("#submit-sign-in").click() page.wait_for_url(origin + "/", wait_until="networkidle") page.evaluate("document.querySelector('#open-human-gates').click()") expect(page.locator("#human-gates")).to_be_visible() expect(page.locator("#human-gate-detail")).to_contain_text("First candidate") page.locator('[data-gate-checklist="exact_hash"]').check() page.locator('[data-gate-checklist="artifacts_reviewed"]').check() page.locator('[data-gate-checklist="provenance_reviewed"]').check() page.locator("[data-gate-reason]").fill("Awaiting final approval") with page.expect_popup() as popup_info: page.get_by_role("link", name="Signed manifest").click() popup = popup_info.value popup.wait_for_load_state("domcontentloaded") assert not page.url.endswith("/evidence/manifest") expect(page.locator("#human-gates")).to_be_visible() expect(page.locator("#human-gate-detail")).to_contain_text("First candidate") popup.close() page.reload(wait_until="networkidle") page.evaluate("document.querySelector('#open-human-gates').click()") expect(page.locator("#human-gates")).to_be_visible() expect(page.locator('[data-gate-checklist="exact_hash"]')).to_be_checked() expect(page.locator('[data-gate-checklist="artifacts_reviewed"]')).to_be_checked() expect(page.locator('[data-gate-checklist="provenance_reviewed"]')).to_be_checked() expect(page.locator("[data-gate-reason]")).to_have_value("Awaiting final approval") progress_key = page.evaluate( "Object.keys(localStorage).find(key => key.startsWith('stackchain.human-gate-review.v1:'))" ) assert progress_key and progress_key.endswith(":g1:1") assert page.evaluate("key => localStorage.getItem(key) !== null", progress_key) page.locator('[data-gate-decision="release"]').click() expect(page.locator("#human-gates-status")).to_contain_text("Decision saved") assert not page.evaluate("key => localStorage.getItem(key) !== null", progress_key) page.evaluate("document.querySelector('#close-human-gates').click()") before_reopen = len(list_requests) current["gate"] = "g2" page.evaluate("document.querySelector('#open-human-gates').click()") expect(page.locator("#human-gate-detail")).to_contain_text("Fresh candidate") expect(page.locator("#human-gates-list")).to_contain_text("Fresh candidate") assert len(list_requests) == before_reopen + 1 assert page.evaluate("window.innerWidth") == width assert page.evaluate("document.documentElement.scrollWidth <= window.innerWidth") bounds = page.locator('[data-human-gate-id="g2"]').bounding_box() assert bounds and bounds["height"] >= 44 assert not browser_errors browser.close() finally: fake.shutdown() fake.server_close() fake_thread.join(timeout=5)