from __future__ import annotations import html import json import os import shutil import sys import unittest from pathlib import Path from unittest import mock HERE = Path(__file__).resolve().parent sys.path[:0] = [str(HERE.parent), str(HERE)] import hub_e2e_kit as base from hub_e2e_kit import WIDTHS from hub_support import ASK from kvb_orchestrator import batches OUT = Path(os.environ.get("E2E_OUT", "/tmp/bulk-grant-shots")) FILES = ("src/app.py", "src/view.js", "docs/notes.md") ROWS = 80 COMMON = """ const row = (ref) => document.querySelector(`#main .inbox-list > li[data-ref="${ref}"]`); const split = () => document.querySelector("#grant-bar .grant-split"); const menu = () => document.querySelector("#grant-bar details.grant-menu"); const main = () => split() && split().firstElementChild; const count = () => menu() && menu().querySelector("summary").getAttribute("aria-label"); const openMenu = async () => { if (!menu().open) { menu().querySelector("summary").click(); } await until(() => menu().open, 2000); await sleep(100); }; const grantOf = (ref) => row(ref) && row(ref).querySelector(".item-actions button.primary"); const wide = () => document.documentElement.scrollWidth > innerWidth + 1; const switchState = () => [name(main()), count(), document.querySelector("#grant-bar #grant-direct") ? "select" : "direct"]; const storedMode = async (mode) => { const end = Date.now() + 6000; while (Date.now() < end) { const view = await (await fetch("/api/view", {cache: "no-store"})).json(); if (view.approval.mode === mode) { return true; } await sleep(200); } return false; }; const key = (value) => document.body.dispatchEvent(new KeyboardEvent("keydown", {key: value, bubbles: true})); """ DIFF = """(async () => {@HELPERS@@COMMON@ if (!(await until(() => row("@REVIEW@"), 8000))) { return {error: text(document.getElementById("main"))}; } row("@REVIEW@").querySelector(".headline").click(); const stack = await until(() => { const found = document.querySelector("#sheet .review-stack"); return found && all(".review-file", found).length === 3 ? found : null; }, 8000); if (!stack) { return {error: text(document.getElementById("sheet"))}; } const files = () => all(".review-file", stack); const state = () => ({opened: files().map((file) => file.open), marked: all("#sheet .file-entry[aria-current='true']").map((entry) => Number(entry.dataset.index))}); const first = state(); const [one, two] = files(); stack.scrollTop = one.offsetTop + one.offsetHeight - stack.clientHeight + 4; await until(() => files()[1].open, 4000); await sleep(300); const ended = state(); stack.scrollTop = two.offsetTop; await until(() => state().marked[0] === 1, 4000); await sleep(200); const second = state(); document.querySelector('#sheet .file-entry[data-index="2"]').click(); await sleep(400); const three = files()[2]; const atTop = Math.abs(three.getBoundingClientRect().top - stack.getBoundingClientRect().top) <= 2 || stack.scrollTop >= stack.scrollHeight - stack.clientHeight - 1; return {first, ended, second, jumped: {...state(), atTop}, wide: wide()}; })()""" MODE = """(async () => {@HELPERS@@COMMON@ key("Escape"); await until(() => document.getElementById("sheet").hidden, 4000); const before = switchState(); main().click(); const kept = await storedMode("select"); await until(() => switchState()[2] === "select", 4000); return {before, kept, after: switchState(), wide: wide()}; })()""" COLLECT = """(async () => {@HELPERS@@COMMON@ const posted = []; const send = window.fetch.bind(window); window.fetch = (path, init) => { if (["/api/sign", "/api/inbox/approve"].includes(String(path))) { posted.push(String(path)); } return send(path, init); }; if (!(await until(() => row("@REVIEW@") && row("@ASK@") && row("@RUN@") && switchState()[2] === "select", 8000))) { return {error: text(document.getElementById("main"))}; } const restored = switchState(); const counts = []; const settle = async (expected) => { await until(() => count() === expected, 3000); counts.push(count()); }; grantOf("@REVIEW@").click(); await settle("1 selected"); const pale = [grantOf("@REVIEW@").disabled, grantOf("@REVIEW@").classList.contains("picked")]; grantOf("@ASK@").click(); await settle("2 selected"); await openMenu(); all("#grant-bar .grant-picked button").find((button) => name(button).startsWith("Remove feat: three files")).click(); await settle("1 selected"); grantOf("@REVIEW@").click(); await settle("2 selected"); if (document.activeElement) { document.activeElement.blur(); } for (let step = 0; step < 40 && !row("@RUN@").classList.contains("cursor"); step += 1) { const cursor = document.querySelector("#main .inbox-list > li.cursor"); key(cursor && Number(cursor.dataset.index) > Number(row("@RUN@").dataset.index) ? "k" : "j"); await sleep(60); } key("a"); await until(() => count() === "3 selected", 3000); counts.push(count()); const pressed = ["@REVIEW@", "@ASK@", "@RUN@"].map((ref) => [grantOf(ref).getAttribute("aria-pressed"), grantOf(ref).disabled]); await openMenu(); const labels = all("#grant-bar .grant-split button").map(name); return {restored, pale, counts, pressed, labels, posted, wide: wide()}; })()""" SHEET = """(async () => {@HELPERS@@COMMON@ main().click(); await until(() => document.querySelector(".sheet-box .actions"), 4000); const confirm = all(".sheet-box .actions button").find((button) => text(button).startsWith("Grant ")); return {listed: all(".sheet-box ul.mini > li strong").map(text), label: text(confirm), wide: wide()}; })()""" CONFIRM = """(async () => {@HELPERS@@COMMON@ navigator.credentials.get = @SIGNS@ ? navigator.credentials.get.bind(navigator.credentials) : () => Promise.reject(new DOMException("The operator canceled.", "NotAllowedError")); const posted = []; const send = window.fetch.bind(window); window.fetch = (path, init) => { if (["/api/sign", "/api/inbox/approve"].includes(String(path))) { posted.push([String(path), JSON.parse(init.body).items.map((item) => `${item.kind}:${item.id}:${item.verb}`).sort()]); } return send(path, init); }; const shown = new Set(all("#toasts .toast")); all(".sheet-box .actions button").find((button) => text(button).startsWith("Grant ")).click(); const toast = await until(() => all("#toasts .toast").filter((node) => !shown.has(node)).map(text).find((entry) => entry.startsWith("Grant selection")), 15000); await sleep(@SIGNS@ ? 2500 : 300); return {posted, toast, count: count(), wide: wide()}; })()""" DIRECT = """(async () => {@HELPERS@@COMMON@ const posted = []; const send = window.fetch.bind(window); window.fetch = (path, init) => { if (String(path) === "/api/sign") { posted.push(JSON.parse(init.body).items.map((item) => `${item.kind}:${item.id}`)); return Promise.resolve(new Response("held by the e2e", {status: 503})); } return send(path, init); }; await openMenu(); document.querySelector("#grant-bar #grant-direct").click(); const kept = await storedMode("direct"); await until(() => switchState()[2] === "direct", 4000); const spare = await until(() => grantOf("@SPARE@"), 4000); const pressed = spare.getAttribute("aria-pressed"); spare.click(); await until(() => posted.length, 4000); return {kept, after: switchState(), pressed, posted, wide: wide()}; })()""" def probe(template: str, **values: str) -> str: found = template.replace("@HELPERS@", base.HELPERS).replace("@COMMON@", COMMON) for name, value in values.items(): found = found.replace(f"@{name}@", value) return found def review_page() -> str: sections = [] for path in FILES: rows = "".join(f'