fix(reliability): stabilize sync rendering and offline recovery
This commit is contained in:
1 parent
3877d4f986
commit
eaa4894894
15 files changed
+131
-27
No files matched your search
+1
-1
@@ -13,7 +13,7 @@ const [rawIndexHtml, addonsJs, backupJs, bootGuardJs, localDbJs, localDbWorkerJs
|
||||
readFile(new URL("../node_modules/@sqlite.org/sqlite-wasm/dist/sqlite3.wasm", import.meta.url)),
|
||||
text("i18n.js"), text("tasks.js"), text("calendar.js"), text("notifications.js"), text("i18n-renderers.js"), text("desktop-personalization.js"), text("desktop-widgets.js"), text("system-navigation.js"), text("browser-app.js"), text("data-actions.js"), text("terminal-app.js"), text("db-admin.js"), text("manifest.webmanifest"), text("service-worker.js"), text("life-os-icon.svg"), readFile(new URL("../public/locales/en.json", import.meta.url), "utf8"), readFile(new URL("../public/locales/zh-CN.json", import.meta.url), "utf8")
|
||||
]);
|
||||
const release = "1.22.0";
|
||||
const release = "1.22.1";
|
||||
const localeEnJs = `window.LifeOSLocales=window.LifeOSLocales||{};window.LifeOSLocales.en=${localeEn};`;
|
||||
const localeZhJs = `window.LifeOSLocales=window.LifeOSLocales||{};window.LifeOSLocales['zh-CN']=${localeZh};`;
|
||||
const extraLocaleCodes = ['zh-HK', 'zh-MO', 'zh-TW', 'ar', 'fr', 'ru', 'es', 'ja'];
|
||||
|
||||
@@ -87,7 +87,7 @@ assert.ok(syncCode.includes("function changes("));
|
||||
const values = new Map();
|
||||
const storage = { getItem: key => values.get(key) ?? null, setItem: (key, value) => values.set(key, String(value)) };
|
||||
const client = vm.createContext({
|
||||
localStorage: storage, window: {}, fetch: async (path, options = {}) => worker.fetch(
|
||||
localStorage: storage, window: { LifeOSLocalDB: { mode: () => 'cloud' } }, navigator: { onLine: true }, fetch: async (path, options = {}) => worker.fetch(
|
||||
new Request("https://life-os.test" + path, { ...options, headers: { ...options.headers, ...headers } }), { DB: db }),
|
||||
clearTimeout, setTimeout, confirm: () => false, t: key => key,
|
||||
toast: () => {}, document: { createElement() {}, body: { append() {} } }
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import vm from 'node:vm';
|
||||
|
||||
const source = readFileSync(new URL('../public/addons.js', import.meta.url), 'utf8');
|
||||
const values = new Map();
|
||||
let writes = 0;
|
||||
const attrs = {};
|
||||
const indicator = { getAttribute: key => attrs[key], setAttribute(key, value) { writes++; attrs[key] = value; } };
|
||||
for (const key of ['className', 'innerHTML', 'title']) {
|
||||
let value = '';
|
||||
Object.defineProperty(indicator, key, { get: () => value, set(next) { writes++; value = next; } });
|
||||
}
|
||||
const ui = vm.createContext({
|
||||
document: { querySelector: () => null },
|
||||
syncState: () => ({ phase: 'ready', pending: 0, conflicts: 0 }),
|
||||
syncCopy: () => ({ status: 'Sync status', pending: 'Pending', conflicts: 'Conflicts' }),
|
||||
syncIndicator: () => indicator, stateLabel: () => 'Up to date'
|
||||
});
|
||||
vm.runInContext(source.slice(source.indexOf(' function renderSyncSurfaces()'), source.indexOf(' function fillSyncCenter(')), ui);
|
||||
ui.renderSyncSurfaces();
|
||||
const firstWrites = writes;
|
||||
assert.ok(firstWrites > 0);
|
||||
ui.renderSyncSurfaces();
|
||||
assert.equal(writes, firstWrites, 'observer-triggered rendering must not mutate unchanged DOM');
|
||||
|
||||
const state = { profile:{}, memories:[], museum:[], settings:{}, system:{schemaVersion:2,tasksBoard:{goals:[],tasks:[]}} };
|
||||
const revisions = { profile:{},settings:{},system:{},memories:{},goals:{},tasks:{},museum:{} };
|
||||
const storage = { getItem: key => values.get(key) ?? null, setItem: (key,value) => values.set(key,String(value)) };
|
||||
const timers = [];
|
||||
const client = vm.createContext({
|
||||
localStorage:storage, window:{LifeOSLocalDB:{mode:()=>'cloud'}}, navigator:{onLine:false},
|
||||
setTimeout(fn, delay) { timers.push({fn,delay}); return timers.length; }, clearTimeout() {},
|
||||
toast() {}, t:key=>key, fetch:async()=> { throw new Error('offline'); }
|
||||
});
|
||||
const cloud = source.slice(source.indexOf(' const CLOUD_KEYS ='), source.indexOf(' const originalSetItem ='));
|
||||
vm.runInContext(cloud + '\nwindow.Test={ready(s,v){baseline=clone(s);versions=v;cloudReady=true},schedule:scheduleCloudSync,upload:uploadCloudState};', client);
|
||||
for (const [field,key] of Object.entries({profile:'lifeos_profile',memories:'lifeos_memories',museum:'lifeos_museum',settings:'lifeos_settings',system:'lifeos_system'})) storage.setItem(key,JSON.stringify(state[field]));
|
||||
client.window.Test.ready(state,revisions);
|
||||
storage.setItem('lifeos_memories',JSON.stringify([{id:'m1',date:'2026-10-13',text:'offline change'}]));
|
||||
client.window.Test.schedule();
|
||||
assert.equal(JSON.parse(storage.getItem('lifeos_sync_outbox_v1')).length,1,'offline edit is queued immediately');
|
||||
assert.equal(timers.length,0,'offline queue must not schedule network requests');
|
||||
client.navigator.onLine=true;
|
||||
await assert.rejects(client.window.Test.upload(),/offline/);
|
||||
assert.equal(JSON.parse(storage.getItem('lifeos_sync_outbox_v1')).length,1,'failed upload retains pending edit');
|
||||
assert.equal(timers.at(-1).delay,2000,'transient failure schedules bounded retry');
|
||||
console.log('Life OS observer idempotence and offline queue recovery valid');
|
||||
Loaded 3 of 15 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user