Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
sync: a first-saved file must carry its CRDT state, or a later offline edit is lost (#809)
nyblneta63213eaa91c
diff --git a/scripts/test-sync-session.ts b/scripts/test-sync-session.tsindex 2de7cb88..1913a595 100644--- a/scripts/test-sync-session.ts+++ b/scripts/test-sync-session.ts@@ -739,6 +739,27 @@ const freshShared = () => {sess.stop?.();}+// A deck's FIRST save must carry its CRDT state, even though a never-saved deck+// isn't share-eligible yet (enableSharing runs after the stamp). Without it, a+// colleague's offline edit on that file is LOST to the author's older edits:+// test-sync.ts measures the engine side; this pins that stampInto writes it.+H("a never-shared deck's FIRST save carries its sync state");+{+ const doc = newDoc(); doc.docId = `first-${Math.random().toString(36).slice(2, 8)}`;+ delete (doc as { collab?: unknown }).collab // as a brand-new deck arrives: no credentials yet+ const st = new Store(doc);+ const ss = new SyncSession(st);+ await settle(); // credentials are minted asynchronously at creation+ ok(!!(st.doc as { collab?: unknown }).collab, 'setup: the session minted credentials for the new deck')+ ok(!(ss as unknown as { shareEligible: () => boolean }).shareEligible(), 'setup: not yet share-eligible (never saved, never shared): the case under test')+ st.commit(() => { st.doc.title = 'first draft'; });+ await settle();+ const saved = JSON.parse(JSON.stringify(st.doc));+ (ss as unknown as { stampInto: (d: unknown) => void }).stampInto(saved);+ ss.stop?.();+ ok(saved.collab?.sync?.v === SYNC_V, 'the first save is stamped with the CRDT state (collab.sync, current SYNC_V)')+}+console.log(`\n${checks - failures}/${checks} checks passed`);// BroadcastChannel and the heartbeat keep node's event loop aliveprocess.exit(failures ? 1 : 0);diff --git a/scripts/test-sync.ts b/scripts/test-sync.tsindex f7a90909..a5a1231e 100644--- a/scripts/test-sync.ts+++ b/scripts/test-sync.ts@@ -554,5 +554,45 @@ const castCount = (d: Doc) =>ok(!r.changed && A.fingerprint() === before, 'v1 snapshot ignored by mergeSnapshot')}+{+ // A FIRST-SAVED file must carry its CRDT state, or a colleague's offline+ // edit on top of it is LOST. session.attach() restores collab.sync when the+ // file has it; without it the file joins as a never-synced adopt, whose null+ // registers [0,''] lose to every real op, and the replica's Lamport clock+ // starts near zero. So the colleague's causally LATER edit carries a lower+ // stamp than the author's OLDER ones, and loses (measured on #792, which+ // skipped stampInto for a deck that wasn't yet share-eligible: its first save).+ console.log('a first-saved file must carry its sync state, or a later offline edit is lost…')+ const clone = <T,>(v: T): T => JSON.parse(JSON.stringify(v))+ const field = (d: Doc) => (d.slides[0].elements[0] as { x: number })+ const scenario = (stamped: boolean) => {+ // A: a never-shared deck, a field edited five times, then the FIRST save+ const docA = baseDoc()+ const A = new SyncState('A'); A.adopt(docA)+ const logA: Op[] = []+ for (let i = 1; i <= 5; i++) {+ const before = clone(docA); field(docA).x = 100 + i+ logA.push(...A.diff(before, docA, { text: true }))+ }+ const file = { doc: clone(docA), sync: stamped ? clone(A.toJSON()) : null }+ // B opens that file OFFLINE, as session.attach() does, and edits on top of it+ const docB = clone(file.doc)+ const B = file.sync ? SyncState.fromJSON('B', file.sync) : new SyncState('B')+ B.adopt(docB)+ const before = clone(docB); field(docB).x = 999+ const opsB = B.diff(before, docB, { text: true })+ // they meet+ A.apply(docA, opsB)+ B.apply(docB, logA)+ return { a: field(docA).x, b: field(docB).x }+ }+ const kept = scenario(true)+ ok(kept.a === 999 && kept.b === 999, "with collab.sync (what stampInto writes): the colleague's later edit wins on both")+ // the control that makes the row above mean something: the same scenario+ // WITHOUT the stamp must show the loss, or this rig can't see the bug+ const lost = scenario(false)+ ok(lost.a === lost.b && lost.a === 105, 'control: WITHOUT collab.sync the later edit is lost (both converge on the older 105)')+}+console.log(failures === 0 ? `\nALL PASS (${checks} checks)` : `\n${failures} FAILURES of ${checks} checks`)process.exit(failures ? 1 : 0)