2026-11-27 19:05:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import shutil
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
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_case import HubCase
|
|
|
|
|
from hub_support import ASK, signing
|
|
|
|
|
from kvb_orchestrator import batches, skill_scripts
|
|
|
|
|
|
2026-11-28 01:47:41 +00:00
|
|
|
OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-096-expired-shots"))
|
2026-11-27 19:05:38 +00:00
|
|
|
AGO = 690
|
2026-11-28 01:47:41 +00:00
|
|
|
GONE_MS = 300
|
2026-11-27 19:05:38 +00:00
|
|
|
TIP = re.compile(r"^expired \d{2}:\d{2}, 12 minutes ago$")
|
|
|
|
|
FLOW = """(async () => {@HELPERS@
|
|
|
|
|
const posted = [];
|
|
|
|
|
const send = window.fetch.bind(window);
|
|
|
|
|
window.fetch = async (path, init) => {
|
2026-11-28 01:47:41 +00:00
|
|
|
if (["/api/sign", "/api/inbox/approve", "/api/requests/dismiss"].includes(String(path))) {
|
|
|
|
|
posted.push([String(path), JSON.parse(init.body).items.map((item) => [item.kind, item.target || null, item.id, item.verb || null, item.scope || null, item.channel || null])]);
|
2026-11-27 19:05:38 +00:00
|
|
|
}
|
|
|
|
|
return send(path, init);
|
|
|
|
|
};
|
2026-11-28 01:47:41 +00:00
|
|
|
const sheet = document.getElementById("sheet");
|
|
|
|
|
let opened = false;
|
|
|
|
|
new MutationObserver(() => {
|
|
|
|
|
opened = opened || !sheet.hidden;
|
|
|
|
|
}).observe(sheet, {attributes: true, attributeFilter: ["hidden"]});
|
2026-11-27 19:05:38 +00:00
|
|
|
const card = () => document.querySelector("#main .match-card.front");
|
|
|
|
|
const front = () => (card() ? card().dataset.card : null);
|
|
|
|
|
if (!(await until(card, 8000))) {
|
|
|
|
|
return {error: text(document.getElementById("main"))};
|
|
|
|
|
}
|
|
|
|
|
const key = (name) => document.body.dispatchEvent(new KeyboardEvent("keydown", {key: name, bubbles: true, cancelable: true}));
|
|
|
|
|
const id = front();
|
|
|
|
|
const stamp = card().querySelector(".match-stamp");
|
|
|
|
|
if (!stamp) {
|
|
|
|
|
return {id, stamp: null};
|
|
|
|
|
}
|
|
|
|
|
stamp.dispatchEvent(new MouseEvent("mouseenter"));
|
|
|
|
|
const style = getComputedStyle(stamp);
|
|
|
|
|
const found = {id, stamp: text(stamp), kind: stamp.dataset.stamp, tip: stamp.title, color: style.color, opacity: style.opacity, pointer: style.pointerEvents,
|
|
|
|
|
accept: document.querySelector("#main .match-accept").disabled, reject: document.querySelector("#main .match-reject").disabled};
|
2026-11-28 01:47:41 +00:00
|
|
|
const swipe = async (share) => {
|
|
|
|
|
const head = card().querySelector(".match-head");
|
|
|
|
|
const box = head.getBoundingClientRect();
|
|
|
|
|
const at = (x) => ({bubbles: true, cancelable: true, pointerId: 9, pointerType: "touch", isPrimary: true, button: 0, clientX: x, clientY: box.top + 20});
|
|
|
|
|
head.dispatchEvent(new PointerEvent("pointerdown", at(box.left + box.width / 2)));
|
|
|
|
|
window.dispatchEvent(new PointerEvent("pointermove", at(box.left + box.width * (0.5 + share / 2))));
|
|
|
|
|
const leaned = card().dataset.leaning || "";
|
|
|
|
|
const start = performance.now();
|
|
|
|
|
window.dispatchEvent(new PointerEvent("pointerup", at(box.left + box.width * (0.5 + share))));
|
|
|
|
|
return {leaned, start};
|
|
|
|
|
};
|
|
|
|
|
const right = await swipe(0.45);
|
2026-11-27 19:05:38 +00:00
|
|
|
await sleep(500);
|
2026-11-28 01:47:41 +00:00
|
|
|
found.leaned = right.leaned;
|
2026-11-27 19:05:38 +00:00
|
|
|
found.kept = front() === id;
|
|
|
|
|
found.tally = text(document.querySelector("#main .match-tally"));
|
|
|
|
|
found.wide = document.documentElement.scrollWidth > window.innerWidth + 1;
|
|
|
|
|
if (@RELEASE@) {
|
2026-11-28 01:47:41 +00:00
|
|
|
const left = await swipe(-0.45);
|
|
|
|
|
found.leftLeaned = left.leaned;
|
|
|
|
|
while (front() === id && performance.now() - left.start < 2000) {
|
|
|
|
|
await sleep(10);
|
2026-11-27 19:05:38 +00:00
|
|
|
}
|
2026-11-28 01:47:41 +00:00
|
|
|
found.goneMs = performance.now() - left.start;
|
|
|
|
|
await sleep(400);
|
|
|
|
|
found.sheetAfterSwipe = opened;
|
|
|
|
|
found.dropRows = all("#main .match-decision").map((row) => [row.dataset.card, row.dataset.verdict, text(row.querySelector(".match-verdict")), Boolean(row.querySelector(".match-decision-feedback"))]);
|
|
|
|
|
key("s");
|
|
|
|
|
found.undone = Boolean(await until(() => front() === id, 3000));
|
|
|
|
|
key("ArrowLeft");
|
2026-11-27 19:05:38 +00:00
|
|
|
const overview = await until(() => document.querySelector("#main .match-overview"), 4000);
|
2026-11-28 01:47:41 +00:00
|
|
|
found.sheetAfterKey = opened;
|
|
|
|
|
found.rows = overview ? all("#main .match-decision").map((row) => [row.dataset.card, row.dataset.verdict, text(row.querySelector(".match-verdict")), Boolean(row.querySelector(".match-decision-feedback"))]) : [];
|
2026-11-27 19:05:38 +00:00
|
|
|
const shown = new Set(all("#toasts .toast"));
|
|
|
|
|
document.querySelector("#main .match-release").click();
|
|
|
|
|
found.toast = await until(() => all("#toasts .toast").filter((node) => !shown.has(node)).map(text).find((entry) => entry.startsWith("Release")), 15000);
|
|
|
|
|
found.emptied = Boolean(await until(() => document.querySelector("#main .match-stack .empty"), 15000));
|
|
|
|
|
found.posted = posted;
|
|
|
|
|
}
|
|
|
|
|
return found;
|
|
|
|
|
})()"""
|
2026-11-28 01:47:41 +00:00
|
|
|
AGAIN = """(async () => {@HELPERS@
|
|
|
|
|
await until(() => document.querySelector("#main .match-stack .empty, #main .match-card.front"), 8000);
|
|
|
|
|
const response = await fetch("/api/match", {cache: "no-store"});
|
|
|
|
|
const cards = (await response.json()).cards.map((card) => card.id);
|
|
|
|
|
const front = document.querySelector("#main .match-card.front");
|
|
|
|
|
return {again: true, cards, front: front ? front.dataset.card : null, empty: Boolean(document.querySelector("#main .match-stack .empty"))};
|
|
|
|
|
})()"""
|
2026-11-27 19:05:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
|
|
|
|
|
class TestMatchExpired(HubCase):
|
|
|
|
|
drive = base.OrchestratorE2ECase.drive
|
|
|
|
|
authenticator = base.OrchestratorE2ECase.authenticator
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super().setUp()
|
|
|
|
|
self.claim("s1", "Skills session")
|
|
|
|
|
self.ask = self.create_ask({**ASK, "call_key": signing.call_key("Bash", {"command": ASK["command"]})})
|
|
|
|
|
self.httpd.hub.asks.change(self.ask, {"expires": time.time() - AGO})
|
|
|
|
|
|
|
|
|
|
def radio(self) -> list[list[str]]:
|
|
|
|
|
return [args for script, args in self.scripts.calls if script == skill_scripts.RADIO]
|
|
|
|
|
|
|
|
|
|
def step(self, width: int, height: int, release: bool) -> dict:
|
2026-11-28 01:47:41 +00:00
|
|
|
flow = FLOW.replace("@HELPERS@", base.HELPERS).replace("@RELEASE@", "true" if release else "false")
|
2026-11-27 19:05:38 +00:00
|
|
|
shot = OUT / f"chromium-{width}-{'released' if release else 'expired'}.png"
|
|
|
|
|
return {"width": width, "height": height, "url": f"http://localhost:{self.port}/?{width}-{release}#tasks/match", "wait": 1500, "probe": flow, "shot": str(shot)}
|
|
|
|
|
|
2026-11-28 01:47:41 +00:00
|
|
|
def again(self) -> dict:
|
|
|
|
|
return {"width": 1366, "height": 768, "url": f"http://localhost:{self.port}/?again#tasks/match", "wait": 1500, "probe": AGAIN.replace("@HELPERS@", base.HELPERS), "shot": str(OUT / "chromium-1366-again.png")}
|
|
|
|
|
|
|
|
|
|
def test_chromium_drops_an_expired_ask_silently_and_the_release_dismisses_it_for_every_page(self):
|
2026-11-27 19:05:38 +00:00
|
|
|
OUT.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
steps = [self.step(width, height, False) for width, height in base.WIDTHS] + [self.step(1366, 768, True)]
|
|
|
|
|
with mock.patch.object(batches, "UNDO", 0.3):
|
|
|
|
|
data = self.drive(steps, self.authenticator())
|
2026-11-28 01:47:41 +00:00
|
|
|
self.settle(lambda: self.httpd.hub.dismissals.listed() != [])
|
|
|
|
|
again = self.drive([self.again()], None)
|
|
|
|
|
(OUT / "chromium-expired.json").write_text(json.dumps({"release": data, "again": again}, indent=1), encoding="utf-8")
|
2026-11-27 19:05:38 +00:00
|
|
|
self.assertEqual([error for error in data["errors"] if "(style-src-elem)" not in error and "/favicon.ico" not in error], [])
|
|
|
|
|
self.assertEqual(len(data["results"]), len(steps), data)
|
2026-11-28 01:47:41 +00:00
|
|
|
card = f"ask:{self.ask}"
|
2026-11-27 19:05:38 +00:00
|
|
|
for found in data["results"]:
|
|
|
|
|
with self.subTest(step=found.get("toast") is not None):
|
|
|
|
|
self.assertNotIn("error", found, found)
|
2026-11-28 01:47:41 +00:00
|
|
|
self.assertEqual((found["id"], found["stamp"], found["kind"]), (card, "EXPIRED", "expired"), found)
|
2026-11-27 19:05:38 +00:00
|
|
|
self.assertRegex(found["tip"], TIP)
|
|
|
|
|
self.assertEqual((found["opacity"], found["pointer"]), ("1", "auto"), found)
|
|
|
|
|
channels = [int(part) for part in found["color"].removeprefix("rgb(").removesuffix(")").split(",")[:3]]
|
|
|
|
|
self.assertGreater(max(channels) - min(channels), 20, found)
|
|
|
|
|
self.assertEqual((found["accept"], found["reject"]), (True, False), found)
|
|
|
|
|
self.assertEqual((found["leaned"], found["kept"], found["tally"]), ("", True, "0"), found)
|
|
|
|
|
self.assertFalse(found["wide"], found)
|
|
|
|
|
released = data["results"][-1]
|
2026-11-28 01:47:41 +00:00
|
|
|
self.assertEqual(released["leftLeaned"], "reject", released)
|
|
|
|
|
self.assertLess(released["goneMs"], GONE_MS, released)
|
|
|
|
|
self.assertFalse(released["sheetAfterSwipe"], released)
|
|
|
|
|
self.assertEqual(released["dropRows"], [[card, "drop", "Drop (expired)", False]], released)
|
|
|
|
|
self.assertTrue(released["undone"], released)
|
|
|
|
|
self.assertFalse(released["sheetAfterKey"], released)
|
|
|
|
|
self.assertEqual(released["rows"], [[card, "drop", "Drop (expired)", False]], released)
|
2026-11-27 19:05:38 +00:00
|
|
|
self.assertNotIn("not possible", released["toast"], released)
|
|
|
|
|
self.assertTrue(released["emptied"], released)
|
|
|
|
|
self.assertEqual([path for path, _ in released["posted"]], ["/api/sign", "/api/inbox/approve"], released)
|
2026-11-28 01:47:41 +00:00
|
|
|
self.assertEqual(released["posted"][1][1], [["dismiss", "ask", self.ask, None, "expired", None]], released)
|
|
|
|
|
self.assertEqual(len(again["results"]), 1, again)
|
|
|
|
|
shown = again["results"][0]
|
|
|
|
|
self.assertNotIn(card, shown["cards"], shown)
|
|
|
|
|
self.assertEqual((shown["front"], shown["empty"]), (None, True), shown)
|
|
|
|
|
self.assertEqual(self.httpd.hub.asks.get(self.ask)["status"], "pending")
|
|
|
|
|
self.assertEqual(self.radio(), [])
|
2026-11-27 19:05:38 +00:00
|
|
|
status, _, body = self.get("/api/match")
|
|
|
|
|
self.assertEqual(status, 200, body)
|
2026-11-28 01:47:41 +00:00
|
|
|
self.assertNotIn(card, [entry["id"] for entry in json.loads(body)["cards"]])
|
2026-11-27 19:05:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|