| 1 | // saver.test.mjs — gate L9's client half (spec §6.5, §7): the saver |
| 2 | // against a scripted server, with the real document, fidelity table and |
| 3 | // merge application. The server half is internal/mapatest's (autosave, |
| 4 | // rebase, commit=1, Resolve, the first scratch commit); make browser |
| 5 | // drives the two together. |
| 6 | |
| 7 | import { test } from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import { editor as makeEditor } from './testkit.mjs' |
| 10 | import { editorAdapter } from './adapter.mjs' |
| 11 | import { Saver } from './saver.mjs' |
| 12 | import { conflictPlugin } from './conflict.mjs' |
| 13 | |
| 14 | // timers runs nothing by itself: run() fires what is due. |
| 15 | function timers () { |
| 16 | let next = 1 |
| 17 | const due = new Map() |
| 18 | return { |
| 19 | setTimeout (fn, ms) { due.set(next, { fn, ms }); return next++ }, |
| 20 | clearTimeout (id) { due.delete(id) }, |
| 21 | pending () { return [...due.values()].map(timer => timer.ms) }, |
| 22 | async run (filter = () => true) { |
| 23 | for (const [id, timer] of [...due]) { |
| 24 | if (!filter(timer.ms)) continue |
| 25 | due.delete(id) |
| 26 | await timer.fn() |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // server answers each POST from a script: answer(url, fields) returns |
| 33 | // {status, json}. Every request is kept. |
| 34 | function server (answer) { |
| 35 | const sent = [] |
| 36 | const post = async (url, fields, options = {}) => { |
| 37 | sent.push({ url, fields: { ...fields }, keepalive: Boolean(options.keepalive) }) |
| 38 | return answer(url, fields, sent.length) |
| 39 | } |
| 40 | return { post, sent } |
| 41 | } |
| 42 | |
| 43 | function store (initial = {}) { |
| 44 | const map = new Map(Object.entries(initial)) |
| 45 | return { |
| 46 | get: key => map.get(key) ?? null, |
| 47 | set: (key, value) => map.set(key, value), |
| 48 | remove: key => map.delete(key), |
| 49 | map |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function page (island, answer, extra = {}) { |
| 54 | // The page's conflict plugin is there too: it is what locks the editor |
| 55 | // while Resolve's POST is out. |
| 56 | const { view, fidelity } = makeEditor(island.source, { |
| 57 | plugins: fidelity => [conflictPlugin({ |
| 58 | fidelity, isRemote: () => ed.remote, isBusy: () => saver.resolving, resolve: () => {} |
| 59 | })] |
| 60 | }) |
| 61 | const ed = editorAdapter({ view }, fidelity) |
| 62 | const net = server(answer) |
| 63 | const clock = timers() |
| 64 | const navigated = [] |
| 65 | const shown = [] |
| 66 | const statuses = [] |
| 67 | const saver = new Saver(island, { |
| 68 | editor: ed, post: net.post, storage: extra.storage ?? store(), timers: clock, |
| 69 | token: () => 'TOKENTOKENTOKEN0001', navigate: href => navigated.push(href), |
| 70 | replaceURL: () => {}, reload: () => navigated.push('reload'), |
| 71 | ui: { status: state => statuses.push(state), notice: (kind) => shown.push(kind), conflict: c => shown.push(c ? 'conflict' : 'settled'), leaving: (kind, go) => shown.push({ leaving: kind, go }) } |
| 72 | }) |
| 73 | // typeAt puts text at the end of a top-level block, as the person |
| 74 | // would; a transaction the editor refuses changes nothing. |
| 75 | const typeAt = (index, text) => { |
| 76 | let end = 0 |
| 77 | for (let at = 0; at <= index; at++) end += view.state.doc.child(at).nodeSize |
| 78 | const before = view.state.doc |
| 79 | view.dispatch(view.state.tr.insertText(text, end - 1)) |
| 80 | if (view.state.doc !== before) saver.changed() |
| 81 | } |
| 82 | const type = text => typeAt(0, text) |
| 83 | return { view, fidelity, saver, net, clock, navigated, shown, statuses, type, typeAt, ed } |
| 84 | } |
| 85 | |
| 86 | const island = (source, extra = {}) => ({ |
| 87 | source, base: '100', draft: null, links: {}, drafts: '/p/acme/draft', page: 'acme', ...extra |
| 88 | }) |
| 89 | |
| 90 | const ok = json => ({ status: 200, json }) |
| 91 | |
| 92 | test('autosave sends only what differs, and the tab owns the draft it wrote', async () => { |
| 93 | const storage = store() |
| 94 | const at = page(island('One.\n\nTwo.'), () => ok({ version: '7', revision: '100' }), { storage }) |
| 95 | await at.saver.autosave() |
| 96 | assert.equal(at.net.sent.length, 0, 'nothing typed, nothing sent') |
| 97 | at.type(' more') |
| 98 | await at.clock.run(ms => ms === 1200) |
| 99 | assert.equal(at.net.sent.length, 1) |
| 100 | assert.deepEqual(at.net.sent[0].fields, { base: '100', version: '0', body: 'One. more\n\nTwo.' }) |
| 101 | assert.equal(storage.get('mapa-draft:/p/acme/draft'), '7') |
| 102 | assert.ok(at.saver.own) |
| 103 | assert.ok(at.clock.pending().length > 0, 'the idle commit is waiting') |
| 104 | }) |
| 105 | |
| 106 | test('an offered draft never commits by itself; Save commits it', async () => { |
| 107 | const draft = { version: '9', base: '100' } |
| 108 | const at = page(island('Offered.', { draft }), url => |
| 109 | url.endsWith('/save') ? ok({ saved: true, merged: false, revision: '101' }) : ok({ version: '10' })) |
| 110 | assert.ok(at.saver.offered && !at.saver.own) |
| 111 | assert.equal(await at.saver.commit(), 'none') |
| 112 | await at.saver.leave('/elsewhere') |
| 113 | at.saver.closing() |
| 114 | assert.equal(at.net.sent.length, 0, 'leave and close wrote nothing') |
| 115 | assert.deepEqual(at.navigated, ['/elsewhere']) |
| 116 | assert.equal(await at.saver.commit({ explicit: true }), 'saved') |
| 117 | assert.deepEqual(at.net.sent.map(request => request.url), ['/p/acme/draft/save']) |
| 118 | assert.equal(at.net.sent[0].fields.version, '9') |
| 119 | }) |
| 120 | |
| 121 | test('reload, then close: sessionStorage says the draft is this tab\'s own', () => { |
| 122 | const draft = { version: '9', base: '100' } |
| 123 | const at = page(island('Mine.', { draft }), () => ok({}), |
| 124 | { storage: store({ 'mapa-draft:/p/acme/draft': '9' }) }) |
| 125 | assert.ok(at.saver.own) |
| 126 | at.saver.closing() |
| 127 | assert.equal(at.net.sent.length, 1) |
| 128 | assert.deepEqual(at.net.sent[0], { |
| 129 | url: '/p/acme/draft', keepalive: true, |
| 130 | fields: { commit: '1', version: '9', base: '100' } |
| 131 | }) |
| 132 | }) |
| 133 | |
| 134 | test('closing carries text not yet sent, and the cap leaves it out', () => { |
| 135 | const at = page(island('Short.'), () => ok({})) |
| 136 | at.type(' typed') |
| 137 | at.saver.closing() |
| 138 | assert.equal(at.net.sent[0].fields.body, 'Short. typed') |
| 139 | assert.equal(at.net.sent[0].fields.version, '0') |
| 140 | const long = page(island('x'), () => ok({}), { storage: store({ 'mapa-draft:/p/acme/draft': '5' }) }) |
| 141 | long.saver.version = '5' |
| 142 | long.saver.own = true |
| 143 | long.type('y'.repeat(70 * 1024)) |
| 144 | long.saver.closing() |
| 145 | assert.equal(long.net.sent[0].fields.body, undefined, 'over the cap: no text, the last autosave commits') |
| 146 | }) |
| 147 | |
| 148 | test('clean rebase: applied, the caret survives, the base moves by the rebase POST', async () => { |
| 149 | const at = page(island('Ana one.\n\nShared two.\n\nThree.'), (url, fields) => { |
| 150 | if (fields.rebase) return ok({ version: '12' }) |
| 151 | return ok({ version: '11', revision: '200', merge: { against: '200', body: 'Ana one. typed\n\nShared two, as Bo has it.\n\nThree.' } }) |
| 152 | }) |
| 153 | at.type(' typed') |
| 154 | const caret = at.view.state.selection.head |
| 155 | await at.saver.autosave() |
| 156 | assert.equal(at.ed.current(), 'Ana one. typed\n\nShared two, as Bo has it.\n\nThree.') |
| 157 | assert.equal(at.view.state.selection.head, caret, 'the caret is where it was') |
| 158 | const rebase = at.net.sent[1] |
| 159 | assert.deepEqual(rebase.fields, { base: '100', version: '11', body: 'Ana one. typed\n\nShared two, as Bo has it.\n\nThree.', rebase: '200' }) |
| 160 | assert.equal(at.saver.base, '200') |
| 161 | assert.equal(at.saver.version, '12') |
| 162 | }) |
| 163 | |
| 164 | test('the block under the caret is the teammate\'s too, and undo brings back yours', async () => { |
| 165 | const at = page(island('Ana one.'), (url, fields) => |
| 166 | fields.rebase ? ok({ version: '12' }) : ok({ version: '11', merge: { against: '200', body: 'Ana one. typed, and Bo.' } })) |
| 167 | at.type(' typed') |
| 168 | await at.saver.autosave() |
| 169 | assert.equal(at.ed.current(), 'Ana one. typed, and Bo.') |
| 170 | const { undo } = await import('prosemirror-history') |
| 171 | undo(at.view.state, tr => at.view.dispatch(tr)) |
| 172 | assert.equal(at.ed.current(), 'Ana one. typed') |
| 173 | }) |
| 174 | |
| 175 | test('typing mid-flight: the merge is not applied and the base does not move', async () => { |
| 176 | let release |
| 177 | const at = page(island('One.'), (url, fields) => { |
| 178 | if (fields.rebase) return ok({ version: '13' }) |
| 179 | return new Promise(resolve => { |
| 180 | release = () => resolve(ok({ version: '11', merge: { against: '200', body: 'One. a\n\nBo.' } })) |
| 181 | }) |
| 182 | }) |
| 183 | at.type(' a') |
| 184 | const first = at.saver.autosave() |
| 185 | at.type('b') |
| 186 | release() |
| 187 | await first |
| 188 | assert.equal(at.ed.current(), 'One. ab', 'nothing applied over the typing') |
| 189 | assert.equal(at.saver.base, '100') |
| 190 | assert.equal(at.net.sent.length, 1, 'no rebase POST') |
| 191 | assert.ok(at.clock.pending().includes(0), 'it asks again at once') |
| 192 | }) |
| 193 | |
| 194 | test('a merge the editor cannot represent stops after three tries', async () => { |
| 195 | // A teammate's empty item: the editor keeps no empty block it is not in. |
| 196 | const merged = 'One. a\n\n- \n\nBo.' |
| 197 | const at = page(island('One.'), (url, fields) => |
| 198 | fields.rebase ? ok({ version: '99' }) : ok({ version: String(at.net.sent.length + 10), merge: { against: '200', body: merged } })) |
| 199 | for (let round = 0; round < 5; round++) { |
| 200 | at.type(round === 0 ? ' a' : '') |
| 201 | await at.saver.autosave(true) |
| 202 | } |
| 203 | assert.equal(at.saver.tries.get('200'), 3) |
| 204 | assert.ok(!at.net.sent.some(request => request.fields.rebase), 'the base never moved') |
| 205 | assert.equal(at.saver.base, '100') |
| 206 | }) |
| 207 | |
| 208 | test('a conflict is shown, locks the editor\'s own commits, and Resolve settles it', async () => { |
| 209 | const hunks = [{ at: 0, count: 1, base: 'One.', mine: 'One. a', theirs: 'One, as Bo.' }] |
| 210 | const at = page(island('One.'), url => { |
| 211 | if (url.endsWith('/resolve')) return ok({ outcome: 'saved', revision: '300', body: 'One. a' }) |
| 212 | return ok({ version: '11', merge: { against: '200', conflict: true, hunks } }) |
| 213 | }) |
| 214 | at.type(' a') |
| 215 | await at.saver.autosave() |
| 216 | assert.deepEqual(at.shown, ['conflict']) |
| 217 | assert.equal(await at.saver.commit(), 'conflict', 'nothing nobody chose is committed') |
| 218 | assert.equal(await at.saver.resolve(['mine']), 'saved') |
| 219 | const resolve = at.net.sent.find(request => request.url === '/p/acme/resolve') |
| 220 | assert.deepEqual(resolve.fields, { current: '200', version: '11', grammar: '2', 'choice-0': 'mine' }) |
| 221 | assert.equal(at.saver.base, '300') |
| 222 | assert.equal(at.saver.version, '0') |
| 223 | assert.equal(at.saver.conflict, null) |
| 224 | }) |
| 225 | |
| 226 | test('leaving: a conflict stays with the choice; a failed flush offers Leave anyway; a stored draft leaves', async () => { |
| 227 | const conflicted = page(island('One.'), url => url.endsWith('/save') |
| 228 | ? ok({ saved: false, href: '/p/acme/conflict' }) |
| 229 | : ok({ version: '11', merge: { against: '200', conflict: true, hunks: [] } })) |
| 230 | conflicted.type(' a') |
| 231 | await conflicted.saver.leave('/x') |
| 232 | assert.deepEqual(conflicted.navigated, []) |
| 233 | assert.equal(conflicted.shown.at(-1).leaving, 'conflict') |
| 234 | const offline = page(island('One.'), () => { throw new Error('offline') }) |
| 235 | offline.type(' a') |
| 236 | await offline.saver.leave('/x') |
| 237 | assert.deepEqual(offline.navigated, []) |
| 238 | assert.equal(offline.shown.at(-1).leaving, 'failed') |
| 239 | offline.shown.at(-1).go() |
| 240 | assert.deepEqual(offline.navigated, ['/x']) |
| 241 | const stored = page(island('One.'), url => { |
| 242 | if (url.endsWith('/save')) throw new Error('lost') |
| 243 | return ok({ version: '11' }) |
| 244 | }) |
| 245 | stored.type(' a') |
| 246 | await stored.saver.leave('/x') |
| 247 | assert.deepEqual(stored.navigated, ['/x']) |
| 248 | }) |
| 249 | |
| 250 | test('a stale tab shows the newer draft, offered, and never overwrites it', async () => { |
| 251 | const storage = store() |
| 252 | const at = page(island('One.'), () => ({ status: 409, json: { stale: true, committed: false, body: 'Newer.', version: '50' } }), { storage }) |
| 253 | at.type(' a') |
| 254 | await at.saver.autosave() |
| 255 | assert.equal(at.ed.current(), 'Newer.') |
| 256 | assert.equal(at.saver.version, '50') |
| 257 | assert.ok(at.saver.offered && !at.saver.own) |
| 258 | assert.equal(storage.get('mapa-draft:/p/acme/draft'), null) |
| 259 | }) |
| 260 | |
| 261 | test('the first scratch commit: empty creates nothing, then one page, then drafts on it', async () => { |
| 262 | const at = page(island('', { drafts: '/scratch', first: true, page: '', base: '0' }), (url, fields, count) => { |
| 263 | if (url === '/scratch' && count === 1) return ok({ href: '/p/new1', page: 'new1', revision: '400' }) |
| 264 | return ok({ version: '21', revision: '400' }) |
| 265 | }) |
| 266 | await at.saver.autosave(true) |
| 267 | assert.equal(at.net.sent.length, 0, 'an empty page sends nothing') |
| 268 | at.type('Hello') |
| 269 | await at.saver.autosave() |
| 270 | assert.deepEqual(at.net.sent[0], { url: '/scratch', keepalive: false, fields: { first: 'TOKENTOKENTOKEN0001', version: '0', body: 'Hello' } }) |
| 271 | assert.equal(at.saver.drafts, '/p/new1/draft') |
| 272 | assert.equal(at.saver.base, '400') |
| 273 | at.type(' again') |
| 274 | await at.saver.autosave() |
| 275 | assert.deepEqual(at.net.sent[1], { url: '/p/new1/draft', keepalive: false, fields: { base: '400', version: '0', body: 'Hello again' } }) |
| 276 | }) |
| 277 | |
| 278 | test('noBase and gone lead to the existing choices', async () => { |
| 279 | const noBase = page(island('One.'), () => ok({ version: '11', merge: { noBase: true } })) |
| 280 | noBase.type(' a') |
| 281 | await noBase.saver.autosave() |
| 282 | assert.deepEqual(noBase.shown, ['noBase']) |
| 283 | const gone = page(island('One.'), () => ok({ version: '11', merge: { gone: true } })) |
| 284 | gone.type(' a') |
| 285 | await gone.saver.autosave() |
| 286 | assert.deepEqual(gone.shown, ['gone']) |
| 287 | assert.ok(gone.saver.gone && !gone.saver.stopped) |
| 288 | }) |
| 289 | |
| 290 | test('a commit that merged is applied when nothing was typed since', async () => { |
| 291 | const at = page(island('One.\n\nTwo.'), url => url.endsWith('/save') |
| 292 | ? ok({ saved: true, merged: true, revision: '500', body: 'One. a\n\nTwo, Bo.' }) |
| 293 | : ok({ version: '11' })) |
| 294 | at.type(' a') |
| 295 | assert.equal(await at.saver.commit(), 'merged') |
| 296 | assert.equal(at.ed.current(), 'One. a\n\nTwo, Bo.') |
| 297 | assert.equal(at.saver.base, '500') |
| 298 | assert.equal(at.saver.version, '0') |
| 299 | }) |
| 300 | |
| 301 | test('back from the cache: nothing unsaved reloads, unsaved text autosaves at once', async () => { |
| 302 | const clean = page(island('One.'), () => ok({ version: '1' })) |
| 303 | clean.saver.shown() |
| 304 | assert.deepEqual(clean.navigated, ['reload']) |
| 305 | const typed = page(island('One.'), () => ok({ version: '1' })) |
| 306 | typed.type(' a') |
| 307 | typed.saver.shown() |
| 308 | await new Promise(resolve => setTimeout(resolve, 0)) |
| 309 | assert.equal(typed.net.sent.length, 1) |
| 310 | }) |
| 311 | |
| 312 | |
| 313 | test('an own draft committed meanwhile goes on as a new draft', async () => { |
| 314 | const draft = { version: '9', base: '100' } |
| 315 | const answers = (url, fields, count) => count === 1 |
| 316 | ? { status: 409, json: { stale: true, committed: true } } |
| 317 | : ok({ version: '30' }) |
| 318 | const own = { storage: store({ 'mapa-draft:/p/acme/draft': '9' }) } |
| 319 | // The page now holds exactly the draft: the new draft starts there. |
| 320 | const at = page(island('Mine.', { draft }), answers, own) |
| 321 | at.saver.deps.refetch = async () => ({ source: 'Mine.', base: '101', draft: null }) |
| 322 | at.type(' more') |
| 323 | await at.saver.autosave() |
| 324 | await at.clock.run(ms => ms === 0) |
| 325 | assert.deepEqual(at.net.sent[1].fields, { base: '101', version: '0', body: 'Mine. more' }) |
| 326 | // A teammate's text was merged into it: the old base, so the merge |
| 327 | // keeps both. |
| 328 | const merged = page(island('Mine.', { draft }), answers, { storage: store({ 'mapa-draft:/p/acme/draft': '9' }) }) |
| 329 | merged.saver.deps.refetch = async () => ({ source: 'Mine.\n\nBo.', base: '102', draft: null }) |
| 330 | merged.type(' more') |
| 331 | await merged.saver.autosave() |
| 332 | await merged.clock.run(ms => ms === 0) |
| 333 | assert.equal(merged.net.sent[1].fields.base, '100') |
| 334 | }) |
| 335 | |
| 336 | test('a reload keeps a stored draft; unsent text still commits', () => { |
| 337 | const draft = { version: '9', base: '100' } |
| 338 | const own = () => ({ storage: store({ 'mapa-draft:/p/acme/draft': '9' }) }) |
| 339 | const stored = page(island('Mine.', { draft }), () => ok({}), own()) |
| 340 | stored.saver.closing({ reloading: true }) |
| 341 | assert.equal(stored.net.sent.length, 0) |
| 342 | const unsent = page(island('Mine.', { draft }), () => ok({}), own()) |
| 343 | unsent.type(' more') |
| 344 | unsent.saver.closing({ reloading: true }) |
| 345 | assert.equal(unsent.net.sent[0].fields.commit, '1') |
| 346 | }) |
| 347 | |
| 348 | // Fable's final review of 58fd476: each case below fails on the saver |
| 349 | // as it was. |
| 350 | |
| 351 | const settle = () => new Promise(resolve => setTimeout(resolve, 0)) |
| 352 | |
| 353 | test('a page deleted mid-edit: what is typed is still stored as the draft, and nothing commits (H1)', async () => { |
| 354 | const at = page(island('One.'), url => url.endsWith('/save') |
| 355 | ? ok({ saved: true, merged: false, revision: '101' }) |
| 356 | : ok({ version: String(10 + at.net.sent.length), merge: { gone: true } })) |
| 357 | at.type(' a') |
| 358 | await at.saver.autosave() |
| 359 | assert.deepEqual(at.shown, ['gone']) |
| 360 | at.type(' b') |
| 361 | await at.clock.run(ms => ms === 1200) |
| 362 | assert.equal(at.net.sent.at(-1).fields.body, 'One. a b', 'typing after the delete is stored') |
| 363 | assert.equal(at.statuses.at(-1), 'draft', 'the status says it is a draft, not saved to the page') |
| 364 | assert.equal(await at.saver.commit({ explicit: true }), 'gone') |
| 365 | await at.saver.leave('/elsewhere') |
| 366 | assert.deepEqual(at.navigated, ['/elsewhere']) |
| 367 | assert.ok(!at.net.sent.some(request => request.url.endsWith('/save') || request.fields.commit), 'nothing commits') |
| 368 | at.type(' c') |
| 369 | at.saver.closing() |
| 370 | assert.deepEqual(at.net.sent.at(-1), { |
| 371 | url: '/p/acme/draft', keepalive: true, fields: { base: '100', version: at.saver.version, body: 'One. a b c' } |
| 372 | }) |
| 373 | assert.deepEqual(at.shown, ['gone'], 'one bar') |
| 374 | }) |
| 375 | |
| 376 | test('an offered draft committed in another tab: typing goes on as a new draft (H1)', async () => { |
| 377 | const draft = { version: '9', base: '100' } |
| 378 | const at = page(island('Offered.', { draft }), (url, fields, count) => count === 1 |
| 379 | ? { status: 409, json: { stale: true, committed: true } } |
| 380 | : ok({ version: '31', revision: '101' })) |
| 381 | at.saver.deps.refetch = async () => ({ source: 'Offered.', base: '101', draft: null }) |
| 382 | await at.saver.autosave(true) |
| 383 | await settle() |
| 384 | assert.deepEqual(at.shown, ['committed']) |
| 385 | at.type(' more') |
| 386 | await at.clock.run(ms => ms === 1200) |
| 387 | assert.deepEqual(at.net.sent.at(-1).fields, { base: '101', version: '0', body: 'Offered. more' }) |
| 388 | }) |
| 389 | |
| 390 | test('a commit reply without a revision is not a commit; an emptied day starts afresh (H2)', async () => { |
| 391 | const day = { drafts: '/d/2026-11-21/draft', page: '', base: '0' } |
| 392 | const old = page(island('', day), url => url.endsWith('/save') ? ok({ href: '/', saved: true }) : ok({ version: '7' })) |
| 393 | old.type('x') |
| 394 | assert.equal(await old.saver.commit(), 'commit-failed') |
| 395 | assert.equal(old.saver.base, '0') |
| 396 | assert.equal(old.saver.version, '7', 'the draft is still the one stored') |
| 397 | const own = { storage: store({ 'mapa-draft:/d/2026-11-21/draft': '5' }) } |
| 398 | const at = page(island('Soon gone.', { ...day, draft: { version: '5', base: '0' } }), (url, fields) => |
| 399 | url.endsWith('/save') ? ok({ href: '/d/2026-11-21', saved: false, empty: true }) : ok({ version: fields.body === '' ? '8' : '9' }), own) |
| 400 | at.view.dispatch(at.view.state.tr.delete(1, at.view.state.doc.firstChild.nodeSize - 1)) |
| 401 | at.saver.changed() |
| 402 | assert.equal(await at.saver.commit(), 'empty') |
| 403 | assert.equal(at.saver.version, '0') |
| 404 | at.type('New.') |
| 405 | await at.saver.autosave() |
| 406 | assert.deepEqual(at.net.sent.at(-1).fields, { base: '0', version: '0', body: 'New.' }) |
| 407 | assert.equal(at.ed.current(), 'New.', 'the text typed is still there') |
| 408 | }) |
| 409 | |
| 410 | test('Resolve stores what was typed first, and takes no typing until its answer is applied (M1)', async () => { |
| 411 | const hunks = [{ at: 0, count: 1, base: 'One.', mine: 'One. a', theirs: 'One, as Bo.' }] |
| 412 | let release = null |
| 413 | const at = page(island('One.\n\nTwo.'), (url, fields) => { |
| 414 | if (url.endsWith('/resolve')) { |
| 415 | return new Promise(resolve => { |
| 416 | release = () => resolve(ok({ outcome: 'saved', revision: '300', body: fields.version === '12' ? 'One. a\n\nTwo. b' : 'One. a\n\nTwo.' })) |
| 417 | }) |
| 418 | } |
| 419 | return ok({ version: fields.body.includes('b') ? '12' : '11', merge: { against: '200', conflict: true, hunks } }) |
| 420 | }) |
| 421 | at.type(' a') |
| 422 | await at.saver.autosave() |
| 423 | at.typeAt(1, ' b') |
| 424 | const resolving = at.saver.resolve(['mine']) |
| 425 | while (!release) await settle() |
| 426 | at.typeAt(1, ' c') |
| 427 | assert.equal(at.ed.current(), 'One. a\n\nTwo. b', 'typing while Resolve is out is refused') |
| 428 | release() |
| 429 | assert.equal(await resolving, 'saved') |
| 430 | const posted = at.net.sent.find(request => request.url === '/p/acme/resolve') |
| 431 | assert.equal(posted.fields.version, '12', 'the typing was stored before the choice was posted') |
| 432 | assert.equal(at.ed.current(), 'One. a\n\nTwo. b') |
| 433 | at.typeAt(1, ' d') |
| 434 | assert.equal(at.ed.current(), 'One. a\n\nTwo. b d', 'typing is taken again') |
| 435 | }) |
| 436 | |
| 437 | test('closing with a choice open, or commits held, still writes the text, never commits (M2)', async () => { |
| 438 | const hunks = [{ at: 0, count: 1, base: 'One.', mine: 'One. a', theirs: 'One, Bo.' }] |
| 439 | const at = page(island('One.\n\nTwo.'), () => ok({ version: '11', merge: { against: '200', conflict: true, hunks } })) |
| 440 | at.type(' a') |
| 441 | await at.saver.autosave() |
| 442 | at.typeAt(1, ' b') |
| 443 | at.saver.closing() |
| 444 | assert.deepEqual(at.net.sent.at(-1), { |
| 445 | url: '/p/acme/draft', keepalive: true, fields: { base: '100', version: '11', body: 'One. a\n\nTwo. b' } |
| 446 | }) |
| 447 | const look = page(island('One.'), () => ok({ version: '11' })) |
| 448 | look.type(' a') |
| 449 | look.saver.hold() |
| 450 | assert.equal(await look.saver.commit(), 'held') |
| 451 | look.type(' b') |
| 452 | look.saver.closing() |
| 453 | assert.deepEqual(look.net.sent.map(request => request.fields), [ |
| 454 | { base: '100', version: '0', body: 'One. a' }, |
| 455 | { base: '100', version: '11', body: 'One. a b' } |
| 456 | ]) |
| 457 | }) |
| 458 | |
| 459 | test('closing inside the settling window sends the loaded version with settling=1 (M3)', () => { |
| 460 | const draft = { version: '9', base: '100' } |
| 461 | const at = page(island('Mine.', { draft }), () => ok({}), |
| 462 | { storage: store({ 'mapa-draft:/p/acme/draft': '9', 'mapa-closing:/p/acme/draft': '9' }) }) |
| 463 | assert.ok(at.saver.closed) |
| 464 | at.type(' more') |
| 465 | at.saver.closing() |
| 466 | assert.deepEqual(at.net.sent[0].fields, |
| 467 | { commit: '1', version: '9', base: '100', body: 'Mine. more', settling: '1' }) |
| 468 | }) |
| 469 | |
| 470 | test('a clean merge not applied because of typing still ends the choice on show (M4)', async () => { |
| 471 | const hunks = [{ at: 0, count: 1, base: 'One.', mine: 'One. a', theirs: 'One, Bo.' }] |
| 472 | let release = null |
| 473 | const at = page(island('One.\n\nTwo.'), (url, fields, count) => { |
| 474 | if (count === 1) return ok({ version: '11', merge: { against: '200', conflict: true, hunks } }) |
| 475 | if (url.endsWith('/save')) return ok({ saved: true, merged: false, revision: '202' }) |
| 476 | if (count > 2) return ok({ version: '13', revision: '201' }) |
| 477 | return new Promise(resolve => { |
| 478 | release = () => resolve(ok({ version: '12', merge: { against: '201', body: 'One. a\n\nTwo. b' } })) |
| 479 | }) |
| 480 | }) |
| 481 | at.type(' a') |
| 482 | await at.saver.autosave() |
| 483 | assert.ok(at.saver.conflict) |
| 484 | at.typeAt(1, ' b') |
| 485 | const second = at.saver.autosave() |
| 486 | while (!release) await settle() |
| 487 | at.typeAt(1, ' c') |
| 488 | release() |
| 489 | await second |
| 490 | assert.equal(at.saver.conflict, null) |
| 491 | assert.equal(at.shown.at(-1), 'settled') |
| 492 | assert.equal(await at.saver.commit(), 'saved', 'commits are no longer blocked') |
| 493 | }) |
| 494 | |
| 495 | // Fable's re-review of 14ab6a6 (round 4). |
| 496 | |
| 497 | test('settling finds the draft committed with a teammate\'s text: a close still writes (M-A)', async () => { |
| 498 | const draft = { version: '9', base: '100' } |
| 499 | const at = page(island('Mine.', { draft }), () => ok({}), |
| 500 | { storage: store({ 'mapa-draft:/p/acme/draft': '9', 'mapa-closing:/p/acme/draft': '9' }) }) |
| 501 | at.saver.deps.refetch = async () => ({ source: 'Mine.\n\nBo.', base: '102', draft: null }) |
| 502 | await at.saver.settle() |
| 503 | at.type(' more') |
| 504 | at.saver.closing() |
| 505 | assert.deepEqual(at.net.sent[0].fields, |
| 506 | { commit: '1', version: '0', base: '100', body: 'Mine. more' }) |
| 507 | }) |
| 508 | |
| 509 | test('settling finds another tab\'s newer draft: the tab shows it before any typing', async () => { |
| 510 | const draft = { version: '9', base: '100' } |
| 511 | const at = page(island('Mine.', { draft }), () => ok({ version: '41' }), |
| 512 | { storage: store({ 'mapa-draft:/p/acme/draft': '9', 'mapa-closing:/p/acme/draft': '9' }) }) |
| 513 | at.saver.deps.refetch = async () => ({ source: 'Other tab.', base: '100', draft: { version: '40', base: '100' } }) |
| 514 | await at.saver.settle() |
| 515 | assert.equal(at.ed.current(), 'Other tab.') |
| 516 | at.type(' more') |
| 517 | await at.saver.autosave() |
| 518 | assert.deepEqual(at.net.sent.at(-1).fields, { base: '100', version: '40', body: 'Other tab. more' }) |
| 519 | }) |
| 520 | |
| 521 | test('a page deleted before the first autosave: the text is offered as a scratch page, and a close makes one (M-B)', async () => { |
| 522 | const at = page(island('One.'), () => ({ status: 404, json: null })) |
| 523 | at.type(' a') |
| 524 | await at.saver.autosave() |
| 525 | assert.deepEqual(at.shown, ['orphan']) |
| 526 | assert.equal(at.statuses.at(-1), 'orphan') |
| 527 | at.type(' b') |
| 528 | await at.clock.run(ms => ms === 1200) |
| 529 | assert.equal(at.net.sent.length, 1, 'nothing more is sent to a route that refuses it') |
| 530 | assert.equal(await at.saver.commit({ explicit: true }), 'orphan') |
| 531 | await at.saver.leave('/elsewhere') |
| 532 | assert.deepEqual(at.navigated, []) |
| 533 | assert.equal(at.shown.at(-1).leaving, 'orphan') |
| 534 | at.saver.closing() |
| 535 | assert.deepEqual(at.net.sent.at(-1), { url: '/scratch', keepalive: true, fields: { body: 'One. a b' } }) |
| 536 | }) |
| 537 | |
| 538 | test('an autosave that never answered but landed: its 409 keeps the typing since (L3)', async () => { |
| 539 | let round = 0 |
| 540 | const at = page(island('One.'), (url, fields) => { |
| 541 | round++ |
| 542 | if (round === 1) throw new Error('aborted after 10s') |
| 543 | if (round === 2) return { status: 409, json: { stale: true, committed: false, body: 'One. a', version: '7' } } |
| 544 | return ok({ version: '8' }) |
| 545 | }) |
| 546 | at.type(' a') |
| 547 | await at.saver.autosave() |
| 548 | at.type(' b') |
| 549 | await at.saver.autosave() |
| 550 | assert.equal(at.ed.current(), 'One. a b', 'the typing is not replaced') |
| 551 | await at.clock.run(ms => ms === 0) |
| 552 | assert.deepEqual(at.net.sent.at(-1).fields, { base: '100', version: '7', body: 'One. a b' }) |
| 553 | }) |
| 554 | |