feat(orchestrator): resources view with legend chips and a server-side view store (007)

The Resources tab draws one hardware chart and one tokens chart with an accumulated switch, plus a combined overlay of hardware, tokens and the burnup, every series switched by a legend chip and all shown by default. The hub keeps every toggle in a validated view store: PATCH /api/view takes a set and the expected old values and answers 409 on a stale expectation, update() re-runs a change on the stored value, list paths change only through update(), and a page shown again from the back-forward cache fetches the stored view anew.

🎯 Fix: 93 %
💥 Break: 4 %
💸 Cost: 100 %
⏱️ Time: 100 %
🛡️ Security: 100 %
🏆 Score: 89
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-13 12:40:25 +00:00
1 parent 3418738993
commit c128fa4000
15 files changed
+1794 -102

No files matched your search

+184
View File
@@ -0,0 +1,184 @@
from __future__ import annotations
import json
import os
import shutil
import sys
import time
import unittest
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path[:0] = [str(HERE.parent), str(HERE)]
import test_orchestrator_e2e as base
import test_workbench_w4_e2e as w4
from hub_case import HubCase
from hub_support import iso
from kvb_orchestrator.viewstore import VIEW
OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-007-shots"))
CPU = "host:CPU"
STAGE = "agent:Stage"
UTIL = "util:0000_3a_01_3a_00.0"
SCOPE = "burn:scope"
READ = """
const charts = () => all("#main svg.res-chart").map((chart) => chart.dataset.chart);
const chip = (id) => document.querySelector(`#main .res-panel:not([hidden]) .legend-chip[data-chip="${id}"]`);
const pressed = () => Object.fromEntries(all("#main .res-panel:not([hidden]) .legend-chip").map((node) => [node.dataset.chip, node.getAttribute("aria-pressed")]));
const lines = () => [...new Set(all("#main path.res-line").map((path) => path.dataset.id))];
const agents = () => [...new Set(all("#main rect.res-bar").map((rect) => rect.dataset.agent))].sort();
const sums = () => all("#main path.res-sum").map((path) => path.dataset.agent).sort();
const rising = () => all("#main path.res-sum").every((path) => JSON.parse(path.dataset.points).every((value, index, list) => !index || value >= list[index - 1]));
const burn = () => all("#main path.res-burn").map((path) => path.dataset.series);
const switched = (key) => document.querySelector(`#main [data-switch="${key}"]`);
const stored = async () => (await (await fetch("/api/view", {cache: "no-store"})).json()).resources;
const storedUntil = async (test, ms) => {
const end = Date.now() + ms;
while (Date.now() < end) {
const view = await stored();
if (test(view)) {
return view;
}
await sleep(200);
}
return null;
};
const legendFits = () => all("#main .res-panel:not([hidden])").every((panel) => panel.querySelector(".res-plot").getBoundingClientRect().height > 40);
const page = () => ({tall: document.documentElement.scrollHeight > innerHeight, wide: document.documentElement.scrollWidth > innerWidth + 1, fits: legendFits()});
"""
TOGGLE = """(async () => {@HELPERS@@READ@
if (!(await until(() => charts().length === 2 && chip("@CPU@") && chip("@STAGE@") && agents().length === 2, 15000))) {
return {error: text(document.getElementById("main")), charts: charts()};
}
const before = {charts: charts(), lines: lines(), agents: agents(), pressed: pressed(), panels: all("#main .res-panel").map((panel) => [panel.dataset.panel, panel.hidden])};
chip("@CPU@").click();
await until(() => chip("@STAGE@") && !lines().includes("@CPU@"), 4000);
chip("@STAGE@").click();
const hidden = Boolean(await until(() => !lines().includes("@CPU@") && agents().join() === "Skills", 4000));
const saved = await storedUntil((view) => view.hidden.includes("@CPU@") && view.hidden.includes("@STAGE@"), 6000);
return {before, hidden, after: {charts: charts(), lines: lines(), agents: agents(), pressed: pressed()}, saved, page: page(), violations: window.e2eViolations || null};
})()"""
COMBINE = """(async () => {@HELPERS@@READ@
if (!(await until(() => charts().length === 2 && chip("@CPU@"), 15000))) {
return {error: text(document.getElementById("main")), charts: charts()};
}
const kept = {charts: charts(), lines: lines(), agents: agents(), cpu: pressed()["@CPU@"], stage: pressed()["@STAGE@"]};
switched("tokens").click();
const accumulated = Boolean(await until(() => sums().length === 1 && !agents().length, 4000));
const sumsAt = {sums: sums(), rising: rising(), pressed: switched("tokens").getAttribute("aria-pressed")};
switched("combined").click();
const combinedOn = Boolean(await until(() => charts().join() === "combined" && burn().length === 2, 4000));
const combined = {charts: charts(), lines: lines(), sums: sums(), burn: burn(), right: all('#main text[data-axis="right"]').length, pressed: pressed(), panels: all("#main .res-panel").map((panel) => [panel.dataset.panel, panel.hidden]), switch: switched("combined").getAttribute("aria-pressed")};
return {kept, accumulated, sumsAt, combinedOn, combined, page: page(), violations: window.e2eViolations || null};
})()"""
DESELECT = """(async () => {@HELPERS@@READ@
chip("@SCOPE@").click();
await until(() => burn().join() === "done" && chip("@UTIL@"), 4000);
chip("@UTIL@").click();
const deselected = Boolean(await until(() => burn().join() === "done" && !lines().includes("@UTIL@"), 4000));
const saved = await storedUntil((view) => view.combinedHidden.length === 2, 6000);
const current = await (await fetch("/api/view", {cache: "no-store"})).json();
const reset = await fetch("/api/view", {method: "PATCH", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({set: @DEFAULTS@, expect: current}), cache: "no-store"});
return {deselected, burn: burn(), lines: lines(), saved, reset: reset.status, violations: window.e2eViolations || null};
})()"""
def probe(template: str) -> str:
names = {"@HELPERS@": base.HELPERS, "@READ@": READ, "@CPU@": CPU, "@STAGE@": STAGE, "@UTIL@": UTIL, "@SCOPE@": SCOPE, "@DEFAULTS@": json.dumps(VIEW)}
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 TestResourcesView(HubCase):
drive = base.TestOrchestratorE2E.drive
sample = w4.TestWorkbenchResources.sample
def setUp(self):
super().setUp()
OUT.mkdir(parents=True, exist_ok=True)
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")
self.write_council({"rasci.json": {
"work/first": {"title": "First", "status": "done", "worker": "Skills", "start": iso(now - 20000), "end": iso(now - 9000), "updated": iso(now - 9000), "roles": {"Skills": "RA"}},
"work/second": {"title": "Second", "status": "running", "worker": "Stage", "start": iso(now - 15000), "updated": iso(now - 600), "roles": {"Stage": "RA"}},
"work/third": {"title": "Third", "status": "done", "worker": "Skills", "start": iso(now - 7000), "end": iso(now - 3000), "updated": iso(now - 3000), "roles": {"Skills": "RA"}},
}})
self.base = f"http://{self.host}/"
def steps(self, browser: str) -> list[dict]:
steps = []
for width, height in base.WIDTHS:
shot = OUT / f"{browser}-{width}"
steps += [
{"width": width, "height": height, "url": f"{self.base}?{width}#resources", "wait": 1200, "probe": probe(TOGGLE), "shot": f"{shot}-chips.png"},
{"width": width, "height": height, "url": f"{self.base}?{width}-reloaded#resources", "wait": 1200, "probe": probe(COMBINE), "shot": f"{shot}-combined.png"},
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(DESELECT), "shot": f"{shot}-deselected.png"},
]
return steps
def check(self, data: dict, browser: str) -> None:
results = iter(data["results"])
hardware = ["disk:/", "disk:/boot/efi", CPU, "host:RAM", "host:Swap", UTIL, "vram:0000_3a_01_3a_00.0"]
for width, _ in base.WIDTHS:
toggle, combine, deselect = next(results), next(results), next(results)
with self.subTest(browser=browser, width=width):
self.assertNotIn("error", toggle, toggle)
before = toggle["before"]
self.assertEqual(before["charts"], ["hardware", "tokens"])
self.assertEqual(before["panels"], [["resources-hardware", False], ["resources-tokens", False], ["resources-combined", True]])
self.assertEqual((before["lines"], before["agents"]), (hardware, ["Skills", "Stage"]))
self.assertEqual(set(before["pressed"].values()), {"true"})
self.assertEqual(set(before["pressed"]), {*hardware, "agent:Skills", STAGE})
self.assertTrue(toggle["hidden"], toggle)
after = toggle["after"]
self.assertEqual((after["charts"], after["lines"], after["agents"]), (["hardware", "tokens"], [line for line in hardware if line != CPU], ["Skills"]))
self.assertEqual((after["pressed"][CPU], after["pressed"][STAGE], after["pressed"]["host:RAM"]), ("false", "false", "true"))
self.assertEqual(toggle["saved"], {**VIEW["resources"], "hidden": [CPU, STAGE]})
self.assertEqual(toggle["page"], {"tall": False, "wide": False, "fits": True})
self.assertEqual(w4.quiet(toggle["violations"]), [])
self.assertNotIn("error", combine, combine)
self.assertEqual(combine["kept"], {"charts": ["hardware", "tokens"], "lines": [line for line in hardware if line != CPU], "agents": ["Skills"], "cpu": "false", "stage": "false"})
self.assertTrue(combine["accumulated"], combine)
self.assertEqual(combine["sumsAt"], {"sums": ["Skills"], "rising": True, "pressed": "true"})
self.assertTrue(combine["combinedOn"], combine)
combined = combine["combined"]
self.assertEqual((combined["charts"], combined["lines"], combined["sums"], combined["burn"]), (["combined"], hardware, ["Skills", "Stage"], ["done", "scope"]))
self.assertEqual((combined["right"], combined["switch"]), (2, "true"))
self.assertEqual(combined["panels"], [["resources-hardware", True], ["resources-tokens", True], ["resources-combined", False]])
self.assertEqual(set(combined["pressed"].values()), {"true"})
self.assertEqual(set(combined["pressed"]), {*hardware, "agent:Skills", STAGE, "burn:done", SCOPE})
self.assertEqual(combine["page"], {"tall": False, "wide": False, "fits": True})
self.assertEqual(w4.quiet(combine["violations"]), [])
self.assertTrue(deselect["deselected"], deselect)
self.assertEqual((deselect["burn"], deselect["lines"]), (["done"], [line for line in hardware if line != UTIL]))
self.assertEqual(deselect["saved"], {"hidden": [CPU, STAGE], "tokens": "accumulated", "combined": True, "combinedHidden": [SCOPE, UTIL]})
self.assertEqual(deselect["reset"], 200)
self.assertEqual(w4.quiet(deselect["violations"]), [])
self.assertEqual(self.httpd.hub.view_store.get(), VIEW)
def test_chromium_draws_two_charts_whose_chips_toggle_and_persist_and_a_combined_chart_at_three_widths(self):
data = self.drive(self.steps("chromium"), None, init=w4.GUARD)
(OUT / "chromium-resources-view.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_draws_two_charts_whose_chips_toggle_and_persist_and_a_combined_chart_at_three_widths(self):
data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=w4.GUARD)
(OUT / "firefox-resources-view.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()
+12 -12
View File
@@ -20,14 +20,14 @@ ROWS = 360
WAITED = (120, 180)
ASLEEP = 180
UNREAD = (300, 320)
PANELS = {"disks": "Disks", "host": "CPU, RAM and swap", "gpu": "GPU and VRAM", "tokens": "Tokens per agent"}
PANELS = {"resources-hardware": "Hardware: disks, CPU, RAM, swap, GPU and VRAM", "resources-tokens": "Tokens per agent"}
GUARD = """() => {
window.e2eViolations = [];
document.addEventListener("securitypolicyviolation", (event) => window.e2eViolations.push(`${event.violatedDirective} ${event.blockedURI}`));
}"""
CHARTS = """(async () => {@HELPERS@
const counted = () => text(document.querySelector('#main [aria-label="Samples"]'));
if (!(await until(() => all("#main svg.res-chart").length === 4 && counted() === "@ROWS@", 15000))) {
if (!(await until(() => all("#main svg.res-chart").length === 2 && counted() === "@ROWS@", 15000))) {
return {error: text(document.getElementById("main")), count: counted()};
}
await sleep(300);
@@ -41,24 +41,24 @@ CHARTS = """(async () => {@HELPERS@
const piles = Object.values(stacks).map((parts) => parts.sort((a, b) => b.y - a.y));
const labelled = (node) => (node ? [node.getAttribute("title"), node.getAttribute("aria-label")] : null);
return {
panels: all("#main .res-panel").map((panel) => panel.dataset.panel),
panels: all("#main .res-panel:not([hidden])").map((panel) => panel.dataset.panel),
axes: [...new Set(charts.map((chart) => `${chart.dataset.from}|${chart.dataset.to}`))],
since: number(charts[0], "from"),
bands: charts.filter((chart) => chart.dataset.panel !== "tokens").map((chart) => {
bands: charts.filter((chart) => chart.dataset.chart === "hardware").map((chart) => {
const [idle, saturated] = [chart.querySelector("rect.band.idle"), chart.querySelector("rect.band.saturated")];
return [chart.dataset.panel, attr(idle, "height") / attr(saturated, "height"), attr(idle, "y") > attr(saturated, "y")];
return [chart.dataset.chart, attr(idle, "height") / attr(saturated, "height"), attr(idle, "y") > attr(saturated, "y")];
}),
series: charts.map((chart) => [chart.dataset.panel, [...new Set(all("path.res-line", chart).map((path) => `${path.dataset.series}|${path.dataset.kind}`))]]),
series: charts.map((chart) => [chart.dataset.chart, [...new Set(all("path.res-line", chart).map((path) => `${path.dataset.series}|${path.dataset.kind}`))]]),
waits: all("#main rect.res-mark.wait").map((rect) => [number(rect, "from"), number(rect, "to"), number(rect, "peak")]),
asleep: all("#main rect.res-mark.asleep").map((rect) => [number(rect, "from"), number(rect, "to")]),
unknown: all("#main rect.res-mark.unknown").map((rect) => [number(rect, "from"), number(rect, "to")]),
agents: [...new Set(all("#main rect.res-bar").map((rect) => rect.dataset.agent))].sort(),
bottoms: [...new Set(piles.map((parts) => parts[0].agent))],
stacked: piles.every((parts) => parts.every((part, index) => !index || Math.abs(part.y + part.height - parts[index - 1].y) < 0.2)),
legend: all("#main .res-host .res-key").map(text),
labels: {tab: labelled(document.getElementById("tab-resources")), reload: labelled(document.querySelector('#main [aria-label="Reload"]')), titles: all("#main .res-title").map(labelled)},
legend: all("#main .res-hardware .legend-chip.k-host").map(text),
labels: {tab: labelled(document.getElementById("tab-resources")), reload: labelled(document.querySelector('#main [aria-label="Reload"]')), titles: all("#main .res-panel:not([hidden]) .res-title").map(labelled)},
unlabelled: all("#main svg.bi").map((svg) => svg.parentElement).filter((owner) => !text(owner) && !(owner.getAttribute("title") && owner.getAttribute("aria-label"))).length,
plots: all("#main .res-plot").map((plot) => plot.getBoundingClientRect().height > 20),
plots: all("#main .res-panel:not([hidden]) .res-plot").map((plot) => plot.getBoundingClientRect().height > 20),
tall: document.documentElement.scrollHeight > innerHeight,
wide: document.documentElement.scrollWidth > innerWidth + 1,
violations: window.e2eViolations || null,
@@ -119,17 +119,17 @@ class TestWorkbenchResources(HubCase):
self.assertEqual(found["panels"], list(PANELS))
self.assertEqual(len(found["axes"]), 1, found["axes"])
self.assertLess(abs(found["since"] - (time.time() - 86400)), 600)
self.assertEqual([(panel, lower) for panel, _, lower in found["bands"]], [("disks", True), ("host", True), ("gpu", True)])
self.assertEqual([(panel, lower) for panel, _, lower in found["bands"]], [("hardware", True)])
for panel, ratio, _ in found["bands"]:
self.assertAlmostEqual(ratio, 3, delta=0.2, msg=panel)
self.assertEqual(found["series"], [["disks", ["/|disk", "/boot/efi|disk"]], ["host", ["CPU|host", "RAM|host", "Swap|host"]], ["gpu", [f"{GPU}|util", f"{GPU}|vram"]], ["tokens", []]])
self.assertEqual(found["series"], [["hardware", ["/|disk", "/boot/efi|disk", "CPU|host", "RAM|host", "Swap|host", f"{GPU}|util", f"{GPU}|vram"]], ["tokens", []]])
self.assertEqual(found["waits"], [[self.start + WAITED[0] * 60, self.start + WAITED[1] * 60, 2]])
self.assertEqual(found["asleep"], [[self.start, self.start + ASLEEP * 60]])
self.assertEqual(found["unknown"], [[self.start + UNREAD[0] * 60, self.start + UNREAD[1] * 60]])
self.assertEqual((found["agents"], found["bottoms"], found["stacked"]), (["Skills", "Stage"], ["Skills"], True))
self.assertEqual(found["legend"], ["CPU 60 %", "RAM 55 %", "Swap 5 %"])
self.assertEqual(found["labels"], {"tab": ["Resources (key 5)", "Resources"], "reload": ["Reload: read the sampler files again", "Reload"], "titles": [[name, name] for name in PANELS.values()]})
self.assertEqual((found["unlabelled"], found["plots"]), (0, [True] * 4))
self.assertEqual((found["unlabelled"], found["plots"]), (0, [True] * 2))
self.assertEqual((found["tall"], found["wide"]), (False, False), "the page scrolls")
self.assertEqual(quiet(found["violations"]), [])
self.assertEqual(results[len(base.WIDTHS)], {"opened": True, "current": "page"})
+176 -29
View File
@@ -6,7 +6,7 @@ import time
import unittest
from hub_case import HubCase
from hub_support import HUB, NAV
from hub_support import HUB, NAV, PLAN
MODULE = HUB / "templates" / "resources.js"
PAGE = HUB / "templates" / "page-resources.js"
@@ -52,43 +52,92 @@ console.log(JSON.stringify({
asleep: R.asleep(samples),
unknown: R.unknown(samples),
stack: R.stack(tokens),
hardware: R.hardware(samples).map((entry) => [entry.name, entry.kind, entry.points.length]),
accumulated: R.accumulate(tokens),
many: (() => {
let seed = 7;
const next = () => {
seed = (seed * 1103515245 + 12345) % 2147483648;
return seed;
};
const rows = Array.from({length: 80}, () => ({t: T + 3600 * (next() % 12), agent: ["A", "B", "C"][next() % 3], model: "m", input: (next() % 900) - 100, output: next() % 50, cache_read: next() % 2 ? NaN : 7, cache_write: 0}));
const sums = R.accumulate(rows);
const totals = R.stack(rows).totals;
return {
rising: sums.lines.every((line) => line.points.every(([, value], index) => !index || value >= line.points[index - 1][1])),
finals: Object.fromEntries(sums.lines.map((line) => [line.agent, line.points[line.points.length - 1][1]])),
totals, top: sums.top, lengths: [...new Set(sums.lines.map((line) => line.points.length))],
};
})(),
burnup: R.burnup([{t: 1, scope: 2, done: 0}, {t: 2, scope: 4, done: 1}, {t: 3, scope: 5, done: 5}, {t: 4, scope: 4, done: 4}]),
empty: R.burnup([{t: 1, scope: 0, done: 0}]),
}));
""".replace("@FIXTURE@", FIXTURE)
PAGE_PROBE = """
const fs = require("fs");
const vm = require("vm");
const Resources = require(process.argv[1]);
const code = fs.readFileSync(process.argv[2], "utf8");
const T = Number(process.argv[3]);
const Plan = require(process.argv[2]);
const code = [process.argv[3], process.argv[4], process.argv[5]].map((path) => fs.readFileSync(path, "utf8")).join("\\n");
const T = Number(process.argv[6]);
@FIXTURE@
const make = (tag, attrs, children) => {
const node = {tag, attrs: {}, children: [], textContent: "", isConnected: true,
const node = {tag, attrs: {}, children: [], textContent: "", isConnected: true, listeners: {},
set className(value) { this.attrs.class = value; }, set type(value) { this.attrs.type = value; },
setAttribute(key, value) { this.attrs[key] = String(value); }, getAttribute(key) { return this.attrs[key]; },
addEventListener(name, run) { this.listeners[name] = run; },
append(...items) { this.children.push(...items); },
replaceChildren(...items) { this.children = items; },
getBoundingClientRect() { return {width: 800, height: 120}; }};
for (const [key, value] of Object.entries(attrs)) {
if (key !== "on" && value !== null && value !== undefined && value !== false) {
if (key === "on") {
Object.assign(node.listeners, value);
} else if (value !== null && value !== undefined && value !== false) {
node.attrs[key] = String(value);
}
}
node.hidden = Object.hasOwn(node.attrs, "hidden");
node.children = children.flat(Infinity).filter((child) => child !== null && child !== undefined && child !== false);
return node;
};
const h = (tag, attrs, ...children) => make(tag, attrs, children);
const icon = (name) => make("svg", {class: "bi", "data-icon": name}, []);
const view = {resources: {hidden: [], tokens: "interval", combined: false, combinedHidden: []}, burn: {overlays: []}, home: {pins: []}};
const served = JSON.parse(JSON.stringify(view));
const fetched = [];
const tasks = [
{task: "a", kind: "task", status: "done", start: T - 300, end: T + 600, points: 0},
{task: "b", kind: "task", status: "running", start: T + 1000, end: null, points: 0},
{task: "c", kind: "task", status: "done", start: T + 2000, end: T + 3000, points: 0},
{task: "q", kind: "queue", status: "done", start: T - 500, end: T - 400, points: 0},
];
const context = {
Resources, h, s: h, icon, DAY: 86400, ui: {preset: 86400, from: null, to: null, asOf: null, offline: false},
Resources, Plan, h, s: h, icon, DAY: 86400, ui: {preset: 86400, from: null, to: null, asOf: null, offline: false},
state: {tasks, now: T + 6000}, settings: {estimation: "none"}, setTimeout, clearTimeout,
named: (label, hint) => ({title: hint || label, "aria-label": label}),
iconBtn: (name, label, cls, run, hint) => make("button", {title: hint, "aria-label": label}, [icon(name)]),
fixed: (value) => value.toFixed(1), dash: (value) => (value === null ? "–" : value), absolute: {format: () => "d"}, clock: {format: () => "c"}, stampOf: (t) => new Date(t * 1000),
window: {setTimeout: (run) => run(), setInterval: () => 1, addEventListener: () => null}, document: {hidden: false},
window: {setTimeout: (run) => run(), setInterval: () => 1, addEventListener: () => null},
document: {
hidden: false, activeElement: null, createElement: (tag) => make(tag, {}, []), getElementById: (name) => (name === "view" ? {textContent: JSON.stringify(view)} : null),
querySelectorAll: (selector) => (selector === ".legend-chip" ? walk(context.screen, []).filter((node) => has(node, "legend-chip")) : []),
},
windowQuery: () => "from=1", nowSeconds: () => 0,
fetch: async (url) => {
fetched.push(url);
return {ok: true, json: async () => ({since: T - 600, until: T + 6000, samples, tokens})};
fetch: async (url, options) => {
if (url !== "/api/view") {
fetched.push(url);
return {ok: true, json: async () => ({since: T - 600, until: T + 6000, samples, tokens})};
}
const changes = JSON.parse(options.body).set;
fetched.push([options.method, url, changes]);
for (const [path, value] of Object.entries(changes)) {
const [section, key] = path.split(".");
served[section][key] = value;
}
return {ok: true, json: async () => JSON.parse(JSON.stringify(served))};
},
};
vm.runInNewContext(`${code}\\nscreen = resourcesScreen();`, context);
vm.runInNewContext(`${code}\\nscreen = resourcesScreen();\\nstore = ViewStore;`, context);
const walk = (node, found) => {
if (node && typeof node === "object") {
found.push(node);
@@ -97,27 +146,52 @@ const walk = (node, found) => {
return found;
};
const has = (node, name) => (node.attrs.class || "").split(" ").includes(name);
(async () => {
const settle = async () => {
for (let round = 0; round < 5; round += 1) {
await new Promise((resolve) => setImmediate(resolve));
}
};
const read = () => {
const all = walk(context.screen, []);
const charts = all.filter((node) => has(node, "res-chart"));
const marks = (kind) => all.filter((node) => has(node, "res-mark") && has(node, kind)).map((node) => [Number(node.attrs["data-from"]), Number(node.attrs["data-to"])]);
console.log(JSON.stringify({
fetched,
return {
count: all.find((node) => node.attrs["aria-label"] === "Samples").textContent,
charts: charts.map((chart) => [chart.attrs["data-panel"], chart.attrs["data-from"], chart.attrs["data-to"]]),
panels: all.filter((node) => has(node, "res-panel")).map((node) => [node.attrs["data-panel"], node.hidden]),
charts: charts.map((chart) => [chart.attrs["data-chart"], chart.attrs["data-from"], chart.attrs["data-to"]]),
bands: charts.map((chart) => walk(chart, []).filter((node) => has(node, "band")).map((node) => node.attrs["data-zone"])),
lines: [...new Set(all.filter((node) => has(node, "res-line")).map((node) => `${node.attrs["data-series"]}|${node.attrs["data-kind"]}`))],
ids: [...new Set(all.filter((node) => has(node, "res-line")).map((node) => node.attrs["data-id"]))],
waits: marks("wait"),
asleep: marks("asleep"),
unknown: marks("unknown"),
words: all.filter((node) => has(node, "res-mark") && has(node, "unknown")).map((node) => node.children[0].children.join("")),
bars: all.filter((node) => has(node, "res-bar")).map((node) => [node.attrs["data-agent"], Number(node.attrs["data-t"])]),
legends: all.filter((node) => has(node, "res-key")).map((node) => node.children.join("")),
bars: all.filter((node) => has(node, "res-bar")).map((node) => [node.attrs["data-agent"], Number(node.attrs["data-t"]), has(node, "faint")]),
sums: all.filter((node) => has(node, "res-sum")).map((node) => [node.attrs["data-agent"], JSON.parse(node.attrs["data-points"])]),
burn: all.filter((node) => node.tag === "path" && has(node, "res-burn")).map((node) => [node.attrs["data-series"], Number(node.attrs["data-last"]), Number(node.attrs["data-top"])]),
right: all.filter((node) => node.attrs["data-axis"] === "right").map((node) => node.children.join("")),
chips: all.filter((node) => has(node, "legend-chip")).map((node) => [node.attrs["data-chip"], node.attrs["data-path"], node.attrs["aria-pressed"], node.textContent]),
switches: all.filter((node) => has(node, "res-switch")).map((node) => [node.attrs["data-switch"], node.attrs["aria-pressed"]]),
titles: all.filter((node) => has(node, "res-title")).map((node) => [node.attrs.title, node.attrs["aria-label"], node.children[0].attrs["data-icon"]]),
}));
};
};
const press = (selector) => walk(context.screen, []).find(selector).listeners.click();
(async () => {
await settle();
const normal = read();
press((node) => node.attrs["data-chip"] === "host:CPU");
press((node) => node.attrs["data-chip"] === "agent:Stage");
const hidden = read();
press((node) => node.attrs["data-switch"] === "tokens");
const accumulated = read();
press((node) => node.attrs["data-switch"] === "combined");
const combined = read();
press((node) => node.attrs["data-chip"] === "burn:scope" && node.attrs["data-path"] === "resources.combinedHidden");
press((node) => node.attrs["data-chip"] === "util:0000_3a_01_3a_00.0" && node.attrs["data-path"] === "resources.combinedHidden");
const deselected = read();
await new Promise((resolve) => setTimeout(resolve, 400));
await settle();
console.log(JSON.stringify({fetched, normal, hidden, accumulated, combined, deselected, stored: context.store.get("resources")}));
})();
""".replace("@FIXTURE@", FIXTURE)
@@ -162,24 +236,97 @@ class TestResourcesModule(unittest.TestCase):
self.assertEqual({key: first["parts"][1][key] for key in ("input", "output", "cache_read", "cache_write")}, {"input": 15, "output": 20, "cache_read": 300, "cache_write": 40})
self.assertEqual((second["t"], [(part["agent"], part["to"]) for part in second["parts"]]), (T + 3600, [("Stage", 500)]))
def test_the_hardware_series_merge_disks_host_and_gpu_lines_onto_one_axis(self):
self.assertEqual(self.data["hardware"], [["/", "disk", 8], ["/mnt", "disk", 1], ["CPU", "host", 8], ["RAM", "host", 8], ["Swap", "host", 8], ["0000:01:00.0", "util", 8], ["0000:01:00.0", "vram", 8]])
def test_accumulated_tokens_are_per_agent_running_sums_from_zero_at_each_hour_end(self):
found = self.data["accumulated"]
self.assertEqual((found["agents"], found["top"]), (["Stage", "Skills"], 510))
self.assertEqual(found["lines"], [{"agent": "Stage", "points": [[T, 0], [T + 3600, 10], [T + 7200, 510]]}, {"agent": "Skills", "points": [[T, 0], [T + 3600, 375], [T + 7200, 375]]}])
def test_accumulated_sums_never_decrease_and_end_at_each_agents_total(self):
many = self.data["many"]
self.assertTrue(many["rising"])
self.assertEqual(many["finals"], many["totals"])
self.assertEqual(many["top"], max(many["totals"].values()))
self.assertEqual(len(many["lengths"]), 1)
def test_burnup_scales_done_and_scope_to_percent_of_the_largest_scope_in_the_window(self):
found = self.data["burnup"]
self.assertEqual(found["top"], 5)
self.assertEqual(found["done"], [[1, 0], [2, 20], [3, 100], [4, 80]])
self.assertEqual(found["scope"], [[1, 40], [2, 80], [3, 100], [4, 80]])
self.assertEqual(self.data["empty"], {"top": 0, "done": [[1, 0]], "scope": [[1, 0]]})
LINES = ["/|disk", "/mnt|disk", "CPU|host", "RAM|host", "Swap|host", "0000:01:00.0|util", "0000:01:00.0|vram"]
GPU_ID = "0000_3a_01_3a_00.0"
HARDWARE_CHIPS = [["disk:/", "/ 25 %"], ["disk:/mnt", "/mnt 25 %"], ["host:CPU", "CPU 10 %"], ["host:RAM", "RAM 10 %"], ["host:Swap", "Swap 0 %"], [f"util:{GPU_ID}", "0000:01:00.0 10 %"], [f"vram:{GPU_ID}", "0000:01:00.0 VRAM 10 %"]]
AGENT_CHIPS = [["agent:Stage", "Stage 510"], ["agent:Skills", "Skills 375"]]
def chips(entries: list[list[str]], path: str, hidden: set[str]) -> list[list[str]]:
return [[chip, path, "false" if chip in hidden else "true", label] for chip, label in entries]
class TestResourcesPage(unittest.TestCase):
def test_the_view_draws_four_panels_on_one_time_axis_from_the_api_rows_alone(self):
done = subprocess.run(["node", "-e", PAGE_PROBE, str(MODULE), str(PAGE), str(T)], capture_output=True, text=True, timeout=60, check=True)
found = json.loads(done.stdout)
self.assertEqual((found["fetched"], found["count"]), (["/api/resources?from=1"], "8"))
@classmethod
def setUpClass(cls):
sources = [str(HUB / "templates" / name) for name in ("view-store.js", "legend-chips.js", "page-resources.js")]
done = subprocess.run(["node", "-e", PAGE_PROBE, str(MODULE), str(PLAN), *sources, str(T)], capture_output=True, text=True, timeout=60, check=True)
cls.found = json.loads(done.stdout)
def test_normal_mode_draws_exactly_a_hardware_and_a_tokens_chart_on_one_time_axis(self):
found = self.found["normal"]
self.assertEqual(found["count"], "8")
self.assertEqual(found["panels"], [["resources-hardware", False], ["resources-tokens", False], ["resources-combined", True]])
axis = [str(T - 600), str(T + 6000)]
self.assertEqual(found["charts"], [[panel, *axis] for panel in ("disks", "host", "gpu", "tokens")])
self.assertEqual(found["bands"], [["idle", "saturated"]] * 3 + [[]])
self.assertEqual(found["lines"], ["/|disk", "/mnt|disk", "CPU|host", "RAM|host", "Swap|host", "0000:01:00.0|util", "0000:01:00.0|vram"])
self.assertEqual(found["charts"], [["hardware", *axis], ["tokens", *axis]])
self.assertEqual(found["bands"], [["idle", "saturated"], []])
self.assertEqual(found["lines"], LINES)
self.assertEqual(found["waits"], [[T + 60, T + 180], [T + 240, T + 300], [T + 5000, T + 5060]])
self.assertEqual(found["asleep"], [[T, T + 120], [T + 5000, T + 5120]])
self.assertEqual((found["unknown"], found["words"]), ([[T + 300, T + 360]], ["0000:01:00.0 unknown: no reading"]))
self.assertEqual(found["bars"], [["Stage", T], ["Skills", T], ["Stage", T + 3600]])
self.assertEqual(found["legends"], ["/ 25 %", "/mnt 25 %", "CPU 10 %", "RAM 10 %", "Swap 0 %", "0000:01:00.0 10 %", "0000:01:00.0 VRAM 10 %", "Stage 510", "Skills 375"])
names = [("Disks", "bi-hdd-stack"), ("CPU, RAM and swap", "bi-cpu"), ("GPU and VRAM", "bi-gpu-card"), ("Tokens per agent", "bi-coin")]
self.assertEqual(found["bars"], [["Stage", T, False], ["Skills", T, False], ["Stage", T + 3600, False]])
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS, "resources.hidden", set()))
self.assertEqual(found["switches"], [["tokens", "false"], ["combined", "false"]])
names = [("Hardware: disks, CPU, RAM, swap, GPU and VRAM", "bi-cpu"), ("Tokens per agent", "bi-coin"), ("Hardware, tokens and burnup", "bi-layers")]
self.assertEqual(found["titles"], [[name, name, icon] for name, icon in names])
def test_hiding_a_chip_removes_its_line_or_its_agent_and_stores_the_list(self):
found = self.found["hidden"]
self.assertEqual(found["lines"], [line for line in LINES if line != "CPU|host"])
self.assertEqual(found["bars"], [["Skills", T, False]])
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS, "resources.hidden", {"host:CPU", "agent:Stage"}))
def test_the_accumulated_switch_draws_running_sums_of_the_shown_agents_instead_of_bars(self):
found = self.found["accumulated"]
self.assertEqual((found["bars"], found["sums"]), ([], [["Skills", [0, 375]]]))
self.assertEqual(found["switches"], [["tokens", "true"], ["combined", "false"]])
def test_the_combined_switch_replaces_both_charts_with_one_holding_every_series_switched_on(self):
found = self.found["combined"]
self.assertEqual(found["panels"], [["resources-hardware", True], ["resources-tokens", True], ["resources-combined", False]])
self.assertEqual(found["charts"], [["combined", str(T - 600), str(T + 6000)]])
self.assertEqual(found["lines"], LINES)
self.assertEqual(found["sums"], [["Stage", [0, 10, 510]], ["Skills", [0, 375, 375]]])
self.assertEqual(found["right"], ["0", "510"])
(done, done_last, done_top), (scope, scope_last, scope_top) = found["burn"]
self.assertEqual((done, scope, done_top, scope_top, scope_last), ("done", "scope", 3, 3, 100))
self.assertAlmostEqual(done_last, 200 / 3)
burn = [["burn:done", "Done 67 %"], ["burn:scope", "Scope 100 %"]]
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS + burn, "resources.combinedHidden", set()))
self.assertEqual(found["switches"], [["tokens", "true"], ["combined", "true"]])
def test_each_combined_series_is_deselectable_and_every_switch_is_stored_in_one_patch(self):
found = self.found["deselected"]
self.assertEqual([series for series, _, _ in found["burn"]], ["done"])
self.assertEqual(found["lines"], [line for line in LINES if line != "0000:01:00.0|util"])
stored = {"hidden": ["host:CPU", "agent:Stage"], "tokens": "accumulated", "combined": True, "combinedHidden": ["burn:scope", f"util:{GPU_ID}"]}
self.assertEqual(self.found["stored"], stored)
changes = {f"resources.{key}": value for key, value in stored.items()}
self.assertEqual(self.found["fetched"], ["/api/resources?from=1", ["PATCH", "/api/view", changes]])
class TestHubResources(HubCase):
def write(self, name: str, rows: list[dict], tail: str = "") -> None:
@@ -218,7 +365,7 @@ class TestHubResources(HubCase):
self.assertIn(MODULE.read_text(encoding="utf-8").strip(), html)
self.assertIn(PAGE.read_text(encoding="utf-8").strip(), html)
self.assertIn(STYLE.read_text(encoding="utf-8").strip(), html)
for name in ("speedometer2", "hdd-stack", "cpu", "gpu-card", "coin", "hourglass-split", "moon", "question-circle"):
for name in ("speedometer2", "cpu", "coin", "layers", "graph-up-arrow", "hourglass-split", "moon", "question-circle"):
with self.subTest(icon=name):
self.assertIn(f'<symbol id="bi-{name}"', html)
done = subprocess.run(["node", "-e", "const N = require(process.argv[1]); console.log(JSON.stringify([N.TABS.at(-1), N.ICONS.resources, N.route('#resources')]));", str(NAV)], capture_output=True, text=True, timeout=60, check=True)
+657
View File
@@ -0,0 +1,657 @@
from __future__ import annotations
import copy
import json
import re
import subprocess
import tempfile
import unittest
from pathlib import Path
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.errors import Conflict, ToolError
from kvb_orchestrator.handler import BODY_LIMIT
from kvb_orchestrator.viewstore import (
CHANGE_LIMIT,
VIEW,
VIEW_LIMIT,
ViewStore,
valid_view,
value_at,
)
STORE = HUB / "templates" / "view-store.js"
CHIPS = HUB / "templates" / "legend-chips.js"
CHIPS_STYLE = HUB / "templates" / "style-legend-chips.css"
FULL = {
"resources": {"hidden": ["host:CPU", "agent:Stage", "disk:/mnt_20_data"], "tokens": "accumulated", "combined": True, "combinedHidden": ["burn:scope"]},
"burn": {"overlays": ["fix", "score"]},
"home": {"pins": [{"kind": "panel", "id": "resources-hardware", "w": 12, "h": 4}, {"kind": "task", "id": "work/feature", "w": 1, "h": 12}]},
}
PIN = {"kind": "panel", "id": "resources-hardware", "w": 6, "h": 3}
EMBEDDED = re.compile(r'<script nonce="[^"]+" id="view" type="application/json">(.*?)</script>')
MAKE = """
const make = (tag) => {
const node = {tag, attrs: {}, children: [], listeners: {}, textContent: "",
set className(value) { this.attrs.class = value; }, set type(value) { this.attrs.type = value; },
setAttribute(key, value) { this.attrs[key] = String(value); }, getAttribute(key) { return this.attrs[key]; },
addEventListener(name, run) { this.listeners[name] = run; }, append(...items) { this.children.push(...items); }};
return node;
};
"""
CLIENT_PROBE = """
const fs = require("fs");
const vm = require("vm");
const doc = JSON.parse(process.argv[2]);
const clone = (value) => JSON.parse(JSON.stringify(value));
const same = (one, other) => JSON.stringify(one) === JSON.stringify(other);
const server = clone(doc);
const sent = [];
const hooks = {};
let refuse = false;
let churn = 0;
let hold = null;
let gets = 0;
const at = (path) => path.split(".").reduce((node, key) => node[key], server);
const context = {
setTimeout, clearTimeout,
addEventListener: (name, run) => {
hooks[name] = (hooks[name] || 0) + 1;
hooks[`${name}:run`] = run;
},
document: {getElementById: (name) => (name === "view" ? {textContent: JSON.stringify(doc)} : null)},
fetch: async (url, options) => {
if (options.method === "GET") {
gets += 1;
return {ok: true, status: 200, json: async () => clone(server)};
}
const body = JSON.parse(options.body);
sent.push({url, method: options.method, headers: options.headers, keepalive: options.keepalive, body});
if (hold) {
await hold;
}
if (refuse) {
refuse = false;
return {ok: false, status: 400, text: async () => "resources.tokens must be one of interval, accumulated\\n"};
}
if (churn && Object.hasOwn(body.set, "resources.hidden")) {
server.resources.hidden = [...server.resources.hidden, `churn${churn}`];
churn += 1;
}
const moved = Object.keys(body.set).filter((path) => !same(at(path), body.expect[path]));
if (moved.length) {
return {ok: false, status: 409, json: async () => ({view: clone(server), paths: moved})};
}
for (const [path, value] of Object.entries(body.set)) {
const [section, key] = path.split(".");
server[section][key] = value;
}
return {ok: true, status: 200, json: async () => clone(server)};
},
};
vm.runInNewContext(`${fs.readFileSync(process.argv[1], "utf8")}\\nstore = ViewStore;`, context);
const V = context.store;
const heard = [];
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const caught = (run) => {
try {
run();
} catch (error) {
return error.message;
}
return null;
};
(async () => {
V.on("resources.hidden", (value) => heard.push(["hidden", value]));
V.on("home.pins", (value) => heard.push(["pins", value]));
const unknown = caught(() => V.get("resources.nope"));
const wide = caught(() => V.update("resources", (value) => value));
const listSet = caught(() => V.set("resources.hidden", ["host:RAM"]));
const first = V.update("resources.hidden", (list) => [...list, "host:CPU"]);
const second = V.set("resources.tokens", "accumulated");
const local = [V.get("resources.hidden"), V.get("resources.tokens"), sent.length, clone(heard)];
const stored = await Promise.all([first, second]);
server.home.pins = [@PIN@];
server.burn.overlays = ["fix"];
const combined = await V.set("resources.combined", true);
const adopted = [V.get("home.pins"), V.get("burn.overlays"), V.get("resources.hidden"), heard.filter(([name]) => name === "pins").at(-1)];
server.resources.hidden = ["host:CPU", "host:RAM"];
const rebased = await V.update("resources.hidden", (list) => [...list, "disk:/"]);
const merged = V.get("resources.hidden");
churn = 1;
const churned = await V.update("resources.hidden", (list) => list.filter((item) => item !== "disk:/"));
churn = 0;
const gaveUp = [V.get("resources.hidden"), V.error()];
refuse = true;
let release = null;
hold = new Promise((resolve) => {
release = resolve;
});
const refusing = V.set("resources.tokens", "interval");
await wait(400);
const newer = V.update("resources.combinedHidden", (list) => [...list, "burn:scope"]);
hold = null;
release();
const refused = await refusing;
const during = [V.get("resources.tokens"), V.get("resources.combinedHidden"), V.error()];
const kept = await newer;
hold = new Promise((resolve) => {
release = resolve;
});
const flying = V.update("burn.overlays", (list) => [...list.filter((item) => item !== "fix"), "score"]);
await wait(400);
const leaving = V.update("home.pins", (pins) => pins.filter((pin) => pin.id !== "@PIN_ID@"));
const beforeLeave = sent.length;
hooks["pagehide:run"]();
await new Promise((resolve) => setImmediate(resolve));
const atLeave = sent.length;
hold = null;
release();
const landed = await Promise.all([flying, leaving]);
await wait(400);
const left = clone(server);
server.resources.combined = false;
await hooks["pageshow:run"]({persisted: false});
const notRestored = [gets, V.get("resources.combined")];
await hooks["pageshow:run"]({persisted: true});
const restored = [gets, V.get("resources.combined"), hooks.pageshow];
const long = "x".repeat(300);
console.log(JSON.stringify({
unknown, wide, listSet, local, stored, combined, adopted, rebased, merged, churned, gaveUp, refused, during, kept, beforeLeave, atLeave, landed, sent, server: left, hooks: hooks.pagehide, error: V.error(), notRestored, restored,
ids: [V.id("host", "CPU"), V.id("disk", "/"), V.id("util", "0000:01:00.0"), V.id("agent", "Stage (this session) [5d88e9]"), V.id("a:b", "c"), V.id("a", "b:c"), V.id("a_3a_b"), V.id("agent", long), V.id("agent", `${long}y`), V.id("agent", "Ärger 🦫")],
}));
})();
""".replace("@PIN@", json.dumps(PIN)).replace("@PIN_ID@", PIN["id"])
TWO_TABS = (
"""
const fs = require("fs");
const vm = require("vm");
const code = `${fs.readFileSync(process.argv[1], "utf8")}\\n${fs.readFileSync(process.argv[2], "utf8")}`;
const base = process.argv[3];
@MAKE@
const SERIES = {
"resources.hidden": [{id: "host:CPU", label: "CPU", tone: "res-c0"}, {id: "host:RAM", label: "RAM", tone: "res-c1"}],
"burn.overlays": [{id: "fix", label: "Fix", tone: "k-fix"}, {id: "score", label: "Score", tone: "k-score"}],
};
const MODE = {"resources.hidden": "hidden", "burn.overlays": "shown"};
const tab = async () => {
const doc = await (await fetch(`${base}/api/view`)).json();
const groups = [];
const context = {
setTimeout, clearTimeout,
document: {getElementById: () => ({textContent: JSON.stringify(doc)}), createElement: make, querySelectorAll: () => groups.flatMap((group) => group.children)},
fetch: (url, options) => fetch(`${base}${url}`, {...options, headers: {...options.headers, Origin: base}}),
};
vm.runInNewContext(`${code}\\nstore = ViewStore;\\nchips = LegendChips;`, context);
const draw = (path) => {
const group = context.chips.render(SERIES[path], path, MODE[path]);
groups.splice(0, groups.length, group);
return group;
};
const press = (path, id) => {
draw(path).children.find((chip) => chip.attrs["data-chip"] === id).listeners.click();
return context.store.update(path, (list) => list);
};
const pressed = (path) => draw(path).children.map((chip) => [chip.attrs["data-chip"], chip.attrs["aria-pressed"]]);
const pin = (id) => context.store.update("home.pins", (pins) => [...pins, {kind: "panel", id, w: 6, h: 3}]);
const refused = (path, value) => {
try {
context.store.set(path, value);
} catch (error) {
return error.message;
}
return null;
};
return {store: context.store, press, pressed, pin, refused};
};
const SCENARIOS = {
merge: async (a, b) => {
const refused = [a.refused("resources.hidden", ["host:RAM"]), b.refused("home.pins", [])];
const results = [await a.press("resources.hidden", "host:CPU"), await b.press("resources.hidden", "host:RAM"), await a.pin("p1"), await b.pin("p2")];
return {refused, results, seen: [a.store.get("resources.hidden"), b.store.get("home.pins").map((item) => item.id)]};
},
same: async (a, b) => {
const results = [];
for (const [path, id] of [["resources.hidden", "host:CPU"], ["burn.overlays", "fix"]]) {
results.push(await a.press(path, id), await b.press(path, id));
}
return {results, chips: [b.pressed("resources.hidden"), b.pressed("burn.overlays")]};
},
};
(async () => {
const a = await tab();
const b = await tab();
const found = await SCENARIOS[process.argv[4]](a, b);
const served = await (await fetch(`${base}/api/view`)).json();
console.log(JSON.stringify({...found, errors: [a.store.error(), b.store.error()], hidden: served.resources.hidden, overlays: served.burn.overlays, pins: served.home.pins.map((item) => item.id)}));
})();
"""
).replace("@MAKE@", MAKE)
CHIPS_PROBE = (
"""
const fs = require("fs");
const vm = require("vm");
@MAKE@
const updates = [];
const groups = [];
const lists = {"resources.hidden": ["b"], "burn.overlays": ["fix"]};
const context = {
document: {createElement: make, querySelectorAll: (selector) => (selector === ".legend-chip" ? groups.flatMap((group) => group.children) : [])},
ViewStore: {get: (path) => [...lists[path]], update: (path, change) => { lists[path] = change([...lists[path]]); updates.push([path, lists[path]]); return Promise.resolve(true); }},
};
vm.runInNewContext(`${fs.readFileSync(process.argv[1], "utf8")}\\nchips = LegendChips;`, context);
const L = context.chips;
const group = L.render([{id: "a", label: "CPU 10 %", tone: "res-c0 k-host"}, {id: "b", label: "RAM 20 %", tone: "res-c1 k-host"}], "resources.hidden", "hidden");
groups.push(group);
const read = (node) => node.children.map((chip) => [chip.attrs["data-chip"], chip.attrs["aria-pressed"], chip.attrs.class, chip.textContent, chip.attrs.title]);
const before = read(group);
group.children[0].listeners.click();
group.children[1].listeners.click();
const after = read(group);
lists["resources.hidden"] = [...Array.from({length: 100}, (_, index) => `old${index}`), "a"];
group.children[1].listeners.click();
const overlays = L.render([{id: "fix", label: "Fix", tone: "k-fix"}, {id: "score", label: "Score", tone: "k-score"}], "burn.overlays", "shown");
const shownBefore = read(overlays);
overlays.children[1].listeners.click();
overlays.children[0].listeners.click();
let bad = null;
try {
L.render([], "burn.overlays", "on");
} catch (error) {
bad = error.message;
}
const many = Array.from({length: 103}, (_, index) => `o${index}`);
console.log(JSON.stringify({
group: [group.tag, group.attrs.class, group.attrs.role], before, after, updates, keep: L.KEEP, shownBefore, shownAfter: read(overlays), overlaysGroup: overlays.attrs["aria-label"], bad,
placed: [L.placed(["a"], "a", false), L.placed(["a"], "b", true), L.placed([], "a", true), L.placed(["a"], "a", true), L.placed([], "a", false)],
pruned: [L.pruned(many, new Set(["o0", "o1"])), L.pruned(["x", "y"], new Set())],
}));
"""
).replace("@MAKE@", MAKE)
def change(values: dict, expected: dict) -> bytes:
return json.dumps({"set": values, "expect": expected}).encode()
class TestViewDocument(unittest.TestCase):
def test_the_defaults_and_a_full_document_pass_unchanged(self):
self.assertEqual(valid_view(copy.deepcopy(VIEW)), VIEW)
self.assertEqual(valid_view(copy.deepcopy(FULL)), FULL)
self.assertEqual(VIEW, {"resources": {"hidden": [], "tokens": "interval", "combined": False, "combinedHidden": []}, "burn": {"overlays": []}, "home": {"pins": []}})
def test_unknown_or_missing_keys_and_malformed_values_are_refused(self):
def changed(section: str | None, key: str | None, value: object) -> dict:
doc = copy.deepcopy(FULL)
if section is None:
doc[key] = value
elif key is None:
doc[section] = value
elif value is KeyError:
del doc[section][key]
else:
doc[section][key] = value
return doc
pin = FULL["home"]["pins"][0]
cases = {
"not an object": [],
"unknown top key": changed(None, "theme", "dark"),
"unknown nested key": changed("resources", "extra", 1),
"missing key": changed("resources", "combinedHidden", KeyError),
"section not an object": changed("burn", None, ["fix"]),
"tokens mode": changed("resources", "tokens", "sum"),
"combined not a flag": changed("resources", "combined", 1),
"hidden not a list": changed("resources", "hidden", "host:CPU"),
"id with a space": changed("resources", "hidden", ["host CPU"]),
"empty id": changed("resources", "combinedHidden", [""]),
"id too long": changed("resources", "hidden", ["x" * 201]),
"repeated id": changed("resources", "hidden", ["a", "a"]),
"too many ids": changed("resources", "hidden", [f"id{index}" for index in range(501)]),
"unknown overlay": changed("burn", "overlays", ["cost"]),
"repeated overlay": changed("burn", "overlays", ["fix", "fix"]),
"null overlay": changed("burn", "overlays", [None]),
"65 pins": changed("home", "pins", [{**pin, "id": f"p{index}"} for index in range(65)]),
"pin width 0": changed("home", "pins", [{**pin, "w": 0}]),
"pin height 13": changed("home", "pins", [{**pin, "h": 13}]),
"pin width true": changed("home", "pins", [{**pin, "w": True}]),
"pin width 1.5": changed("home", "pins", [{**pin, "w": 1.5}]),
"pin kind too long": changed("home", "pins", [{**pin, "kind": "k" * 33}]),
"pin id too long": changed("home", "pins", [{**pin, "id": "i" * 201}]),
"pin unknown key": changed("home", "pins", [{**pin, "x": 1}]),
"pin twice": changed("home", "pins", [pin, dict(pin)]),
}
for name, doc in cases.items():
with self.subTest(case=name), self.assertRaises(ToolError):
valid_view(doc)
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)]
self.assertEqual(valid_view(doc), doc)
def store(self) -> ViewStore:
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
return ViewStore(Path(folder.name) / "view.json")
def test_two_tabs_changing_other_paths_both_land_in_the_store(self):
store = self.store()
store.patch(change({"home.pins": [PIN], "burn.overlays": ["fix"]}, {"home.pins": [], "burn.overlays": []}))
found = store.patch(change({"resources.hidden": ["host:CPU"]}, {"resources.hidden": []}))
expected = {**VIEW, "resources": {**VIEW["resources"], "hidden": ["host:CPU"]}, "burn": {"overlays": ["fix"]}, "home": {"pins": [PIN]}}
self.assertEqual((found, ViewStore(store.file).get()), (expected, expected))
def test_a_stale_tab_on_the_same_path_gets_a_conflict_and_its_retry_keeps_both_changes(self):
store = self.store()
store.patch(change({"resources.hidden": ["host:CPU"], "home.pins": [PIN]}, {"resources.hidden": [], "home.pins": []}))
before = store.file.read_bytes()
second = {**PIN, "id": "p2"}
with self.assertRaises(Conflict) as caught:
store.patch(change({"resources.hidden": ["host:RAM"], "home.pins": [second]}, {"resources.hidden": [], "home.pins": []}))
self.assertEqual(caught.exception.payload["paths"], ["resources.hidden", "home.pins"])
self.assertEqual(value_at(caught.exception.payload["view"], "home.pins"), [PIN])
self.assertEqual((store.file.read_bytes(), store.get()["resources"]["hidden"]), (before, ["host:CPU"]))
found = store.patch(change({"resources.hidden": ["host:CPU", "host:RAM"], "home.pins": [PIN, second]}, {"resources.hidden": ["host:CPU"], "home.pins": [PIN]}))
self.assertEqual((found["resources"]["hidden"], found["home"]["pins"]), (["host:CPU", "host:RAM"], [PIN, second]))
class TestViewClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
done = subprocess.run(["node", "-e", CLIENT_PROBE, str(STORE), json.dumps(VIEW)], capture_output=True, text=True, timeout=60, check=True)
cls.data = json.loads(done.stdout)
chips = subprocess.run(["node", "-e", CHIPS_PROBE, str(CHIPS)], capture_output=True, text=True, timeout=60, check=True)
cls.chips = json.loads(chips.stdout)
def bodies(self) -> list[dict]:
return [entry["body"] for entry in self.data["sent"]]
def test_set_changes_the_value_at_once_and_sends_one_debounced_patch_against_the_stored_values(self):
data = self.data
self.assertEqual(data["unknown"], "unknown view path resources.nope")
self.assertEqual(data["wide"], "a view path to change must be section.key, not resources")
self.assertEqual(data["local"], [["host:CPU"], "accumulated", 0, [["hidden", ["host:CPU"]]]])
self.assertEqual(data["stored"], [True, True])
first = data["sent"][0]
self.assertEqual((first["url"], first["method"], first["headers"], first["keepalive"]), ("/api/view", "PATCH", {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, False))
self.assertEqual(self.bodies()[:2], [
{"set": {"resources.hidden": ["host:CPU"], "resources.tokens": "accumulated"}, "expect": {"resources.hidden": [], "resources.tokens": "interval"}},
{"set": {"resources.combined": True}, "expect": {"resources.combined": False}},
])
def test_an_older_tab_adopts_what_another_browser_stored_on_other_paths(self):
self.assertTrue(self.data["combined"])
self.assertEqual(self.data["adopted"], [[PIN], ["fix"], ["host:CPU"], ["pins", [PIN]]])
def test_a_conflict_on_the_same_path_reruns_the_change_on_the_stored_value_and_keeps_both(self):
self.assertTrue(self.data["rebased"])
self.assertEqual(self.data["merged"], ["host:CPU", "host:RAM", "disk:/"])
self.assertEqual(self.bodies()[2:4], [
{"set": {"resources.hidden": ["host:CPU", "disk:/"]}, "expect": {"resources.hidden": ["host:CPU"]}},
{"set": {"resources.hidden": ["host:CPU", "host:RAM", "disk:/"]}, "expect": {"resources.hidden": ["host:CPU", "host:RAM"]}},
])
def test_three_conflicts_in_a_row_give_up_with_the_stored_value_and_a_reason(self):
self.assertFalse(self.data["churned"])
self.assertEqual(len([body for body in self.bodies()[4:7] if "resources.hidden" in body["set"]]), 3)
self.assertEqual(self.data["gaveUp"], [["host:CPU", "host:RAM", "disk:/", "churn1", "churn2", "churn3"], "another browser kept changing resources.hidden; try again"])
def test_a_refused_change_falls_back_to_the_stored_value_and_keeps_a_newer_change(self):
self.assertFalse(self.data["refused"])
self.assertEqual(self.data["during"], ["accumulated", ["burn:scope"], "resources.tokens must be one of interval, accumulated"])
self.assertTrue(self.data["kept"])
def test_leaving_the_page_while_a_patch_is_in_flight_sends_the_pending_change_at_once_with_keepalive(self):
data = self.data
self.assertEqual(data["hooks"], 1)
self.assertEqual((data["beforeLeave"], data["atLeave"]), (10, 11))
last = data["sent"][-1]
self.assertEqual((last["keepalive"], last["body"]), (True, {"set": {"home.pins": []}, "expect": {"home.pins": [PIN]}}))
self.assertEqual(data["landed"], [True, True])
self.assertEqual((data["server"]["burn"]["overlays"], data["server"]["home"]["pins"], data["error"]), (["score"], [], ""))
def test_set_refuses_a_list_path_so_a_retry_cannot_overwrite_what_another_browser_stored(self):
self.assertEqual(self.data["listSet"], "a list view path changes through update(), not set(): resources.hidden")
self.assertEqual(self.data["local"][0], ["host:CPU"])
def test_a_page_restored_from_the_back_forward_cache_refetches_the_stored_view(self):
self.assertEqual(self.data["notRestored"], [0, True])
self.assertEqual(self.data["restored"], [1, False, 1])
def test_ids_follow_the_view_grammar_and_stay_distinct(self):
ids = self.data["ids"]
self.assertEqual(ids[:4], ["host:CPU", "disk:/", "util:0000_3a_01_3a_00.0", "agent:Stage_20__28_this_20_session_29__20__5b_5d88e9_5d_"])
self.assertEqual(len(set(ids)), len(ids))
for value in ids:
with self.subTest(id=value):
self.assertRegex(value, r"\A[A-Za-z0-9:_./-]{1,200}\Z")
self.assertEqual(len(ids[7]), 200)
def test_hidden_mode_chips_are_pressed_while_shown_and_toggle_through_update(self):
chips = self.chips
self.assertEqual(chips["group"], ["div", "legend-chips", "group"])
self.assertEqual(chips["before"], [
["a", "true", "legend-chip res-c0 k-host", "CPU 10 %", "CPU 10 %: shown, press to hide"],
["b", "false", "legend-chip res-c1 k-host", "RAM 20 %", "RAM 20 %: hidden, press to show"],
])
self.assertEqual([chip[1] for chip in chips["after"]], ["false", "true"])
self.assertEqual(chips["updates"][:2], [["resources.hidden", ["b", "a"]], ["resources.hidden", ["a"]]])
self.assertEqual(chips["placed"], [[], ["a", "b"], ["a"], ["a"], []])
def test_shown_mode_chips_are_pressed_while_switched_on_and_store_the_on_list(self):
chips = self.chips
self.assertEqual([chip[:2] for chip in chips["shownBefore"]], [["fix", "true"], ["score", "false"]])
self.assertEqual(chips["shownBefore"][1][4], "Score: off, press to switch it on")
self.assertEqual(chips["updates"][3:], [["burn.overlays", ["fix", "score"]], ["burn.overlays", ["score"]]])
self.assertEqual([chip[1] for chip in chips["shownAfter"]], ["false", "true"])
self.assertEqual(chips["overlaysGroup"], "Overlays: press a chip to switch it on or off")
self.assertEqual(chips["bad"], "a legend chip mode must be hidden or shown, not on")
def test_hidden_ids_of_series_gone_from_the_page_are_dropped_oldest_first_beyond_the_cap(self):
chips = self.chips
self.assertEqual(chips["keep"], 100)
many, short = chips["pruned"]
self.assertEqual((len(many), many[:3]), (100, ["o0", "o1", "o5"]))
self.assertEqual(short, ["x", "y"])
path, last = chips["updates"][2]
self.assertEqual((path, len(last), last[:2], last[-2:]), ("resources.hidden", 100, ["old2", "old3"], ["a", "b"]))
class TestHubView(HubCase):
def patch(self, body: bytes, headers: dict[str, str]) -> tuple[int, bytes]:
status, _, answer = self.call("PATCH", "/api/view", body, {**headers, "Content-Type": "application/json"})
return status, answer
def view(self) -> dict:
status, _, body = self.get("/api/view")
self.assertEqual(status, 200, body)
return json.loads(body)
def write(self, values: dict) -> tuple[int, bytes]:
stored = self.view()
return self.patch(change(values, {path: value_at(stored, path) for path in values}), self.guard(self.host))
def reopen(self) -> None:
self.httpd.hub = server.open_hub(
council=self.council, pages=self.pages, public_port=self.port, state=self.state, host_state=HOST_STATE, transcripts=self.projects, scripts=self.scripts,
templates=HUB / "templates",
places=server.Places(tree_file=self.tree_file, timers=self.timers, vendor=HUB / "vendor", cleanup=self.cleanup, offices=self.offices, kept=self.kept),
model=server.Model(url="", model="", key=""), engines=self.engines, approvers=self.approvers, redirects=self.redirects(),
)
def test_a_get_and_a_patch_round_trip_and_the_file_is_private(self):
self.assertEqual(self.view(), VIEW)
status, body = self.write(FULL)
self.assertEqual((status, json.loads(body)), (200, FULL))
self.assertEqual(self.view(), FULL)
stored = self.state / "view.json"
self.assertEqual(json.loads(stored.read_text(encoding="utf-8")), FULL)
self.assertEqual(stored.stat().st_mode & 0o777, 0o600)
def test_two_tabs_changing_other_paths_keep_both_changes(self):
self.assertEqual(self.patch(change({"home.pins": [PIN], "burn.overlays": ["fix"]}, {"home.pins": [], "burn.overlays": []}), self.guard(self.host))[0], 200)
status, body = self.patch(change({"resources.hidden": ["host:CPU"]}, {"resources.hidden": []}), self.guard(self.host))
expected = {**VIEW, "resources": {**VIEW["resources"], "hidden": ["host:CPU"]}, "burn": {"overlays": ["fix"]}, "home": {"pins": [PIN]}}
self.assertEqual((status, json.loads(body), self.view()), (200, expected, expected))
def test_a_stale_expect_answers_409_with_the_stored_view_and_stores_nothing(self):
self.assertEqual(self.write({"resources.hidden": ["host:CPU"]})[0], 200)
before = (self.state / "view.json").read_bytes()
status, body = self.patch(change({"resources.hidden": ["host:RAM"], "burn.overlays": ["fix"]}, {"resources.hidden": [], "burn.overlays": []}), self.guard(self.host))
found = json.loads(body)
self.assertEqual((status, found["paths"], found["view"]["resources"]["hidden"]), (409, ["resources.hidden"], ["host:CPU"]))
self.assertEqual(((self.state / "view.json").read_bytes(), self.view()["burn"]["overlays"]), (before, []))
def tabs(self, scenario: str) -> dict:
done = subprocess.run(["node", "-e", TWO_TABS, str(STORE), str(CHIPS), self.origin, scenario], capture_output=True, text=True, timeout=60, check=True)
return json.loads(done.stdout)
def test_two_stale_tabs_of_the_page_scripts_keep_both_hidden_series_and_both_pins(self):
found = self.tabs("merge")
refusal = "a list view path changes through update(), not set(): "
self.assertEqual(found["refused"], [f"{refusal}resources.hidden", f"{refusal}home.pins"])
self.assertEqual((found["results"], found["errors"]), ([True, True, True, True], ["", ""]))
self.assertEqual((found["hidden"], found["pins"]), (["host:CPU", "host:RAM"], ["p1", "p2"]))
self.assertEqual(found["seen"], [["host:CPU", "host:RAM"], ["p1", "p2"]])
self.assertEqual((self.view()["resources"]["hidden"], [pin["id"] for pin in self.view()["home"]["pins"]]), (["host:CPU", "host:RAM"], ["p1", "p2"]))
def test_a_stale_tab_pressing_a_chip_another_tab_already_pressed_keeps_that_choice_in_both_modes(self):
found = self.tabs("same")
self.assertEqual((found["results"], found["errors"]), ([True, True, True, True], ["", ""]))
self.assertEqual((found["hidden"], found["overlays"]), (["host:CPU"], ["fix"]))
self.assertEqual(found["chips"], [[["host:CPU", "false"], ["host:RAM", "true"]], [["fix", "true"], ["score", "false"]]])
self.assertEqual((self.view()["resources"]["hidden"], self.view()["burn"]["overlays"]), (["host:CPU"], ["fix"]))
def test_a_malformed_change_an_unknown_path_or_a_body_past_the_change_limit_answers_400_and_stores_nothing(self):
self.assertEqual(self.write(FULL)[0], 200)
stored = self.state / "view.json"
before = stored.read_bytes()
padded = change({"resources.tokens": "interval"}, {"resources.tokens": "accumulated"})
bodies = {
"not json": b"{resources",
"not utf-8": b"\xff\xfe{}",
"a list": b"[]",
"no expect": json.dumps({"set": {"resources.tokens": "interval"}}).encode(),
"expect other paths": change({"resources.tokens": "interval"}, {"resources.combined": True}),
"set and more": json.dumps({"set": {"resources.tokens": "interval"}, "expect": {"resources.tokens": "accumulated"}, "theme": "dark"}).encode(),
"empty set": change({}, {}),
"set a list": json.dumps({"set": [["resources.tokens", "interval"]], "expect": {}}).encode(),
"unknown section": change({"theme": "dark"}, {"theme": "sky"}),
"unknown key": change({"resources.extra": 1}, {"resources.extra": 0}),
"too deep": change({"home.pins.0": PIN}, {"home.pins.0": PIN}),
"a section missing a key": change({"resources": {"hidden": []}}, {"resources": FULL["resources"]}),
"malformed value": change({"resources.combined": "yes"}, {"resources.combined": True}),
"null overlay": change({"burn.overlays": [None]}, {"burn.overlays": ["fix", "score"]}),
"one bad of two": change({"resources.tokens": "interval", "home.pins": [{**PIN, "w": 13}]}, {"resources.tokens": "accumulated", "home.pins": FULL["home"]["pins"]}),
"the change limit and one byte": padded + b" " * (CHANGE_LIMIT + 1 - len(padded)),
"512 KiB": padded + b" " * (512 << 10),
}
self.assertEqual((CHANGE_LIMIT, len(bodies["the change limit and one byte"])), (129 << 10, CHANGE_LIMIT + 1))
for name, body in bodies.items():
with self.subTest(case=name):
status, answer = self.patch(body, self.guard(self.host))
self.assertEqual(status, 400, answer)
self.assertEqual(stored.read_bytes(), before)
self.assertEqual(self.view(), FULL)
exact = padded + b" " * (CHANGE_LIMIT - len(padded))
self.assertEqual(self.patch(exact, self.guard(self.host))[0], 200)
def test_a_list_near_the_document_cap_can_still_shrink_but_never_grow(self):
ids = [f"agent:{'a' * 190}{index:04d}" for index in range(320)]
status, answer = self.write({"resources.hidden": ids})
self.assertEqual(status, 200, answer)
stored = self.state / "view.json"
self.assertGreater(len(stored.read_bytes()), VIEW_LIMIT - (1 << 10))
before = stored.read_bytes()
status, answer = self.write({"resources.hidden": [*ids, *(f"agent:{'b' * 190}{index:04d}" for index in range(3))]})
self.assertEqual(status, 400, answer)
self.assertIn(b"the view document must not exceed", answer)
self.assertEqual((stored.read_bytes(), self.view()["resources"]["hidden"]), (before, ids))
shrink = change({"resources.hidden": ids[:-1]}, {"resources.hidden": ids})
self.assertGreater(len(shrink), VIEW_LIMIT)
status, answer = self.patch(shrink, self.guard(self.host))
self.assertEqual(status, 200, answer)
self.assertEqual(self.view()["resources"]["hidden"], ids[:-1])
def test_changes_that_grow_the_document_past_64_kib_are_refused(self):
ids = [f"agent:{'a' * 180}{index:04d}" for index in range(300)]
self.assertEqual(self.write({"resources.hidden": ids})[0], 200)
before = (self.state / "view.json").read_bytes()
status, answer = self.write({"resources.combinedHidden": [f"burn:{'b' * 180}{index:04d}" for index in range(100)]})
self.assertEqual(status, 400, answer)
self.assertEqual((self.state / "view.json").read_bytes(), before)
self.assertEqual(self.view()["resources"]["combinedHidden"], [])
def test_a_body_beyond_the_hub_wide_limit_answers_413_and_stores_nothing(self):
status, _, answer = self.call("PATCH", "/api/view", b" " * (BODY_LIMIT + 1), {**self.guard(self.host), "Content-Type": "application/json"})
self.assertEqual(status, 413, answer)
self.assertFalse((self.state / "view.json").exists())
def test_a_whole_document_put_no_longer_exists(self):
status, _, _ = self.call("PUT", "/api/view", json.dumps(FULL).encode(), {**self.guard(self.host), "Content-Type": "application/json"})
self.assertEqual(status, 501)
self.assertEqual(self.view(), VIEW)
def test_a_patch_needs_no_passkey_and_leaves_the_signed_settings_file_byte_identical(self):
saved = {**self.snapshot()["settings"], "theme": "dark"}
status, _, body = self.post_json("/api/settings", saved, self.guard(self.host))
self.assertEqual(status, 200, body)
settings = self.state / "settings.json"
before, stamp = settings.read_bytes(), settings.stat().st_mtime_ns
self.assertFalse(self.approvers.exists())
status, body = self.write(FULL)
self.assertEqual(status, 200, body)
self.assertEqual((settings.read_bytes(), settings.stat().st_mtime_ns), (before, stamp))
self.assertEqual(self.snapshot()["settings"], saved)
def test_a_patch_without_the_header_from_a_foreign_origin_or_host_is_refused(self):
body = change(FULL, VIEW)
cases = {
"no header": {"Host": self.host, "Origin": f"http://{self.host}"},
"foreign origin": {"Host": self.host, "Origin": "http://evil.example", "X-Kvb-Orchestrator": "1"},
"no origin": {"Host": self.host, "X-Kvb-Orchestrator": "1"},
}
for name, headers in cases.items():
with self.subTest(case=name):
self.assertEqual(self.patch(body, headers)[0], 403)
self.assertEqual(self.patch(body, {**self.guard(self.host), "Host": "evil.example"})[0], 403)
self.assertEqual(self.call("PATCH", "/api/settings", body, self.guard(self.host))[0], 404)
self.assertFalse((self.state / "view.json").exists())
self.assertEqual(self.view(), VIEW)
def test_a_failed_write_keeps_the_stored_file_and_the_served_document(self):
self.assertEqual(self.write(VIEW)[0], 200)
stored = self.state / "view.json"
before = stored.read_bytes()
with mock.patch.object(records.os, "replace", side_effect=OSError(28, "No space left on device")):
status, body = self.write(FULL)
self.assertEqual(status, 500, body)
self.assertEqual(stored.read_bytes(), before)
self.assertEqual(self.view(), VIEW)
def test_the_store_survives_a_hub_restart_and_the_page_carries_it(self):
self.assertEqual(self.write(FULL)[0], 200)
self.reopen()
self.assertEqual(self.view(), FULL)
embedded = EMBEDDED.search(self.index())
self.assertIsNotNone(embedded)
self.assertEqual(json.loads(embedded.group(1)), FULL)
def test_a_broken_stored_file_falls_back_to_the_defaults(self):
(self.state / "view.json").write_text('{"resources": {"hidden": ["bad id"]}}', encoding="utf-8")
self.reopen()
self.assertEqual(self.view(), VIEW)
def test_the_page_carries_the_store_the_chips_and_their_style(self):
html = self.index()
for path in (STORE, CHIPS, CHIPS_STYLE):
with self.subTest(file=path.name):
self.assertIn(path.read_text(encoding="utf-8").strip(), html)
if __name__ == "__main__":
unittest.main()