diff --git a/orchestrator/templates/page-dialog.js b/orchestrator/templates/page-dialog.js index 313f53d..7c8b58c 100644 --- a/orchestrator/templates/page-dialog.js +++ b/orchestrator/templates/page-dialog.js @@ -20,6 +20,27 @@ } } + /** + * @param {HTMLElement} root the element draw() rebuilds. + * @param {Function} draw replaces root's children; an element scrolled inside a row with data-item gets its offsets back when draw() rebuilds it in the same row as the same nth element of its tag and class, so stars that Panels.decorate adds later do not shift it. + */ + function keepScroll(root, draw) { + const twins = (row, tag, name) => [...row.getElementsByTagName(tag)].filter((node) => node.className === name); + const held = [...root.querySelectorAll("[data-item] *")].filter((node) => node.scrollTop || node.scrollLeft).map((node) => { + const row = node.closest("[data-item]"); + return [row.dataset.item, node.tagName, node.className, twins(row, node.tagName, node.className).indexOf(node), node.scrollTop, node.scrollLeft]; + }); + draw(); + held.forEach(([item, tag, name, index, top, left]) => { + const row = root.querySelector(`[data-item="${CSS.escape(item)}"]`); + const node = row ? twins(row, tag, name)[index] : null; + if (node) { + node.scrollTop = top; + node.scrollLeft = left; + } + }); + } + /** * @param {Object} item a decision, a decision row, a decision card or a permission ask: id, options [{label, description, preview}], recommended (labels joined by PICKED), multi. * @param {Object} how pick: called with the answer, one label, the toggled labels in option order joined by PICKED, or the trimmed Other text, and true for the Other text only; other: the Other field's placeholder; toggled: the Set of toggled labels of a multi decision, kept by the caller across redraws; chosen: the answer given so far, "" for none; locked: whether the dialog takes no answer; hint: what an answer does, shown in the titles; group: an extra class of the option list. diff --git a/orchestrator/templates/page-stream.js b/orchestrator/templates/page-stream.js index c25160a..af3d1af 100644 --- a/orchestrator/templates/page-stream.js +++ b/orchestrator/templates/page-stream.js @@ -164,10 +164,10 @@ stream.dom.list.replaceChildren(emptyState("Stream not readable", stream.error)); return; } - stream.dom.list.replaceChildren(rows.length + keepTyping(stream.dom.list, () => keepScroll(stream.dom.list, () => stream.dom.list.replaceChildren(rows.length ? h("div", {class: "card flush"}, h("ol", {class: "feed"}, shown.map(eventRow)), rows.length > shown.length ? h("p", {class: "footnote"}, `${rows.length - shown.length} older entries hidden; narrow down with a filter or the time range.`) : null) - : emptyState(stream.busy ? "Loading …" : "No entries", activeWaiting() ? "Nothing is waiting for you right now." : "Nothing in the chosen time range and filter.")); + : emptyState(stream.busy ? "Loading …" : "No entries", activeWaiting() ? "Nothing is waiting for you right now." : "Nothing in the chosen time range and filter.")))); } function streamScreen() { diff --git a/tests/e2e/test_stream_keeps_typing_e2e.py b/tests/e2e/test_stream_keeps_typing_e2e.py new file mode 100644 index 0000000..16f0190 --- /dev/null +++ b/tests/e2e/test_stream_keeps_typing_e2e.py @@ -0,0 +1,82 @@ +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 hub_e2e_kit as base +from hub_case import HubCase +from hub_support import ASK, iso + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-stream-keep-shots")) +COMMAND = "git commit -m 'stream keeps typing'" +TYPED = "rebase onto main first" +LINES = 300 +DIFF = [{"path": "long.txt", "status": "M", "mode": "100644", "additions": LINES, "deletions": 0, "patch": f"@@ -0,0 +1,{LINES} @@\n" + "\n".join(f"+line {n}" for n in range(LINES)), "note": ""}] +PROBE = """(async () => {@HELPERS@ + const rowOf = () => all("#main .feed > li.k-ask").find((li) => li.textContent.includes(@COMMAND@) && li.querySelector(".diff-scroll") && li.querySelector(".option.other input")); + const row = await until(rowOf, 10000); + if (!row) { + return {error: text(document.getElementById("main"))}; + } + row.dataset.probe = "old"; + const scroller = row.querySelector(".diff-scroll"); + const field = row.querySelector(".option.other input"); + scroller.scrollTop = 200; + field.focus(); + field.value = @TYPED@; + field.dispatchEvent(new Event("input", {bubbles: true})); + field.setSelectionRange(6, 6); + const top = scroller.scrollTop; + const redrawn = await until(() => { + const now = rowOf(); + return now && now.dataset.probe !== "old" ? now : null; + }, 15000); + if (!redrawn) { + return {error: "the stream never redrew", top}; + } + const again = redrawn.querySelector(".option.other input"); + return {top, after: redrawn.querySelector(".diff-scroll").scrollTop, focused: document.activeElement === again, value: again.value, caret: [again.selectionStart, again.selectionEnd]}; +})()""" + + +class TestStreamKeepsTyping(HubCase): + drive = base.OrchestratorE2ECase.drive + + def setUp(self): + super().setUp() + now = time.time() + self.write_council({"work/s1/claim.json": {"office": "work/s1", "session": "s1", "runtime": "claude", "name": "Skills session", "since": iso(now - 86400), "beat": now, "work": "stream"}}) + status, _, body = self.post_json("/api/asks", {**ASK, "command": COMMAND, "session": "s1", "diff": DIFF}, self.local()) + self.assertEqual(status, 201, body) + OUT.mkdir(parents=True, exist_ok=True) + + def check(self, browser: str, driver: str, binary: str) -> None: + script = PROBE.replace("@HELPERS@", base.HELPERS).replace("@COMMAND@", json.dumps(COMMAND)).replace("@TYPED@", json.dumps(TYPED)) + steps = [{"width": 1366, "height": 768, "url": f"http://localhost:{self.port}/?1366#stream", "wait": 1500, "probe": script, "shot": str(OUT / f"{browser}-1366-stream.png")}] + data = self.drive(steps, None, driver, binary) + (OUT / f"{browser}-stream.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual([error for error in data["errors"] if "(style-src-elem)" not in error and "/favicon.ico" not in error], []) + found = data["results"][0] + self.assertNotIn("error", found, found) + self.assertGreater(found["top"], 0, found) + self.assertEqual((found["after"], found["focused"], found["value"], found["caret"]), (found["top"], True, TYPED, [6, 6]), found) + + @unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") + def test_chromium_keeps_the_typed_answer_its_focus_and_the_diff_scroll_across_a_stream_redraw(self): + self.check("chromium", "cdp.mjs", os.environ.get("CHROMIUM", "chromium")) + + @unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox") + def test_firefox_keeps_the_typed_answer_its_focus_and_the_diff_scroll_across_a_stream_redraw(self): + self.check("firefox", "bidi.mjs", os.environ.get("FIREFOX", "firefox")) + + +if __name__ == "__main__": + unittest.main()