Refactor live.js into pure-function bridge + goja test harness #19
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
posta/chat#19
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What to build
Implement ADR 0001. Restructure
internal/web/static/live.jsso its decision logic lives in pure functions that return data verdicts, with a thin DOM-touching shell around them. Addgithub.com/dop251/gojaas a Go test dependency. Add alive_test.go(location TBD:internal/web/or a dedicatedinternal/web/static_test/) that loadslive.jsinto goja and exercises the pure functions.Refactor shape:
peerFromPath(pathname) → string,decideThreadUpdated(eventData, currentPath) → {kind, ...}, plus whatever decision points the active-peer migration left behind.addEventListenercalls,dispatchEvent,EventSourcelifecycle.window.__liveUpdates(extending the existing pattern atlive.js:228) so the goja test can call them directly.Initial test coverage (matching CONTEXT.md § Live updates protocol):
peerFromPath:/c/<encoded>round-trips correctly; non-thread paths return""; bad-encoding paths return"".decideThreadUpdated: peer-match →{kind: "dispatch"}; peer-mismatch →{kind: "skip-peer-mismatch"}; no thread open →{kind: "skip-no-thread"}; malformed JSON →{kind: "skip-malformed"}.Acceptance criteria
go test ./internal/web/...runs the goja-based JS testslive.jsare exposed onwindow.__liveUpdatesaddEventListener,dispatchEvent,EventSourcelifecycle) is grouped in a single named shell function so the boundary is explicitpeerFromPathround-trip,decideThreadUpdatedpeer-match,decideThreadUpdatedpeer-mismatchBlocked by
Agent Brief
Category: enhancement
Summary: Restructure the live-updates bridge script so its decision logic lives in pure functions returning data verdicts, separate from a thin DOM-mutating shell. Add a
goja-based Go test that exercises the pure functions. Establishes the regression-test seam captured in ADR 0001.Current behavior:
The bridge script (
internal/web/static/live.js) intermixes pure decisions (e.g. parsing the peer URL fromlocation.pathname, deciding whether to react to athread-updatedSSE frame given the open peer) with DOM side effects (addEventListeneron theEventSource,dispatchEventon#main,EventSourcelifecycle, htmxsseOpeninterop). There is no automated regression test for the bridge — only the pre-existingwindow.__liveUpdates = { peerFromPath }hook, which proves the pattern is viable but is currently used only by a small manual assertion.Desired behavior:
Decision logic in the bridge becomes pure functions. Each function takes plain values (the event payload, the current path, etc.) and returns a plain-data verdict object describing what the side-effect shell should do (e.g.
{kind: "dispatch"},{kind: "skip-peer-mismatch"},{kind: "skip-no-thread"},{kind: "skip-malformed"}). The shell — the DOM-touching code that applies verdicts — is a single named function. Pure functions are exposed onwindow.__liveUpdatesso a Go test usinggojacan call them with controlled inputs and assert returned verdicts.A new Go test (in the
webpackage, in a file colocated with the other web tests) loads the bridge script into agojaruntime and exercises the pure decision points. The test does not polyfill the DOM, does not need a browser, does not need a fake posta-server. It runs ingo test.Initial test coverage (matching the protocol table in
CONTEXT.md § Live updates protocol):peerFromPath:/c/<URL-encoded peer>round-trip to the decoded peerthread-updateddecision function:{kind: "dispatch"}{kind: "skip-peer-mismatch"}{kind: "skip-no-thread"}{kind: "skip-malformed"}Future bridge tests follow the same pattern: extend
window.__liveUpdateswith the new pure function, add Go test cases.Key principles for the agent:
window.__liveUpdatesis a deliberate contract, not a leak. It exists to make the bridge testable from outside a browser.addEventListener,dispatchEvent,fetch, andEventSourcelifecycle live.Acceptance criteria:
go test ./internal/web/...runs the new bridge tests viagojawindow.__liveUpdatespeerFromPathround-trip plus both non-thread and bad-encoding paths; thethread-updateddecision in all four outcomes listed abovegojais added as ago.moddependency only; no npm artifact appears in the repoOut of scope: