feat: move offline work into IndexedDB (Closes #525)
This commit is contained in:
1 parent
7cadb36b41
commit
0a96dfb173
20 files changed
+451
-69
No files matched your search
@@ -303,4 +303,4 @@ async def test_unread_update_offers_reply_mark_read_and_next_independent_of_toda
|
||||
assert '.update-reply-actions { display:grid; grid-template-columns:repeat(2,minmax(0,1fr));' in html
|
||||
assert '.update-reply-actions button { min-height:44px;' in html
|
||||
worker = (Path(__file__).parents[1] / "frontend" / "service-worker.js").read_text()
|
||||
assert "stackchain-dashboard-shell-v85" in worker
|
||||
assert "stackchain-dashboard-shell-v86" in worker
|
||||
@@ -346,7 +346,9 @@ process.stdout.write(JSON.stringify(state));
|
||||
)
|
||||
|
||||
assert result["deletedAtIdle"] == []
|
||||
assert result["deletedDatabases"] == ["stackchain-background-outbox-v1"]
|
||||
assert result["deletedDatabases"] == [
|
||||
"stackchain-background-outbox-v1", "stackchain-offline-work-v2"
|
||||
]
|
||||
assert result["remaining"] == ["gitea.preference"]
|
||||
assert result["replaced"] == [
|
||||
"/dashboard/login?reason=session-idle",
|
||||
@@ -369,7 +371,9 @@ process.stdout.write(JSON.stringify(state));
|
||||
|
||||
assert result["valid"] is False
|
||||
assert result["remaining"] == ["gitea.preference"]
|
||||
assert result["deletedDatabases"] == ["stackchain-background-outbox-v1"]
|
||||
assert result["deletedDatabases"] == [
|
||||
"stackchain-background-outbox-v1", "stackchain-offline-work-v2"
|
||||
]
|
||||
assert result["deletedCaches"] == ["stackchain-dashboard-shell-v15"]
|
||||
assert result["replaced"] == ["/dashboard/login?reason=session-expired"]
|
||||
assert result["replacedAfterDeletion"] is True
|
||||
@@ -497,7 +501,9 @@ process.stdout.write(JSON.stringify(state));
|
||||
"/dashboard/login?reason=session-expired"
|
||||
]
|
||||
assert result["remaining"] == ["gitea.preference"]
|
||||
assert result["deletedDatabases"] == ["stackchain-background-outbox-v1"]
|
||||
assert result["deletedDatabases"] == [
|
||||
"stackchain-background-outbox-v1", "stackchain-offline-work-v2"
|
||||
]
|
||||
assert result["deletedCaches"] == ["stackchain-dashboard-shell-v15"]
|
||||
assert result["workerMessages"] == [{"type": "stackchain-purge-outbox"}]
|
||||
assert result["expiredAfterDeletion"] is True
|
||||
@@ -517,7 +523,9 @@ process.stdout.write(JSON.stringify(state));
|
||||
)
|
||||
|
||||
assert result["remaining"] == ["gitea.preference"]
|
||||
assert result["deletedDatabases"] == ["stackchain-background-outbox-v1"]
|
||||
assert result["deletedDatabases"] == [
|
||||
"stackchain-background-outbox-v1", "stackchain-offline-work-v2"
|
||||
]
|
||||
assert result["deletedCaches"] == ["stackchain-dashboard-shell-v15"]
|
||||
assert result["workerMessages"] == [{"type": "stackchain-purge-outbox"}]
|
||||
assert result["replaced"] == ["/dashboard/login?reason=session-revoked"]
|
||||
@@ -610,7 +618,9 @@ process.stdout.write(JSON.stringify(state));
|
||||
assert request["headers"]["x-csrf-token"] == "csrf-proof"
|
||||
assert "stackchain.private" in result["removed"]
|
||||
assert result["remaining"] == ["gitea.preference"]
|
||||
assert result["deletedDatabases"] == ["stackchain-background-outbox-v1"]
|
||||
assert result["deletedDatabases"] == [
|
||||
"stackchain-background-outbox-v1", "stackchain-offline-work-v2"
|
||||
]
|
||||
assert result["deletedCaches"] == ["stackchain-dashboard-shell-v15"]
|
||||
assert result["assigned"] == "/dashboard/login"
|
||||
assert result["assignedAfterDeletion"] is True
|
||||
|
||||
@@ -89,7 +89,7 @@ def test_legacy_cache_marker_is_normalized_out_of_build_identity(tmp_path):
|
||||
worker = changed_frontend / "service-worker.js"
|
||||
worker.write_text(
|
||||
worker.read_text().replace(
|
||||
"const CACHE = 'stackchain-dashboard-shell-v85';",
|
||||
"const CACHE = 'stackchain-dashboard-shell-v86';",
|
||||
"const CACHE = 'stackchain-dashboard-shell-v999';",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -347,5 +347,5 @@ async def test_dashboard_syncs_every_later_change_and_exposes_account_status():
|
||||
def test_later_sync_ships_atomically_in_the_offline_shell():
|
||||
source = (Path(__file__).parents[1] / "frontend" / "service-worker.js").read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/later-sync.js'" in source
|
||||
@@ -137,4 +137,4 @@ def test_markdown_work_bodies_are_mobile_safe_block_containers():
|
||||
assert ".markdown-content { min-width:0; max-width:100%; overflow-wrap:anywhere;" in css
|
||||
assert ".markdown-content pre { max-width:100%; overflow-x:auto;" in css
|
||||
assert ".markdown-content a { min-height:44px;" in css
|
||||
assert "stackchain-dashboard-shell-v85" in worker
|
||||
assert "stackchain-dashboard-shell-v86" in worker
|
||||
@@ -41,7 +41,7 @@ def test_offline_shell_contains_every_local_dashboard_runtime_asset():
|
||||
shell_assets = set(re.findall(r"BASE \+ '([^']+)'", worker.split("async function sessionCsrf", 1)[0]))
|
||||
|
||||
assert local_assets <= shell_assets, f"Offline shell is missing: {sorted(local_assets - shell_assets)}"
|
||||
assert "stackchain-dashboard-shell-v85" in worker
|
||||
assert "stackchain-dashboard-shell-v86" in worker
|
||||
|
||||
|
||||
def test_all_conversation_composers_offer_accessible_mobile_mentions():
|
||||
|
||||
@@ -56,6 +56,33 @@ process.stdout.write(JSON.stringify({peak, loaded, saved, status}));
|
||||
assert result["status"] == {"total": 5, "ready": 5, "failed": 0, "pending": 0}
|
||||
|
||||
|
||||
def test_readiness_waits_for_durable_async_admission_and_reports_rejection():
|
||||
result = run_scenario("""
|
||||
const events = [];
|
||||
const warmer = createOfflineToday({
|
||||
loadDetail: async () => ({title:'Fetched detail'}),
|
||||
loadSavedDetail: async () => null,
|
||||
saveDetail: async () => {
|
||||
events.push('admission-started');
|
||||
await new Promise(resolve => setTimeout(resolve, 5));
|
||||
events.push('admission-rejected');
|
||||
return false;
|
||||
},
|
||||
onStatus: status => events.push('ready:' + status.ready + '/failed:' + status.failed),
|
||||
});
|
||||
const status = await warmer.warm('timmy', [{
|
||||
kind:'issue', repository:'stackchain/dashboard', number:1, updated_at:'v1',
|
||||
}]);
|
||||
process.stdout.write(JSON.stringify({events, status, failed:warmer.failedKeys()}));
|
||||
""")
|
||||
|
||||
assert result["status"] == {"total": 1, "ready": 0, "failed": 1, "pending": 0}
|
||||
assert result["failed"] == ["issue:stackchain/dashboard:1"]
|
||||
assert result["events"].index("admission-rejected") < result["events"].index(
|
||||
"ready:0/failed:1"
|
||||
)
|
||||
|
||||
|
||||
def test_refreshes_only_changed_revisions_and_keeps_prior_copy_on_failure():
|
||||
result = run_scenario("""
|
||||
const items = [
|
||||
|
||||
+143
-1
@@ -27,6 +27,136 @@ const storage = {{
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
def run_async_scenario(scenario: str) -> dict:
|
||||
script = f"""
|
||||
const createOfflineWorkStore = require({json.dumps(str(OFFLINE_WORK))});
|
||||
const values = new Map();
|
||||
const records = new Map();
|
||||
const storage = {{
|
||||
getItem: key => values.has(key) ? values.get(key) : null,
|
||||
setItem: (key, value) => values.set(key, value),
|
||||
removeItem: key => values.delete(key),
|
||||
}};
|
||||
const transaction = async work => work({{
|
||||
get: async key => records.get(key),
|
||||
getAll: async () => [...records.values()],
|
||||
put: async value => records.set(value.id, structuredClone(value)),
|
||||
delete: async key => records.delete(key),
|
||||
clear: async () => records.clear(),
|
||||
}});
|
||||
(async () => {{
|
||||
{scenario}
|
||||
}})().catch(error => {{ console.error(error); process.exit(1); }});
|
||||
"""
|
||||
result = subprocess.run(
|
||||
["node", "-e", script], check=True, capture_output=True, text=True
|
||||
)
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
def test_indexeddb_commit_persists_snapshot_and_details_without_localstorage_payloads():
|
||||
result = run_async_scenario("""
|
||||
const store = createOfflineWorkStore({storage, transaction, now:() => new Date('2026-08-07T12:00:00Z')});
|
||||
store.setEnabled(true);
|
||||
const snapshotSaved = await store.save({
|
||||
user:{login:'timmy', full_name:'Timmy'}, issues:[{number:8, title:'Large offline review'}],
|
||||
pull_requests:[], notifications:[],
|
||||
});
|
||||
const item = {kind:'pull', repository:'stackchain/dashboard', number:8, is_review:true};
|
||||
const detailSaved = await store.saveDetail('timmy', item, {
|
||||
title:'Large offline review', head_sha:'abc123',
|
||||
files:[{filename:'large.diff', diff_lines:['+private review line']}],
|
||||
});
|
||||
const restarted = createOfflineWorkStore({storage, transaction, now:() => new Date('2026-08-07T12:01:00Z')});
|
||||
process.stdout.write(JSON.stringify({
|
||||
snapshotSaved, detailSaved,
|
||||
loaded:await restarted.load('timmy'),
|
||||
detail:await restarted.loadDetail('timmy', item),
|
||||
localValues:[...values.values()].join(' '),
|
||||
recordIds:[...records.keys()].sort(),
|
||||
}));
|
||||
""")
|
||||
|
||||
assert result["snapshotSaved"] is True
|
||||
assert result["detailSaved"] is True
|
||||
assert result["loaded"]["user"]["login"] == "timmy"
|
||||
assert result["detail"]["head_sha"] == "abc123"
|
||||
assert "Large offline review" not in result["localValues"]
|
||||
assert "private review line" not in result["localValues"]
|
||||
assert result["recordIds"] == ["detail:timmy:pull:stackchain/dashboard:8", "snapshot"]
|
||||
|
||||
|
||||
def test_legacy_localstorage_payloads_migrate_only_after_indexeddb_commit():
|
||||
result = run_async_scenario("""
|
||||
values.set('stackchain.offline-work.enabled.v1', 'true');
|
||||
values.set('stackchain.offline-work.snapshot.v1', JSON.stringify({
|
||||
version:1, user_login:'timmy', saved_at:'2026-08-07T12:00:00.000Z',
|
||||
data:{user:{login:'timmy'}, issues:[], pull_requests:[], notifications:[]},
|
||||
}));
|
||||
values.set('stackchain.offline-work.details.v1', JSON.stringify([{
|
||||
key:'issue:stackchain/dashboard:9', user_login:'timmy', saved_at:'2026-08-07T12:00:00.000Z',
|
||||
data:{title:'Migrated issue'},
|
||||
}]));
|
||||
const store = createOfflineWorkStore({storage, transaction, now:() => new Date('2026-08-07T12:01:00Z')});
|
||||
const snapshot = await store.load('timmy');
|
||||
const detail = await store.loadDetail('timmy', {kind:'issue', repository:'stackchain/dashboard', number:9});
|
||||
process.stdout.write(JSON.stringify({
|
||||
snapshot, detail,
|
||||
legacySnapshot:values.has('stackchain.offline-work.snapshot.v1'),
|
||||
legacyDetails:values.has('stackchain.offline-work.details.v1'),
|
||||
recordIds:[...records.keys()].sort(),
|
||||
}));
|
||||
""")
|
||||
|
||||
assert result["snapshot"]["user"]["login"] == "timmy"
|
||||
assert result["detail"]["title"] == "Migrated issue"
|
||||
assert result["legacySnapshot"] is False
|
||||
assert result["legacyDetails"] is False
|
||||
assert result["recordIds"] == ["detail:timmy:issue:stackchain/dashboard:9", "snapshot"]
|
||||
|
||||
|
||||
def test_failed_migration_keeps_legacy_snapshot_readable_and_untouched():
|
||||
result = run_async_scenario("""
|
||||
values.set('stackchain.offline-work.enabled.v1', 'true');
|
||||
values.set('stackchain.offline-work.snapshot.v1', JSON.stringify({
|
||||
version:1, user_login:'timmy', saved_at:'2026-08-07T12:00:00.000Z',
|
||||
data:{user:{login:'timmy'}, issues:[{number:7, title:'Keep me'}], pull_requests:[], notifications:[]},
|
||||
}));
|
||||
let attempts = 0;
|
||||
const flakyTransaction = async work => {
|
||||
attempts += 1;
|
||||
if (attempts === 1) throw new Error('quota admission failed');
|
||||
return transaction(work);
|
||||
};
|
||||
const store = createOfflineWorkStore({storage, transaction:flakyTransaction, now:() => new Date('2026-08-07T12:01:00Z')});
|
||||
const snapshot = await store.load('timmy');
|
||||
process.stdout.write(JSON.stringify({
|
||||
snapshot, attempts,
|
||||
legacySnapshot:values.has('stackchain.offline-work.snapshot.v1'),
|
||||
recordIds:[...records.keys()],
|
||||
}));
|
||||
""")
|
||||
|
||||
assert result["snapshot"]["issues"][0]["title"] == "Keep me"
|
||||
assert result["legacySnapshot"] is True
|
||||
assert result["recordIds"] == []
|
||||
|
||||
|
||||
def test_ready_atomically_prunes_all_expired_indexeddb_records():
|
||||
result = run_async_scenario("""
|
||||
const writer = createOfflineWorkStore({storage, transaction, now:() => new Date('2026-08-07T12:00:00Z'), maxAgeMs:1000});
|
||||
writer.setEnabled(true);
|
||||
await writer.save({user:{login:'timmy'}, issues:[], pull_requests:[], notifications:[]});
|
||||
await writer.saveDetail('timmy', {kind:'issue', repository:'stackchain/dashboard', number:1}, {title:'One'});
|
||||
await writer.saveDetail('timmy', {kind:'pull', repository:'stackchain/dashboard', number:2}, {title:'Two'});
|
||||
const expired = createOfflineWorkStore({storage, transaction, now:() => new Date('2026-08-07T12:00:02Z'), maxAgeMs:1000});
|
||||
await expired.ready();
|
||||
process.stdout.write(JSON.stringify({recordIds:[...records.keys()]}));
|
||||
""")
|
||||
|
||||
assert result["recordIds"] == []
|
||||
|
||||
|
||||
def test_opted_in_snapshot_survives_restart_with_only_queue_card_fields():
|
||||
result = run_scenario("""
|
||||
const store = createOfflineWorkStore({storage, now:() => new Date('2026-08-07T12:00:00Z')});
|
||||
@@ -213,11 +343,23 @@ async def test_dashboard_offers_private_offline_work_controls_and_read_only_hydr
|
||||
assert '.offline-work-controls button { min-height:44px;' in html
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_dashboard_uses_async_indexeddb_offline_store_end_to_end():
|
||||
html = await dashboard()
|
||||
|
||||
assert "createOfflineWorkStore({ storage: localStorage, indexedDB:window.indexedDB })" in html
|
||||
assert "async function hydrateOfflineWork" in html
|
||||
assert "const saved = await offlineWorkStore.load();" in html
|
||||
assert "await offlineWorkStore.save({" in html
|
||||
assert "await offlineWorkStore.clear();" in html
|
||||
assert "Offline saving unavailable · retry after reconnect." in html
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_initial_http_outage_hydrates_saved_work_and_recovers_on_live_snapshot():
|
||||
html = await dashboard()
|
||||
|
||||
assert "if (!hasContextSnapshot && hydrateOfflineWork('outage')) return;" in html
|
||||
assert "if (!hasContextSnapshot && await hydrateOfflineWork('outage')) return;" in html
|
||||
assert "Outage · saved " in html
|
||||
assert "Server unavailable · showing private My Work saved " in html
|
||||
assert "Live details and actions will return automatically." in html
|
||||
|
||||
@@ -292,6 +292,6 @@ async def test_plan_today_wires_cancel_back_and_success_through_overlay_history(
|
||||
def test_plan_today_controller_is_available_in_the_offline_shell():
|
||||
source = SERVICE_WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/plan-today.js'" in source
|
||||
assert "BASE + 'static/plan-today-preview.js'" in source
|
||||
@@ -43,7 +43,10 @@ const clear = createPrivateDeviceDataPurger({{
|
||||
state = json.loads(result.stdout)
|
||||
|
||||
assert state["remaining"] == ["other.preference"]
|
||||
assert state["databases"] == ["stackchain-background-outbox-v1"]
|
||||
assert state["databases"] == [
|
||||
"stackchain-background-outbox-v1",
|
||||
"stackchain-offline-work-v2",
|
||||
]
|
||||
assert state["caches"] == ["stackchain-dashboard-shell-v37"]
|
||||
assert state["workerMessages"] == [{"type": "stackchain-purge-outbox"}]
|
||||
assert state["complete"] is True
|
||||
@@ -125,7 +125,7 @@ async function dispatchNotificationClick(route) {{
|
||||
def test_resumable_today_session_ships_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/my-work.js'" in source
|
||||
assert "BASE + 'static/dashboard.js'" in source
|
||||
assert "BASE + 'static/dashboard.css'" in source
|
||||
@@ -134,14 +134,14 @@ def test_resumable_today_session_ships_in_a_new_offline_shell():
|
||||
def test_ownership_exit_runtime_rolls_the_offline_shell_cache():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/dashboard.js'" in source
|
||||
|
||||
|
||||
def test_offline_review_next_ships_today_completion_atomically():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/today-completion.js'" in source
|
||||
assert "BASE + 'static/dashboard.js'" in source
|
||||
|
||||
@@ -149,7 +149,7 @@ def test_offline_review_next_ships_today_completion_atomically():
|
||||
def test_duplicate_aware_capture_ships_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/create-issue-sheet.js'" in source
|
||||
assert "BASE + 'static/dashboard.js'" in source
|
||||
|
||||
@@ -157,14 +157,14 @@ def test_duplicate_aware_capture_ships_in_a_new_offline_shell():
|
||||
def test_exact_later_picker_ships_atomically_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/later-picker.js'" in source
|
||||
|
||||
|
||||
def test_navigation_deadline_ships_in_a_new_shell_cache():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/dashboard.css'" in source
|
||||
assert "BASE + 'static/dashboard.js'" in source
|
||||
assert "BASE + 'static/install-app.js'" in source
|
||||
@@ -173,21 +173,21 @@ def test_navigation_deadline_ships_in_a_new_shell_cache():
|
||||
def test_today_convergence_ships_in_a_new_shell_cache():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/today-sync.js'" in source
|
||||
|
||||
|
||||
def test_mobile_search_viewport_ships_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/mobile-search-viewport.js'" in source
|
||||
|
||||
|
||||
def test_update_ownership_flow_ships_atomically_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/update-ownership.js'" in source
|
||||
|
||||
|
||||
@@ -397,7 +397,7 @@ def test_one_session_bound_csrf_proof_is_reused_for_a_background_drain():
|
||||
def test_queue_today_ships_atomically_in_a_new_offline_shell():
|
||||
source = WORKER.read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/queue-today.js'" in source
|
||||
|
||||
|
||||
|
||||
@@ -221,7 +221,7 @@ async def test_today_blocker_opens_existing_preview_and_preserves_readiness_gate
|
||||
def test_readiness_runtime_is_available_in_offline_shell():
|
||||
service_worker = SERVICE_WORKER.read_text()
|
||||
|
||||
assert "const CACHE = 'stackchain-dashboard-shell-v85';" in service_worker
|
||||
assert "const CACHE = 'stackchain-dashboard-shell-v86';" in service_worker
|
||||
assert "BASE + 'static/today-readiness.js'" in service_worker
|
||||
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ sync.enqueueConfiguration(120, {{'issue:r:1:':60}});
|
||||
def test_inflight_today_drain_ships_in_a_new_offline_shell():
|
||||
source = (Path(__file__).parents[1] / "frontend" / "service-worker.js").read_text()
|
||||
|
||||
assert "stackchain-dashboard-shell-v85" in source
|
||||
assert "stackchain-dashboard-shell-v86" in source
|
||||
assert "BASE + 'static/today-sync.js'" in source
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user