File size: 7,147 Bytes
47a5323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { merge, DocumentSync } = require('../media/document-sync');
const { saveDocument } = require('../file-sync');

function document(text = 'alpha\nbeta\ngamma\n') {
  const sent = [], updates = [];
  const doc = new DocumentSync('main.tex', text, { send: m => sent.push(m), update: t => updates.push(t) });
  return { doc, sent, updates };
}
function file(t, text = 'alpha\nbeta\ngamma\n') {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'overleaf-sync-'));
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const p = path.join(dir, 'main.tex'); fs.writeFileSync(p, text);
  return p;
}

test('force reload pauses saves and ignores obsolete acknowledgements and watcher text', () => {
  const { doc, sent } = document('base');
  doc.edit('first draft');
  const oldId = sent[0].id;
  doc.edit('latest draft');
  doc.beginReload();
  doc.flush();
  doc.saved({ id: oldId, text: 'first draft' });
  doc.external('stale watcher');
  assert.equal(doc.text, 'latest draft');
  assert.equal(sent.length, 1);
  assert.equal(doc.dirty, true);
  doc.reset('fresh disk');
  doc.saved({ id: oldId, text: 'first draft' });
  assert.equal(doc.text, 'fresh disk');
  assert.equal(doc.dirty, false);
  assert.equal(sent.length, 1);
  doc.edit('fresh disk plus edit');
  assert.equal(sent[1].base, 'fresh disk');
});

test('a keystroke sends a save immediately, without waiting for a debounce', () => {
  const { doc, sent } = document('a'); doc.edit('ab');
  assert.equal(sent.length, 1); assert.equal(sent[0].base, 'a');
  assert.equal(sent[0].text, 'ab'); assert.equal(doc.dirty, true);
});
test('typing during an in-flight save is sent after its acknowledgement', () => {
  const { doc, sent } = document('a'); doc.edit('ab'); doc.edit('abc');
  assert.equal(sent.length, 1);
  doc.saved({ id: sent[0].id, text: 'ab' });
  assert.equal(doc.dirty, true); assert.equal(sent[1].base, 'ab'); assert.equal(sent[1].text, 'abc');
  doc.saved({ id: sent[1].id, text: 'abc' }); assert.equal(doc.dirty, false);
});
test('an obsolete or duplicate acknowledgement cannot clear a newer edit', () => {
  const { doc, sent } = document('a'); doc.edit('ab'); doc.edit('abc');
  doc.saved({ id: sent[0].id, text: 'ab' });
  doc.saved({ id: sent[0].id, text: 'ab' });
  assert.equal(doc.text, 'abc'); assert.equal(doc.flight.id, sent[1].id);
});
test('external changes update a clean editor and are never written back', () => {
  const { doc, sent, updates } = document('old'); doc.external('new');
  assert.equal(doc.text, 'new'); assert.equal(doc.base, 'new');
  assert.equal(sent.length, 0); assert.deepEqual(updates, ['new']);
});
test('a stale editor merges an unrelated edit without restoring the old paragraph', t => {
  const p = file(t), base = fs.readFileSync(p, 'utf8');
  fs.writeFileSync(p, 'NEW PARAGRAPH\nbeta\ngamma\n');
  const result = saveDocument(p, base, 'alpha\nbeta\ngamma!\n');
  assert.equal(result.text, 'NEW PARAGRAPH\nbeta\ngamma!\n');
  assert.equal(fs.readFileSync(p, 'utf8'), result.text);
});
test('overlapping edits leave disk unchanged and retain the most recent typing', t => {
  const p = file(t, 'old'), { doc, sent } = document('old');
  doc.edit('mine'); doc.edit('mine newest'); fs.writeFileSync(p, 'external');
  assert.equal(saveDocument(p, sent[0].base, sent[0].text).conflict, true);
  doc.failed({ id: sent[0].id });
  assert.equal(fs.readFileSync(p, 'utf8'), 'external'); assert.equal(doc.blocked, true);
  assert.equal(sent[1].type, 'recoverDraft'); assert.equal(sent[1].text, 'mine newest');
  doc.flush(); assert.equal(sent.length, 2);
});
test('an external notification preceding an ack cannot roll the ack back', () => {
  const { doc, sent } = document('a\nz'); doc.edit('ab\nz');
  doc.external('a\nNEW'); doc.saved({ id: sent[0].id, text: 'ab\nNEW' });
  assert.equal(doc.text, 'ab\nNEW'); assert.equal(doc.dirty, false);
});
test('pending typing survives a backend three-way merge', () => {
  const { doc, sent } = document('a\nz'); doc.edit('ab\nz'); doc.edit('abc\nz');
  doc.saved({ id: sent[0].id, text: 'ab\nNEW' });
  assert.equal(doc.text, 'abc\nNEW'); assert.equal(sent[1].base, 'ab\nNEW');
});
test('pending typing that overlaps a backend merge becomes a recoverable conflict', () => {
  const { doc, sent } = document('a\nz'); doc.edit('ab\nz'); doc.edit('ab\nlocal');
  doc.saved({ id: sent[0].id, text: 'ab\nexternal' });
  assert.equal(doc.blocked, true); assert.equal(sent[1].text, 'ab\nlocal');
});
test('failed writes remain dirty and request durable recovery', () => {
  const { doc, sent } = document('a'); doc.edit('ab');
  doc.failed({ id: sent[0].id, error: 'EACCES' });
  assert.equal(doc.dirty, true); assert.equal(doc.error, 'EACCES');
  assert.equal(sent[1].type, 'recoverDraft');
});
test('unsaved drafts restored after reload merge against current disk', () => {
  const { doc, sent } = document('a\nexternal');
  doc.restore('a\nz', 'ab\nz');
  assert.equal(doc.text, 'ab\nexternal'); assert.equal(sent[0].base, 'a\nexternal');
});
test('a restored draft with conflicting edits is preserved instead of auto-overwriting', () => {
  const { doc, sent } = document('external'); doc.restore('old', 'local');
  assert.equal(doc.blocked, true); assert.equal(sent[0].type, 'recoverDraft');
});
test('deleted files are not recreated by a stale save', t => {
  const p = file(t); fs.unlinkSync(p);
  assert.throws(() => saveDocument(p, 'old', 'local'), { code: 'ENOENT' });
  assert.equal(fs.existsSync(p), false);
});
test('atomic writes preserve symlinks and file permissions', t => {
  const p = file(t, 'old'), link = path.join(path.dirname(p), 'link.tex');
  fs.chmodSync(p, 0o647); fs.symlinkSync(p, link);
  saveDocument(link, 'old', 'new');
  assert.equal(fs.lstatSync(link).isSymbolicLink(), true);
  assert.equal(fs.statSync(p).mode & 0o777, 0o647); assert.equal(fs.readFileSync(link, 'utf8'), 'new');
});
test('an external write during temp-file preparation is detected before replace', t => {
  const p = file(t, 'old'); let injected = false;
  const io = Object.create(fs);
  io.writeFileSync = (...args) => {
    fs.writeFileSync(...args);
    if (!injected) { injected = true; fs.writeFileSync(p, 'EXTERNAL'); }
  };
  assert.equal(saveDocument(p, 'old', 'LOCAL', io).conflict, true);
  assert.equal(fs.readFileSync(p, 'utf8'), 'EXTERNAL');
  assert.deepEqual(fs.readdirSync(path.dirname(p)), ['main.tex']);
});
test('legacy unversioned saves cannot bypass conflict detection', t => {
  const p = file(t, 'external');
  assert.throws(() => saveDocument(p, undefined, 'old'), /protocol mismatch/);
  assert.equal(fs.readFileSync(p, 'utf8'), 'external');
});
test('merge handles identical edits, non-overlapping insertions, and conservative overlaps', () => {
  assert.equal(merge('abcd', 'aXbcd', 'abcYd'), 'aXbcYd');
  assert.equal(merge('abc', 'aXbc', 'aXbc'), 'aXbc');
  assert.equal(merge('abc', 'aXbc', 'aYbc'), null);
  assert.equal(merge('abc', 'aXc', 'aYc'), null);
});