diff --git a/docs/requirements/008-home-dashboard.md b/docs/requirements/008-home-dashboard.md index 549d48f..0345761 100644 --- a/docs/requirements/008-home-dashboard.md +++ b/docs/requirements/008-home-dashboard.md @@ -83,6 +83,45 @@ flowchart TD - [ ] With no pins the hub opens on Stream, with one or more on Home; an explicit hash wins. +### More bookmarks (operator 2026-11-14) + +- [ ] The hardware chart, the tokens chart and the combined overlay of + Resources each carry exactly one star, uncovered, and none per + series. +- [ ] A metric pin stores the chip selection of the moment it was starred: + the chart, accumulated or not, and the series switched on (null while + all are on); the hub refuses a metric pin without that view or with + an unknown chart, an unsorted, repeated or oversized series list. +- [ ] Another selection is another pin: with only CPU on, the hardware star + pins `metric:hardware/`, with all series on + `metric:hardware/all`, and the star shows the pressed state of the + selection shown now. +- [ ] A metric pin made with only CPU on draws only the CPU line on Home, + after a reload too, while Resources shows all series again. +- [ ] A pin of the combined overlay draws the combined chart on Home with + its right token axis and the burnup lines. +- [ ] The artifacts tree view, Agent priority and Waits on Operator each + carry a star and pin as a live tile. +- [ ] Every repository, branch, worktree and artifact row of the artifacts + tree carries a star; its pin shows that one element on Home. +- [ ] A row whose escaped id runs past 200 characters, such as an artifact + of a worktree under `~/Repositories/.../.worktrees/...`, pins + `#` instead, which Home resolves against the live tree, + so its star is never disabled for the length of a real path. +- [ ] With more than 100 series switched on but not all, a chart star is + disabled and names why, so no press ends in "Home not saved"; the hub + refuses a metric pin whose id is not the one its view gives. +- [ ] The tree tile on Home is a read-only glance: no delete button; with + no repository at all it says so instead of naming a filter. +- [ ] A star in a slot that a redraw moves keeps the keyboard focus; a tile + keeps its scroll position when Home draws it again. +- [ ] The time labels of a resources chart never overlap, at 390 px too. +- [ ] A pinned single agent card and a pinned single artifact row show + "gone" with an unpin button once the agent or the artifact + disappears, and Home reads the tree again at most 30 s after it last + did. +- [ ] Every view starred before keeps its star and pins as before. + ### Tests - [ ] Unit: registry lookup, layout validation, reorder math. @@ -92,3 +131,10 @@ flowchart TD - [ ] E2E: Chromium and Firefox at 1920, 1366 and 390: star and unstar, resize, reorder by drag and keyboard, the gone tile, the start tab; screenshots for the checkpoint. +- [ ] E2E (`tests/e2e/test_home_bookmarks_e2e.py`): Chromium and Firefox at + 1920, 1366 and 390: every new star exists and is uncovered, pins and + renders live on Home; the CPU-only metric pin survives a reload; the + combined pin shows combined mode; the single agent and artifact + tiles show their element and then "gone"; a repository, a worktree, + a core worktree and an artifact row on paths of real length pin, + show that one element on Home, turn "gone" and unpin; unpin works. diff --git a/orchestrator/kvb_orchestrator/viewstore.py b/orchestrator/kvb_orchestrator/viewstore.py index b91bbcc..e4d4ef5 100644 --- a/orchestrator/kvb_orchestrator/viewstore.py +++ b/orchestrator/kvb_orchestrator/viewstore.py @@ -26,6 +26,11 @@ TOKEN_MODES = ("interval", "accumulated") OVERLAYS = ("fix", "break", "tokens", "velocity", "security", "score") PINS_LIMIT = 64 SPAN = (1, 12) +METRIC = "metric" +CHARTS = ("hardware", "tokens", "combined") +SHOWN_LIMIT = 100 +FNV_PRIME = 0x01000193 +FNV_SEEDS = (0x811C9DC5, 0x01000193) def series_ids(value: object, name: str) -> list[str]: @@ -43,14 +48,75 @@ def span(value: object, name: str) -> int: return value +def fnv(text: str, seed: int) -> str: + value = seed + for char in text: + value = ((value ^ ord(char)) * FNV_PRIME) & 0xFFFFFFFF + return f"{value:08x}" + + +def digest(text: str) -> str: + """Args: + text: any text, such as the series ids of a metric pin joined by + newlines. + + Returns: + The 16 hex characters Panels.digest gives for the same text: FNV-1a + over its code points from FNV_SEEDS, one hash per seed. + """ + return "".join(fnv(text, seed) for seed in FNV_SEEDS) + + +def metric_id(view: dict) -> str: + """Args: + view: a metric pin's view as metric_view returns it. + + Returns: + The only id resourcePin gives a pin of that view: the chart, the + token mode unless the chart is hardware, and all or the digest of the + series switched on. + """ + shown = "all" if view["shown"] is None else digest("\n".join(view["shown"])) + return "/".join([view["chart"], shown] if view["chart"] == "hardware" else [view["chart"], view["tokens"], shown]) + + +def metric_view(raw: object, name: str) -> dict: + """Args: + raw: the view a metric pin stores: the chart, its token mode and the + series switched on when it was starred, null for all of them. + name: where the view sits, for the error message. + + Returns: + The view; a chart outside CHARTS, a hardware chart with a token mode + other than interval, or a series list that is not a sorted list of + at most SHOWN_LIMIT distinct series ids raises ToolError. + """ + view = section(raw, name, ("chart", "tokens", "shown")) + if not isinstance(view["chart"], str) or view["chart"] not in CHARTS: + raise ToolError(f"{name}.chart must be one of {', '.join(CHARTS)}") + if not isinstance(view["tokens"], str) or view["tokens"] not in TOKEN_MODES or (view["chart"] == "hardware" and view["tokens"] != "interval"): + raise ToolError(f"{name}.tokens must be one of {', '.join(TOKEN_MODES)}, interval for the hardware chart") + shown = view["shown"] + if shown is not None and (len(series_ids(shown, f"{name}.shown")) > SHOWN_LIMIT or shown != sorted(shown)): + raise ToolError(f"{name}.shown must be null or a sorted list of at most {SHOWN_LIMIT} series ids") + return {"chart": view["chart"], "tokens": view["tokens"], "shown": None if shown is None else list(shown)} + + def pin_entry(raw: object, index: int) -> dict: name = f"home.pins[{index}]" - entry = section(raw, name, ("kind", "id", "w", "h")) + metric = isinstance(raw, dict) and raw.get("kind") == METRIC + entry = section(raw, name, ("kind", "id", "w", "h", "view") if metric else ("kind", "id", "w", "h")) if not isinstance(entry["kind"], str) or not PIN_KIND.fullmatch(entry["kind"]): raise ToolError(f"{name}.kind must be 1 to 32 characters from A-Z, a-z, 0-9 and :_./-") if not isinstance(entry["id"], str) or not SERIES_ID.fullmatch(entry["id"]): raise ToolError(f"{name}.id must be 1 to 200 characters from A-Z, a-z, 0-9 and :_./-") - return {"kind": entry["kind"], "id": entry["id"], "w": span(entry["w"], f"{name}.w"), "h": span(entry["h"], f"{name}.h")} + pin = {"kind": entry["kind"], "id": entry["id"], "w": span(entry["w"], f"{name}.w"), "h": span(entry["h"], f"{name}.h")} + if not metric: + return pin + view = metric_view(entry["view"], f"{name}.view") + if entry["id"] != metric_id(view): + raise ToolError(f"{name}.id must be {metric_id(view)}, the chart, token mode and series of its view") + return {**pin, "view": view} def pins(value: object) -> list[dict]: diff --git a/orchestrator/templates/page-agent-order.js b/orchestrator/templates/page-agent-order.js index bb8350b..2cc9f8d 100644 --- a/orchestrator/templates/page-agent-order.js +++ b/orchestrator/templates/page-agent-order.js @@ -1,4 +1,4 @@ - const orderDom = {box: null, dragged: null, focus: null}; + const orderDom = {box: null, slot: null, dragged: null, focus: null}; function openAgentOrder() { go("agents", "hierarchy"); @@ -84,15 +84,15 @@ const key = orderDom.focus || focusKey(box); orderDom.focus = null; const changed = JSON.stringify(ui.order) !== JSON.stringify(state.agent_order); - box.replaceChildren( - h("div", {class: "inbox-head"}, h("h2", {class: "group-title", id: "agent-order-title", tabindex: "-1", "data-focus": "order:title"}, "Agent priority", counter(ui.order.length))), + Panels.keepFocus(orderDom.slot, () => box.replaceChildren( + h("div", {class: "inbox-head"}, h("h2", {class: "group-title", id: "agent-order-title", tabindex: "-1", "data-focus": "order:title"}, "Agent priority", counter(ui.order.length)), h("span", {class: "spacer"}), orderDom.slot), h("p", {class: "muted"}, "Orders the chair's grants and decides only between equal task priorities; the tasks' --prio pins stay unchanged."), ui.order.length ? h("ol", {class: "order-list", "aria-label": "Agent order"}, ui.order.map(orderItem)) : h("p", {class: "muted"}, "No agent with an office or an open task."), h("div", {class: "actions"}, h("span", {class: "muted"}, "Drag or use the arrows to reorder"), h("span", {class: "spacer"}), h("button", {type: "button", class: "btn ghost", "data-focus": "order:reset", disabled: !changed, on: {click: () => placeAgent([...state.agent_order], "order:reset")}}, "Reset"), - h("button", {type: "button", class: "btn primary", "data-focus": "order:save", disabled: !changed, on: {click: saveAgentOrder}}, "Save order"))); + h("button", {type: "button", class: "btn primary", "data-focus": "order:save", disabled: !changed, on: {click: saveAgentOrder}}, "Save order")))); const target = key && [...box.querySelectorAll("[data-focus]")].find((element) => element.dataset.focus === key); if (target && target.disabled) { refocus(box, key.startsWith("up:") ? `down:${key.slice(3)}` : key.startsWith("down:") ? `up:${key.slice(5)}` : "order:title"); diff --git a/orchestrator/templates/page-artifacts.js b/orchestrator/templates/page-artifacts.js index 72ebd14..e8e8a03 100644 --- a/orchestrator/templates/page-artifacts.js +++ b/orchestrator/templates/page-artifacts.js @@ -60,7 +60,17 @@ } } const ids = new Set(nodes.map((node) => node.id)); - return {nodes, links: links.filter((link) => ids.has(link.source) && ids.has(link.target)), byId: new Map(nodes.map((node) => [node.id, node]))}; + return {nodes, links: links.filter((link) => ids.has(link.source) && ids.has(link.target)), byId: new Map(nodes.map((node) => [node.id, node])), byRef: new Map(nodes.map((node) => [artifactRef(node.id), node]))}; + } + + /** + * @param id - an artifacts model node id, such as wt: or artefact:wt:\u0000\u0000. + * @returns the id its star pins after the artifact: kind: the node id while Panels.token keeps it within 200 + * characters, else #, which no node id equals and which Home resolves + * against the live tree through byRef. + */ + function artifactRef(id) { + return Panels.token(id) === null ? `${id.slice(0, id.indexOf(":"))}#${Panels.digest(id)}` : id; } function chosenRepo() { @@ -216,7 +226,12 @@ return h("div", {class: "legend"}, marks, h("span", {class: "muted"}, words[view])); } - function artPopup(node) { + /** + * @param node - an artifacts model node. + * @param glance - true for the read-only copy a Home tile shows, without the delete button. + * @returns the popup head and body of the node. + */ + function artPopup(node, glance) { const [kindName, kindIcon] = ART_KINDS[node.kind]; const data = node.data; const body = []; @@ -226,7 +241,7 @@ } else if (node.kind === "branch") { body.push(h("div", {class: "states"}, branchStates(data), gateIcons(node.repo, data.name)), meta([["Upstream", data.upstream], ["Head", data.head.slice(0, 12), data.head], ["Merged into", data.merged_into], ["Reason", data.reason]]), data.worktrees.map((wt) => flowDetails(wt.flow, data.name)), - h("h3", {}, "Dry run"), h("pre", {class: "notes"}, data.plan.join("\n")), h("div", {class: "actions"}, deleteButton(data))); + h("h3", {}, "Dry run"), h("pre", {class: "notes"}, data.plan.join("\n")), glance ? null : h("div", {class: "actions"}, deleteButton(data))); } else if (node.kind === "worktree") { body.push(h("div", {class: "states"}, worktreeStates(data)), workChips(data.open_work), meta([["Path", data.path], ["Branch", data.branch], ["HEAD", data.head.slice(0, 12), data.head]]), flowDetails(data.flow, data.branch), @@ -237,7 +252,7 @@ } else { body.push(meta([["Title", data.title], ["Status", data.status]])); } - return [popupHead(icon(kindIcon), node.label, h("span", {class: "muted"}, kindName)), h("div", {class: "popup-body", "data-item": `artifact:${node.id}`}, body)]; + return [popupHead(icon(kindIcon), node.label, h("span", {class: "muted"}, kindName)), h("div", {class: "popup-body", "data-item": `artifact:${artifactRef(node.id)}`}, body)]; } function artifactsScreen() { @@ -264,7 +279,7 @@ }, 0); const controls = h("div", {class: "card controls"}, viewSwitch("artifacts"), view === "3d" ? layoutTools("artifacts") : view === "tree" ? null : picker, h("div", {class: "toolbar", role: "toolbar", "aria-label": "Artifacts"}, search, reload, removeAll, apply), count); - const body = view === "tree" ? list : h("div", {class: "card stage-card", "data-panel": `artifacts/${view}`}, stageBox, legendBox); + const body = view === "tree" ? h("div", {class: "tree-panel", "data-panel": "artifacts/tree"}, list) : h("div", {class: "card stage-card", "data-panel": `artifacts/${view}`}, stageBox, legendBox); return h("div", {class: "graph"}, controls, body); } diff --git a/orchestrator/templates/page-hierarchy.js b/orchestrator/templates/page-hierarchy.js index 5c9764f..0da1602 100644 --- a/orchestrator/templates/page-hierarchy.js +++ b/orchestrator/templates/page-hierarchy.js @@ -4,7 +4,8 @@ function hierarchyScreen() { hier.chart = h("section", {class: "card org-chart", "data-panel": "agents/hierarchy", "aria-labelledby": "org-title"}); - orderDom.box = h("section", {class: "card agent-order", id: "agent-order", "aria-labelledby": "agent-order-title"}); + orderDom.box = h("section", {class: "card agent-order", id: "agent-order", "aria-labelledby": "agent-order-title", "data-panel": "agents/order"}); + orderDom.slot = h("span", {"data-star-slot": ""}); hier.sig = ""; return h("div", {class: "hierarchy-body"}, hier.chart, orderDom.box); } diff --git a/orchestrator/templates/page-home.js b/orchestrator/templates/page-home.js index 6ab341a..a945cd4 100644 --- a/orchestrator/templates/page-home.js +++ b/orchestrator/templates/page-home.js @@ -67,8 +67,10 @@ } function homeTree(draw) { - if (!tree.data) { + if (!tree.data || Date.now() - (homeView.fetched.tree || 0) > HOME_TREE) { homeFetch("tree", fetchTree); + } + if (!tree.data) { return homeWait("Reading repositories …"); } return tree.data.error ? emptyState("No tree", tree.data.error) : draw(); @@ -81,10 +83,13 @@ }); } - function homeArtifact(id) { + const artifactNode = (ref) => (tree.model ? tree.model.byRef.get(ref) || null : null); + const artifactTitle = (ref) => `Artifact ${(artifactNode(ref) ? artifactNode(ref).id : ref).replace(/\u0000/g, " ")}`; + + function homeArtifact(ref) { return homeTree(() => { - const node = tree.model.byId.get(id); - return node ? h("div", {class: "home-popup"}, artPopup(node)) : null; + const node = artifactNode(ref); + return node ? h("div", {class: "home-popup"}, artPopup(node, true)) : null; }); } @@ -161,9 +166,68 @@ Panels.panel("tasks/3d", "Tasks: 3D", () => home3d("tasks"), "3d"); Panels.item("task", (id) => `Task ${id}`, (size, id) => homeTask(id)); Panels.item("message", "Message", (size, id) => homeMessage(id)); - Panels.item("artifact", (id) => `Artifact ${id.replace(/\u0000/g, " ")}`, (size, id) => homeArtifact(id)); + Panels.item("artifact", artifactTitle, (size, id) => homeArtifact(id)); Panels.item("agent", (id) => `Agent ${labelOf(id)}`, (size, id) => agentTile(id)); Panels.item("ancestry", (id) => `Ancestry ${id}`, (size, id) => homeAncestry(id)); + Panels.panel("artifacts/tree", "Artifacts: tree", () => homeTree(() => h("div", {class: "repos"}, treeCards("", true).nodes))); + Panels.panel("agents/order", "Agents: priority", homeOrder); + Panels.panel("tasks/operator", "Tasks: Waits on Operator", homeInbox); + Panels.item("metric", (id, view) => metricTitle(view), (size, id, view) => homeMetric(id, view)); + Panels.capture("resources-hardware", () => resourcePin("hardware")); + Panels.capture("resources-tokens", () => resourcePin("tokens")); + Panels.capture("resources-combined", () => resourcePin("combined")); + + const HOME_TREE = 30000; + const HOME_METRICS = {hardware: "Hardware", tokens: "Tokens", combined: "Combined"}; + const homeMetricView = (view) => Boolean(view) && Object.hasOwn(HOME_METRICS, view.chart) && (view.shown === null || Array.isArray(view.shown)); + const seriesName = (id) => Panels.raw(id).slice(Panels.raw(id).indexOf(":") + 1); + + function homeOrder() { + const order = state.agent_order; + return order.length ? h("ol", {class: "order-list home-order"}, order.map((name, index) => h("li", {"data-name": name}, h("span", {class: "rank"}, `${index + 1}.`), face(name, false), person(name)))) + : h("p", {class: "muted"}, "No agent with an office or an open task."); + } + + function homeInbox() { + const items = visibleInbox(); + return items.length ? h("ol", {class: "home-inbox"}, items.map((item) => h("li", {class: `k-${item.kind}`, "data-wait": keyOf(item)}, + h("div", {class: "msg-meta"}, when(item.ts), pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), item.agent ? [face(item.agent, false), person(item.agent)] : null), + inboxHeadline(item), + h("div", {class: "item-actions line-actions"}, actionsFor(item))))) : emptyState("All done", "Nothing is waiting for you right now."); + } + + function metricTitle(view) { + if (!homeMetricView(view)) { + return "Metric"; + } + const mode = view.chart !== "hardware" && view.tokens === "accumulated" ? " (accumulated)" : ""; + const names = view.shown === null ? "all series" : view.shown.length > 3 ? `${view.shown.length} series` : view.shown.map(seriesName).join(", ") || "no series"; + return `${HOME_METRICS[view.chart]}${mode}: ${names}`; + } + + function homeMetric(id, view) { + if (!homeMetricView(view)) { + return null; + } + homeFetch("resources", () => (resourceView.data === null || resourcesStale() ? fetchResources() : null)); + const key = `metric:${id}`; + const host = homeView.hosts.get(key) || h("div", {class: "home-metric", "data-chart": view.chart}, h("div", {class: "legend-chips"}), h("div", {class: "res-plot"})); + homeView.hosts.set(key, host); + window.setTimeout(() => host.isConnected && drawMetric(host, view), 0); + return host; + } + + function drawMetric(host, view) { + const [legend, plot] = host.children; + if (resourceView.data === null) { + plot.replaceChildren(homeWait("Loading …")); + return; + } + const box = plot.getBoundingClientRect(); + const drawn = metricChart(view, {width: Math.round(box.width), height: Math.round(box.height)}); + plot.replaceChildren(...(drawn.svg ? [drawn.svg] : [])); + legend.replaceChildren(...drawn.series.map((line) => h("span", {class: `legend-chip ${line.tone}`, "data-series": line.id}, line.label))); + } function homeRefused(why) { homeView.pins = homeStore.read(); @@ -197,21 +261,21 @@ } } - function homePress(kind, id, title) { + function homePress(kind, id, title, view) { homeView.touched = true; if (Panels.has(homePins(), kind, id)) { homeSave((pins) => Panels.remove(pins, kind, id)); return; } - const {refused, holder} = Panels.add(homePins(), kind, id); + const {refused, holder} = Panels.add(homePins(), kind, id, view); if (refused) { homeRefusal(refused, holder, title); return; } - homeSave((pins) => Panels.add(pins, kind, id).pins).then((stored) => { + homeSave((pins) => Panels.add(pins, kind, id, view).pins).then((stored) => { const now = homeStore.read(); if (stored && !Panels.has(now, kind, id)) { - const late = Panels.add(now, kind, id); + const late = Panels.add(now, kind, id, view); homeRefusal(late.refused, late.holder, title); } }); @@ -328,7 +392,7 @@ function homeFill(tile, pin, index, count) { const key = Panels.keyOf(pin); - const entry = Panels.lookup(pin.kind, pin.id); + const entry = Panels.lookup(pin.kind, pin.id, pin.view); const content = entry ? homeContent(entry, pin) : null; const name = entry ? entry.title : `${pin.kind} ${pin.id}`; tile.classList.toggle("gone", !content); diff --git a/orchestrator/templates/page-inbox.js b/orchestrator/templates/page-inbox.js index f9ebd51..15a0253 100644 --- a/orchestrator/templates/page-inbox.js +++ b/orchestrator/templates/page-inbox.js @@ -1,3 +1,5 @@ + let inboxSlot = null; + async function submit(entries, label) { ui.busy = true; const signed = await proofFor(entries, label); @@ -150,6 +152,10 @@ return buttons; } + function inboxHeadline(item) { + return h("button", {type: "button", class: "linkish headline", title: "Open", on: {click: () => openItem(item)}}, revealed(item.kind === "operator" ? item.id : item.kind === "ask" ? item.title.split(": ")[0] : item.title)); + } + function inboxItem(item, index) { const red = item.kind === "review" && item.gate !== "pass"; const commanded = item.kind === "ask" || item.kind === "operator"; @@ -166,7 +172,7 @@ item.until ? pill(`until ${absolute.format(stampOf(item.until))}`, "neutral") : null), h("div", {class: "entry-body"}, h("div", {class: "route"}, - h("button", {type: "button", class: "linkish headline", title: "Open", on: {click: () => openItem(item)}}, revealed(item.kind === "operator" ? item.id : item.kind === "ask" ? item.title.split(": ")[0] : item.title)), + inboxHeadline(item), item.agent ? [face(item.agent, false), person(item.agent)] : null), item.kind === "review" ? h("div", {class: "muted"}, revealed(item.detail), item.score ? revealed(` · ${item.score}`) : "") : null, commanded ? h("div", {class: "command", "data-path": item.directory || "-"}, h("code", {}, revealed(item.command)), copyButton(item.command)) : null, @@ -189,7 +195,8 @@ } function inboxScreen() { - inboxDom = h("div", {class: "inbox"}); + inboxDom = h("div", {class: "inbox", "data-panel": "tasks/operator"}); + inboxSlot = h("span", {"data-star-slot": ""}); return inboxDom; } @@ -223,12 +230,12 @@ const choose = h("button", {type: "button", class: "btn small soft", ...named(`Grant selection (${grantable.length})`), disabled: !grantable.length || ui.busy, on: {click: () => bulk(chosen, "Grant selection", false)}}, icon("bi-check2-square"), String(grantable.length)); const deny = h("button", {type: "button", class: "btn small danger", ...named(`Deny selection (${deniable.length})`, `Deny selection (${deniable.length}): each requester gets its denial by post, one passkey signature, 3 s to undo`), disabled: !deniable.length || ui.busy, on: {click: () => bulk(chosen, "Deny selection", true)}}, icon("bi-x-square"), String(deniable.length)); const all = h("button", {type: "button", class: "btn small primary", ...named(`Grant all green (${green.length})`), disabled: !green.length || ui.busy, on: {click: () => bulk(green, "Grant all green", false)}}, icon("bi-check2-all"), String(green.length)); - inboxDom.replaceChildren( + Panels.keepFocus(inboxSlot, () => inboxDom.replaceChildren( h("div", {class: "inbox-head"}, h("h2", {class: "group-title"}, "Waits on Operator", counter(waitingCount())), h("span", {class: "spacer"}), - hidden ? h("label", {class: "check"}, snoozed, `${hidden} snoozed`) : null), + hidden ? h("label", {class: "check"}, snoozed, `${hidden} snoozed`) : null, inboxSlot), h("div", {class: "inbox-actions"}, choose, deny, all), onLocalhost() ? "" : h("p", {class: "muted"}, "The passkey signs grants only under ", localLink()), - items.length ? h("ol", {class: "inbox-list"}, items.map(inboxItem)) : emptyState("All done", "Nothing is waiting for you right now.")); + items.length ? h("ol", {class: "inbox-list"}, items.map(inboxItem)) : emptyState("All done", "Nothing is waiting for you right now."))); settleConsoles(items); const current = inboxDom.querySelector(`.item[data-index="${ui.cursor}"]`); if (current && document.activeElement === document.body) { diff --git a/orchestrator/templates/page-resources.js b/orchestrator/templates/page-resources.js index fd3a71d..fc790b7 100644 --- a/orchestrator/templates/page-resources.js +++ b/orchestrator/templates/page-resources.js @@ -1,4 +1,4 @@ - const RESOURCE = {left: 46, right: 10, wide: 50, top: 6, bottom: 6, axis: 18, poll: 5000, stale: 60, tones: 6, narrow: 600}; + const RESOURCE = {left: 46, right: 10, wide: 50, top: 6, bottom: 6, axis: 18, poll: 5000, stale: 60, tones: 6, glyph: 6.5, shown: 100}; const RESOURCE_PANELS = [["hardware", "bi-cpu", "Hardware: disks, CPU, RAM, swap, GPU and VRAM"], ["tokens", "bi-coin", "Tokens per agent"], ["combined", "bi-layers", "Hardware, tokens and burnup"]]; const RESOURCE_ZONES = {idle: "Idle: below 30 %", saturated: "Saturated: above 90 %"}; const RESOURCE_MARKS = { @@ -7,8 +7,9 @@ unknown: ["#bi-question-circle", (mark) => `${mark.name} unknown: no reading`], }; const RESOURCE_BURN = {done: "Done", scope: "Scope"}; - const resourceView = {data: null, dom: null, busy: false, key: "", at: 0, timer: 0}; + const resourceView = {data: null, dom: null, busy: false, key: "", at: 0, timer: 0, series: {}}; const resourceKey = () => JSON.stringify([ui.preset, ui.from, ui.to, ui.asOf]); + const resourcesStale = () => resourceKey() !== resourceView.key || nowSeconds() - resourceView.at >= RESOURCE.stale; const compactCount = (value) => (value >= 1e6 ? `${fixed(value / 1e6)}M` : value >= 1e3 ? `${fixed(value / 1e3)}k` : String(Math.round(value))); const resourceLabel = (line) => (line.kind === "vram" ? `${line.name} VRAM` : line.name); const percentOf = (value) => `${value} %`; @@ -28,7 +29,7 @@ const legend = h("div", {class: "res-legend"}); const plot = h("div", {class: "res-plot"}); const panel = h("section", {class: `card res-panel res-${kind}`, "data-panel": `resources-${kind}`, "aria-label": label, hidden: kind === "combined"}, - h("div", {class: "res-head"}, h("span", {class: "res-title", title: label, "aria-label": label}, icon(name)), legend), plot); + h("div", {class: "res-head", "data-star-slot": ""}, h("span", {class: "res-title", title: label, "aria-label": label}, icon(name)), legend), plot); return {kind, label, legend, plot, panel}; }); resourceView.dom = {count, switches, panels}; @@ -52,7 +53,7 @@ if (!dom || !dom.count.isConnected || document.hidden || resourceView.busy) { return; } - if (resourceKey() !== resourceView.key || nowSeconds() - resourceView.at >= RESOURCE.stale) { + if (resourcesStale()) { fetchResources(); } } @@ -82,17 +83,31 @@ const bottom = size.height - (axis ? RESOURCE.axis : RESOURCE.bottom); const x = (t) => RESOURCE.left + ((Math.min(Math.max(t, span.from), span.to) - span.from) / (span.to - span.from)) * (right - RESOURCE.left); const svg = s("svg", {class: `chart res-chart res-${entry.kind}`, "data-chart": entry.kind, "data-from": span.from, "data-to": span.to, viewBox: `0 0 ${size.width} ${size.height}`, role: "img", "aria-label": entry.label}); - const ticks = size.width < RESOURCE.narrow ? 2 : 4; - for (let index = 0; index <= ticks; index += 1) { - const t = span.from + ((span.to - span.from) * index) / ticks; + const ticks = resourceTicks(span, right - RESOURCE.left); + for (let index = 0; index <= ticks.count; index += 1) { + const t = span.from + ((span.to - span.from) * index) / ticks.count; svg.append(s("line", {class: "grid", x1: fixed(x(t)), x2: fixed(x(t)), y1: RESOURCE.top, y2: fixed(bottom)})); - if (axis) { - svg.append(s("text", {class: "axis", x: fixed(x(t)), y: size.height - 4, "text-anchor": index === 0 ? "start" : index === ticks ? "end" : "middle"}, (span.to - span.from > DAY ? absolute : clock).format(stampOf(t)))); + if (axis && (ticks.all || index === ticks.count)) { + svg.append(s("text", {class: "axis", "data-axis": "time", x: fixed(x(t)), y: size.height - 4, "text-anchor": index === 0 ? "start" : index === ticks.count ? "end" : "middle"}, ticks.format.format(stampOf(t)))); } } return {svg, x, top: RESOURCE.top, bottom, right}; } + /** + * @param span - {from, to} of the chart in epoch seconds. + * @param room - the plot width in px. + * @returns {count, all, format}: up to 4 intervals between time labels, as many as fit apart at RESOURCE.glyph px + * per character, all false when not even the first and the last label fit, so that only the last is drawn. + */ + function resourceTicks(span, room) { + const format = span.to - span.from > DAY ? absolute : clock; + const wide = Math.max(...[span.from, span.to].map((t) => format.format(stampOf(t)).length)) * RESOURCE.glyph + RESOURCE.glyph; + const fits = (count) => room / count >= wide * (count === 1 ? 2 : 1.5); + const count = [4, 3, 2, 1].find(fits); + return {count: count || 1, all: Boolean(count), format}; + } + function resourceScale(frame, values, label, side) { const y = (value) => frame.top + (1 - value / values[values.length - 1]) * (frame.bottom - frame.top); const left = side === "left"; @@ -203,7 +218,7 @@ const y = resourceScale(frame, [0, 50, 100], percentOf, "left"); resourceBackdrop(frame, y, parts.marks, span); resourceLines(frame, y, parts.hardware.filter((line) => !parts.hidden.has(line.id))); - return {svg: frame.svg, series: parts.hardware, path: "resources.hidden"}; + return {svg: frame.svg, series: RESOURCE_SERIES.hardware(parts), path: "resources.hidden"}; } function tokensChart(entry, parts, span, size) { @@ -211,7 +226,7 @@ const plot = tokenPlot(shownRows(parts.tokens, parts.agents, parts.hidden), parts.mode); const y = resourceScale(frame, [0, plot.top], compactCount, "left"); tokenMarks(frame, y, plot, parts.agents.tones, span, false); - return {svg: frame.svg, series: parts.agents.list, path: "resources.hidden"}; + return {svg: frame.svg, series: RESOURCE_SERIES.tokens(parts), path: "resources.hidden"}; } function combinedChart(entry, parts, span, size) { @@ -224,10 +239,45 @@ for (const line of parts.burn.filter((item) => !parts.combinedHidden.has(item.id) && item.points.length)) { frame.svg.append(s("path", {class: line.tone, "data-id": line.id, "data-series": line.name, "data-last": line.points[line.points.length - 1][1], "data-top": line.top, d: resourceSteps(frame, y, line.points)}, s("title", {}, `${line.label} of the largest scope in the window (${line.top})`))); } - return {svg: frame.svg, series: [...parts.hardware, ...parts.agents.list, ...parts.burn], path: "resources.combinedHidden"}; + return {svg: frame.svg, series: RESOURCE_SERIES.combined(parts), path: "resources.combinedHidden"}; } const RESOURCE_CHARTS = {hardware: hardwareChart, tokens: tokensChart, combined: combinedChart}; + const RESOURCE_SERIES = {hardware: (parts) => parts.hardware, tokens: (parts) => parts.agents.list, combined: (parts) => [...parts.hardware, ...parts.agents.list, ...parts.burn]}; + const resourceSmall = (size) => size.width < RESOURCE.left + RESOURCE.wide + 10 || size.height < RESOURCE.axis + 10; + + /** + * @param kind - hardware, tokens or combined. + * @returns the metric pin the chart's star makes now: {kind: "metric", id, view}, view being the chart, its token + * mode and the sorted ids of the series switched on, null while all are on; the id names the chart, the mode and + * all or a digest of the series, so another selection is another pin. With more than RESOURCE.shown series on but + * not all, the pin also carries refused, as the view store keeps at most that many. + */ + function resourcePin(kind) { + const drawn = resourceView.series[kind] || {ids: [], shown: [], tokens: ViewStore.get("resources").tokens}; + const shown = [...drawn.shown].sort(); + const view = {chart: kind, tokens: kind === "hardware" ? "interval" : drawn.tokens, shown: shown.length === drawn.ids.length ? null : shown}; + const id = [kind, kind === "hardware" ? "" : view.tokens, view.shown === null ? "all" : Panels.digest(shown.join("\n"))].filter(Boolean).join("/"); + const over = view.shown !== null && view.shown.length > RESOURCE.shown; + return over ? {kind: "metric", id, view, refused: `more than ${RESOURCE.shown} series are switched on: switch some off, or all of them on`} : {kind: "metric", id, view}; + } + + /** + * @param view - a metric pin's view: {chart, tokens, shown}. + * @param size - {width, height} of the plot in px. + * @returns {svg, series}: the chart drawn from the loaded samples with only the view's series, svg null when the + * plot is too small, and those series for a legend. + */ + function metricChart(view, size) { + const data = resourceView.data; + const span = {from: data.since, to: Math.max(data.until, data.since + 60)}; + const parts = resourceParts(data, {tokens: view.tokens, hidden: [], combinedHidden: [], combined: view.chart === "combined"}, span); + const series = RESOURCE_SERIES[view.chart](parts); + const hidden = new Set(view.shown === null ? [] : series.map((line) => line.id).filter((id) => !view.shown.includes(id))); + const [, , label] = RESOURCE_PANELS.find(([kind]) => kind === view.chart); + const drawn = RESOURCE_CHARTS[view.chart]({kind: view.chart, label}, {...parts, hidden, combinedHidden: hidden}, span, size); + return {svg: resourceSmall(size) ? null : drawn.svg, series: series.filter((line) => !hidden.has(line.id))}; + } function resourceParts(data, view, span) { return { @@ -274,8 +324,9 @@ const box = entry.plot.getBoundingClientRect(); const size = {width: Math.round(box.width), height: Math.round(box.height)}; const drawn = RESOURCE_CHARTS[entry.kind](entry, parts, span, size); - const small = size.width < RESOURCE.left + RESOURCE.wide + 10 || size.height < RESOURCE.axis + 10; - entry.plot.replaceChildren(...(small ? [] : [drawn.svg])); + const hidden = drawn.path === "resources.hidden" ? parts.hidden : parts.combinedHidden; + resourceView.series[entry.kind] = {ids: drawn.series.map((line) => line.id), shown: drawn.series.map((line) => line.id).filter((id) => !hidden.has(id)), tokens: view.tokens}; + entry.plot.replaceChildren(...(resourceSmall(size) ? [] : [drawn.svg])); entry.legend.replaceChildren(LegendChips.render(drawn.series, drawn.path, "hidden")); const chip = focused === null ? null : [...entry.legend.querySelectorAll("[data-chip]")].find((node) => node.getAttribute("data-chip") === focused); if (chip) { diff --git a/orchestrator/templates/page-tree.js b/orchestrator/templates/page-tree.js index 1036b59..ea52b2d 100644 --- a/orchestrator/templates/page-tree.js +++ b/orchestrator/templates/page-tree.js @@ -64,8 +64,9 @@ tree.data.delete_ready !== true ? iconBtn("bi-key-fill", "Store token", "btn small soft", storeToken, "Store token: add the operator row that stores the GitHub token in the Secret Service") : null); } - function leafRow(found) { - return h("li", {class: "leaf"}, h("span", {class: "state", "data-tone": found.ended ? "warn" : "neutral", role: "img", ...named(found.kind)}, icon(ARTEFACT_ICONS[found.kind] || "bi-paperclip")), + function leafRow(holder, found) { + return h("li", {class: "leaf", "data-item": `artifact:${artifactRef(`artefact:${holder}\u0000${found.task}\u0000${found.ref}`)}`}, + h("span", {class: "state", "data-tone": found.ended ? "warn" : "neutral", role: "img", ...named(found.kind)}, icon(ARTEFACT_ICONS[found.kind] || "bi-paperclip")), h("code", {}, found.ref), h("span", {class: "muted"}, found.task), found.ended ? pill("task ended", "warn") : null); } @@ -75,8 +76,8 @@ function worktreeRow(wt) { const review = wt.review; - return h("div", {class: wt.open_work.length ? "wt open-work" : "wt", "data-open-work": wt.open_work.join(" ") || null}, - h("div", {class: "wt-head"}, + return h("div", {class: wt.open_work.length ? "wt open-work" : "wt", "data-open-work": wt.open_work.join(" ") || null, "data-item": `artifact:${artifactRef(`wt:${wt.path}`)}`}, + h("div", {class: "wt-head", "data-star-slot": ""}, h("span", {class: "states"}, worktreeStates(wt), openWork(wt)), h("code", {}, wt.path), h("span", {class: "muted", title: wt.head}, wt.head.slice(0, 10)), @@ -84,7 +85,7 @@ review ? [status(review.status), review.gate ? pill(review.gate === "pass" ? "Gate green" : "Gate red", review.gate === "pass" ? "ok" : "bad") : null] : null, copyButton(wt.path)), flowLine(wt.flow, wt.branch), - wt.artefacts && wt.artefacts.length ? h("ul", {class: "leaves", "aria-label": "Artifacts"}, wt.artefacts.map(leafRow)) : null); + wt.artefacts && wt.artefacts.length ? h("ul", {class: "leaves", "aria-label": "Artifacts"}, wt.artefacts.map((found) => leafRow(`wt:${wt.path}`, found))) : null); } function resultOf(repo, entry) { @@ -97,11 +98,11 @@ return chip(name, `${label}${result.reason ? `: ${result.reason}` : ""}`, tone); } - function branchRow(repo, entry) { + function branchRow(repo, entry, glance) { const result = resultOf(repo, entry); - return h("div", {class: "branch-row"}, - h("div", {class: "branch-name"}, h("strong", {}, entry.name), h("span", {class: "states"}, branchStates(entry)), entry.upstream ? h("span", {class: "muted"}, `↔ ${entry.upstream}`) : null, - h("span", {class: "muted", title: entry.head}, entry.head.slice(0, 10)), result ? resultChip(result) : null, h("span", {class: "spacer"}), gateIcons(repo.path, entry.name), deleteButton(entry)), + return h("div", {class: "branch-row", "data-item": `artifact:${artifactRef(`branch:${repo.path}\u0000${entry.name}`)}`}, + h("div", {class: "branch-name", "data-star-slot": ""}, h("strong", {}, entry.name), h("span", {class: "states"}, branchStates(entry)), entry.upstream ? h("span", {class: "muted"}, `↔ ${entry.upstream}`) : null, + h("span", {class: "muted", title: entry.head}, entry.head.slice(0, 10)), result ? resultChip(result) : null, h("span", {class: "spacer"}), gateIcons(repo.path, entry.name), glance ? null : deleteButton(entry)), entry.worktrees.map(worktreeRow)); } @@ -116,7 +117,19 @@ return; } tree.dom.removeAll.replaceChildren(deleteAll(), gateApply() || ""); - const q = tree.q.trim().toLowerCase(); + const shown = treeCards(tree.q.trim().toLowerCase()); + tree.dom.count.textContent = `${shown.count} repositories · as of ${clock.format(stampOf(tree.data.scanned))}${tree.data.delete_ready ? "" : ` · Delete blocked: ${tree.data.delete_reason}`}`; + tree.dom.list.replaceChildren(...shown.nodes); + } + + /** + * @param q - the lower-case filter text, "" for all. + * @param glance - true for the read-only copy a Home tile shows, without the delete buttons. + * @returns {count, nodes}: how many repositories match and the cards of the tree view for them, the tasks without a + * worktree and the recently deleted branches, or an empty state when nothing matches or, without a filter, when + * pkgmgr names nothing. + */ + function treeCards(q, glance) { const hit = (...texts) => !q || texts.some((text) => String(text).toLowerCase().includes(q)); const repos = []; for (const repo of tree.data.repos) { @@ -129,22 +142,20 @@ const tasks = (tree.data.tasks || []).filter((task) => hit(task.task, task.title, ...task.artefacts.map((found) => found.ref))); const listed = new Set(tree.data.repos.flatMap((repo) => repo.branches.map((entry) => `${repo.path}\u0000${entry.name}`))); const gone = (tree.data.cleanups || []).flatMap((result) => result.items).filter((item) => !listed.has(`${item.repository}\u0000${item.branch}`) && hit(item.repository, item.branch)); - tree.dom.count.textContent = `${repos.length} repositories · as of ${clock.format(stampOf(tree.data.scanned))}${tree.data.delete_ready ? "" : ` · Delete blocked: ${tree.data.delete_reason}`}`; if (!repos.length && !tasks.length && !gone.length) { - tree.dom.list.replaceChildren(emptyState("Nothing found", "No repository matches the filter.")); - return; + return {count: 0, nodes: [q ? emptyState("Nothing found", "No repository matches the filter.") : emptyState("No repository", "pkgmgr names no repository.")]}; } const cards = repos.map(({repo, branches, detached}) => { const trees = repo.branches.flatMap((entry) => entry.worktrees).concat(repo.detached); const dirty = trees.filter((wt) => wt.dirty).length; const merged = repo.branches.filter((entry) => entry.cleanup).length; const busy = trees.filter((wt) => wt.open_work.length).length; - const details = h("details", {class: "card repo", open: Boolean(q) || tree.open.has(repo.path)}, - h("summary", {}, icon("bi-git"), h("span", {}, repo.name), counter(`${repo.branches.length} branches`), counter(`${trees.length} worktrees`), + const details = h("details", {class: "card repo", open: Boolean(q) || tree.open.has(repo.path), "data-item": `artifact:${artifactRef(`repo:${repo.path}`)}`}, + h("summary", {"data-star-slot": ""}, icon("bi-git"), h("span", {}, repo.name), counter(`${repo.branches.length} branches`), counter(`${trees.length} worktrees`), dirty ? chip("bi-pencil-square", `${dirty} worktrees with changes`, "warn", dirty) : null, merged ? chip("bi-sign-merge-left", `${merged} merged, deletable`, "ok", merged) : null, busy ? chip("bi-lightning-charge", `${busy} worktrees with open work`, "warn", busy) : null), repo.branches.length ? branchAncestry(repo) : null, - branches.map((entry) => branchRow(repo, entry)), + branches.map((entry) => branchRow(repo, entry, glance)), detached.length ? h("div", {class: "branch-row"}, h("div", {class: "branch-name"}, h("strong", {}, "detached HEAD")), detached.map(worktreeRow)) : null); details.addEventListener("toggle", () => { if (details.open) { @@ -157,8 +168,8 @@ }); const loose = tasks.length ? h("details", {class: "card repo", open: true}, h("summary", {}, icon("bi-card-checklist"), h("span", {}, "Tasks without a worktree"), counter(tasks.length)), tasks.map((task) => h("div", {class: "branch-row"}, h("div", {class: "branch-name"}, h("strong", {}, task.task), h("span", {class: "muted"}, task.title), status(task.status)), - h("ul", {class: "leaves", "aria-label": "Artifacts"}, task.artefacts.map(leafRow))))) : null; + h("ul", {class: "leaves", "aria-label": "Artifacts"}, task.artefacts.map((found) => leafRow(`task:${task.task}`, found)))))) : null; const past = gone.length ? h("details", {class: "card repo", open: true}, h("summary", {}, icon("bi-trash3"), h("span", {}, "Recently deleted"), counter(gone.length)), gone.map((item) => h("div", {class: "branch-row", "data-result": item.status}, h("div", {class: "branch-name"}, h("strong", {}, item.branch), h("code", {}, item.repository), resultChip(item))))) : null; - tree.dom.list.replaceChildren(...cards, loose || "", past || ""); + return {count: repos.length, nodes: [...cards, loose || "", past || ""]}; } diff --git a/orchestrator/templates/panels.js b/orchestrator/templates/panels.js index 4711c92..565edf8 100644 --- a/orchestrator/templates/panels.js +++ b/orchestrator/templates/panels.js @@ -10,15 +10,19 @@ const Panels = (() => { const PANEL = "panel"; const SIZES = {panel: [6, 4], item: [4, 3]}; const HOSTS = "[data-panel], [data-item]"; + const TOO_LONG = "the id runs past 200 characters"; const XHTML = "http://www.w3.org/1999/xhtml"; const SVG = "http://www.w3.org/2000/svg"; const panels = new Map(); const items = new Map(); + const captures = new Map(); const placed = new WeakMap(); const keyOf = (pin) => `${pin.kind}:${pin.id}`; const span = (value) => Number.isInteger(value) && value >= 1 && value <= SPAN; const clamp = (value) => Math.min(Math.max(Math.round(value), 1), SPAN); + const plain = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value); + const keysOf = (pin) => Object.keys(pin).sort().join(","); /** * @param raw - an item's own id, any text. @@ -32,8 +36,22 @@ const Panels = (() => { const raw = (id) => id.replace(ESCAPED, (_, hex) => String.fromCodePoint(parseInt(hex, 16))); + function fnv(text, seed) { + let value = seed; + for (const char of text) { + value = Math.imul(value ^ char.codePointAt(0), 0x01000193) >>> 0; + } + return value.toString(16).padStart(8, "0"); + } + + /** + * @param text - any text, such as the canonical form of a pin's view. + * @returns 16 hex characters, two FNV-1a hashes of the text with different offsets. + */ + const digest = (text) => `${fnv(text, 0x811c9dc5)}${fnv(text, 0x01000193)}`; + function valid(pin) { - return Boolean(pin) && typeof pin === "object" && !Array.isArray(pin) && Object.keys(pin).sort().join(",") === "h,id,kind,w" + return plain(pin) && ["h,id,kind,w", "h,id,kind,view,w"].includes(keysOf(pin)) && (!Object.hasOwn(pin, "view") || plain(pin.view)) && typeof pin.kind === "string" && KIND.test(pin.kind) && typeof pin.id === "string" && ID.test(pin.id) && span(pin.w) && span(pin.h); } @@ -46,7 +64,8 @@ const Panels = (() => { return []; } const seen = new Set(); - return pins.filter((pin) => valid(pin) && !seen.has(keyOf(pin)) && Boolean(seen.add(keyOf(pin)))).slice(0, MAX).map(({kind, id, w, h}) => ({kind, id, w, h})); + return pins.filter((pin) => valid(pin) && !seen.has(keyOf(pin)) && Boolean(seen.add(keyOf(pin)))).slice(0, MAX) + .map(({kind, id, w, h, view}) => (view ? {kind, id, w, h, view} : {kind, id, w, h})); } /** @@ -61,39 +80,61 @@ const Panels = (() => { /** * @param kind - the item kind, the part of data-item before the first colon. - * @param title - the tile title, or (id) => title. - * @param render - (size, id) => Node, or null once the item no longer exists. + * @param title - the tile title, or (id, view) => title. + * @param render - (size, id, view) => Node, or null once the item no longer exists; view is the pin's stored view, + * undefined for a pin without one. */ function item(kind, title, render) { items.set(kind, {title, render, group: ""}); } - function lookup(kind, id) { + /** + * @param id - the data-panel of a source element whose star pins a bookmark of what it shows instead of itself. + * @param take - (element) => {kind, id, view, refused}: the item pin the star makes now, such as a chart with the + * series switched on at this moment; the star shows that pin's pressed state, or stays disabled with refused as its + * reason while refused is a non-empty text. + */ + function capture(id, take) { + captures.set(id, take); + } + + function lookup(kind, id, view) { const entry = kind === PANEL ? panels.get(id) : items.get(kind); if (!entry) { return null; } - const plain = kind === PANEL ? id : raw(id); - return {kind, id, group: entry.group, title: typeof entry.title === "function" ? entry.title(plain) : entry.title, render: (size) => entry.render(size, plain)}; + const name = kind === PANEL ? id : raw(id); + return {kind, id, group: entry.group, title: typeof entry.title === "function" ? entry.title(name, view) : entry.title, render: (size) => entry.render(size, name, view)}; } + /** + * @param element - a source element with data-panel or data-item. + * @returns {kind, id, view, refused} of the pin its star makes now, or null for a data-item without a kind; refused + * names why no pin is possible, such as an id past 200 characters, and id is null then unless a capture gave one. + */ function targetOf(element) { if (element.hasAttribute("data-panel")) { - return {kind: PANEL, id: element.getAttribute("data-panel")}; + const id = element.getAttribute("data-panel"); + return captures.has(id) ? captures.get(id)(element) : {kind: PANEL, id}; } const value = element.getAttribute("data-item") || ""; const cut = value.indexOf(":"); - return cut > 0 ? {kind: value.slice(0, cut), id: token(value.slice(cut + 1))} : null; + if (cut < 1) { + return null; + } + const id = token(value.slice(cut + 1)); + return id ? {kind: value.slice(0, cut), id} : {kind: value.slice(0, cut), id: null, refused: TOO_LONG}; } const has = (pins, kind, id) => pins.some((pin) => pin.kind === kind && pin.id === id); const groupOf = (pin) => (lookup(pin.kind, pin.id) || {group: ""}).group; /** + * @param view - optional plain object the new pin stores, such as the chart selection of a metric bookmark. * @returns {pins, refused, holder}: pins unchanged when the pin is already there or refused; refused is "" when the * pin is there, "full" at 64 pins and "group" when holder, a pin of the same single-instance group, is on Home. */ - function add(pins, kind, id) { + function add(pins, kind, id, view) { if (has(pins, kind, id)) { return {pins, refused: "", holder: null}; } @@ -106,7 +147,7 @@ const Panels = (() => { return {pins, refused: "group", holder}; } const [w, h] = kind === PANEL ? SIZES.panel : SIZES.item; - return {pins: [...pins, {kind, id, w, h}], refused: "", holder: null}; + return {pins: [...pins, plain(view) ? {kind, id, w, h, view} : {kind, id, w, h}], refused: "", holder: null}; } const firstOfGroup = (pins, pin) => !groupOf(pin) || pins.find((other) => groupOf(other) === groupOf(pin)) === pin; @@ -214,7 +255,7 @@ const Panels = (() => { return svg; } - function paint(button, on, title) { + function paint(button, on, title, refused) { const label = on ? "Unpin from Home" : "Pin to Home"; if (button.getAttribute("aria-pressed") !== String(on)) { button.setAttribute("aria-pressed", String(on)); @@ -223,10 +264,17 @@ const Panels = (() => { if (button.getAttribute("aria-label") !== label) { button.setAttribute("aria-label", label); } - const hint = `${label}: ${title}`; + const hint = refused ? `${label}: not possible, ${refused}` : `${label}: ${title}`; if (button.getAttribute("title") !== hint) { button.setAttribute("title", hint); } + if (button.hasAttribute("disabled") !== Boolean(refused)) { + if (refused) { + button.setAttribute("disabled", ""); + } else { + button.removeAttribute("disabled"); + } + } } function slotOf(element) { @@ -244,25 +292,18 @@ const Panels = (() => { return row; } - function button(element, options, enabled) { + function button(element, options) { const made = document.createElement("button"); made.setAttribute("type", "button"); made.setAttribute("class", "star btn small ghost icon-only"); - if (!enabled) { - made.setAttribute("disabled", ""); - made.setAttribute("aria-label", "Pin to Home"); - made.setAttribute("title", "Pin to Home: not possible, the id runs past 200 characters"); - made.append(glyph("bi-star")); - return made; - } made.addEventListener("pointerdown", (event) => event.stopPropagation()); made.addEventListener("click", (event) => { event.preventDefault(); event.stopPropagation(); const now = targetOf(element); - const entry = now && now.id ? lookup(now.kind, now.id) : null; + const entry = now && now.id && !now.refused ? lookup(now.kind, now.id, now.view) : null; if (entry) { - options.press(now.kind, now.id, entry.title); + options.press(now.kind, now.id, entry.title, now.view); } }); return made; @@ -273,26 +314,39 @@ const Panels = (() => { return; } const target = targetOf(element); - const entry = target && target.id ? lookup(target.kind, target.id) : null; + const entry = target && target.id ? lookup(target.kind, target.id, target.view) : null; if (!target || (target.id && !entry)) { return; } let made = placed.get(element); if (!made || !element.contains(made)) { - made = button(element, options, Boolean(entry)); + made = button(element, options); placed.set(element, made); slotOf(element).append(made); } - if (entry) { - paint(made, options.pinned(target.kind, target.id), entry.title); + const refused = target.refused || ""; + paint(made, !refused && options.pinned(target.kind, target.id), entry ? entry.title : "", refused); + } + + /** + * @param slot - a data-star-slot element that draw moves into freshly drawn content. + * @param draw - () => void, the redraw. + * @returns nothing; the star in the slot gets back the focus it had before draw moved it. + */ + function keepFocus(slot, draw) { + const held = slot && slot.contains(document.activeElement) ? document.activeElement : null; + draw(); + if (held && held.isConnected && document.activeElement !== held) { + held.focus(); } } /** * @param root - the element whose subtree gets a star on every element with data-panel or data-item; the star goes * into the element's own data-star-slot descendant, else into a star row the element gets as its first child. - * @param options - {pinned(kind, id) => boolean, press(kind, id, title), marks: [[selector, panel id], ...]}, marks - * naming source elements whose page carries no data-panel of its own. + * @param options - {pinned(kind, id) => boolean, press(kind, id, title, view), marks: [[selector, panel id], ...]}, + * marks naming source elements whose page carries no data-panel of its own; view is undefined unless a capture + * gave one. * @returns mark(): tags the marks, stars what lacks one and repaints the pressed state of all. */ function decorate(root, options) { @@ -307,8 +361,8 @@ const Panels = (() => { } return { - MAX, SPAN, PANEL, ID, HOSTS, keyOf, token, raw, valid, validLayout, clean, panel, item, lookup, targetOf, has, add, remove, firstOfGroup, - place, move, resize, placeKey, moveKey, resizeKey, resizeBy, stretch, memory, store, decorate, + MAX, SPAN, PANEL, ID, HOSTS, keyOf, token, raw, digest, valid, validLayout, clean, panel, item, capture, lookup, targetOf, has, add, remove, firstOfGroup, + place, move, resize, placeKey, moveKey, resizeKey, resizeBy, stretch, memory, store, decorate, keepFocus, }; })(); if (typeof module === "object" && module.exports) { diff --git a/orchestrator/templates/style-home.css b/orchestrator/templates/style-home.css index 0cfc254..7ed529d 100644 --- a/orchestrator/templates/style-home.css +++ b/orchestrator/templates/style-home.css @@ -18,6 +18,14 @@ .home-gone { display: grid; place-content: center; justify-items: center; gap: var(--sp-2); height: 100%; text-align: center; } .home-gone-word { color: var(--bad); font-size: var(--fs-lg); text-transform: uppercase; letter-spacing: .06em; } .home-agent { display: grid; gap: var(--sp-2); } +.home-metric { display: flex; flex-direction: column; gap: var(--sp-1); height: 100%; min-height: 0; } +.home-metric .res-plot { flex: 1; min-height: 60px; } +.home-metric .res-plot > svg { height: 100%; max-height: none; } +.home-metric .legend-chip, .home-order li { cursor: default; } +.home-inbox { display: grid; gap: var(--sp-2); margin: 0; padding: 0; list-style: none; } +.home-inbox > li { display: grid; gap: var(--sp-1); padding: var(--sp-2); border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--sunken); } +.home-inbox .msg-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); } +.leaf > .star-row { order: 1; margin-inline-start: auto; } @media (max-width: 640px) { .home-tile { grid-column: 1 / -1; } .legend[data-star-slot] > .star { order: 1; } diff --git a/tests/e2e/test_home_bookmarks_e2e.py b/tests/e2e/test_home_bookmarks_e2e.py new file mode 100644 index 0000000..eb3e3cc --- /dev/null +++ b/tests/e2e/test_home_bookmarks_e2e.py @@ -0,0 +1,485 @@ +from __future__ import annotations + +import json +import os +import shutil +import sys +import time +import unittest +from collections.abc import Callable +from pathlib import Path +from unittest import mock + +HERE = Path(__file__).resolve().parent +sys.path[:0] = [str(HERE.parent), str(HERE)] + +import test_home_dashboard_e2e as dash +import test_orchestrator_e2e as base +import test_resources_view_e2e as res +import test_workbench_w4_e2e as w4 +from hub_case import HubCase +from hub_support import ( + CORE_WORKTREE, + LONG_REPO, + LONG_TASK, + LONG_VOLUME, + LONG_WORKTREE, + iso, + long_repos, + long_task, +) + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/bookmarks-shots")) +CUE = "e2e-bookmarks-vanish" +GHOST = "Ghost" +KEPT = "feature/kept" +HARDWARE = ["disk:/", "disk:/boot/efi", res.CPU, "host:RAM", "host:Swap", res.UTIL, "vram:0000_3a_01_3a_00.0"] +WALKED = ["resources", "artifacts/tree", "agents/hierarchy", "tasks/operator"] +LONG_NAME = "/".join(Path(LONG_REPO).parts[-3:]) +LONG_ROWS = ("repo", "worktree", "core", "leaf") +READ = res.READ.replace("const stored = async", "const storedResources = async").replace("await stored()", "await storedResources()") +KIT = """ + const stars = (selector) => all(selector).map((owner) => Boolean(ownStar(owner))); + const panel = (kind) => `#main [data-panel="resources-${kind}"]`; + const lineIds = (root) => [...new Set(all("path.res-line", root).map((path) => path.dataset.id))]; + const tileOf = (kind) => all("#main .home-tile").find((node) => node.dataset.pin.startsWith(`metric:${kind}/`)) || null; + const pinOf = (kind) => (ViewStore.get("home.pins") || []).find((pin) => pin.kind === "metric" && pin.id.startsWith(`${kind}/`)) || null; + const kept = () => all("#main .branch-row[data-item]").find((row) => row.dataset.item.endsWith("\\u0000@KEPT@")) || null; + const settled = () => until(() => !all("#main .home-tile .empty h2").some((title) => text(title).endsWith("…")), 8000); + const longRows = { + repo: () => all("#main details.repo[data-item]").find((node) => text(node.querySelector("summary")).includes("@LONG_NAME@")) || null, + worktree: () => all("#main .wt[data-item]").find((node) => text(node.querySelector(".wt-head > code")) === "@LONG_WORKTREE@") || null, + core: () => all("#main .wt[data-item]").find((node) => text(node.querySelector(".wt-head > code")) === "@CORE_WORKTREE@") || null, + leaf: () => all("#main li.leaf[data-item]").find((node) => text(node.querySelector("code")) === "@LONG_VOLUME@") || null, + }; + const pinKey = (node) => `artifact:${Panels.token(node.dataset.item.slice(node.dataset.item.indexOf(":") + 1))}`; + const timeOverlaps = (root) => { + const boxes = all("text.axis", root).filter((node) => !["left", "right"].includes(node.dataset.axis)).map((node) => node.getBoundingClientRect()); + return boxes.filter((box, index) => boxes.slice(index + 1).some((other) => Math.min(box.right, other.right) - Math.max(box.left, other.left) > 0.5 && Math.min(box.bottom, other.bottom) - Math.max(box.top, other.top) > 0.5)).length; + }; +""" +CLEAR = """(async () => { + sessionStorage.clear(); + await Promise.all([ViewStore.update("home.pins", () => []), ViewStore.update("resources.hidden", () => []), ViewStore.update("resources.combinedHidden", () => []), + ViewStore.set("resources.combined", false), ViewStore.set("resources.tokens", "interval")]); + return true; +})()""" +LOADED = """(async () => {@HELPERS@@READ@ + await screenShown(8000); + const drawn = Boolean(await until(() => charts().length === 2 && chip("@CPU@"), 15000)); + return {drawn, charts: charts()}; +})()""" +FOUND = """(async () => {@HELPERS@@HOME@@KIT@ + const found = {}; + await go("resources"); + found.charts = stars('#main section.res-panel[data-panel^="resources-"]'); + found.chips = all("#main .legend-chip .star").length; + found.inHead = all("#main .res-panel .star").every((star) => star.parentElement.classList.contains("res-head")); + await go("artifacts/tree"); + await until(() => kept(), 8000); + found.tree = stars('#main [data-panel="artifacts/tree"]'); + found.rows = Object.fromEntries(["repo", "branch", "wt"].map((kind) => [kind, stars(`#main [data-item^="artifact:${kind}:"]`)])); + await until(() => Object.values(longRows).every((find) => find()), 8000); + found.long = Object.fromEntries(Object.entries(longRows).map(([key, find]) => [key, find() ? find().dataset.item.length : null])); + found.disabled = all('#main [data-item^="artifact:"]').filter((node) => !ownStar(node) || ownStar(node).disabled).map((node) => node.dataset.item.slice(0, 80)); + found.deletes = all('#main button[aria-label="Delete"]').length; + await go("agents/hierarchy"); + found.order = stars('#main [data-panel="agents/order"]'); + await go("tasks/operator"); + found.operator = stars('#main [data-panel="tasks/operator"]'); + return found; +})()""" +METRIC = """(async () => {@HELPERS@@HOME@@READ@@KIT@ + await go("resources"); + if (!(await until(() => charts().length === 2 && chip("@CPU@") && lines().length === @COUNT@, 15000))) { + return {error: text(document.getElementById("main")), charts: charts()}; + } + const axisOverlaps = timeOverlaps(document.querySelector("#main .res-panels")); + const others = @HARDWARE@.filter((id) => id !== "@CPU@"); + for (const id of others) { + chip(id).click(); + await sleep(120); + } + const cpuOnly = Boolean(await until(() => lineIds(document.querySelector(panel("hardware"))).join() === "@CPU@", 5000)); + const hardware = await press(panel("hardware")); + const cpuTitle = starOf(panel("hardware")).getAttribute("title"); + const tokens = await press(panel("tokens")); + for (const id of others) { + chip(id).click(); + await sleep(120); + } + const back = Boolean(await until(() => lineIds(document.querySelector(panel("hardware"))).length === @COUNT@, 5000)); + await sleep(200); + const allState = [starOf(panel("hardware")).getAttribute("aria-pressed"), starOf(panel("hardware")).getAttribute("title")]; + switched("combined").click(); + const combinedOn = Boolean(await until(() => charts().join() === "combined" && burn().length === 2, 5000)); + const combined = await press(panel("combined")); + return {cpuOnly, hardware, cpuTitle, tokens, back, allState, combinedOn, combined, axisOverlaps, stored: (ViewStore.get("home.pins") || []).map((pin) => [pin.kind, pin.id, pin.view || null])}; +})()""" +COMBINED_OFF = """(async () => {@HELPERS@@HOME@@READ@@KIT@ + await go("resources"); + await until(() => charts().join() === "combined", 8000); + switched("combined").click(); + const off = Boolean(await until(() => charts().length === 2 && lines().length === @COUNT@, 8000)); + await sleep(300); + return {off, hardware: [starOf(panel("hardware")).getAttribute("aria-pressed"), starOf(panel("hardware")).getAttribute("title")]}; +})()""" +HOME_METRICS = """(async () => {@HELPERS@@HOME@@KIT@ + await go("home"); + await until(() => ["hardware", "tokens", "combined"].every((kind) => tileOf(kind) && tileOf(kind).querySelector("svg.res-chart")), 15000); + await sleep(300); + const read = (kind) => { + const found = tileOf(kind); + return found ? { + title: text(found.querySelector(".home-title")), lines: lineIds(found), legend: all(".legend-chip", found).map((node) => node.dataset.series), + chart: (found.querySelector("svg.res-chart") || {dataset: {}}).dataset.chart || null, right: all('text[data-axis="right"]', found).length, + burn: all("path.res-burn", found).length, bars: [...new Set(all("rect.res-bar", found).map((rect) => rect.dataset.agent))].sort(), + gone: found.classList.contains("gone"), + } : null; + }; + const overlaps = Object.fromEntries(["hardware", "tokens", "combined"].map((kind) => [kind, tileOf(kind) ? timeOverlaps(tileOf(kind)) : null])); + const sized = (kind, step) => ViewStore.update("home.pins", (pins) => pins.map((pin) => (pin.kind === "metric" && pin.id.startsWith(`${kind}/`) ? {...pin, h: pin.h + step} : pin))); + await sized("combined", -1); + await sleep(600); + const body = tileOf("combined").querySelector(".home-body"); + const scroll = {scrollable: body.scrollHeight > body.clientHeight + 10}; + if (scroll.scrollable) { + body.scrollTop = body.scrollHeight; + scroll.before = body.scrollTop; + await sized("hardware", 1); + await sleep(400); + await sized("hardware", -1); + await sleep(400); + scroll.after = body.scrollTop; + } + await sized("combined", 1); + await sleep(300); + return {tiles: tiles(), hardware: read("hardware"), tokens: read("tokens"), combined: read("combined"), errors: all("#main .home-error").map(text), stored: pinOf("hardware"), overlaps, scroll}; +})()""" +PM = """(async () => {@HELPERS@@HOME@@KIT@ + await go("artifacts/tree"); + const row = await until(() => kept(), 8000); + row.closest("details").open = true; + const tree = await press('#main [data-panel="artifacts/tree"]'); + const rowStar = row ? ownStar(row) : null; + if (rowStar) { + rowStar.click(); + await sleep(200); + } + const branch = rowStar ? (ownStar(kept()) || rowStar).getAttribute("aria-pressed") : null; + await go("agents/hierarchy"); + const order = await press('#main [data-panel="agents/order"]'); + await go("tasks/operator"); + const operator = await press('#main [data-panel="tasks/operator"]'); + await go("agents/orbit"); + const node = await until(() => all("#main svg .node[data-pick]").find((found) => (found.getAttribute("aria-label") || "").startsWith("@GHOST@,")), 8000); + let agent = null; + if (node) { + node.dispatchEvent(new MouseEvent("click", {bubbles: true})); + await until(() => document.querySelector('#popup[open] [data-item^="agent:"]'), 5000); + agent = await press('#popup[open] [data-item^="agent:"]'); + document.getElementById("popup").close(); + } + await go("home"); + await settled(); + await sleep(500); + const keys = stored(); + const pick = (kind) => keys.find((key) => key.startsWith(`${kind}:`)) || null; + const shown = (key) => { + const found = key ? tile(key) : null; + return found ? {gone: found.classList.contains("gone"), title: text(found.querySelector(".home-title"))} : null; + }; + return { + pressed: {tree, branch, order, operator, agent}, keys, + tree: [shown("panel:artifacts/tree"), all('#main .home-tile[data-pin="panel:artifacts/tree"] details.repo').length], + order: [shown("panel:agents/order"), all('#main .home-tile[data-pin="panel:agents/order"] .home-order li').map((item) => item.dataset.name)], + operator: [shown("panel:tasks/operator"), all('#main .home-tile[data-pin="panel:tasks/operator"] .home-inbox > li').length], + artifact: [shown(pick("artifact")), text(tile(pick("artifact")) && tile(pick("artifact")).querySelector(".home-body h2.panel-title"))], + agent: [shown(pick("agent")), Boolean(tile(pick("agent")) && tile(pick("agent")).querySelector(".home-agent"))], + errors: all("#main .home-error").map(text), + }; +})()""" +LONG = """(async () => {@HELPERS@@HOME@@KIT@ + await go("artifacts/tree"); + await until(() => Object.values(longRows).every((find) => find()), 8000); + const pressed = {}; + const keys = {}; + for (const [key, find] of Object.entries(longRows)) { + const star = ownStar(find()); + keys[key] = pinKey(find()); + pressed[key] = star ? [star.disabled, star.title.startsWith("Pin to Home: Artifact ")] : null; + if (star && !star.disabled) { + star.click(); + await sleep(200); + } + pressed[key].push(ownStar(longRows[key]()).getAttribute("aria-pressed")); + } + await go("home"); + await settled(); + await until(() => Object.values(keys).every((key) => tile(key) && tile(key).querySelector(".home-popup")), 8000); + await sleep(300); + const shown = Object.fromEntries(Object.entries(keys).map(([key, pin]) => { + const found = tile(pin); + return [key, found ? {gone: found.classList.contains("gone"), title: text(found.querySelector(".home-title")), label: text(found.querySelector(".home-body h2.panel-title")), body: text(found.querySelector(".home-body"))} : null]; + })); + const treeTile = tile("panel:artifacts/tree"); + const body = treeTile ? treeTile.querySelector(".home-body") : null; + const scroll = {deletes: treeTile ? all('button[aria-label="Delete"]', treeTile).length : null, scrollable: body ? body.scrollHeight > body.clientHeight + 40 : null}; + if (scroll.scrollable) { + body.scrollTop = 40; + scroll.before = body.scrollTop; + const order = (step) => ViewStore.update("home.pins", (pins) => pins.map((pin) => (pin.kind === "panel" && pin.id === "agents/order" ? {...pin, h: pin.h + step} : pin))); + await order(1); + await sleep(400); + await order(-1); + await sleep(400); + scroll.after = body.isConnected ? body.scrollTop : null; + } + return {pressed, keys, stored: stored(), shown, scroll, errors: all("#main .home-error").map(text)}; +})()""" +RELOADED = """(async () => {@HELPERS@@HOME@@KIT@ + await screenShown(8000); + await until(() => tileOf("hardware") && tileOf("hardware").querySelector("svg.res-chart"), 15000); + await sleep(300); + return {hash: location.hash, lines: lineIds(tileOf("hardware")), title: text(tileOf("hardware").querySelector(".home-title")), stored: pinOf("hardware")}; +})()""" +GONE = """(async () => {@HELPERS@@HOME@@KIT@ + await go("home"); + const keys = stored(); + const agentKey = keys.find((key) => key.startsWith("agent:")); + const artifactKeys = keys.filter((key) => key.startsWith("artifact:")); + const before = [agentKey, ...artifactKeys].map((key) => tile(key).classList.contains("gone")); + await fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: "@CUE@", voice: ""}), cache: "no-store"}); + const started = Date.now(); + const vanished = await until(() => [agentKey, ...artifactKeys].every((key) => tile(key).classList.contains("gone")), 50000); + const seconds = Math.round((Date.now() - started) / 1000); + const words = (key) => (tile(key) ? [text(tile(key).querySelector(".home-gone-word")), name(tile(key).querySelector(".home-gone-unpin"))] : null); + tile(artifactKeys[0]).scrollIntoView({block: "start"}); + await sleep(300); + return {before, vanished: Boolean(vanished), seconds, agent: words(agentKey), artifacts: artifactKeys.map(words), agentKey, artifactKeys}; +})()""" +UNPIN = """(async () => {@HELPERS@@HOME@@KIT@ + await go("home"); + const keys = stored(); + const metric = keys.find((key) => key.startsWith("metric:hardware/")); + const artifacts = keys.filter((key) => key.startsWith("artifact:")); + tile(metric).querySelector(".home-unpin").click(); + await sleep(300); + for (const key of artifacts) { + const gone = tile(key) ? tile(key).querySelector(".home-gone-unpin") : null; + if (gone) { + gone.click(); + await sleep(300); + } + } + return {metric, artifacts, tiles: tiles(), stored: stored()}; +})()""" +UNPIN_PLAIN = """(async () => {@HELPERS@@HOME@@KIT@ + await go("home"); + const metric = stored().find((key) => key.startsWith("metric:hardware/")); + tile(metric).querySelector(".home-unpin").click(); + await sleep(300); + return {metric, artifacts: [], tiles: tiles(), stored: stored()}; +})()""" + + +def probe(template: str, screens: list[str] | None = None) -> str: + names = { + "@HELPERS@": base.HELPERS, "@HOME@": dash.HOME, "@READ@": READ, "@KIT@": KIT, "@KEPT@": KEPT, "@GHOST@": GHOST, "@CUE@": CUE, "@CPU@": res.CPU, + "@HARDWARE@": json.dumps(HARDWARE), "@COUNT@": str(len(HARDWARE)), "@SCREENS@": json.dumps(screens or WALKED), + "@LONG_NAME@": LONG_NAME, "@LONG_WORKTREE@": LONG_WORKTREE, "@CORE_WORKTREE@": CORE_WORKTREE, "@LONG_VOLUME@": LONG_VOLUME, + } + for key, value in names.items(): + template = template.replace(key, value) + return template + + +@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") +class TestHomeBookmarks(HubCase): + seed = base.TestOrchestratorE2E.seed + subagents = base.TestOrchestratorE2E.subagents + drive = base.TestOrchestratorE2E.drive + sample = w4.TestWorkbenchResources.sample + + def setUp(self): + super().setUp() + OUT.mkdir(parents=True, exist_ok=True) + self.seed() + now = int(time.time()) + self.start = now - now % 60 - w4.ROWS * 60 + hour = self.start - self.start % 3600 + tokens = [{"t": hour + step * 3600, "agent": "Skills", "model": "claude-opus-5-5", "input": 1000 * (step + 1), "output": 500, "cache_read": 20000, "cache_write": 1000} for step in range(7)] + tokens += [{"t": hour + step * 3600, "agent": "Stage", "model": "claude-haiku-4-5", "input": 200, "output": 100, "cache_read": 3000, "cache_write": 0} for step in range(0, 7, 2)] + folder = self.kept / "resources" + folder.mkdir() + (folder / "samples.jsonl").write_text(self.jsonl([self.sample(index) for index in range(w4.ROWS)]), encoding="utf-8") + (folder / "tokens.jsonl").write_text(self.jsonl([row for row in tokens if row["t"] <= now]), encoding="utf-8") + register = self.council / "rasci.json" + rows = json.loads(register.read_text(encoding="utf-8")) + rows["work/ghost"] = {"title": "Ghost line", "status": "running", "worker": GHOST, "start": iso(now - 1800), "roles": {GHOST: "RA"}} + rows[LONG_TASK] = long_task(now) + register.write_text(json.dumps(rows), encoding="utf-8") + scan = json.loads(self.tree_file.read_text(encoding="utf-8")) + scan["repos"] += long_repos() + self.tree_file.write_text(json.dumps(scan), encoding="utf-8") + + def vanish(self) -> None: + register = self.council / "rasci.json" + rows = json.loads(register.read_text(encoding="utf-8")) + rows.pop("work/ghost", None) + rows.pop(LONG_TASK, None) + register.write_text(json.dumps(rows), encoding="utf-8") + scan = json.loads(self.tree_file.read_text(encoding="utf-8")) + long = {repo["path"] for repo in long_repos()} + scan["repos"] = [repo for repo in scan["repos"] if repo["path"] not in long] + for repo in scan["repos"]: + repo["items"] = [item for item in repo["items"] if item.get("branch") != KEPT] + self.tree_file.write_text(json.dumps(scan), encoding="utf-8") + + def cue(self) -> None: + spoken = base.tone(1, 8000) + + def synthesize(body: bytes, admit: Callable[[str], None]) -> bytes: + if json.loads(body)["text"] == CUE: + self.vanish() + return spoken + + patch = mock.patch.object(self.httpd.hub.speech, "synthesize", synthesize) + patch.start() + self.addCleanup(patch.stop) + + def steps(self, browser: str) -> list[dict]: + self.cue() + steps = [] + for index, (width, height) in enumerate(base.WIDTHS): + shot = str(OUT / f"{browser}-{width}") + page = f"{self.base}?{width}" + last = index == len(base.WIDTHS) - 1 + steps += [ + {"width": width, "height": height, "url": f"{page}-clear#stream", "wait": 600, "probe": CLEAR, "shot": None}, + {"width": width, "height": height, "url": f"{page}-loaded#resources", "wait": 1200, "probe": probe(LOADED), "shot": None}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(FOUND), "shot": None}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(dash.STARS), "shot": None}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(METRIC), "shot": f"{shot}-combined-star.png"}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(dash.STARS, ["resources"]), "shot": None}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(COMBINED_OFF), "shot": f"{shot}-metric-star.png"}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(HOME_METRICS), "shot": f"{shot}-home-metrics.png"}, + {"width": width, "height": height, "url": f"{page}-reloaded#home", "wait": 1500, "probe": probe(RELOADED), "shot": None}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(PM), "shot": f"{shot}-home-bookmarks.png"}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(LONG), "shot": f"{shot}-home-long-paths.png"}, + ] + if last: + steps.append({"width": width, "height": height, "url": None, "wait": 0, "probe": probe(GONE), "shot": f"{shot}-gone.png"}) + steps.append({"width": width, "height": height, "url": None, "wait": 0, "probe": probe(UNPIN if last else UNPIN_PLAIN), "shot": None}) + return steps + + def check(self, data: dict, browser: str) -> None: + results = iter(data["results"]) + for index, (width, _) in enumerate(base.WIDTHS): + last = index == len(base.WIDTHS) - 1 + with self.subTest(browser=browser, width=width): + next(results) + loaded = next(results) + self.assertTrue(loaded["drawn"], loaded) + found = next(results) + self.assertEqual((found["charts"], found["chips"], found["inHead"]), ([True, True, True], 0, True), found) + self.assertEqual(found["tree"], [True], found) + self.assertTrue(found["rows"]["repo"] and all(found["rows"]["repo"]), found) + self.assertTrue(found["rows"]["branch"] and all(found["rows"]["branch"]), found) + self.assertTrue(found["rows"]["wt"] and all(found["rows"]["wt"]), found) + self.assertEqual((found["order"], found["operator"]), ([True], [True]), found) + self.assertTrue(all(found["long"].values()), found) + self.assertGreater(found["long"]["leaf"], 0, found) + self.assertEqual(found["disabled"], [], found) + self.assertGreaterEqual(found["deletes"], 1, found) + walked = next(results) + self.assertEqual((walked["missing"], walked["covered"]), ([], []), walked) + metric = next(results) + self.assertNotIn("error", metric, metric) + self.assertEqual((metric["cpuOnly"], metric["hardware"], metric["tokens"], metric["back"], metric["combinedOn"], metric["combined"]), (True, "true", "true", True, True, "true"), metric) + self.assertEqual(metric["cpuTitle"], "Unpin from Home: Hardware: CPU") + self.assertEqual(metric["axisOverlaps"], 0, metric) + self.assertEqual(metric["allState"], ["false", "Pin to Home: Hardware: all series"]) + kinds = [[kind, key.split("/")[0], view] for kind, key, view in metric["stored"]] + self.assertEqual(kinds, [ + ["metric", "hardware", {"chart": "hardware", "tokens": "interval", "shown": [res.CPU]}], + ["metric", "tokens", {"chart": "tokens", "tokens": "interval", "shown": None}], + ["metric", "combined", {"chart": "combined", "tokens": "interval", "shown": None}], + ]) + self.assertRegex(metric["stored"][0][1], r"^hardware/[0-9a-f]{16}$") + self.assertEqual([key for _, key, _ in metric["stored"][1:]], ["tokens/interval/all", "combined/interval/all"]) + combined_walk = next(results) + self.assertEqual((combined_walk["missing"], combined_walk["covered"]), ([], []), combined_walk) + self.assertEqual(next(results), {"off": True, "hardware": ["false", "Pin to Home: Hardware: all series"]}) + home = next(results) + self.assertEqual(home["errors"], [], home) + self.assertEqual((home["hardware"]["title"], home["hardware"]["lines"], home["hardware"]["legend"], home["hardware"]["chart"]), ("Hardware: CPU", [res.CPU], [res.CPU], "hardware"), home) + self.assertEqual((home["tokens"]["title"], home["tokens"]["bars"]), ("Tokens: all series", ["Skills", "Stage"]), home) + self.assertEqual((home["combined"]["title"], home["combined"]["chart"], home["combined"]["lines"]), ("Combined: all series", "combined", HARDWARE), home) + self.assertGreater(home["combined"]["right"], 0, home) + self.assertEqual(home["combined"]["burn"], 2, home) + self.assertEqual(home["overlaps"], {"hardware": 0, "tokens": 0, "combined": 0}, home) + if home["scroll"]["scrollable"]: + self.assertEqual(home["scroll"]["after"], home["scroll"]["before"], home) + reloaded = next(results) + self.assertEqual((reloaded["hash"], reloaded["lines"], reloaded["title"]), ("#home", [res.CPU], "Hardware: CPU"), reloaded) + self.assertEqual(reloaded["stored"]["view"], {"chart": "hardware", "tokens": "interval", "shown": [res.CPU]}) + pm = next(results) + self.assertEqual(pm["pressed"], {"tree": "true", "branch": "true", "order": "true", "operator": "true", "agent": "true"}, pm) + self.assertEqual(pm["errors"], [], pm) + self.assertEqual((pm["tree"][0], pm["order"][0]["gone"], pm["operator"]), ({"gone": False, "title": "Artifacts: tree"}, False, [{"gone": False, "title": "Tasks: Waits on Operator"}, 2]), pm) + self.assertGreaterEqual(pm["tree"][1], 2, pm) + self.assertGreaterEqual(len(pm["order"][1]), 2, pm) + self.assertEqual((pm["artifact"][0]["gone"], pm["artifact"][1]), (False, KEPT), pm) + self.assertEqual((pm["agent"][0], pm["agent"][1]), ({"gone": False, "title": f"Agent {GHOST}"}, True), pm) + self.check_long(next(results)) + if last: + gone = next(results) + self.assertEqual(gone["before"], [False] * 6, gone) + self.assertTrue(gone["vanished"], gone) + self.assertEqual((gone["agent"], gone["artifacts"]), (["gone", "Unpin"], [["gone", "Unpin"]] * 5), gone) + unpinned = next(results) + self.assertNotIn(unpinned["metric"], unpinned["tiles"]) + self.assertNotIn(unpinned["metric"], unpinned["stored"]) + if last: + self.assertEqual(len(unpinned["artifacts"]), 5, unpinned) + self.assertFalse(set(unpinned["artifacts"]) & set(unpinned["tiles"] + unpinned["stored"]), unpinned) + + def check_long(self, long: dict) -> None: + self.assertEqual(long["errors"], [], long) + self.assertEqual(long["pressed"], {key: [False, True, "true"] for key in LONG_ROWS}, long) + self.assertEqual([key for key in long["stored"] if key in long["keys"].values()], list(long["keys"].values()), long) + for key in ("core", "leaf"): + self.assertRegex(long["keys"][key], r"^artifact:(wt|artefact)_23_[0-9a-f]{16}$", long) + labels = {"repo": LONG_REPO, "worktree": LONG_WORKTREE, "core": CORE_WORKTREE, "leaf": LONG_VOLUME} + names = {"repo": LONG_NAME, "worktree": "/".join(Path(LONG_WORKTREE).parts[-2:]), "core": "/".join(Path(CORE_WORKTREE).parts[-2:]), "leaf": LONG_VOLUME.split(":", 1)[1]} + for key in LONG_ROWS: + shown = long["shown"][key] + self.assertIsNotNone(shown, (key, long)) + self.assertFalse(shown["gone"], (key, shown)) + self.assertEqual(shown["label"], names[key], (key, shown)) + self.assertIn(labels[key], shown["body"], (key, shown)) + self.assertTrue(shown["title"].startswith("Artifact "), (key, shown)) + self.assertIn(LONG_WORKTREE, long["shown"]["worktree"]["title"]) + self.assertIn(CORE_WORKTREE, long["shown"]["core"]["title"]) + self.assertIn(LONG_VOLUME, long["shown"]["leaf"]["title"]) + self.assertEqual(long["scroll"]["deletes"], 0, long) + if long["scroll"]["scrollable"]: + self.assertEqual(long["scroll"]["after"], long["scroll"]["before"], long) + + def test_chromium_pins_metrics_with_their_selection_views_and_single_elements_at_three_widths(self): + data = self.drive(self.steps("chromium"), None, init=w4.GUARD) + (OUT / "chromium-bookmarks.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual(data["errors"], []) + self.check(data, "chromium") + + @unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox") + def test_firefox_pins_metrics_with_their_selection_views_and_single_elements_at_three_widths(self): + data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=w4.GUARD) + (OUT / "firefox-bookmarks.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, "firefox") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/hub_support.py b/tests/hub_support.py index 5525a06..47daea0 100644 --- a/tests/hub_support.py +++ b/tests/hub_support.py @@ -43,7 +43,16 @@ GIT_ENV = { "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", } TREE = "0123456789abcdef0123456789abcdef01234567" -PAGE = "diff" +LONG_REPO = "/home/operator-example-01/Repositories/github.com/operator-example-01/skills" +LONG_WORKTREE = f"{LONG_REPO}/.worktrees/feature/hub-bookmarks-everywhere" +LONG_BRANCH = "feature/hub-bookmarks-everywhere" +LONG_TASK = "work/bookmarks" +LONG_VOLUME = "volume:infinito_nexus_core_mariadb_data" +CORE_REPO = "/home/operator-example-01/Repositories/github.com/infinito-nexus/core" +CORE_BRANCH = "feature/store-workspace-proxmox-cosmos-requirements-statically-into-iam-before-the-final-compose-and-swarm-roundtrip" +CORE_WORKTREE = f"/home/operator-example-01/.local/share/worktrees/github.com/infinito-nexus/core/{CORE_BRANCH}" +MERGED_BRANCH = "feature/merged-away" +PAGE ="diff" HOST_STATE = Path("/home/op/.local/state/kvb-orchestrator") ASK = {"tool": "Bash", "command": "make deploy", "cwd": "/repo", "session": "s1", "agent": "", "timeout": 60} HOOK = { @@ -98,6 +107,51 @@ def iso(ts: float) -> str: return datetime.datetime.fromtimestamp(ts, datetime.UTC).isoformat().replace("+00:00", "Z") +def scan_item(branch: str, worktree: str | None, state: str, sha: str) -> dict: + """Args: + branch: the branch name. + worktree: its worktree path, None for a branch without one. + state: the gc state, such as unmerged or merged-clean. + sha: the local tip. + + Returns: + One pkgmgr branch tree item; a merged-clean one without a worktree + is a branch the tree offers to delete. + """ + merged = state == "merged-clean" + counts = 0 if worktree else None + return dict({ + "branch": branch, "worktree": worktree, "head": sha, "detached": False, "locked": None, "worktree_state": "clean" if worktree else "none", + "staged": counts, "unstaged": counts, "untracked": counts, "upstream": None, "ahead": None, "behind": None, "gone": False, "local_sha": sha, "remotes": {}, + "compose_projects": [], "images": [], "state": state, "merged_by": "#7" if merged else None, "merged_into": "main" if merged else None, "size_bytes": None, + "skip": None, "reason": state, "result": None, "flow": None, + }, actions=["local-branch"]) + + +def long_repos() -> list[dict]: + """Returns: + pkgmgr scan repositories on paths of real length: the skills + repository with the worktree of LONG_BRANCH and the merged + MERGED_BRANCH, and infinito-nexus/core with a worktree path of 196 + characters. + """ + def entry(path: str, items: list[dict]) -> dict: + return {"path": path, "remote": f"{path}.git", "default": "main", "missing": False, "known": True, "forge": "none", "scanned_at": "2026-11-14T12:00:00+00:00", "warnings": [], "items": items} + + return [ + entry(LONG_REPO, [scan_item(LONG_BRANCH, LONG_WORKTREE, "unmerged", "1" * 40), scan_item(MERGED_BRANCH, None, "merged-clean", "2" * 40)]), + entry(CORE_REPO, [scan_item(CORE_BRANCH, CORE_WORKTREE, "unmerged", "3" * 40)]), + ] + + +def long_task(now: float) -> dict: + """Returns: + The register row of LONG_TASK, working in LONG_WORKTREE and holding + the volume LONG_VOLUME. + """ + return {"title": "Bookmarks everywhere", "status": "running", "worker": "Skills session", "workdir": LONG_WORKTREE, "roles": {"Skills session": "RA"}, "artifacts": [{"ref": LONG_VOLUME, "added": iso(now - 600)}]} + + KEYS = tempfile.TemporaryDirectory() ORIGIN = "http://localhost:8737" PRESENT = 0x05 diff --git a/tests/test_hub_home.py b/tests/test_hub_home.py index 1e67d8b..9937096 100644 --- a/tests/test_hub_home.py +++ b/tests/test_hub_home.py @@ -3,10 +3,27 @@ from __future__ import annotations import json import re import subprocess +import time import unittest +from typing import ClassVar from hub_case import HubCase -from hub_support import HUB, NAV +from hub_support import ( + CORE_BRANCH, + CORE_REPO, + CORE_WORKTREE, + HUB, + LONG_BRANCH, + LONG_REPO, + LONG_TASK, + LONG_VOLUME, + LONG_WORKTREE, + MERGED_BRANCH, + NAV, + long_repos, + long_task, +) +from kvb_orchestrator.viewstore import SERIES_ID, SHOWN_LIMIT TEMPLATES = HUB / "templates" PANELS = TEMPLATES / "panels.js" @@ -185,7 +202,7 @@ function viewStore(server) { function page(server) { const store = viewStore(server); const toasts = []; - const context = vm.createContext({ViewStore: store, labelOf: (key) => key, toast: (title, lines, good) => toasts.push([title, lines.join(" "), good]), console}); + const context = vm.createContext({ViewStore: store, labelOf: (key) => key, toast: (title, lines, good) => toasts.push([title, lines.join(" "), good]), tree: {data: null, model: null}, console}); vm.runInContext(fs.readFileSync(panelsFile, "utf8"), context); vm.runInContext(`${fs.readFileSync(homeFile, "utf8")} globalThis.probe = {homePress, homeUnpin, homeView, Panels};`, context); @@ -198,9 +215,9 @@ PAGE_PROBE = TAB_KIT + """ const server = hub([]); const tab = page(server); const {homePress, homeUnpin, homeView, Panels} = tab; -const ids = Object.fromEntries(["panel", "task", "message", "artifact", "agent", "ancestry", "nope"].map((kind) => [kind, Boolean(Panels.lookup(kind, kind === "panel" ? "stream" : "x"))])); -const panels = ["stream", "radio-log", "agents/orbit", "agents/chord", "agents/swimlane", "agents/3d", "agents/tree", "agents/hierarchy", "artifacts/orbit", "artifacts/chord", "artifacts/swimlane", "artifacts/3d", - "tasks/list", "tasks/board", "tasks/rasci", "tasks/gantt", "tasks/burn", "tasks/3d"].filter((id) => !Panels.lookup("panel", id)); +const ids = Object.fromEntries(["panel", "task", "message", "artifact", "agent", "ancestry", "metric", "nope"].map((kind) => [kind, Boolean(Panels.lookup(kind, kind === "panel" ? "stream" : "x"))])); +const panels = ["stream", "radio-log", "agents/orbit", "agents/chord", "agents/swimlane", "agents/3d", "agents/tree", "agents/hierarchy", "agents/order", "artifacts/orbit", "artifacts/chord", "artifacts/swimlane", "artifacts/tree", "artifacts/3d", + "tasks/list", "tasks/board", "tasks/rasci", "tasks/gantt", "tasks/burn", "tasks/3d", "tasks/operator"].filter((id) => !Panels.lookup("panel", id)); (async () => { homePress("panel", "tasks/gantt", "Gantt"); await settle(); @@ -261,7 +278,7 @@ RACE_PROBE = TAB_KIT + """ """ -STAR_PROBE = """ +DOM_KIT = """ const fs = require("fs"); const vm = require("vm"); const XHTML = "http://www.w3.org/1999/xhtml"; @@ -275,8 +292,16 @@ class El { setAttribute(name, value) { this.attrs.set(name, String(value)); } getAttribute(name) { return this.attrs.has(name) ? this.attrs.get(name) : null; } hasAttribute(name) { return this.attrs.has(name); } + removeAttribute(name) { this.attrs.delete(name); } get classList() { const list = (this.getAttribute("class") || "").split(/\\s+/); return {contains: (name) => list.includes(name)}; } - detach() { if (this.parentNode) { this.parentNode.children = this.parentNode.children.filter((child) => child !== this); this.parentNode = null; } } + get isConnected() { let at = this; while (at.parentNode) { at = at.parentNode; } return at === document.body; } + focus() { document.activeElement = this; } + detach() { + if (this.parentNode) { + if (this.contains(document.activeElement)) { document.activeElement = document.body; } + this.parentNode.children = this.parentNode.children.filter((child) => child !== this); this.parentNode = null; + } + } append(...nodes) { nodes.forEach((child) => { child.detach(); child.parentNode = this; this.children.push(child); }); } prepend(...nodes) { nodes.reverse().forEach((child) => { child.detach(); child.parentNode = this; this.children.unshift(child); }); } replaceChildren(...nodes) { [...this.children].forEach((child) => child.detach()); this.append(...nodes); } @@ -292,10 +317,26 @@ const el = (tag, attrs, ...children) => { made.append(...children); return made; }; -const document = {createElement: (tag) => new El(tag), createElementNS: (ns, tag) => new El(tag, ns)}; +const document = {createElement: (tag) => new El(tag), createElementNS: (ns, tag) => new El(tag, ns), body: null, activeElement: null}; +document.body = new El("body"); +document.activeElement = document.body; +const text = (value) => { + const made = new El("#text"); + made.textContent = String(value); + return made; +}; +const h = (tag, attrs, ...children) => { + const made = new El(tag); + Object.entries(attrs || {}).filter(([name, value]) => name !== "on" && value !== null && value !== undefined && value !== false).forEach(([name, value]) => made.setAttribute(name, value)); + made.append(...children.flat(Infinity).filter((child) => child !== null && child !== undefined && child !== false && child !== "").map((child) => (child instanceof El ? child : text(child)))); + return made; +}; +const textOf = (node) => (node.tagName === "#TEXT" ? node.textContent : node.children.map(textOf).join("")); const context = vm.createContext({document, MutationObserver: class { observe() {} }, console}); vm.runInContext(fs.readFileSync(process.argv[1], "utf8"), context); const P = vm.runInContext("Panels", context); +""" +STAR_PROBE = DOM_KIT + """ P.panel("tasks/list", "Tasks: list", () => null); P.panel("radio-log", "Radio log", () => null); P.item("task", (id) => `Task ${id}`, () => null); @@ -366,11 +407,181 @@ const settle = () => new Promise((resolve) => setTimeout(resolve, 0)); """ +VIEW_PROBE = """ +const P = require(process.argv[1]); +const view = {chart: "hardware", tokens: "interval", shown: ["host:CPU"]}; +const pin = {kind: "metric", id: "hardware/x", w: 4, h: 3, view}; +P.item("metric", (id, seen) => `Metric ${id} ${seen ? seen.shown.join() : "none"}`, (size, id, seen) => (seen ? `${id} ${size.w} ${seen.chart}` : null)); +const added = P.add([], "metric", "hardware/x", view); +const looked = P.lookup("metric", "hardware/x", view); +const bare = P.lookup("metric", "hardware/x"); +console.log(JSON.stringify({ + valid: [P.validLayout([pin]), P.validLayout([{...pin, view: []}]), P.validLayout([{...pin, view: "x"}]), P.validLayout([{...pin, view: null}]), P.validLayout([{...pin, extra: 1}])], + cleaned: P.clean([pin, {...pin, id: "hardware/y", view: [1]}]), + added: [added.pins, P.add([], "task", "work/a").pins, P.add([], "task", "work/a", "x").pins], + resized: P.resizeKey(added.pins, "metric:hardware/x", 6, 5), + lookup: [looked.title, looked.render({w: 4, h: 3}), bare.title, bare.render({w: 1, h: 1})], + digest: [P.digest("host:CPU") === P.digest("host:CPU"), P.digest("host:CPU") === P.digest("host:RAM"), /^[0-9a-f]{16}$/.test(P.digest("")), P.ID.test(`hardware/${P.digest("x")}`)], +})); +""" +CAPTURE_PROBE = DOM_KIT + """ +P.item("metric", (id, view) => `Metric ${view.shown === null ? "all" : view.shown.join()}`, () => null); +let selection = null; +P.capture("resources-hardware", () => ({kind: "metric", id: selection === null ? "hardware/all" : "hardware/cpu", view: {chart: "hardware", tokens: "interval", shown: selection}})); +const head = el("div", {class: "res-head", "data-star-slot": ""}, el("span", {class: "res-title"})); +const chart = el("section", {"data-panel": "resources-hardware"}, head, el("div", {class: "res-plot"})); +const loose = el("section", {"data-panel": "resources-unknown"}); +const root = el("main", {}, chart, loose); +const clicked = []; +const mark = P.decorate(root, {pinned: (kind, id) => `${kind}:${id}` === "metric:hardware/cpu", press: (...args) => clicked.push(args)}); +const star = () => head.children.find((child) => child.classList.contains("star")); +const before = [star().getAttribute("aria-pressed"), star().getAttribute("title"), loose.children.length, chart.children[0] === head]; +selection = ["host:CPU"]; +mark(); +const after = [star().getAttribute("aria-pressed"), star().getAttribute("title"), head.children.filter((child) => child.classList.contains("star")).length]; +star().listeners.click[0]({preventDefault: () => null, stopPropagation: () => null}); +console.log(JSON.stringify({before, after, clicked, target: P.targetOf(chart)})); +""" +METRIC_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const [panelsFile, resourcesFile, homeFile] = process.argv.slice(1); +const context = vm.createContext({ViewStore: {get: () => ({tokens: "accumulated"}), update: () => Promise.resolve(true), on: () => null}, labelOf: (key) => key, toast: () => null, console}); +vm.runInContext(fs.readFileSync(panelsFile, "utf8"), context); +vm.runInContext(`${fs.readFileSync(resourcesFile, "utf8")}\\n${fs.readFileSync(homeFile, "utf8")}\\nglobalThis.probe = {resourcePin, resourceView, Panels};`, context); +const {resourcePin, resourceView, Panels} = context.probe; +const empty = resourcePin("tokens"); +resourceView.series.hardware = {ids: ["host:RAM", "host:CPU", "disk:/"], shown: ["host:CPU"], tokens: "accumulated"}; +resourceView.series.tokens = {ids: ["agent:Skills", "agent:Stage"], shown: ["agent:Stage", "agent:Skills"], tokens: "accumulated"}; +resourceView.series.combined = {ids: ["host:CPU", "agent:Skills", "burn:done"], shown: ["host:CPU", "burn:done"], tokens: "interval"}; +const pins = ["hardware", "tokens", "combined"].map(resourcePin); +resourceView.series.hardware = {ids: ["host:RAM", "host:CPU", "disk:/"], shown: ["host:RAM", "host:CPU", "disk:/"], tokens: "interval"}; +const all = resourcePin("hardware"); +const titles = [...pins, all, empty].map((pin) => Panels.lookup(pin.kind, pin.id, pin.view).title); +const four = Panels.lookup("metric", "x", {chart: "tokens", tokens: "interval", shown: ["agent:a", "agent:b", "agent:c", "agent:d"]}).title; +const broken = [Panels.lookup("metric", "x").title, Panels.lookup("metric", "x", {chart: "disks", shown: null}).render({w: 1, h: 1})]; +const limit = Number(process.argv[4]); +const agents = (count) => Array.from({length: count}, (_, index) => `agent:a${String(index).padStart(3, "0")}`); +const selected = (count, off) => { + resourceView.series.tokens = {ids: agents(count), shown: agents(count).slice(off), tokens: "interval"}; + const pin = resourcePin("tokens"); + return [pin.view.shown === null ? null : pin.view.shown.length, pin.refused || null, pin.id.split("/").length]; +}; +const limits = [selected(limit + 1, 1), selected(limit + 2, 1), selected(limit + 2, 0), selected(limit + 2, limit + 2)]; +console.log(JSON.stringify({empty, pins, all, titles, four, broken, limits, digests: [Panels.digest("host:CPU"), Panels.digest("burn:done\\nhost:CPU")]})); +""" +PRESS_PROBE = TAB_KIT + """ +const server = hub([]); +const tab = page(server); +const view = {chart: "hardware", tokens: "interval", shown: ["host:CPU"]}; +(async () => { + tab.homePress("metric", "hardware/abc", "Hardware: CPU", view); + await settle(); + const stored = clone(server.value); + tab.homePress("metric", "hardware/abc", "Hardware: CPU", view); + await settle(); + console.log(JSON.stringify({stored, after: server.value, pins: tab.homeView.pins})); +})(); +""" +REFUSED_PROBE = DOM_KIT + """ +P.item("metric", (id) => `Metric ${id}`, () => null); +P.item("artifact", (id) => `Artifact ${id}`, () => null); +let over = true; +P.capture("resources-tokens", () => (over ? {kind: "metric", id: "tokens/interval/x", view: {chart: "tokens"}, refused: "too many series"} : {kind: "metric", id: "tokens/interval/y", view: {chart: "tokens"}})); +const chart = el("section", {"data-panel": "resources-tokens"}, el("div", {"data-star-slot": ""})); +const long = el("li", {"data-item": `artifact:wt:/${"p".repeat(300)}`}); +const root = el("main", {}, chart, long); +const clicked = []; +const mark = P.decorate(root, {pinned: () => false, press: (...args) => clicked.push(args)}); +const star = (host) => host.all().find((node) => node.classList.contains("star")); +const read = (host) => [star(host).hasAttribute("disabled"), star(host).getAttribute("title"), star(host).getAttribute("aria-pressed")]; +const fire = (host) => (star(host).listeners.click || []).forEach((listen) => listen({preventDefault: () => null, stopPropagation: () => null})); +const refused = read(chart); +const first = star(chart); +fire(chart); +over = false; +mark(); +const freed = [...read(chart), star(chart) === first]; +fire(chart); +over = true; +mark(); +const again = read(chart); +fire(chart); +fire(long); +console.log(JSON.stringify({refused, freed, again, long: read(long), clicked})); +""" +ORDER_PROBE = DOM_KIT + """ +Object.assign(context, {h, state: {agent_order: ["Skills", "Stage"]}, ui: {order: null}, counter: (value) => text(value), face: () => text("face"), person: (name) => text(name), + icon: () => text("icon"), officeBtn: (name, label) => h("button", {"aria-label": label}), focusKey: () => null, refocus: () => null, act: async () => true, go: () => null}); +vm.runInContext(`${fs.readFileSync(process.argv[2], "utf8")}\\nglobalThis.probe = {orderDom, drawAgentOrder};`, context); +const {orderDom, drawAgentOrder} = context.probe; +P.panel("agents/order", "Agents: priority", () => null); +orderDom.box = el("section", {"data-panel": "agents/order"}); +orderDom.slot = el("span", {"data-star-slot": ""}); +document.body.append(orderDom.box); +const mark = P.decorate(document.body, {pinned: () => false, press: () => null}); +const stars = () => orderDom.box.all().filter((node) => node.classList.contains("star")); +drawAgentOrder(); +mark(); +const first = stars()[0]; +first.focus(); +drawAgentOrder(); +mark(); +context.state.agent_order = ["Stage", "Skills", "Docs"]; +drawAgentOrder(); +mark(); +console.log(JSON.stringify({count: stars().length, same: stars()[0] === first, focused: document.activeElement === first, head: first.parentNode.parentNode === orderDom.box.children[0], + ranks: orderDom.box.all().filter((node) => node.tagName === "LI").map((node) => node.getAttribute("data-name"))})); +""" +TREE_PROBE = DOM_KIT + """ +const [, artifactsFile, treeFile, homeFile, dataFile] = process.argv.slice(1); +Object.assign(context, { + h, icon: () => text(""), named: (label, hint) => ({title: hint || label, "aria-label": label}), iconBtn: (name, label, cls, run, hint) => h("button", {"aria-label": label, title: hint}), + counter: (value) => text(value), emptyState: (title, words) => h("div", {class: "empty", "data-title": title}, words), branchAncestry: () => null, gateIcons: () => h("span", {class: "gates"}), + flowLine: () => null, flowDetails: () => null, flowEdges: () => [], copyButton: () => null, face: () => null, person: (name) => text(name), status: (value) => text(value), pill: (words) => text(words), + workChips: () => null, popupHead: (glyph, label) => h("h2", {class: "panel-title"}, label), meta: (rows) => h("dl", {}, rows.map(([key, value]) => h("div", {"data-key": key}, value === null || value === undefined ? "" : value))), + WORK_CHIPS: {}, clock: {format: () => "c"}, stampOf: (t) => t, nowSeconds: () => 1763000000, labelOf: (key) => key, toast: () => null, + tree: {data: JSON.parse(fs.readFileSync(dataFile, "utf8")), model: null, busy: false, q: "", open: new Set(), dom: null}, +}); +vm.runInContext(`${[artifactsFile, treeFile, homeFile].map((file) => fs.readFileSync(file, "utf8")).join("\\n")} +tree.model = artifactModel(tree.data); +homeView.fetched.tree = Date.now(); +globalThis.probe = {treeCards};`, context); +const {treeCards} = context.probe; +const tree = context.tree; +const flat = (nodes) => nodes.filter((node) => node instanceof El).flatMap((node) => [node, ...node.all()]); +const deletes = (nodes) => flat(nodes).filter((node) => node.getAttribute("aria-label") === "Delete").length; +const rows = flat(treeCards("", false).nodes).filter((node) => node.hasAttribute("data-item")); +const pinned = rows.map((row) => { + const target = P.targetOf(row); + const entry = target.id ? P.lookup(target.kind, target.id) : null; + const shown = entry ? entry.render({w: 4, h: 3}) : null; + const head = shown ? shown.all().find((node) => node.classList.contains("panel-title")) : null; + const path = shown ? shown.all().find((node) => node.getAttribute("data-key") === "Path" || node.getAttribute("data-key") === "Ref") : null; + return {id: target.id, refused: target.refused || null, title: entry ? entry.title : null, label: head ? textOf(head) : null, path: path ? textOf(path) : null, deletes: shown ? deletes([shown]) : null}; +}); +const home = P.lookup("panel", "artifacts/tree").render({w: 6, h: 4}); +const glance = [deletes(treeCards("", false).nodes), deletes(treeCards("", true).nodes), deletes([home])]; +const digested = pinned.map((entry) => entry.id).filter((id) => /_23_/.test(id || "")); +const kept = tree.data; +tree.data = {...kept, repos: [], tasks: [], cleanups: []}; +const empty = [treeCards("", true).nodes[0].getAttribute("data-title"), treeCards("zz", false).nodes[0].getAttribute("data-title"), textOf(P.lookup("panel", "artifacts/tree").render({w: 6, h: 4}).children[0])]; +tree.data = kept; +tree.model = context.artifactModel({...kept, repos: [], tasks: []}); +const gone = digested.map((id) => [P.lookup("artifact", id).render({w: 4, h: 3}), P.lookup("artifact", id).title]); +console.log(JSON.stringify({pinned, glance, empty, gone})); +""" + + def node(probe: str, *args: str) -> dict: done = subprocess.run(["node", "-e", probe, *args], capture_output=True, text=True, timeout=60, check=True) return json.loads(done.stdout) +def escaped(raw: str) -> str: + return re.sub(r"[^A-Za-z0-9./-]", lambda found: f"_{ord(found.group()):x}_", raw) + + class TestHomeLayout(unittest.TestCase): def test_the_layout_validation_takes_at_most_64_pins_of_kind_id_w_and_h_in_their_ranges(self): data = node(LAYOUT_PROBE, str(PANELS)) @@ -447,7 +658,7 @@ class TestHomeStart(unittest.TestCase): class TestHomePage(unittest.TestCase): def test_the_page_registers_every_panel_and_item_kind_and_refuses_the_65th_pin_before_any_update(self): data = node(PAGE_PROBE, str(PANELS), str(HOME)) - self.assertEqual(data["ids"], {"panel": True, "task": True, "message": True, "artifact": True, "agent": True, "ancestry": True, "nope": False}) + self.assertEqual(data["ids"], {"panel": True, "task": True, "message": True, "artifact": True, "agent": True, "ancestry": True, "metric": True, "nope": False}) self.assertEqual(data["panels"], []) self.assertEqual((data["one"], data["none"], data["solid"], data["full"], data["after"]), (["panel:tasks/gantt"], 0, ["panel:agents/3d"], [64, 0], 63)) self.assertEqual(data["paths"], ["home.pins"]) @@ -479,11 +690,16 @@ class TestHomePage(unittest.TestCase): dynamic = {f"{tab}/{view}" for tab in re.findall(r'"data-panel": `(\w+)/\$\{view\}`', sources) for view in views[tab]} used = set(re.findall(r'(?:"data-item"|item): `(\w+):', sources)) marked = set(re.findall(r"\['[^']+', \"([^\"]+)\"\]", home)) - self.assertEqual((len(literal), marked), (4, {"radio-log"})) + captured = set(re.findall(r'Panels\.capture\("([^"]+)"', home)) + made = set(re.findall(r'\{kind: "(\w+)", id, view[,}]', sources)) + self.assertEqual((len(literal), marked), (7, {"radio-log"})) literal |= marked self.assertEqual(len(dynamic), 13) self.assertEqual(sorted(literal | dynamic), sorted(registered)) - self.assertEqual(sorted(used), sorted(kinds)) + self.assertIn('"data-panel": `resources-${kind}`', sources) + self.assertEqual(captured, {"resources-hardware", "resources-tokens", "resources-combined"}) + self.assertFalse(captured & registered) + self.assertEqual((made, sorted(used | made)), ({"metric"}, sorted(kinds))) class TestHomeStars(unittest.TestCase): @@ -493,7 +709,7 @@ class TestHomeStars(unittest.TestCase): self.assertEqual(data["task"], {"inSlot": True, "slotLast": True, "rows": 0, "pressed": "true", "label": "Unpin from Home"}) self.assertEqual(data["bare"], {"first": "star-row", "stars": 1}) self.assertEqual((data["svg"], data["radio"], data["again"], data["redrawn"]), (0, ["radio-log", "star-row"], [4, 4], [True, 4])) - self.assertEqual(data["clicked"], [["task", "work/a", "Task work/a"]]) + self.assertEqual(data["clicked"], [["task", "work/a", "Task work/a", None]]) def test_the_star_css_positions_nothing_and_leaves_the_starred_elements_alone(self): css = (TEMPLATES / "style-home.css").read_text(encoding="utf-8") @@ -504,6 +720,107 @@ class TestHomeStars(unittest.TestCase): self.assertNotIn("[data-item]", css) +class TestHomeBookmarks(unittest.TestCase): + VIEW: ClassVar[dict] = {"chart": "hardware", "tokens": "interval", "shown": ["host:CPU"]} + + def test_a_pin_may_carry_a_plain_view_that_cleaning_adding_resizing_and_the_registry_keep(self): + data = node(VIEW_PROBE, str(PANELS)) + pin = {"kind": "metric", "id": "hardware/x", "w": 4, "h": 3, "view": self.VIEW} + self.assertEqual(data["valid"], [True, False, False, False, False]) + self.assertEqual(data["cleaned"], [pin]) + self.assertEqual(data["added"], [[pin], [{"kind": "task", "id": "work/a", "w": 4, "h": 3}], [{"kind": "task", "id": "work/a", "w": 4, "h": 3}]]) + self.assertEqual(data["resized"], [{**pin, "w": 6, "h": 5}]) + self.assertEqual(data["lookup"], ["Metric hardware/x host:CPU", "hardware/x 4 hardware", "Metric hardware/x none", None]) + self.assertEqual(data["digest"], [True, False, True, True]) + + def test_a_captured_panel_stars_the_pin_its_capture_makes_now_and_presses_it_with_its_view(self): + data = node(CAPTURE_PROBE, str(PANELS)) + self.assertEqual(data["before"], ["false", "Pin to Home: Metric all", 0, True]) + self.assertEqual(data["after"], ["true", "Unpin from Home: Metric host:CPU", 1]) + self.assertEqual(data["clicked"], [["metric", "hardware/cpu", "Metric host:CPU", self.VIEW]]) + self.assertEqual(data["target"], {"kind": "metric", "id": "hardware/cpu", "view": self.VIEW}) + + def test_a_chart_star_pins_the_series_switched_on_now_so_another_selection_is_another_pin(self): + data = node(METRIC_PROBE, str(PANELS), str(TEMPLATES / "page-resources.js"), str(HOME)) + cpu, mixed = data["digests"] + self.assertEqual(data["empty"], {"kind": "metric", "id": "tokens/accumulated/all", "view": {"chart": "tokens", "tokens": "accumulated", "shown": None}}) + self.assertEqual(data["pins"], [ + {"kind": "metric", "id": f"hardware/{cpu}", "view": self.VIEW}, + {"kind": "metric", "id": "tokens/accumulated/all", "view": {"chart": "tokens", "tokens": "accumulated", "shown": None}}, + {"kind": "metric", "id": f"combined/interval/{mixed}", "view": {"chart": "combined", "tokens": "interval", "shown": ["burn:done", "host:CPU"]}}, + ]) + self.assertEqual(data["all"], {"kind": "metric", "id": "hardware/all", "view": {"chart": "hardware", "tokens": "interval", "shown": None}}) + self.assertEqual(data["titles"], ["Hardware: CPU", "Tokens (accumulated): all series", "Combined: done, CPU", "Hardware: all series", "Tokens (accumulated): all series"]) + self.assertEqual((data["four"], data["broken"]), ("Tokens: 4 series", ["Metric", None])) + + def test_pressing_a_metric_star_stores_its_view_with_the_pin_and_pressing_again_unpins_it(self): + data = node(PRESS_PROBE, str(PANELS), str(HOME)) + pin = {"kind": "metric", "id": "hardware/abc", "w": 4, "h": 3, "view": self.VIEW} + self.assertEqual((data["stored"], data["after"], data["pins"]), ([pin], [], [])) + + def test_a_chart_star_refuses_a_selection_past_the_view_stores_series_limit_and_names_why(self): + data = node(METRIC_PROBE, str(PANELS), str(TEMPLATES / "page-resources.js"), str(HOME), str(SHOWN_LIMIT)) + why = f"more than {SHOWN_LIMIT} series are switched on: switch some off, or all of them on" + self.assertEqual(data["limits"], [[SHOWN_LIMIT, None, 3], [SHOWN_LIMIT + 1, why, 3], [None, None, 3], [0, None, 3]]) + + def test_a_refused_capture_or_an_overlong_id_disables_the_star_with_its_reason_and_a_press_pins_nothing(self): + data = node(REFUSED_PROBE, str(PANELS)) + refused = [True, "Pin to Home: not possible, too many series", "false"] + self.assertEqual((data["refused"], data["again"]), (refused, refused)) + self.assertEqual(data["freed"], [False, "Pin to Home: Metric tokens/interval/y", "false", True]) + self.assertEqual(data["long"], [True, "Pin to Home: not possible, the id runs past 200 characters", "false"]) + self.assertEqual(data["clicked"], [["metric", "tokens/interval/y", "Metric tokens/interval/y", {"chart": "tokens"}]]) + + def test_the_agent_priority_star_stays_the_same_button_and_keeps_its_focus_through_a_redraw(self): + data = node(ORDER_PROBE, str(PANELS), str(TEMPLATES / "page-agent-order.js")) + self.assertEqual(data, {"count": 1, "same": True, "focused": True, "head": True, "ranks": ["Stage", "Skills", "Docs"]}) + + def test_the_client_pin_id_grammar_is_the_hubs(self): + done = subprocess.run(["node", "-e", "console.log(JSON.stringify(require(process.argv[1]).ID.source))", str(PANELS)], capture_output=True, text=True, timeout=60, check=True) + self.assertEqual(json.loads(done.stdout), f"^{SERIES_ID.pattern}$") + + +class TestHomeTree(HubCase): + def served_tree(self) -> str: + self.tree_file.write_text(json.dumps({"generated_at": "2026-11-14T12:00:00+00:00", "delete_ready": False, "delete_reason": "Secret Service locked", "repos": long_repos()}), encoding="utf-8") + self.write_council({"rasci.json": {LONG_TASK: long_task(time.time())}}) + status, _, body = self.get("/api/tree") + self.assertEqual(status, 200, body[:300]) + served = self.outside / "served-tree.json" + served.write_bytes(body) + return str(served) + + def test_every_tree_row_on_paths_of_real_length_pins_shows_that_one_element_on_home_and_turns_gone(self): + data = node(TREE_PROBE, str(PANELS), str(TEMPLATES / "page-artifacts.js"), str(TEMPLATES / "page-tree.js"), str(HOME), self.served_tree()) + rows = [ + (f"repo:{CORE_REPO}", "github.com/infinito-nexus/core", CORE_REPO), + (f"branch:{CORE_REPO}\0{CORE_BRANCH}", CORE_BRANCH, None), + (f"wt:{CORE_WORKTREE}", CORE_BRANCH, CORE_WORKTREE), + (f"repo:{LONG_REPO}", "github.com/operator-example-01/skills", LONG_REPO), + (f"branch:{LONG_REPO}\0{LONG_BRANCH}", LONG_BRANCH, None), + (f"wt:{LONG_WORKTREE}", LONG_BRANCH, LONG_WORKTREE), + (f"artefact:wt:{LONG_WORKTREE}\0{LONG_TASK}\0{LONG_VOLUME}", LONG_VOLUME.split(":", 1)[1], LONG_VOLUME), + (f"branch:{LONG_REPO}\0{MERGED_BRANCH}", MERGED_BRANCH, None), + ] + self.assertEqual(len(data["pinned"]), len(rows), data["pinned"]) + digested = [] + for found, (raw, label, path) in zip(data["pinned"], rows, strict=True): + with self.subTest(row=raw): + self.assertEqual((found["refused"], found["title"], found["label"], found["path"], found["deletes"]), (None, f"Artifact {raw.replace(chr(0), ' ')}", label, path, 0), found) + self.assertTrue(SERIES_ID.fullmatch(found["id"]), found) + if len(escaped(raw)) <= 200: + self.assertEqual(found["id"], escaped(raw)) + else: + self.assertRegex(found["id"], rf"^{raw.split(':', 1)[0]}_23_[0-9a-f]{{16}}$") + digested.append(found["id"].split("_23_")[0]) + self.assertEqual(digested, ["wt", "artefact"]) + self.assertEqual(data["glance"], [1, 0, 0]) + self.assertEqual(data["empty"], ["No repository", "Nothing found", "pkgmgr names no repository."]) + self.assertEqual([gone[0] for gone in data["gone"]], [None, None]) + for gone, kind in zip(data["gone"], digested, strict=True): + self.assertRegex(gone[1], rf"^Artifact {kind}#[0-9a-f]{{16}}$") + + class TestHomeMessage(unittest.TestCase): def test_a_pinned_message_resolves_by_its_own_id_and_time_whatever_the_stream_shows(self): data = node(MESSAGE_PROBE, str(PANELS), str(HOME)) diff --git a/tests/test_hub_resources.py b/tests/test_hub_resources.py index 697e629..9d0c603 100644 --- a/tests/test_hub_resources.py +++ b/tests/test_hub_resources.py @@ -1,5 +1,6 @@ from __future__ import annotations +import itertools import json import subprocess import time @@ -328,6 +329,31 @@ class TestResourcesPage(unittest.TestCase): self.assertEqual(self.found["fetched"], ["/api/resources?from=1", ["PATCH", "/api/view", changes]]) +AXIS_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const s = (tag, attrs, ...children) => ({tag, attrs, children: children.flat(), append(...more) { this.children.push(...more); }}); +const context = {s, h: s, DAY: 86400, fixed: (value) => value.toFixed(1), stampOf: (t) => t, absolute: {format: () => "14 Nov 2026, 10:00"}, clock: {format: () => "10:00"}}; +vm.runInNewContext(`${fs.readFileSync(process.argv[1], "utf8")}\\nglobalThis.frame = resourceFrame;`, context); +const labels = (width, days) => context.frame({kind: "tokens", label: "Tokens"}, {from: 0, to: days * 86400}, {width, height: 120}, false).svg.children + .filter((node) => node.tag === "text").map((node) => [Number(node.attrs.x), node.attrs["text-anchor"], node.children.join("")]); +console.log(JSON.stringify([[1600, 2], [390, 2], [340, 2], [150, 2], [340, 0.5]].map(([width, days]) => labels(width, days)))); +""" +GLYPH = 6.2 + + +class TestResourcesAxis(unittest.TestCase): + def test_time_labels_never_overlap_and_a_plot_too_narrow_for_two_keeps_the_last(self): + done = subprocess.run(["node", "-e", AXIS_PROBE, str(PAGE)], capture_output=True, text=True, timeout=60, check=True) + found = json.loads(done.stdout) + self.assertEqual([len(labels) for labels in found], [5, 2, 2, 1, 5]) + self.assertEqual(found[3][0][1:], ["end", "14 Nov 2026, 10:00"]) + for labels in found: + spans = sorted((x - {"start": 0, "middle": 0.5, "end": 1}[anchor] * len(words) * GLYPH, x + {"start": 1, "middle": 0.5, "end": 0}[anchor] * len(words) * GLYPH) for x, anchor, words in labels) + with self.subTest(labels=labels): + self.assertTrue(all(left[1] <= right[0] for left, right in itertools.pairwise(spans)), spans) + + class TestHubResources(HubCase): def write(self, name: str, rows: list[dict], tail: str = "") -> None: folder = self.kept / "resources" diff --git a/tests/test_hub_view.py b/tests/test_hub_view.py index 44e2565..261b6bd 100644 --- a/tests/test_hub_view.py +++ b/tests/test_hub_view.py @@ -11,7 +11,7 @@ from unittest import mock from hub_case import HubCase from hub_support import HOST_STATE, HUB, server -from kvb_orchestrator import records +from kvb_orchestrator import records, viewstore from kvb_orchestrator.errors import Conflict, ToolError from kvb_orchestrator.handler import BODY_LIMIT from kvb_orchestrator.viewstore import ( @@ -24,6 +24,8 @@ from kvb_orchestrator.viewstore import ( ) STORE = HUB / "templates" / "view-store.js" +PANELS = HUB / "templates" / "panels.js" +DIGESTS = "const P = require(process.argv[1]); console.log(JSON.stringify(process.argv.slice(2).map(P.digest)));" CHIPS = HUB / "templates" / "legend-chips.js" CHIPS_STYLE = HUB / "templates" / "style-legend-chips.css" FULL = { @@ -277,6 +279,11 @@ def change(values: dict, expected: dict) -> bytes: return json.dumps({"set": values, "expect": expected}).encode() +def client_digests(*texts: str) -> list[str]: + done = subprocess.run(["node", "-e", DIGESTS, str(PANELS), *texts], capture_output=True, text=True, timeout=60, check=True) + return json.loads(done.stdout) + + class TestViewDocument(unittest.TestCase): def test_the_defaults_and_a_full_document_pass_unchanged(self): self.assertEqual(valid_view(copy.deepcopy(VIEW)), VIEW) @@ -328,6 +335,73 @@ class TestViewDocument(unittest.TestCase): with self.subTest(case=name), self.assertRaises(ToolError): valid_view(doc) + def test_the_hub_digests_a_selection_exactly_as_panels_js_does(self): + texts = ["", "host:CPU", "burn:done\nhost:CPU", "agent:Ä€\U0001f600", "x" * 5000] + self.assertEqual([viewstore.digest(text) for text in texts], client_digests(*texts)) + + def test_a_metric_pin_id_must_be_the_one_its_view_gives_so_no_view_hides_under_another_charts_id(self): + cpu = {"chart": "hardware", "tokens": "interval", "shown": ["host:CPU"]} + tokens = {"chart": "tokens", "tokens": "accumulated", "shown": None} + mixed = {"chart": "combined", "tokens": "interval", "shown": ["burn:done", "host:CPU"]} + hundred = {"chart": "tokens", "tokens": "interval", "shown": sorted(f"agent:a{index:03d}" for index in range(100))} + cpu_digest, ram_digest, mixed_digest, hundred_digest = client_digests("host:CPU", "host:RAM", "burn:done\nhost:CPU", "\n".join(hundred["shown"])) + + def doc(*pins: dict) -> dict: + made = copy.deepcopy(VIEW) + made["home"]["pins"] = [{"kind": "metric", "w": 4, "h": 3, **pin} for pin in pins] + return made + + accepted = doc({"id": f"hardware/{cpu_digest}", "view": cpu}, {"id": "tokens/accumulated/all", "view": tokens}, {"id": f"combined/interval/{mixed_digest}", "view": mixed}, + {"id": f"tokens/interval/{hundred_digest}", "view": hundred}) + self.assertEqual(valid_view(copy.deepcopy(accepted)), accepted) + cases = { + "hardware all id with a tokens view": doc({"id": "hardware/all", "view": tokens}), + "tokens id with a hardware view": doc({"id": "tokens/accumulated/all", "view": {**cpu, "shown": None}}), + "all id with a selection": doc({"id": "hardware/all", "view": cpu}), + "digest of another selection": doc({"id": f"hardware/{ram_digest}", "view": cpu}), + "other token mode": doc({"id": "tokens/interval/all", "view": tokens}), + "hardware id with a mode": doc({"id": f"hardware/interval/{cpu_digest}", "view": cpu}), + "combined id without a mode": doc({"id": f"combined/{mixed_digest}", "view": mixed}), + } + for name, broken in cases.items(): + with self.subTest(case=name), self.assertRaises(ToolError): + valid_view(broken) + + def test_a_metric_pin_keeps_its_chart_selection_and_a_malformed_one_is_refused(self): + metric = {"kind": "metric", "id": f"hardware/{client_digests('host:CPU')[0]}", "w": 4, "h": 3, "view": {"chart": "hardware", "tokens": "interval", "shown": ["host:CPU"]}} + everything = {"kind": "metric", "id": "combined/accumulated/all", "w": 4, "h": 3, "view": {"chart": "combined", "tokens": "accumulated", "shown": None}} + doc = copy.deepcopy(VIEW) + doc["home"]["pins"] = [metric, everything, PIN] + self.assertEqual(valid_view(copy.deepcopy(doc)), doc) + + def view(**changes: object) -> dict: + broken = copy.deepcopy(VIEW) + broken["home"]["pins"] = [{**metric, "view": {**metric["view"], **changes}}] + return broken + + bare = copy.deepcopy(VIEW) + bare["home"]["pins"] = [{key: value for key, value in metric.items() if key != "view"}] + task = copy.deepcopy(VIEW) + task["home"]["pins"] = [{**PIN, "view": metric["view"]}] + extra = copy.deepcopy(VIEW) + extra["home"]["pins"] = [{**metric, "view": {**metric["view"], "hidden": []}}] + cases = { + "metric without view": bare, + "view on a panel pin": task, + "unknown view key": extra, + "unknown chart": view(chart="disks"), + "hardware accumulated": view(tokens="accumulated"), + "unknown token mode": view(chart="tokens", tokens="sum"), + "unsorted series": view(shown=["host:RAM", "host:CPU"]), + "series with a space": view(shown=["host CPU"]), + "repeated series": view(shown=["host:CPU", "host:CPU"]), + "101 series": view(shown=sorted(f"s{index:03d}" for index in range(101))), + "series not a list": view(shown="host:CPU"), + } + for name, broken in cases.items(): + with self.subTest(case=name), self.assertRaises(ToolError): + valid_view(broken) + def test_sixty_four_pins_of_one_by_one_to_twelve_by_twelve_pass(self): doc = copy.deepcopy(VIEW) doc["home"]["pins"] = [{"kind": "panel", "id": f"p{index}", "w": 1 + index % 12, "h": 12 - index % 12} for index in range(64)]