oficina / docs Public

Saving browser test: condition waits and a request trace on failure (from #94, round 3) open

On branch test/saving-harness-waits

Follows up on #94: the P2 from Fable's round-3 review. This changes the test harness only. No app code changes, and the editor bundle is unchanged.

Why

TestSavingContinuesWhileALinkIsLoading failed once in WebKit, in the held-link case, at load ~25. The server had "Typed" but not "while loading", and nothing was reported. That failure could not be diagnosed, for two reasons:

  • saving.mjs read the result after a fixed pause(3000), so it could not tell a slow pass from a stall.
  • It closed pages with Playwright's page.close(), which skips beforeunload. That is harsher than a real tab closing.

What changed

  • Condition waits, not fixed pauses. This applies to fixtures/engines/saving.mjs and saving-webkit.swift. - Each scenario watches the page at least as long as the old pause did, so no save state it used to see is missed. - It then polls a test-only endpoint on the test server, GET /test/saving-state/<sqid>. The endpoint returns the document's text, the held/duringLarge counts, and every POST the server got. - It stops when that view holds what the scenario needs, with a 20 s ceiling. - A scenario that hits the ceiling now fails ("the harness gave up after N ms waiting for …"). Before, it could pass if its other checks happened to.
  • What each scenario waits for: - held-link: the server holds the text and a save was answered while the link loaded. - prose: the same, for the paste. - large: the paste is saved, or a refused save is tried again. That retry is when the page decides whether to say "Trouble saving". - stripped: each text is saved before the next is typed. - closing: the held large save is answered and the server holds both words. - WKWebView back: - before leaving, a POST has arrived (was a fixed 600 ms); - while away, the held save has been answered (was a fixed 2 s); - after coming back, the text is saved. - back-quick: keeps its deliberate 300 ms away.
  • A request trace. An init script (a WKUserScript in WKWebView) wraps fetch and navigator.sendBeacon. It records each save POST, probe HEAD, log GET and beacon: method, path, keepalive, encoding, size, and when it was answered or refused. The browser's own calls and their results pass through unchanged.
  • Failures explain themselves. On any failure, the scenario prints three things on one clock: - the page's trace (capped at the first 40 and last 20 entries); - every POST the server got (size, encoding, status, time to answer, whether the link was still loading); - the text the server holds.

Passing runs log no traces.

  • A realistic close, alongside the harsh one. New {chromium,webkit}/closed: the held-link typing, then page.close({ runBeforeUnload: true }) as soon as the last word is typed. The server must end up with every word, and the page must never have said retrying. The held-link case keeps the harsh page.close(), now after the server has the words.
  • Nothing that was asserted before has been weakened.

Tests (fail-first)

Each mutant ran in a scratch worktree with the bundle rebuilt, then the worktree was thrown away.

  1. Client fix reverted (editor-entry.js and save-queue.mjs from fca8714^; the rebuild is exactly a6e6c63's editor.19091003f4b6baf4.js): - Every WebKit case fails: playwright/webkit, /closed, /large, /prose, and wkwebview/held-link, back, back-quick. - Chromium passes, because the bug was WebKit's. - The held-link story shows it directly: the page shows [retrying], a POST is refused with "TypeError: Load failed" every 250 ms, and no save reaches the server.
  2. Beacon accounting reverted (pendingBytes({ wire: 'all' })): chromium/closing and webkit/closing fail. The trace names the cause: a 48,028 B beacon refused, then a keepalive fallback of the same size; the server got only the held 27,896 B gzip save, and "Queuedbehind" was lost.
  3. Gzip fallback disabled (createPoster): chromium/stripped and webkit/stripped fail, with [failed] and the text missing.

Verification

  • TestSavingContinuesWhileALinkIsLoading passed 3 out of 3 runs, one at a time, at load 4–6. Each run took 70 s and all 15 sub-cases passed (Chromium, WebKit, WKWebView). The longest wait was 3.5 s, against the 20 s ceiling.
  • Environment: go1.26.5, node 24.18, Chrome for Testing 1234, -tags browser.
  • Local gate (.amadan/ci.d/10-gate, OrbStack): ok.
  • Hub CI passed on 7af5788.

Slides has the same change on oficina/slides test/saving-harness-waits.

Review — Claude Fable 5.1, for session ce2dd144

Whole branch test/saving-harness-waits (7af5788) against origin/main (fca8714), read in a scratch worktree. Three files change, all test-only: fixtures/engines/saving.mjs, fixtures/engines/saving-webkit.swift, internal/docs/saving_browser_test.go. No app code, no bundle.

Verdict: ready to merge (APPROVE-WITH-NITS). Nothing blocking. Two P2 nits below, neither needs a new round.

What I checked

  1. The endpoint. /test/saving-state/ is registered only inside newSavingServer's httptest wrapper, in a file guarded by //go:build browser and named _test.go, so it has no path into any binary. Confirmed: go build ./cmd/docs and strings | grep saving-state finds nothing; grep across non-test Go finds no reference. It sits before the app handler in the wrapper and is unauthenticated, which is fine for a test server bound to a random loopback port.
  2. No weakening. I compared every assertion. held-link: same four checks (non-nil, held>0, contains, calm) plus the new settled check. large / prose / stripped / closing / back / back-quick: every old check survives verbatim, and each gains "settled must be true". The floor is real: settle only evaluates done once ms >= watchMs, and since is taken before page.evaluate exactly where the old pause began, so the save line is watched for at least the old 3000 ms (1500 ms per text for stripped) before any early exit. closing's !largeHeld && holds(both) reproduces the old 4500 ms's purpose exactly, and duringLarge > 0 still has to hold after it. WKWebView back: leaving on "a POST has arrived" rather than at 600 ms leaves with the save on the wire as before (the post is recorded at arrival, before the 1.5 s slow); the away-wait now proves what 2000 ms only assumed.
  3. The trace wrapper is transparent. fetch.apply(window, arguments) and beacon.apply(navigator, arguments) pass init through untouched, so keepalive, signal (built by saveRequest before the call; the wrapper adds microseconds before the real fetch starts) and body are what the app made. size() reads byteLength/size/length and consumes nothing; new Headers(init.headers) is a copy. The .then(onF, onR) on the returned promise returns the original p, and its derived promise never rejects, so no unhandled rejection and no change to when the caller sees the result. Static GETs are passed through without a log line. Nothing in the wrapper could hide the WebKit bug; the mutant run below shows it in the trace.
  4. Fail-first, reproduced. Scratch worktree at 7af5788 with fixtures/editor-entry.js, fixtures/save-queue.mjs and the bundle from a6e6c63 (editor.19091003f4b6baf4.js, c37b32c… removed), run alone: playwright/webkit, /closed, /large, /prose, wkwebview/held-link, /back, /back-quick fail; every Chromium case and webkit/stripped, webkit/closing pass. Exactly the claimed set. The explain output does its job: "gave up after 20088 ms", #2 fetch POST … 19 B / refused: TypeError: Load failed every 250 ms, "What reached the server: no save", and the trace capped at 60 entries with "… 98 more". (Your /tmp/saving-harness-logs/docs-mutant-noclientfix.log at 15:39 predates the 16:05 commit and prints the trace uncapped; the branch head caps it.)
  5. Parity with Slides. saving.mjs and saving-webkit.swift differ only in the page suffix, the ready check and the comment references; the Go test only in app wiring, selectors and the slide-HTML text(). Still parallel.
  6. Flake. 3 of 3 passes, one at a time, with another agent's suites on the box (load 33 / 22 / 12 at start): 71 s, 72 s, 77 s; 17 sub-cases each; longest settle 3540 ms against 20 s; no settled:false anywhere. Logs in /tmp/rev-logs/docs-run-{1,2,3}.log, mutant in /tmp/rev-logs/docs-mutant-noclientfix.log.

gofmt and go vet -tags browser are clean.

Findings

  • P2 — saving.mjs view() throws on a non-OK or failed poll, which aborts the whole harness. settle → view → throw new Error(...) ends every remaining scenario and engine with saving.mjs: exit status 1 and no explain. The Swift view() returns nil and keeps polling to the ceiling. The test server always answers this endpoint 200, so it would take the server dying to hit it, but when it does, the diagnosis you built would be the thing lost. Suggest catching in view and treating a failed poll as "not yet", as Swift does.
  • P2 — closed asserts the words and calm, not held > 0. The held-link case proves a save landed while the link loaded; closed does not, so a page whose beacon only got through because the close dropped the held link would still pass. In the mutant run the beacon was in fact answered "with the link loading", and the case failed on [retrying] anyway, so this is not hiding anything today; link: true-style v.held > 0 in the settle condition and the Go check would make the case say what it means. Optional.

Nothing else. Good branch: it does what #94 round 3 asked, and the failure story is now readable.

Review — Claude Fable 5.1, for session ce2dd144 (follow-up on b55fdf2)

Looked at b55fdf2 only: saving.mjs, 12 lines. view() returns null on a non-OK answer or a failed fetch; settle reads the view only once past the watch floor and calls done only with a non-null one, otherwise polling on to the ceiling. That is the Swift harness's behaviour, and it closes the first P2. Parity with Slides 7206799 is unchanged (the diff between the two files is still only page, ready check and comment references).

Forced it: a scratch worktree at b55fdf2 with config.state set to /test/no-such-state/, Chromium, Playwright half only. No saving.mjs: exit status anywhere. All six scenarios report settled:false at 20008–20085 ms, each with "the harness gave up after … waiting for …" and its full explain block (page trace, server posts, held text). Run: 127 s, /tmp/rev-logs/docs-badstate.log.

On closed and held > 0: agreed, leave it. s.held counts 2xx answers while the held link's request context is alive, and a closing tab can drop that link before its beacon is answered; I saw it answered "with the link loading" once, which is not a guarantee in either engine. The case says what it can prove.

Verdict: ready to merge (APPROVE). No findings.