From 3583ff2a05ac174296ac95e8046600f945d73229 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 12 Nov 2026 23:14:42 +0000 Subject: [PATCH] feat(orchestrator): speaking bubble in front, faces in the ancestry view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bubble whose speaker is talking gets z-index 1 while it is not leaving, so it is always the front one. With look.faces on, the ancestry view draws the face of the Operator, Chair and Session nodes (r13 ring, data-face); subagents keep their circles. anchorOf intersects a bubble's anchor with its clipping ancestors, so a bubble never points at a hidden part of a node. Unit tests, a clip mutant and an e2e run with screenshots cover it. ๐ŸŽฏ Fix: 95 % ๐Ÿ’ฅ Break: 5 % ๐Ÿ’ธ Cost: 100 % โฑ๏ธ Time: 100 % ๐Ÿ›ก๏ธ Security: 100 % ๐Ÿ† Score: 90 ๐Ÿšฆ Gate: โœ… 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 --- orchestrator/templates/page-ancestry.js | 27 ++- orchestrator/templates/page-bubbles.js | 26 ++- orchestrator/templates/style-bubbles.css | 1 + tests/e2e/test_hub_bubble_faces_e2e.py | 232 +++++++++++++++++++++++ tests/test_hub_bubble_front.py | 186 ++++++++++++++++++ tests/test_hub_spawn_faces.py | 90 +++++++++ 6 files changed, 551 insertions(+), 11 deletions(-) create mode 100644 tests/e2e/test_hub_bubble_faces_e2e.py create mode 100644 tests/test_hub_bubble_front.py create mode 100644 tests/test_hub_spawn_faces.py diff --git a/orchestrator/templates/page-ancestry.js b/orchestrator/templates/page-ancestry.js index 9a19e40..4131e57 100644 --- a/orchestrator/templates/page-ancestry.js +++ b/orchestrator/templates/page-ancestry.js @@ -1,5 +1,6 @@ const gateStage = {items: [], drag: null}; const DRAG_SLOP = 6; + const AGENT_R = {dot: 9, face: 13}; function stageGate(key, change) { gateStage.items = gateStage.items.filter((item) => item.key !== key); @@ -140,16 +141,22 @@ }; return ancestryChart(plan, "Ancestry: operator, chair, sessions, subagents", { edge: (link, d) => s("path", {class: "ancestry-edge", d, "data-child": link.child, "data-parent": link.parent}), - node: (node) => s("g", { - class: `node ancestry-node k-${node.kind}`, transform: `translate(${fixed(node.x)} ${fixed(node.y)})`, tabindex: 0, role: "button", "aria-label": node.label, - "data-agent": node.id, "data-kind": node.kind, "data-generation": node.generation, - on: {click: () => open(node), keydown: (event) => { - if (event.key === "Enter" || event.key === " ") { - event.preventDefault(); - open(node); - } - }}, - }, s("title", {}, `${node.label} ยท ${NODE_KINDS[node.kind] || "Subagent"}`), s("circle", {r: 9}), s("text", {class: "label", y: 26, "text-anchor": "middle"}, short(node.label).slice(0, 24))), + node: (node) => { + const faced = look.faces && Object.hasOwn(TOP, node.kind); + const r = faced ? AGENT_R.face : AGENT_R.dot; + return s("g", { + class: `node ancestry-node k-${node.kind}`, transform: `translate(${fixed(node.x)} ${fixed(node.y)})`, tabindex: 0, role: "button", "aria-label": node.label, + "data-agent": node.id, "data-kind": node.kind, "data-generation": node.generation, "data-face": faced ? node.id : null, + on: {click: () => open(node), keydown: (event) => { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + open(node); + } + }}, + }, s("title", {}, `${node.label} ยท ${NODE_KINDS[node.kind] || "Subagent"}`), s("circle", {r}), + faced ? s("g", {class: "face-art", transform: `translate(${-r} ${-r}) scale(${(2 * r) / Faces.SIZE})`}, faceParts(node.id)) : null, + s("text", {class: "label", y: r + 17, "text-anchor": "middle"}, short(node.label).slice(0, 24))); + }, }); } diff --git a/orchestrator/templates/page-bubbles.js b/orchestrator/templates/page-bubbles.js index ffad52c..876be1a 100644 --- a/orchestrator/templates/page-bubbles.js +++ b/orchestrator/templates/page-bubbles.js @@ -23,12 +23,36 @@ return box.width > 0 && box.height > 0 && box.left + box.width > 0 && box.top + box.height > 0 && box.left < window.innerWidth && box.top < window.innerHeight; } + function clipped(box, frame) { + const left = Math.max(box.left, frame.left); + const top = Math.max(box.top, frame.top); + return {left, top, width: Math.min(box.left + box.width, frame.left + frame.width) - left, height: Math.min(box.top + box.height, frame.top + frame.height) - top}; + } + + function clipsOf(node) { + const frames = []; + for (let at = node; at.parentElement && at.parentElement !== document.body && getComputedStyle(at).position !== "fixed"; at = at.parentElement) { + const style = getComputedStyle(at.parentElement); + if (style.display !== "contents" && style.display !== "inline" && (style.overflowX !== "visible" || style.overflowY !== "visible")) { + frames.push(at.parentElement.getBoundingClientRect()); + } + } + return frames; + } + function anchorOf(key) { const projected = g3.tab === "agents" ? anchor3d(key)() : null; if (projected && inView(projected)) { return projected; } - return [...document.querySelectorAll(`[data-face="${CSS.escape(key)}"]`)].map((node) => node.getBoundingClientRect()).find(inView) || null; + for (const node of document.querySelectorAll(`[data-face="${CSS.escape(key)}"]`)) { + const box = node.getBoundingClientRect(); + const shown = inView(box) ? clipsOf(node).reduce(clipped, box) : null; + if (shown && inView(shown)) { + return shown; + } + } + return null; } function bubbleNode(item) { diff --git a/orchestrator/templates/style-bubbles.css b/orchestrator/templates/style-bubbles.css index acaaad6..964d185 100644 --- a/orchestrator/templates/style-bubbles.css +++ b/orchestrator/templates/style-bubbles.css @@ -6,6 +6,7 @@ .speech-bubble::before { margin-left: -9px; border-width: 12px 9px 0; border-top-color: var(--text); } .speech-bubble::after { margin-left: -6px; border-width: 8px 6px 0; border-top-color: var(--panel); } .speech-bubble.leaving { opacity: 0; } +.speech-bubble[data-state="speaking"]:not(.leaving) { z-index: 1; } .bubble-words { display: block; max-height: calc(6 * 1.35em); overflow: hidden; overflow-wrap: anywhere; scroll-behavior: smooth; } .speech-bubble[data-state="waiting"] .bubble-words { letter-spacing: .12em; } .bubble-who { display: none; color: var(--muted); font: 650 var(--fs-xs)/1.2 var(--sans); } diff --git a/tests/e2e/test_hub_bubble_faces_e2e.py b/tests/e2e/test_hub_bubble_faces_e2e.py new file mode 100644 index 0000000..79bc574 --- /dev/null +++ b/tests/e2e/test_hub_bubble_faces_e2e.py @@ -0,0 +1,232 @@ +from __future__ import annotations + +import json +import os +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 test_orchestrator_e2e as base +from hub_case import HubCase +from hub_support import iso + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-bubble-faces-shots")) +WIDTH, HEIGHT = 1366, 768 +CUE = "front" +SPEAKERS = ("Skills session", "Stage", "Docs") +SAID = "disk almost full on the build host so rotate the logs and retry the gate before the next push" +SPAWN = """ + const agentChart = () => document.querySelector('#main svg.ancestry[aria-label="Ancestry: operator, chair, sessions, subagents"]'); + const agentNodes = () => all("g.ancestry-node", agentChart()).map((node) => { + const art = node.querySelector(".face-art"); + return {agent: node.dataset.agent, kind: node.dataset.kind, face: node.dataset.face || null, r: Number(node.querySelector("circle").getAttribute("r")), art: art ? art.innerHTML : null}; + }); +""" +SPAWN_ON = """(async () => {@HELPERS@@SPAWN@ + const ready = await until(() => agentChart() && agentChart().querySelector('g.ancestry-node[data-kind="twig"]'), 10000); + if (!ready) { + return {error: "no agent ancestry", main: (text(document.getElementById("main")) || "").slice(0, 300)}; + } + agentChart().scrollIntoView({block: "center"}); + await sleep(300); + return {nodes: agentNodes()}; +})()""" +GRAPH = """(async () => {@HELPERS@ + await until(() => all("#main svg.chart .node[data-face] .face-art").length, 8000); + return {faces: Object.fromEntries(all("#main svg.chart .node[data-face]").map((node) => [node.dataset.face, node.querySelector(".face-art").innerHTML]))}; +})()""" +SPAWN_OFF = """(async () => {@HELPERS@@SPAWN@ + await until(() => agentChart(), 8000); + const box = document.querySelector('#main input[data-look="faces"]'); + if (!box) { + return {error: "no faces switch"}; + } + box.click(); + const plain = await until(() => agentChart() && agentChart().querySelector("g.ancestry-node") && !agentChart().querySelector(".face-art"), 6000); + agentChart().scrollIntoView({block: "center"}); + await sleep(300); + return {switched: Boolean(plain), checked: box.checked, nodes: agentNodes()}; +})()""" +FRONT = """ + const bubbles = () => all("#bubbles .speech-bubble:not(.leaving)"); + const speaker = () => bubbles().find((node) => node.dataset.state === "speaking") || null; + const where = (node) => (node.parentElement.classList.contains("bubble-bar") ? "bar" : "layer"); + const middle = () => [Math.round(innerWidth / 2) - 13, Math.round(innerHeight / 2)]; + const pin = (key, x, y) => { + let node = all("[data-e2e-pin]").find((item) => item.dataset.e2ePin === key); + if (!node) { + node = document.createElement("span"); + node.dataset.e2ePin = key; + node.dataset.face = key; + Object.assign(node.style, {position: "fixed", width: "26px", height: "26px", borderRadius: "50%", background: "#9aa4b1"}); + document.body.prepend(node); + } + Object.assign(node.style, {left: `${x}px`, top: `${y}px`}); + }; + const unpin = () => all("[data-e2e-pin]").forEach((node) => node.remove()); + const front = (x, y) => { + const hit = document.elementFromPoint(x, y); + const bubble = hit ? hit.closest(".speech-bubble") : null; + return bubble ? bubble.dataset.who : null; + }; + const inside = (box, x, y) => x > box.left && x < box.right && y > box.top && y < box.bottom; + const stacked = (scene) => { + const layer = document.getElementById("bubbles"); + layer.style.pointerEvents = "auto"; + const top = speaker(); + const others = bubbles().filter((node) => node !== top); + const box = top.getBoundingClientRect(); + const overlaps = others.flatMap((node) => { + const other = node.getBoundingClientRect(); + const [left, right, upper, lower] = [Math.max(box.left, other.left), Math.min(box.right, other.right), Math.max(box.top, other.top), Math.min(box.bottom, other.bottom)]; + return right - left > 4 && lower - upper > 4 ? [{other: node.dataset.who, where: [where(top), where(node)], front: front((left + right) / 2, (upper + lower) / 2)}] : []; + }); + const tip = [box.left + box.width / 2 + parseFloat(getComputedStyle(top).getPropertyValue("--tail") || "0"), box.bottom + 4]; + const under = where(top) === "layer" ? others.find((node) => inside(node.getBoundingClientRect(), ...tip)) : null; + const found = { + scene, speaker: top.dataset.who, overlaps, tail: under ? {under: under.dataset.who, front: front(...tip)} : null, + bubbles: bubbles().map((node) => [node.dataset.who, node.dataset.state, where(node), getComputedStyle(node).zIndex]), + }; + layer.style.removeProperty("pointer-events"); + return found; + }; +""" +SAME = """(async () => {@HELPERS@@FRONT@ + await screenShown(8000); + await sleep(800); + await fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: "@CUE@", voice: ""}), cache: "no-store"}); + if (!(await until(() => bubbles().length === 3 && speaker(), 30000))) { + return {error: "no bubbles", shown: bubbles().map((node) => [node.dataset.who, node.dataset.state])}; + } + const first = speaker().dataset.who; + window.e2eFirst = first; + const [x, y] = middle(); + pin(first, x, y); + bubbles().map((node) => node.dataset.who).filter((key) => key !== first).forEach((key, index) => pin(key, x + index * 34, y + 22 + index * 20)); + await sleep(1500); + return stacked("same"); +})()""" +BAR = """(async () => {@HELPERS@@FRONT@ + if (!speaker() || speaker().dataset.who !== window.e2eFirst) { + return {error: "the first speaker ended before the bar scene"}; + } + unpin(); + await sleep(400); + const inBar = bubbles().filter((node) => where(node) === "bar"); + if (!inBar.length) { + return {error: "no bubble in the bar", shown: bubbles().map((node) => [node.dataset.who, where(node)])}; + } + const host = where(speaker()) === "bar" ? speaker() : inBar[0]; + const movers = where(speaker()) === "bar" ? bubbles().filter((node) => node !== host).map((node) => node.dataset.who) : [speaker().dataset.who]; + for (let round = 0; round < 2; round += 1) { + const box = host.getBoundingClientRect(); + movers.forEach((key, index) => pin(key, box.left + box.width / 2 - 13 + index * 30, box.top + 14)); + await sleep(400); + } + return stacked("bar"); +})()""" +NEXT = """(async () => {@HELPERS@@FRONT@ + const changed = await until(() => speaker() && speaker().dataset.who !== window.e2eFirst && !document.querySelector("#bubbles .speech-bubble.leaving"), 25000); + if (!changed) { + return {error: "the speaker did not change", shown: bubbles().map((node) => [node.dataset.who, node.dataset.state])}; + } + const next = speaker().dataset.who; + unpin(); + const [x, y] = middle(); + pin(next, x, y); + bubbles().map((node) => node.dataset.who).filter((key) => key !== next).forEach((key, index) => pin(key, x + index * 34, y + 22 + index * 20)); + await sleep(1500); + return {...stacked("next"), first: window.e2eFirst}; +})()""" + + +def probe(template: str) -> str: + return template.replace("@HELPERS@", base.HELPERS).replace("@SPAWN@", SPAWN).replace("@FRONT@", FRONT).replace("@CUE@", CUE) + + +@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") +class TestHubBubbleFrontAndSpawnFaces(HubCase): + drive = base.TestOrchestratorE2E.drive + subagents = base.TestOrchestratorE2E.subagents + + def setUp(self): + super().setUp() + OUT.mkdir(parents=True, exist_ok=True) + now = time.time() + self.claim("s1", "Skills session") + self.write_council({ + "work/s2/claim.json": {"office": "work/s2", "session": "s2", "runtime": "claude", "name": "Stage", "since": iso(now - 7200), "beat": now - 60, "work": "x"}, + "chair/claim.json": {"office": "chair", "session": "s1", "runtime": "claude", "name": "Skills session", "since": iso(now - 7200), "beat": now, "work": "allocate"}, + }) + self.subagents(now) + hub = self.httpd.hub + hub.settings.put({**hub.settings.get(), "bubbles": True, "speech": {"on": False, "classes": ["mayday", "security"], "muted": []}}, hub.settings.get()) + + def synthesize(body: bytes) -> bytes: + if json.loads(body)["text"] == CUE: + rows = [{"ts": time.time(), "from": name, "to": [], "text": f"{name}: {SAID}, {SAID}", "class": "security", "v": 2} for name in SPEAKERS] + with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle: + handle.write(self.jsonl(rows)) + return base.tone(1, 8000) + + patch = mock.patch.object(hub.speech, "synthesize", synthesize) + patch.start() + self.addCleanup(patch.stop) + self.base = f"http://{self.host}/" + + def steps(self) -> list[dict]: + shot = str(OUT / f"chromium-{WIDTH}") + page = {"width": WIDTH, "height": HEIGHT} + return [ + {**page, "url": f"{self.base}?faces#agents/tree", "wait": 1200, "probe": probe(SPAWN_ON), "shot": f"{shot}-spawn-faces-on.png"}, + {**page, "url": f"{self.base}?graph#agents/orbit", "wait": 1200, "probe": probe(GRAPH), "shot": None}, + {**page, "url": f"{self.base}?plain#agents/tree", "wait": 1200, "probe": probe(SPAWN_OFF), "shot": f"{shot}-spawn-faces-off.png"}, + {**page, "url": f"{self.base}?bubbles#stream", "wait": 1200, "probe": probe(SAME), "shot": f"{shot}-bubbles-same-layer.png"}, + {**page, "url": None, "wait": 0, "probe": probe(BAR), "shot": f"{shot}-bubbles-bar.png"}, + {**page, "url": None, "wait": 0, "probe": probe(NEXT), "shot": f"{shot}-bubbles-next-speaker.png"}, + ] + + def check(self, data: dict) -> None: + faces_on, graph, faces_off, same, bar, following = data["results"] + for found in data["results"]: + self.assertNotIn("error", found, found) + nodes = {node["agent"]: node for node in faces_on["nodes"]} + faced = {agent for agent, node in nodes.items() if node["art"]} + self.assertEqual({nodes[agent]["kind"] for agent in faced}, {"operator", "chair", "session"}, faces_on) + self.assertTrue({"s1", "s2"} <= faced, faces_on) + for agent, node in nodes.items(): + with self.subTest(agent=agent): + self.assertEqual((node["face"], node["r"]), (agent, 13) if agent in faced else (None, 9)) + shared = faced & set(graph["faces"]) + self.assertTrue({"s1", "s2"} <= shared, graph) + for agent in shared: + with self.subTest(agent=agent): + self.assertEqual(nodes[agent]["art"], graph["faces"][agent]) + self.assertEqual((faces_off["switched"], faces_off["checked"]), (True, False), faces_off) + self.assertEqual({(node["art"], node["face"], node["r"]) for node in faces_off["nodes"]}, {(None, None, 9)}) + self.assertEqual(len(faces_off["nodes"]), len(faces_on["nodes"])) + for scene in (same, bar, following): + with self.subTest(scene=scene["scene"]): + self.assertTrue(scene["overlaps"], scene) + self.assertEqual({overlap["front"] for overlap in scene["overlaps"]}, {scene["speaker"]}, scene) + if scene["tail"]: + self.assertEqual(scene["tail"]["front"], scene["speaker"], scene) + self.assertIsNotNone(same["tail"], same) + self.assertTrue(any(overlap["where"][0] != overlap["where"][1] for overlap in bar["overlaps"]), bar) + self.assertNotEqual(following["speaker"], following["first"]) + + def test_chromium_keeps_the_speaking_bubble_in_front_and_draws_faces_in_the_spawn_ancestry(self): + data = self.drive(self.steps(), None) + (OUT / "chromium-bubble-faces.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual([error for error in data["errors"] if "/favicon.ico" not in error], []) + self.check(data) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hub_bubble_front.py b/tests/test_hub_bubble_front.py new file mode 100644 index 0000000..136ad99 --- /dev/null +++ b/tests/test_hub_bubble_front.py @@ -0,0 +1,186 @@ +from __future__ import annotations + +import json +import re +import subprocess +import unittest + +from hub_support import HUB + +BUBBLES = HUB / "templates" / "bubbles.js" +LAYER = HUB / "templates" / "page-bubbles.js" +STYLE = HUB / "templates" / "style-bubbles.css" +RAISED = '.speech-bubble[data-state="speaking"]:not(.leaving)' +STACKING = ("z-index", "transform", "opacity", "filter", "backdrop-filter", "isolation", "will-change", "contain", "mix-blend-mode", "perspective", "clip-path", "mask") +LAYER_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const Bubbles = require(process.argv[1]); +const code = fs.readFileSync(process.argv[2], "utf8"); +const element = (tag, attrs) => { + const node = {tag, names: new Set(String(attrs.class || "").split(" ").filter(Boolean)), dataset: {}, parentElement: null, kids: [], offsetWidth: 120, offsetHeight: 40, textContent: "", scrollTop: 0, scrollHeight: 0, + style: {setProperty() {}, removeProperty() {}}, + append(...kids) { + for (const kid of kids.flat().filter((item) => typeof item === "object")) { + if (kid.parentElement) { + kid.parentElement.kids = kid.parentElement.kids.filter((other) => other !== kid); + } + kid.parentElement = node; + node.kids.push(kid); + } + }, + remove() {}, + }; + node.classList = {add: (name) => node.names.add(name)}; + for (const [key, value] of Object.entries(attrs).filter(([key]) => key.startsWith("data-"))) { + node.dataset[key.slice(5)] = value; + } + return node; +}; +const h = (tag, attrs, ...children) => { + const node = element(tag, attrs); + node.append(...children); + return node; +}; +const layer = element("div", {class: "bubbles"}); +const bar = element("div", {class: "bubble-bar"}); +layer.append(bar); +layer.querySelector = () => bar; +const near = {A: {left: 200, top: 300, width: 26, height: 26}}; +const document = { + getElementById: () => layer, + querySelectorAll: (selector) => { + const key = /data-face="([^"]*)"/.exec(selector)[1]; + return near[key] ? [{getBoundingClientRect: () => near[key]}] : []; + }, +}; +const item = (who) => ({who, line: `${who} says four words`, words: [who, "says", "four", "words"], started: null}); +const [a, b, c] = ["A", "B", "C"].map(item); +const speech = {current: null, loading: null, pending: []}; +const context = { + Bubbles, speech, document, h, settings: {bubbles: true}, g3: {tab: "stream"}, CSS: {escape: (value) => value}, nameOf: (key) => key, fixed: (value) => value.toFixed(1), + performance: {now: () => 5000}, window: {innerWidth: 1366, innerHeight: 768, setTimeout: () => 0, requestAnimationFrame: () => 1, cancelAnimationFrame: () => {}}, +}; +vm.createContext(context); +vm.runInContext(code, context); +const shown = () => [...layer.kids, ...bar.kids].filter((node) => node.names.has("speech-bubble")) + .map((node) => [node.dataset.who, node.dataset.state, node.names.has("leaving"), node.parentElement === bar ? "bar" : "layer"]) + .sort((one, two) => one[0].localeCompare(two[0])); +a.started = 4000; +Object.assign(speech, {current: a, pending: [b, c]}); +vm.runInContext("drawBubbles()", context); +const first = shown(); +b.started = 4900; +Object.assign(speech, {current: b, pending: [c]}); +vm.runInContext("drawBubbles()", context); +console.log(JSON.stringify({first, after: shown()})); +""" +CLIP_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const code = fs.readFileSync(process.argv[1], "utf8"); +const body = {}; +const styles = new Map(); +const box = (left, top, width, height) => ({left, top, width, height, right: left + width, bottom: top + height}); +const element = (rect, parent, style) => { + const node = {parentElement: parent, getBoundingClientRect: () => rect}; + styles.set(node, {display: "block", position: "static", overflowX: "visible", overflowY: "visible", ...style}); + return node; +}; +const page = element(box(0, 0, 1366, 768), body, {}); +const frame = element(box(40, 100, 500, 300), page, {overflowX: "auto", overflowY: "hidden"}); +const svg = element(box(40, 100, 1360, 300), frame, {overflowX: "hidden", overflowY: "hidden"}); +const wrapper = element(box(0, 0, 0, 0), page, {display: "contents", overflowX: "hidden", overflowY: "hidden"}); +const faces = { + inside: [element(box(100, 200, 26, 26), svg, {})], + scrolled: [element(box(800, 200, 26, 26), svg, {})], + half: [element(box(527, 200, 26, 26), svg, {})], + next: [element(box(800, 200, 26, 26), svg, {}), element(box(200, 600, 26, 26), page, {})], + contents: [element(box(800, 200, 26, 26), wrapper, {})], + fixed: [element(box(800, 200, 26, 26), frame, {position: "fixed"})], + away: [element(box(1400, 200, 26, 26), page, {})], +}; +const context = { + g3: {tab: "stream"}, CSS: {escape: (value) => value}, window: {innerWidth: 1366, innerHeight: 768}, getComputedStyle: (node) => styles.get(node), + document: {body, querySelectorAll: (selector) => faces[/data-face="([^"]*)"/.exec(selector)[1]] || []}, +}; +vm.createContext(context); +vm.runInContext(code, context); +const pick = (found) => (found ? [found.left, found.top, found.width, found.height] : null); +console.log(JSON.stringify(Object.fromEntries(Object.keys(faces).map((key) => [key, pick(vm.runInContext(`anchorOf(${JSON.stringify(key)})`, context))])))); +""" + + +def rules() -> dict[str, dict[str, str]]: + css = STYLE.read_text(encoding="utf-8") + top = re.sub(r"@media[^{]*\{.*?\n\}", "", css, flags=re.DOTALL) + found: dict[str, dict[str, str]] = {} + for selectors, body in re.findall(r"([^{}]+)\{([^{}]*)\}", top): + declarations = dict(tuple(part.strip() for part in line.split(":", 1)) for line in body.split(";") if ":" in line) + for selector in selectors.split(","): + found.setdefault(selector.strip(), {}).update(declarations) + return found + + +class TestSpeakingBubbleInFront(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.rules = rules() + + def test_only_the_speaking_bubble_that_is_not_leaving_rises_above_the_others(self): + raised = {selector: declared["z-index"] for selector, declared in self.rules.items() if ".speech-bubble" in selector and "z-index" in declared} + self.assertEqual(raised, {RAISED: "1"}) + + def test_bubbles_in_the_layer_and_in_the_bar_are_positioned_so_the_z_index_takes_effect(self): + self.assertEqual(self.rules[".speech-bubble"]["position"], "absolute") + self.assertEqual(self.rules[".bubble-bar .speech-bubble"]["position"], "relative") + + def test_the_tail_is_drawn_by_the_bubble_itself_inside_its_own_stacking_context(self): + self.assertIn("transform", self.rules[".speech-bubble"]) + self.assertIn("transform", self.rules[".bubble-bar .speech-bubble"]) + for tail in (".speech-bubble::before", ".speech-bubble::after"): + with self.subTest(tail=tail): + self.assertEqual(self.rules[tail]["content"], '""') + self.assertNotIn("z-index", self.rules[tail]) + + def test_floating_and_bar_bubbles_share_the_one_stacking_context_of_the_layer(self): + self.assertEqual((self.rules[".bubbles"]["position"], self.rules[".bubbles"]["z-index"]), ("fixed", "20")) + self.assertEqual([name for name in STACKING if name in self.rules[".bubble-bar"]], []) + + +class TestSpeakingMark(unittest.TestCase): + def test_the_speaking_mark_follows_the_speaker_across_containers_and_a_leaving_bubble_keeps_its_old_mark(self): + done = subprocess.run(["node", "-e", LAYER_PROBE, str(BUBBLES), str(LAYER)], capture_output=True, text=True, timeout=60, check=True) + data = json.loads(done.stdout) + self.assertEqual(data["first"], [["A", "speaking", False, "layer"], ["B", "waiting", False, "bar"], ["C", "waiting", False, "bar"]]) + self.assertEqual(data["after"], [["A", "speaking", True, "layer"], ["B", "speaking", False, "bar"], ["C", "waiting", False, "bar"]]) + for step, bubbles in data.items(): + with self.subTest(step=step): + self.assertEqual([who for who, state, leaving, _ in bubbles if state == "speaking" and not leaving], ["A" if step == "first" else "B"]) + + +class TestClippedAnchor(unittest.TestCase): + @classmethod + def setUpClass(cls): + done = subprocess.run(["node", "-e", CLIP_PROBE, str(LAYER)], capture_output=True, text=True, timeout=60, check=True) + cls.data = json.loads(done.stdout) + + def test_a_face_inside_its_scrolling_frame_anchors_with_its_full_box(self): + self.assertEqual(self.data["inside"], [100, 200, 26, 26]) + + def test_a_face_scrolled_out_of_its_frame_is_no_anchor_so_the_bubble_falls_back_to_the_bar(self): + self.assertIsNone(self.data["scrolled"]) + self.assertIsNone(self.data["away"]) + + def test_a_half_clipped_face_anchors_on_its_visible_part(self): + self.assertEqual(self.data["half"], [527, 200, 13, 26]) + + def test_the_next_face_of_the_same_agent_anchors_when_the_first_is_clipped(self): + self.assertEqual(self.data["next"], [200, 600, 26, 26]) + + def test_boxless_wrappers_and_ancestors_of_a_fixed_face_do_not_clip(self): + self.assertEqual((self.data["contents"], self.data["fixed"]), ([800, 200, 26, 26], [800, 200, 26, 26])) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hub_spawn_faces.py b/tests/test_hub_spawn_faces.py new file mode 100644 index 0000000..84550d1 --- /dev/null +++ b/tests/test_hub_spawn_faces.py @@ -0,0 +1,90 @@ +from __future__ import annotations + +import json +import subprocess +import unittest + +from hub_support import FACES, HUB + +ANCESTRY = HUB / "templates" / "ancestry.js" +SPAWN = HUB / "templates" / "page-spawn.js" +PAGE = HUB / "templates" / "page-ancestry.js" +GRAPH3D = HUB / "templates" / "page-graph3d.js" +FACED = {"Operator": "operator", "s1": "chair", "s2": "session"} +PLAIN = {"s2/x": "subagent", "s1/abc": "twig"} +SPAWN_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const [faces, ancestry, spawn, page, graph3d] = process.argv.slice(1); +const Faces = require(faces); +const Ancestry = require(ancestry); +const code = [spawn, page].map((path) => fs.readFileSync(path, "utf8")).join("\\n"); +const TOP = vm.runInNewContext(`(${/const TOP = (\\{[^}]*\\});/.exec(fs.readFileSync(graph3d, "utf8"))[1]})`); +const s = (tag, attrs, ...children) => { + const node = {tag, attrs: Object.fromEntries(Object.entries(attrs).filter(([key, value]) => key !== "on" && value !== null && value !== undefined)), children: []}; + node.append = (...kids) => node.children.push(...kids.flat(Infinity).filter((child) => child !== null && child !== undefined && child !== false)); + node.append(...children); + return node; +}; +const entry = (id, kind, children) => ({id, kind, label: id, children: children || []}); +const root = entry("Operator", "operator", [entry("s1", "chair", [entry("s2", "session", [entry("s2/x", "subagent")]), entry("s1/abc", "twig")])]); +const nodes = (tree) => (tree.attrs && String(tree.attrs.class || "").includes("ancestry-node") ? [tree] : (tree.children || []).filter((child) => typeof child === "object").flatMap(nodes)); +const mark = (node) => { + const art = node.children.find((child) => child.attrs.class === "face-art"); + return { + kind: node.attrs["data-kind"], face: node.attrs["data-face"] ?? null, tags: node.children.map((child) => child.tag), + r: node.children.find((child) => child.tag === "circle").attrs.r, label: node.children.find((child) => child.tag === "text").attrs.y, + art: art ? {transform: art.attrs.transform, parts: art.children.map((part) => [part.tag, part.attrs])} : null, + }; +}; +const drawn = (on) => { + const context = { + Faces, Ancestry, TOP, s, h: s, root, look: {faces: on, hidden: new Set()}, NODE_KINDS: {operator: "Operator", chair: "Chair", session: "Session", subagent: "Subagent"}, + fixed: (value) => value.toFixed(1), short: (text) => text, faceParts: (key) => Faces.of(key).parts.map(([tag, attrs]) => s(tag, attrs)), document: {addEventListener() {}}, + }; + return Object.fromEntries(nodes(vm.runInNewContext(`${code}\\nagentAncestry(root);`, context)).map((node) => [node.attrs["data-agent"], mark(node)])); +}; +const keys = ["Operator", "s1", "s2", "s2/x", "s1/abc"]; +console.log(JSON.stringify({on: drawn(true), off: drawn(false), faces: Object.fromEntries(keys.map((key) => [key, Faces.of(key).parts]))})); +""" + + +class TestSpawnAncestryFaces(unittest.TestCase): + @classmethod + def setUpClass(cls): + done = subprocess.run(["node", "-e", SPAWN_PROBE, str(FACES), str(ANCESTRY), str(SPAWN), str(PAGE), str(GRAPH3D)], capture_output=True, text=True, timeout=60, check=True) + cls.data = json.loads(done.stdout) + + def test_every_agent_of_the_spawn_tree_gets_one_node(self): + for case in ("on", "off"): + with self.subTest(case=case): + self.assertEqual({key: node["kind"] for key, node in self.data[case].items()}, {**FACED, **PLAIN}) + + def test_with_faces_on_operator_chair_and_sessions_wear_the_face_the_graph_and_3d_draw_for_their_key(self): + for key in FACED: + node = self.data["on"][key] + with self.subTest(key=key): + self.assertEqual(node["tags"], ["title", "circle", "g", "text"]) + self.assertEqual(node["art"]["parts"], self.data["faces"][key]) + self.assertEqual(node["face"], key) + + def test_the_face_fills_its_ring_and_the_label_moves_below_it(self): + for key in FACED: + node = self.data["on"][key] + with self.subTest(key=key): + self.assertEqual((node["r"], node["art"]["transform"], node["label"]), (13, "translate(-13 -13) scale(0.65)", 30)) + + def test_subagents_and_transcript_twigs_keep_the_plain_circle_as_in_the_graph_and_3d_views(self): + for key in PLAIN: + node = self.data["on"][key] + with self.subTest(key=key): + self.assertEqual((node["tags"], node["r"], node["label"], node["art"], node["face"]), (["title", "circle", "text"], 9, 26, None, None)) + + def test_with_faces_off_every_node_is_the_plain_circle_with_the_label_below_it_as_before(self): + for key, node in self.data["off"].items(): + with self.subTest(key=key): + self.assertEqual((node["tags"], node["r"], node["label"], node["art"], node["face"]), (["title", "circle", "text"], 9, 26, None, None)) + + +if __name__ == "__main__": + unittest.main()