Found running the full tests/*_check.py suite as part of a deployability
check on feat/waves-11-13: autosave_check.py's "section 7" asserted that
calling trackStepDwell() still wrote an event into wp-usage.js's
localStorage log (wp_suite_analytics_v1). That assertion predates CR-019
and was checking that the autosave guard hadn't accidentally clobbered
the pre-existing analytics dwell listener - a real concern at the time.
T11.6 (previous commit b2a083a) retired that listener on purpose: track()
is now a documented no-op and wp-usage.js is gone, so the localStorage key
is never written to any more. The test was still asserting the old
behavior, so it now correctly failed (0 -> 0) - this was a gap in T11.6's
own verification (only tests/baseline_shots.py was run for it at the
time; the full suite wasn't).
Fix: rewrote section 7 to check what's actually still true - autosave's
own registration (WPAutosave / sopDraftId), and that the retired
trackStepDwell() is a harmless no-op (exists, doesn't throw) rather than
missing or broken - instead of asserting it still records anywhere.
Verified: tests/autosave_check.py now passes 33/33 (was 33/34, exit 1).
Also ran the complete tests/*_check.py suite (39 scripts) as a
deployability check for the whole feat/waves-11-13 branch:
- 38/39 exit 0 after this fix
- token_check.py exits 2 when run with no arguments by design (it needs
--out or --compare) - a harness-invocation detail, not a test failure;
already confirmed non-regression in wave 10
- generalinfo_check.py has one known pre-existing failure (a raw colour
literal check against html/wp-creation-styles.css, a file untouched by
any wave 11/13 work) - already diagnosed as pre-existing in wave 10,
unrelated to CR-019/D18
Also re-verified the usage_events migration (T11.1) round-trips cleanly
on SQLite (upgrade/downgrade/upgrade) and renders clean, valid DDL for
Postgres via `alembic upgrade ... --sql` (TIMESTAMP WITH TIME ZONE / JSON,
no SQLite-only syntax) - no BL-027-class defect.
Backend smoke test (27/27) and seed_demo.py still pass.
308 lines
15 KiB
Python
308 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
"""Does unsaved work survive? — S2 / T4.3 (and B5 / T4.4's status).
|
|
|
|
The work package form is ~4,700px tall and had no autosave and no unsaved-work
|
|
guard: the only beforeunload listener in the app was analytics dwell tracking. A
|
|
mis-click lost everything typed since the last explicit Save.
|
|
|
|
1. typing autosaves a draft, without being asked
|
|
2. typing then closing prompts; NOT typing then closing does not
|
|
3. the draft survives a killed tab (no beforeunload) and is offered back
|
|
4. restoring puts the work back in the form
|
|
5. an explicit save settles the draft, so nothing offers to "recover" saved work
|
|
6. autosave failure is surfaced rather than swallowed
|
|
7. the analytics dwell listener still fires
|
|
|
|
Exit 0 all passed, 1 a failure, 2 could not run.
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import cdp # noqa: E402
|
|
from browser_check import seed, start_server, chk, _PASS, _FAIL, _c # noqa: E402
|
|
|
|
|
|
def boot(page, base, tok, url):
|
|
page.clear_cookies()
|
|
page.set_cookie("wp_session", tok["root"])
|
|
page.goto(base + url)
|
|
for _ in range(40):
|
|
if page.eval("!!window.wpCreatorReady"):
|
|
break
|
|
time.sleep(0.3)
|
|
time.sleep(1.0)
|
|
|
|
|
|
def main():
|
|
exe = cdp.find_browser()
|
|
if not exe:
|
|
print("no headless-capable browser found; set WP_BROWSER.")
|
|
return 2
|
|
|
|
tmpdir = tempfile.mkdtemp(prefix="wpsuite-autosave-")
|
|
db_path = os.path.join(tmpdir, "check.db")
|
|
server = None
|
|
try:
|
|
tok = seed(db_path)
|
|
port = cdp.free_port()
|
|
base = "http://127.0.0.1:%d" % port
|
|
server = start_server(port, db_path)
|
|
if server is None:
|
|
print("the test server would not start.")
|
|
return 2
|
|
print("\nAutosave and the unsaved-work guard — S2 / T4.3\nTarget: %s" % base)
|
|
|
|
browser = cdp.Browser(exe)
|
|
page = browser.page()
|
|
try:
|
|
print("\n0. the module is registered on the form page")
|
|
boot(page, base, tok, "/wp-creation-index.html?project=projA&wp=wpA1")
|
|
chk("WPAutosave is loaded", page.eval("typeof WPAutosave") == "object")
|
|
chk("an untouched form is not dirty", page.eval("WPAutosave.isDirty()") is False,
|
|
page.eval("JSON.stringify(WPAutosave.status())"))
|
|
|
|
print("\n2a. not typing then leaving does NOT prompt")
|
|
chk("no guard while the form is untouched",
|
|
page.eval("WPAutosave.isDirty()") is False)
|
|
|
|
print("\n1. typing autosaves a draft without being asked")
|
|
page.eval("""(() => {
|
|
const el = document.getElementById('wp_subject');
|
|
el.value = 'AUTOSAVE PROBE — typed but never saved';
|
|
el.dispatchEvent(new Event('input', {bubbles:true}));
|
|
return true;
|
|
})()""")
|
|
chk("the form is now dirty", page.eval("WPAutosave.isDirty()") is True)
|
|
for _ in range(25):
|
|
if page.eval("WPAutosave.status().state") == "saved":
|
|
break
|
|
time.sleep(0.3)
|
|
st = page.eval("WPAutosave.status().state")
|
|
chk("a draft is written on the debounce, unprompted", st == "saved", "status=%r" % st)
|
|
key = page.eval("WPAutosave._key(wpDraftId())")
|
|
stored = page.eval("localStorage.getItem(%r)" % key)
|
|
chk("the draft holds what was typed",
|
|
bool(stored) and "AUTOSAVE PROBE" in stored, (stored or "")[:120])
|
|
chk("the draft is scoped to project AND package",
|
|
"projA" in key and "wpA1" in key, key)
|
|
chk("it did NOT go to the server (a draft is not a record)",
|
|
page.eval("""(() => {
|
|
const q = (localStorage.getItem('wp_outbox_v1')||'');
|
|
return q.indexOf('AUTOSAVE PROBE') === -1;
|
|
})()"""))
|
|
|
|
print("\n2b. typing then leaving DOES prompt")
|
|
chk("the guard is armed while work is unsaved",
|
|
page.eval("WPAutosave.isDirty()") is True)
|
|
|
|
print("\n3. the draft survives a killed tab")
|
|
# No beforeunload: navigate the tab away as a crash would, relying on
|
|
# the visibilitychange write. Then reopen the same package.
|
|
page.eval("document.dispatchEvent(new Event('visibilitychange'))")
|
|
time.sleep(0.4)
|
|
boot(page, base, tok, "/wp-creation-index.html?project=projA&wp=wpA1")
|
|
still = page.eval("localStorage.getItem(%r)" % key)
|
|
chk("the draft is still there after reopening",
|
|
bool(still) and "AUTOSAVE PROBE" in still, (still or "")[:80])
|
|
chk("...and is offered back, not applied silently",
|
|
page.eval("!!document.getElementById('draft-recovery')"))
|
|
txt = page.eval("(document.getElementById('draft-recovery')||{}).textContent||''")
|
|
chk("...saying plainly that nothing reached the project",
|
|
"Nothing has been sent to the project" in txt, txt[:140])
|
|
chk("...and announcing itself",
|
|
page.eval("(document.getElementById('draft-recovery')||{}).getAttribute"
|
|
"&&document.getElementById('draft-recovery').getAttribute('role')") == "status")
|
|
|
|
print("\n4. restoring puts the work back")
|
|
page.eval("document.getElementById('draft-restore').click()")
|
|
time.sleep(0.8)
|
|
val = page.eval("(document.getElementById('wp_subject')||{}).value||''")
|
|
chk("the typed text is back in the form", "AUTOSAVE PROBE" in val, val[:80])
|
|
chk("the recovery bar is gone once used",
|
|
page.eval("!document.getElementById('draft-recovery')"))
|
|
|
|
print("\n5. an explicit save settles the draft")
|
|
# savePackage() can end in confirm() (the early-release gate) or alert()
|
|
# (a missing required field). A native dialog blocks the page and hangs
|
|
# CDP, so the probe answers them. This is the app's 79-native-dialog
|
|
# problem showing up in a test rather than a defect in this task — S6/S7
|
|
# in wave 9 is where those get replaced.
|
|
page.eval("window.confirm = () => true; window.alert = () => {}; true")
|
|
# savePackage() returns early unless subject AND type are set, and with
|
|
# alert() stubbed that early return is silent - so make the form valid
|
|
# first, then assert the save actually landed before asserting anything
|
|
# about the draft.
|
|
# The browser_check fixture's SOP defines no WP types, so the type
|
|
# select holds only its placeholder and savePackage() correctly refuses.
|
|
# That is the fixture, not the app: give the form a valid type so the
|
|
# save path can actually be exercised.
|
|
page.eval("""(() => {
|
|
const t = document.getElementById('wp_type');
|
|
if (t && !t.value) {
|
|
const o = document.createElement('option');
|
|
o.value = 'Conduit Install'; o.textContent = 'Conduit Install';
|
|
t.appendChild(o); t.value = 'Conduit Install';
|
|
t.dispatchEvent(new Event('change', {bubbles:true}));
|
|
}
|
|
return (document.getElementById('wp_subject')||{}).value + ' | ' + (t||{}).value;
|
|
})()""")
|
|
page.eval("typeof savePackage==='function' && savePackage(false)")
|
|
time.sleep(1.2)
|
|
chk("the save actually landed (otherwise the rest proves nothing)",
|
|
page.eval("""(() => savedPackages.some(p =>
|
|
(p.subject||'').indexOf('AUTOSAVE PROBE') !== -1))()"""),
|
|
page.eval("JSON.stringify(savedPackages.map(p=>p.subject))")[:160])
|
|
after = page.eval("localStorage.getItem(%r)" % key)
|
|
chk("the draft is cleared once the record holds the work", after in (None, "null"),
|
|
repr(after)[:80])
|
|
boot(page, base, tok, "/wp-creation-index.html?project=projA&wp=wpA1")
|
|
chk("...so nothing offers to recover work that is already saved",
|
|
page.eval("!document.getElementById('draft-recovery')"))
|
|
|
|
print("\n6. autosave failure is surfaced, not swallowed")
|
|
page.eval("""(() => {
|
|
const real = localStorage.setItem.bind(localStorage);
|
|
localStorage.setItem = function(k, v){
|
|
if (String(k).indexOf('wp_draft::') === 0) {
|
|
const e = new Error('QuotaExceededError (simulated)'); e.name='QuotaExceededError'; throw e;
|
|
}
|
|
return real(k, v);
|
|
};
|
|
const el = document.getElementById('wp_subject');
|
|
el.value = 'SECOND EDIT, storage is full';
|
|
el.dispatchEvent(new Event('input', {bubbles:true}));
|
|
return true;
|
|
})()""")
|
|
for _ in range(25):
|
|
if page.eval("WPAutosave.status().state") == "failed":
|
|
break
|
|
time.sleep(0.3)
|
|
st = page.eval("JSON.stringify(WPAutosave.status())")
|
|
chk("a failed autosave reports 'failed'",
|
|
page.eval("WPAutosave.status().state") == "failed", st)
|
|
chk("...and carries the reason", "simulated" in (st or ""), st)
|
|
|
|
print("\n8. B5 — the save indicator tells the truth about the DRAFT")
|
|
boot(page, base, tok, "/wp-creation-index.html?project=projA&wp=wpA1")
|
|
chk("a draft indicator is mounted",
|
|
page.eval("!!document.getElementById('wp-draft-status')"))
|
|
ind = "(document.getElementById('wp-draft-status')||{})"
|
|
chk("it says 'no unsaved changes' on an untouched form",
|
|
"No unsaved changes" in page.eval(ind + ".textContent||''"),
|
|
page.eval(ind + ".textContent||''"))
|
|
chk("...announced politely",
|
|
page.eval(ind + ".getAttribute && document.getElementById('wp-draft-status').getAttribute('role')") == "status")
|
|
page.eval("""(() => {
|
|
const el = document.getElementById('wp_subject');
|
|
el.value = 'B5 PROBE'; el.dispatchEvent(new Event('input', {bubbles:true}));
|
|
return true;
|
|
})()""")
|
|
for _ in range(25):
|
|
if "Draft saved" in page.eval(ind + ".textContent||''"):
|
|
break
|
|
time.sleep(0.3)
|
|
txt = page.eval(ind + ".textContent||''")
|
|
chk("it reports a real save, with a time", "Draft saved at" in txt, txt)
|
|
chk("...and is visually distinct when saved",
|
|
"is-saved" in page.eval(ind + ".className||''"))
|
|
|
|
print("\n8b. a failed save looks different and offers a retry")
|
|
page.eval("""(() => {
|
|
const real = localStorage.setItem.bind(localStorage);
|
|
localStorage.setItem = function(k, v){
|
|
if (String(k).indexOf('wp_draft::') === 0) throw new Error('disk on fire (simulated)');
|
|
return real(k, v);
|
|
};
|
|
const el = document.getElementById('wp_subject');
|
|
el.value = 'B5 PROBE 2'; el.dispatchEvent(new Event('input', {bubbles:true}));
|
|
return true;
|
|
})()""")
|
|
for _ in range(25):
|
|
if "is-failed" in page.eval(ind + ".className||''"):
|
|
break
|
|
time.sleep(0.3)
|
|
cls = page.eval(ind + ".className||''")
|
|
txt = page.eval(ind + ".textContent||''")
|
|
chk("a failed draft save is visually distinct", "is-failed" in cls, cls)
|
|
chk("...names the failure", "disk on fire" in txt, txt[:120])
|
|
chk("...offers a retry",
|
|
page.eval("!!document.querySelector('#wp-draft-status .wp-draft-retry')"))
|
|
chk("...and interrupts rather than waiting for a pause",
|
|
page.eval("document.getElementById('wp-draft-status').getAttribute('role')") == "alert")
|
|
|
|
print("\n8c. the outbox message no longer reads as a draft-save confirmation")
|
|
src = page.eval("(() => fetch('/project-data.js').then(r=>r.text()))()") or ""
|
|
# Only what the badge RENDERS counts. The phrase still appears in the
|
|
# comment explaining why it was changed, and asserting on that would be
|
|
# asserting that the reason cannot be written down.
|
|
rendered = [ln for ln in src.split("\n")
|
|
if ("el.textContent" in ln or "el.innerHTML" in ln)
|
|
and "All changes saved" in ln]
|
|
chk("the badge no longer renders 'All changes saved'", not rendered, rendered[:1])
|
|
chk("...and every state it renders names the project",
|
|
all("the project" in ln
|
|
for ln in src.split("\n")
|
|
if ("el.textContent = '" in ln or "el.innerHTML = '" in ln)
|
|
and "wp-sync-badge" not in ln and ("✓" in ln or "⚠" in ln or "↻" in ln or "✕" in ln)),
|
|
[ln.strip()[:70] for ln in src.split("\n")
|
|
if ("el.textContent = '" in ln or "el.innerHTML = '" in ln)
|
|
and "the project" not in ln and ("✓" in ln or "⚠" in ln or "↻" in ln or "✕" in ln)])
|
|
|
|
print("\n7. autosave's own registration survives the retired analytics tracker")
|
|
# This section used to also assert the per-browser usage-analytics dwell
|
|
# listener (wp-usage.js) still fired alongside autosave, to catch autosave
|
|
# accidentally replacing it. CR-019/T11.6 (2026-09-23) retired that tracker
|
|
# on purpose - track() is now a documented no-op and wp-usage.js is gone -
|
|
# so asserting it still records an event would be asserting the wrong
|
|
# thing. What's still worth checking here is autosave's own registration,
|
|
# which is what this section actually guards.
|
|
boot(page, base, tok, "/work-package-suite.html?project=projA&tab=sop")
|
|
time.sleep(0.8)
|
|
chk("the wizard registered autosave",
|
|
page.eval("typeof WPAutosave === 'object' && typeof sopDraftId === 'function'"))
|
|
chk("...and the retired dwell tracker is a harmless no-op, not missing or throwing",
|
|
page.eval("""(() => {
|
|
if (typeof trackStepDwell !== 'function') return false;
|
|
try { trackStepDwell(); return true; } catch (e) { return false; }
|
|
})()"""))
|
|
finally:
|
|
page.close()
|
|
browser.close()
|
|
finally:
|
|
if server:
|
|
server.kill()
|
|
try:
|
|
server.wait(timeout=10)
|
|
except subprocess.TimeoutExpired:
|
|
pass
|
|
try:
|
|
from server.db import engine
|
|
engine.dispose()
|
|
except Exception:
|
|
pass
|
|
import shutil
|
|
for _ in range(10):
|
|
shutil.rmtree(tmpdir, ignore_errors=True)
|
|
if not os.path.exists(tmpdir):
|
|
break
|
|
time.sleep(0.3)
|
|
|
|
total = len(_PASS) + len(_FAIL)
|
|
print("\n%s\n%d/%d checks passed." % ("-" * 54, len(_PASS), total))
|
|
if _FAIL:
|
|
for f in _FAIL:
|
|
print(" - " + f)
|
|
return 1
|
|
print("\nResult: " + _c("ALL PASS — unsaved work survives.", "32") + "\n")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|