Files
Project-SDE-WP-Suite/tests/autosave_check.py
Matt Mabrey 6c2e457225 T14.3 (D26, BL-061): the sync badge shows the true state and gets out of the way
The badge words live in one function, badgeModel(). States: sending, retrying,
held for sign-in, not saved on this device, rejected (with the reason), read and
queue, another person's changes, and all sent. Every state names the project.
Sign in is a real link to login.html?next=. Dismiss is a real button and removes
only the changes the project refused. Both are 44px or more with a focus ring.

BL-061: the console pages (Admin, User Directory) never show the badge. All sent
shows only after real work. The badge picks a free spot on the screen. One very
dense screen case is left and logged as BL-072.

tests/sync_badge_check.py (87 checks). autosave_check reads the new badge words.
2026-10-17 15:12:25 -07:00

306 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.
# T14.3: the badge's words now live in one place, badgeModel(), as
# `text: '...'` entries. Those are the lines that render.
texts = [ln for ln in src.split("\n") if "text: '" in ln and "tone: '" in ln]
rendered = [ln for ln in texts if "All changes saved" in ln]
chk("the badge no longer renders 'All changes saved'", not rendered, rendered[:1])
chk("the badge words were found (a rename must not make this check empty)", len(texts) >= 7,
"found %d" % len(texts))
chk("...and every state it renders names the project",
all("the project" in ln for ln in texts),
[ln.strip()[:70] for ln in texts if "the project" not 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())