Keep unsent Needs you replies across live updates
Astra round 1, P2: the reply box sits in a live work region, and work-stream.js swapped regions without keeping input values. A reply typed and left was wiped by the next work event, and one typed while the page's fetch was in flight was wiped when it arrived, since busy() was checked only before the fetch. Before a swap, text fields in forms with data-draft-key are kept by draft key plus field name and written back into the matching form, with focus and caret when the field had them (the semantics design-feedback-1 uses). When the response arrives while the person is busy, the swap is skipped and rescheduled on the refresh timer. Browser coverage for both cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+80
−1
internal/equipo/static/work-stream.js+35 −1internal/thintest/needs_drafts_browser_test.go+45 −0
CI failed — run details
CI log
=== prepare ===
ci-box: machine: aarch64, 32 CPUs, 62 GiB memory, kernel 7.0.0-1017-aws
ci-box: python 3.12.3; Docker version 29.1.3, build 29.1.3-0ubuntu3~24.04.2
ci-box: user namespaces allowed: 233151
ci-box: cgroup: cgroup2fs
ci-box: disk /home/ci/work/repos/oficina__equipo/wt/needs-you-ux: Type Size Avail | ext4 96G 84G
ci-box: disk /home/ci/.cache/equipo-ci-box: Type Size Avail | ext4 96G 84G
ci-box: bwrap: fetched and verified in 0.3s
ci-box: claude: fetched and verified in 1.6s
ci-box: codex: fetched and verified in 3.6s
ci-box: codex-code-mode-host: fetched and verified in 1.4s
ci-box: libwrap: fetched and verified in 0.3s
ci-box: socat: fetched and verified in 0.4s
npm warn deprecated node-domexception@1.0.0: Use your platform's native DOMException instead
ci-box: image pulled in 9.9s
ci-box: pi: installed and verified in 7.5s
ci-box: opencode: installed and verified in 2.3s
ci-box: chromium 156.0.8078.79-1~deb13u1: 76 packages fetched, verified and unpacked in 8.9s
ci-box: clone deepened in 4.7s
ci-box: file times set from content: 3781 files, 245 directories
ci-box: ready in 18.6s
=== copy ===
EQUIPO_RELEASE_COPY_CHECK=1 CGO_ENABLED=0 go test ./internal/equipotest -run '^TestReleaseCopyReviewed$' -count=1
--- FAIL: TestReleaseCopyReviewed (0.09s)
task_pages_test.go:236: COPY REVIEW REQUIRED: copy-status.py verify found copy pending review or stale bookkeeping
FAIL
rastrillo-budget/v1 ran 92ms
FAIL amadan.net/oficina/equipo/internal/equipotest 0.244s
FAIL
make: *** [Makefile:260: copy-release-check] Error 1
amadan: step copy failed: exit status 2
amadan: job failed at step copy — its output is above, not at the tail of this log
diff --git a/internal/equipo/static/work-stream.js b/internal/equipo/static/work-stream.js| index 222baca1..8dbba9cc 100644 |
| --- a/internal/equipo/static/work-stream.js |
| +++ b/internal/equipo/static/work-stream.js |
| @@ -18,16 +18,47 @@ |
| }; |
| window.equipoPulse(document.body); |
| if (!document.querySelector("[data-work-live]") || !window.fetch) return; |
| - var inflight = null, again = false, timer = 0; |
| + var inflight = null, again = false, held = false, timer = 0; |
| function busy() { |
| // Never swap a menu or a filter out from under the person. |
| return !!document.querySelector("[data-work-region] [equipo-menu][open], [data-work-region] [equipo-filter][open]") || |
| !!(document.activeElement && document.activeElement.closest && document.activeElement.closest("[data-work-region] form")); |
| } |
| + // Unsent words in a form with data-draft-key (a reply box) survive a |
| + // swap: kept by draft key and field name, written back into the same |
| + // form, with the caret where it was. |
| + var fields = "textarea, input:not([type=hidden]):not([type=submit]):not([type=button])"; |
| + function drafts(root) { |
| + var out = { values: {}, focus: null }; |
| + root.querySelectorAll("form[data-draft-key]").forEach(function (f) { |
| + f.querySelectorAll(fields).forEach(function (el) { |
| + if (!el.name) return; |
| + var key = f.getAttribute("data-draft-key") + "\n" + el.name; |
| + out.values[key] = el.value; |
| + if (el === document.activeElement) out.focus = { key: key, start: el.selectionStart, end: el.selectionEnd }; |
| + }); |
| + }); |
| + return out; |
| + } |
| + function restore(root, saved) { |
| + root.querySelectorAll("form[data-draft-key]").forEach(function (f) { |
| + f.querySelectorAll(fields).forEach(function (el) { |
| + var key = f.getAttribute("data-draft-key") + "\n" + el.name; |
| + if (!el.name || !(key in saved.values)) return; |
| + el.value = saved.values[key]; |
| + if (saved.focus && saved.focus.key === key) { |
| + el.focus(); |
| + try { el.setSelectionRange(saved.focus.start, saved.focus.end); } catch (e) {} |
| + } |
| + }); |
| + }); |
| + } |
| function load() { |
| return fetch(location.href, { credentials: "same-origin", headers: { "Accept": "text/html" } }) |
| .then(function (r) { if (!r.ok) throw new Error("page"); return r.text(); }) |
| .then(function (body) { |
| + // The person started typing while it loaded: swap later. |
| + if (busy()) { held = true; return; } |
| var doc = new DOMParser().parseFromString(body, "text/html"); |
| document.querySelectorAll("[data-work-region][id]").forEach(function (old) { |
| var next = doc.getElementById(old.id); |
| @@ -36,7 +67,9 @@ |
| old.querySelectorAll("details[id]").forEach(function (d) { open[d.id] = d.open; }); |
| var had = old.querySelectorAll("[equipo-tl] > li").length; |
| var focus = document.activeElement && old.contains(document.activeElement) ? document.activeElement.getAttribute("href") : null; |
| + var saved = drafts(old); |
| old.replaceWith(next); |
| + restore(next, saved); |
| window.equipoPulse(next); |
| Object.keys(open).forEach(function (id) { var d = document.getElementById(id); if (d) d.open = open[id]; }); |
| var items = next.querySelectorAll("[equipo-tl] > li"); |
| @@ -53,6 +86,7 @@ |
| if (busy()) { clearTimeout(timer); timer = setTimeout(refresh, 1000); return; } |
| inflight = load().catch(function () {}).finally(function () { |
| inflight = null; |
| + if (held) { held = again = false; clearTimeout(timer); timer = setTimeout(refresh, 1000); return; } |
| if (again) { again = false; refresh(); } |
| }); |
| } |
diff --git a/internal/thintest/needs_drafts_browser_test.go b/internal/thintest/needs_drafts_browser_test.go| new file mode 100644 |
| index 00000000..4ebd50cd |
| --- /dev/null |
| +++ b/internal/thintest/needs_drafts_browser_test.go |
| @@ -0,0 +1,45 @@ |
| +//go:build browser |
| + |
| +package thintest |
| + |
| +import ( |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// TestBrowserNeedsReplyDrafts (needs-you-ux, Astra round 1): a reply |
| +// typed on Needs you survives the live updates. Typed and left, the next |
| +// work event swaps the region and the words come back into the same |
| +// form (draft key plus field name). Typed while the page's fetch is in |
| +// flight, the response does not swap the region out from under the |
| +// person; it waits until they leave the form, then swaps with the words |
| +// kept. |
| +func TestBrowserNeedsReplyDrafts(t *testing.T) { |
| + r := newToolRigAt(t, wire.ProtocolCurrent) |
| + q := r.openQuestion("b1", "I can't find anything called Patrix. Where does it live?") |
| + p := openInner(t, r.inst.URL, r.inst.Cookie(), 1280, 900, false, "light", "reduce") |
| + defer p.stop() |
| + p.open("/tasks?needs=you") |
| + field := `document.querySelector('[data-draft-key="needs-` + q.ID + `"] [name=choice]')` |
| + p.wait(field, "the reply box") |
| + mark := `(()=>{document.getElementById('work-needs').setAttribute('data-old','');return true})()` |
| + swapped := `!document.querySelector('#work-needs[data-old]')` |
| + |
| + // Typed, then left: a work event swaps the region, the words stay. |
| + p.check(mark, "mark the region") |
| + p.check(`(()=>{const f=`+field+`;f.focus();f.value='In oficina/sample';f.blur();document.dispatchEvent(new Event('equipo:work'));return true})()`, "type, leave, a work event") |
| + p.wait(swapped, "the region swapped") |
| + p.check(field+`.value==='In oficina/sample'`, "the typed reply was erased by the swap") |
| + |
| + // Typed while the fetch is in flight: held until the person leaves. |
| + p.check(mark, "mark the region again") |
| + p.check(`(()=>{const real=window.fetch;window.fetch=function(){const args=arguments;return new Promise(res=>{window.equipoRelease=()=>{window.fetch=real;const pr=real.apply(window,args);res(pr);pr.then(x=>x.clone().text()).then(()=>setTimeout(()=>{window.equipoDone=true},400))}})};document.dispatchEvent(new Event('equipo:work'));return true})()`, "hold the fetch, a work event") |
| + p.wait(`typeof window.equipoRelease==='function'`, "the fetch in flight") |
| + p.check(`(()=>{const f=`+field+`;f.focus();f.value='Typed while it loaded';window.equipoRelease();return true})()`, "type during the fetch") |
| + p.wait(`window.equipoDone===true`, "the response") |
| + p.check(`!(`+swapped+`) && document.activeElement===`+field+` && `+field+`.value==='Typed while it loaded'`, "the response swapped the region while the person was typing") |
| + p.check(`(()=>{`+field+`.blur();return true})()`, "leave the box") |
| + p.wait(swapped, "the deferred swap") |
| + p.check(field+`.value==='Typed while it loaded'`, "the reply typed during the fetch was erased") |
| +} |