ApplyingUpdatesToADocTests.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { setTimeout } = require('node:timers/promises')
  4. const Settings = require('@overleaf/settings')
  5. const rclientProjectHistory = require('@overleaf/redis-wrapper').createClient(
  6. Settings.redis.project_history
  7. )
  8. const rclientDU = require('@overleaf/redis-wrapper').createClient(
  9. Settings.redis.documentupdater
  10. )
  11. const Keys = Settings.redis.documentupdater.key_schema
  12. const ProjectHistoryKeys = Settings.redis.project_history.key_schema
  13. const MockWebApi = require('./helpers/MockWebApi')
  14. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  15. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  16. const { RequestFailedError } = require('@overleaf/fetch-utils')
  17. async function sendUpdateAndWait(projectId, docId, update) {
  18. await DocUpdaterClient.sendUpdate(projectId, docId, update)
  19. // It seems that we need to wait for a little while
  20. await setTimeout(200)
  21. }
  22. describe('Applying updates to a doc', function () {
  23. beforeEach(async function () {
  24. sinon.spy(MockWebApi, 'getDocument')
  25. this.lines = ['one', 'two', 'three']
  26. this.version = 42
  27. this.op = {
  28. i: 'one and a half\n',
  29. p: 4,
  30. }
  31. this.project_id = DocUpdaterClient.randomId()
  32. this.doc_id = DocUpdaterClient.randomId()
  33. this.update = {
  34. doc: this.doc_id,
  35. op: [this.op],
  36. v: this.version,
  37. }
  38. this.historyOTUpdate = {
  39. doc: this.doc_id,
  40. op: [{ textOperation: [4, 'one and a half\n', 9] }],
  41. v: this.version,
  42. meta: { source: 'random-publicId' },
  43. }
  44. this.result = ['one', 'one and a half', 'two', 'three']
  45. await DocUpdaterApp.ensureRunning()
  46. })
  47. afterEach(function () {
  48. sinon.restore()
  49. })
  50. describe('when the document is not loaded', function () {
  51. beforeEach(async function () {
  52. this.startTime = Date.now()
  53. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  54. lines: this.lines,
  55. version: this.version,
  56. })
  57. await sendUpdateAndWait(this.project_id, this.doc_id, this.update)
  58. const result = await rclientProjectHistory.get(
  59. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  60. project_id: this.project_id,
  61. })
  62. )
  63. this.firstOpTimestamp = parseInt(result, 10)
  64. })
  65. it('should load the document from the web API', function () {
  66. MockWebApi.getDocument
  67. .calledWith(this.project_id, this.doc_id)
  68. .should.equal(true)
  69. })
  70. it('should update the doc', async function () {
  71. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  72. doc.lines.should.deep.equal(this.result)
  73. })
  74. it('should push the applied updates to the project history changes api', function (done) {
  75. rclientProjectHistory.lrange(
  76. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  77. 0,
  78. -1,
  79. (error, updates) => {
  80. if (error != null) {
  81. throw error
  82. }
  83. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  84. done()
  85. }
  86. )
  87. })
  88. it('should set the first op timestamp', function () {
  89. this.firstOpTimestamp.should.be.within(this.startTime, Date.now())
  90. })
  91. it('should yield last updated time', async function () {
  92. const { lastUpdatedAt } = await DocUpdaterClient.getProjectLastUpdatedAt(
  93. this.project_id
  94. )
  95. lastUpdatedAt.should.be.within(this.startTime, Date.now())
  96. })
  97. it('should yield no last updated time for another project', async function () {
  98. const body = await DocUpdaterClient.getProjectLastUpdatedAt(
  99. DocUpdaterClient.randomId()
  100. )
  101. body.should.deep.equal({})
  102. })
  103. describe('when sending another update', function () {
  104. beforeEach(async function () {
  105. this.timeout(10000)
  106. this.second_update = Object.assign({}, this.update)
  107. this.second_update.v = this.version + 1
  108. this.secondStartTime = Date.now()
  109. await sendUpdateAndWait(
  110. this.project_id,
  111. this.doc_id,
  112. this.second_update
  113. )
  114. })
  115. it('should update the doc', async function () {
  116. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  117. doc.lines.should.deep.equal([
  118. 'one',
  119. 'one and a half',
  120. 'one and a half',
  121. 'two',
  122. 'three',
  123. ])
  124. })
  125. it('should not change the first op timestamp', function (done) {
  126. rclientProjectHistory.get(
  127. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  128. project_id: this.project_id,
  129. }),
  130. (error, result) => {
  131. if (error != null) {
  132. throw error
  133. }
  134. result = parseInt(result, 10)
  135. result.should.equal(this.firstOpTimestamp)
  136. done()
  137. }
  138. )
  139. })
  140. it('should yield last updated time', async function () {
  141. const { lastUpdatedAt } =
  142. await DocUpdaterClient.getProjectLastUpdatedAt(this.project_id)
  143. lastUpdatedAt.should.be.within(this.secondStartTime, Date.now())
  144. })
  145. })
  146. describe('when another client is sending a concurrent update', function () {
  147. beforeEach(async function () {
  148. this.timeout(10000)
  149. this.otherUpdate = {
  150. doc: this.doc_id,
  151. op: [{ p: 8, i: 'two and a half\n' }],
  152. v: this.version,
  153. meta: { source: 'other-random-publicId' },
  154. }
  155. this.secondStartTime = Date.now()
  156. await sendUpdateAndWait(this.project_id, this.doc_id, this.otherUpdate)
  157. })
  158. it('should update the doc', async function () {
  159. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  160. doc.lines.should.deep.equal([
  161. 'one',
  162. 'one and a half',
  163. 'two',
  164. 'two and a half',
  165. 'three',
  166. ])
  167. })
  168. it('should not change the first op timestamp', function (done) {
  169. rclientProjectHistory.get(
  170. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  171. project_id: this.project_id,
  172. }),
  173. (error, result) => {
  174. if (error != null) {
  175. throw error
  176. }
  177. result = parseInt(result, 10)
  178. result.should.equal(this.firstOpTimestamp)
  179. done()
  180. }
  181. )
  182. })
  183. it('should yield last updated time', async function () {
  184. const { lastUpdatedAt } =
  185. await DocUpdaterClient.getProjectLastUpdatedAt(this.project_id)
  186. lastUpdatedAt.should.be.within(this.secondStartTime, Date.now())
  187. })
  188. })
  189. })
  190. describe('when the document is not loaded (history-ot)', function () {
  191. beforeEach(async function () {
  192. this.startTime = Date.now()
  193. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  194. lines: this.lines,
  195. version: this.version,
  196. otMigrationStage: 1,
  197. })
  198. await sendUpdateAndWait(
  199. this.project_id,
  200. this.doc_id,
  201. this.historyOTUpdate
  202. )
  203. const result = await rclientProjectHistory.get(
  204. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  205. project_id: this.project_id,
  206. })
  207. )
  208. this.firstOpTimestamp = parseInt(result, 10)
  209. })
  210. it('should load the document from the web API', function () {
  211. MockWebApi.getDocument
  212. .calledWith(this.project_id, this.doc_id)
  213. .should.equal(true)
  214. })
  215. it('should update the doc', async function () {
  216. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  217. doc.lines.should.deep.equal(this.result)
  218. })
  219. it('should push the applied updates to the project history changes api', function (done) {
  220. rclientProjectHistory.lrange(
  221. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  222. 0,
  223. -1,
  224. (error, updates) => {
  225. if (error != null) {
  226. throw error
  227. }
  228. JSON.parse(updates[0]).op.should.deep.equal(this.historyOTUpdate.op)
  229. JSON.parse(updates[0]).meta.pathname.should.equal('/a/b/c.tex')
  230. done()
  231. }
  232. )
  233. })
  234. it('should set the first op timestamp', function () {
  235. this.firstOpTimestamp.should.be.within(this.startTime, Date.now())
  236. })
  237. it('should yield last updated time', async function () {
  238. const { lastUpdatedAt } = await DocUpdaterClient.getProjectLastUpdatedAt(
  239. this.project_id
  240. )
  241. lastUpdatedAt.should.be.within(this.startTime, Date.now())
  242. })
  243. it('should yield no last updated time for another project', async function () {
  244. const body = await DocUpdaterClient.getProjectLastUpdatedAt(
  245. DocUpdaterClient.randomId()
  246. )
  247. body.should.deep.equal({})
  248. })
  249. describe('when sending another update', function () {
  250. beforeEach(async function () {
  251. this.timeout(10000)
  252. this.second_update = Object.assign({}, this.historyOTUpdate)
  253. this.second_update.op = [
  254. {
  255. textOperation: [4, 'one and a half\n', 24],
  256. },
  257. ]
  258. this.second_update.v = this.version + 1
  259. this.secondStartTime = Date.now()
  260. await sendUpdateAndWait(
  261. this.project_id,
  262. this.doc_id,
  263. this.second_update
  264. )
  265. })
  266. it('should update the doc', async function () {
  267. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  268. doc.lines.should.deep.equal([
  269. 'one',
  270. 'one and a half',
  271. 'one and a half',
  272. 'two',
  273. 'three',
  274. ])
  275. })
  276. it('should not change the first op timestamp', function (done) {
  277. rclientProjectHistory.get(
  278. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  279. project_id: this.project_id,
  280. }),
  281. (error, result) => {
  282. if (error != null) {
  283. throw error
  284. }
  285. result = parseInt(result, 10)
  286. result.should.equal(this.firstOpTimestamp)
  287. done()
  288. }
  289. )
  290. })
  291. it('should yield last updated time', async function () {
  292. const { lastUpdatedAt } =
  293. await DocUpdaterClient.getProjectLastUpdatedAt(this.project_id)
  294. lastUpdatedAt.should.be.within(this.secondStartTime, Date.now())
  295. })
  296. })
  297. describe('when another client is sending a concurrent update', function () {
  298. beforeEach(async function () {
  299. this.timeout(10000)
  300. this.otherUpdate = {
  301. doc: this.doc_id,
  302. op: [{ textOperation: [8, 'two and a half\n', 5] }],
  303. v: this.version,
  304. meta: { source: 'other-random-publicId' },
  305. }
  306. this.secondStartTime = Date.now()
  307. await sendUpdateAndWait(this.project_id, this.doc_id, this.otherUpdate)
  308. })
  309. it('should update the doc', async function () {
  310. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  311. doc.lines.should.deep.equal([
  312. 'one',
  313. 'one and a half',
  314. 'two',
  315. 'two and a half',
  316. 'three',
  317. ])
  318. })
  319. it('should not change the first op timestamp', function (done) {
  320. rclientProjectHistory.get(
  321. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  322. project_id: this.project_id,
  323. }),
  324. (error, result) => {
  325. if (error != null) {
  326. throw error
  327. }
  328. result = parseInt(result, 10)
  329. result.should.equal(this.firstOpTimestamp)
  330. done()
  331. }
  332. )
  333. })
  334. it('should yield last updated time', async function () {
  335. const { lastUpdatedAt } =
  336. await DocUpdaterClient.getProjectLastUpdatedAt(this.project_id)
  337. lastUpdatedAt.should.be.within(this.secondStartTime, Date.now())
  338. })
  339. })
  340. })
  341. describe('when the document is loaded', function () {
  342. beforeEach(async function () {
  343. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  344. lines: this.lines,
  345. version: this.version,
  346. })
  347. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  348. sinon.resetHistory()
  349. await sendUpdateAndWait(this.project_id, this.doc_id, this.update)
  350. })
  351. it('should not need to call the web api', function () {
  352. MockWebApi.getDocument.called.should.equal(false)
  353. })
  354. it('should update the doc', async function () {
  355. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  356. doc.lines.should.deep.equal(this.result)
  357. })
  358. it('should push the applied updates to the project history changes api', function (done) {
  359. rclientProjectHistory.lrange(
  360. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  361. 0,
  362. -1,
  363. (error, updates) => {
  364. if (error) return done(error)
  365. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  366. done()
  367. }
  368. )
  369. })
  370. })
  371. describe('when the document is loaded and is using project-history only', function () {
  372. beforeEach(async function () {
  373. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  374. lines: this.lines,
  375. version: this.version,
  376. })
  377. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  378. sinon.resetHistory()
  379. await sendUpdateAndWait(this.project_id, this.doc_id, this.update)
  380. })
  381. it('should update the doc', async function () {
  382. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  383. doc.lines.should.deep.equal(this.result)
  384. })
  385. it('should push the applied updates to the project history changes api', function (done) {
  386. rclientProjectHistory.lrange(
  387. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  388. 0,
  389. -1,
  390. (error, updates) => {
  391. if (error) return done(error)
  392. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  393. done()
  394. }
  395. )
  396. })
  397. })
  398. describe('when the document is loaded (history-ot)', function () {
  399. beforeEach(async function () {
  400. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  401. lines: this.lines,
  402. version: this.version,
  403. otMigrationStage: 1,
  404. })
  405. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  406. await sendUpdateAndWait(
  407. this.project_id,
  408. this.doc_id,
  409. this.historyOTUpdate
  410. )
  411. })
  412. it('should update the doc', async function () {
  413. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  414. doc.lines.should.deep.equal(this.result)
  415. })
  416. it('should push the applied updates to the project history changes api', function (done) {
  417. rclientProjectHistory.lrange(
  418. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  419. 0,
  420. -1,
  421. (error, updates) => {
  422. if (error) return done(error)
  423. JSON.parse(updates[0]).op.should.deep.equal(this.historyOTUpdate.op)
  424. JSON.parse(updates[0]).meta.pathname.should.equal('/a/b/c.tex')
  425. done()
  426. }
  427. )
  428. })
  429. })
  430. describe('when the document has been deleted', function () {
  431. describe('when the ops come in a single linear order', function () {
  432. beforeEach(async function () {
  433. const lines = ['', '', '']
  434. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  435. lines,
  436. version: 0,
  437. })
  438. this.updates = [
  439. { doc_id: this.doc_id, v: 0, op: [{ i: 'h', p: 0 }] },
  440. { doc_id: this.doc_id, v: 1, op: [{ i: 'e', p: 1 }] },
  441. { doc_id: this.doc_id, v: 2, op: [{ i: 'l', p: 2 }] },
  442. { doc_id: this.doc_id, v: 3, op: [{ i: 'l', p: 3 }] },
  443. { doc_id: this.doc_id, v: 4, op: [{ i: 'o', p: 4 }] },
  444. { doc_id: this.doc_id, v: 5, op: [{ i: ' ', p: 5 }] },
  445. { doc_id: this.doc_id, v: 6, op: [{ i: 'w', p: 6 }] },
  446. { doc_id: this.doc_id, v: 7, op: [{ i: 'o', p: 7 }] },
  447. { doc_id: this.doc_id, v: 8, op: [{ i: 'r', p: 8 }] },
  448. { doc_id: this.doc_id, v: 9, op: [{ i: 'l', p: 9 }] },
  449. { doc_id: this.doc_id, v: 10, op: [{ i: 'd', p: 10 }] },
  450. ]
  451. this.my_result = ['hello world', '', '']
  452. for (const update of this.updates.slice(0, 6)) {
  453. await DocUpdaterClient.sendUpdate(
  454. this.project_id,
  455. this.doc_id,
  456. update
  457. )
  458. }
  459. await DocUpdaterClient.deleteDoc(this.project_id, this.doc_id)
  460. for (const update of this.updates.slice(6)) {
  461. await DocUpdaterClient.sendUpdate(
  462. this.project_id,
  463. this.doc_id,
  464. update
  465. )
  466. }
  467. await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  468. })
  469. it('should be able to continue applying updates when the project has been deleted', async function () {
  470. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  471. doc.lines.should.deep.equal(this.my_result)
  472. })
  473. it('should store the doc ops in the correct order', function (done) {
  474. rclientDU.lrange(
  475. Keys.docOps({ doc_id: this.doc_id }),
  476. 0,
  477. -1,
  478. (error, updates) => {
  479. if (error) return done(error)
  480. updates = updates.map(u => JSON.parse(u))
  481. for (let i = 0; i < this.updates.length; i++) {
  482. const appliedUpdate = this.updates[i]
  483. appliedUpdate.op.should.deep.equal(updates[i].op)
  484. }
  485. done()
  486. }
  487. )
  488. })
  489. })
  490. describe('when older ops come in after the delete', function () {
  491. beforeEach(function () {
  492. const lines = ['', '', '']
  493. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  494. lines,
  495. version: 0,
  496. })
  497. this.updates = [
  498. { doc_id: this.doc_id, v: 0, op: [{ i: 'h', p: 0 }] },
  499. { doc_id: this.doc_id, v: 1, op: [{ i: 'e', p: 1 }] },
  500. { doc_id: this.doc_id, v: 2, op: [{ i: 'l', p: 2 }] },
  501. { doc_id: this.doc_id, v: 3, op: [{ i: 'l', p: 3 }] },
  502. { doc_id: this.doc_id, v: 4, op: [{ i: 'o', p: 4 }] },
  503. { doc_id: this.doc_id, v: 0, op: [{ i: 'world', p: 1 }] },
  504. ]
  505. this.my_result = ['hello', 'world', '']
  506. })
  507. it('should be able to continue applying updates when the project has been deleted', async function () {
  508. for (const update of this.updates.slice(0, 5)) {
  509. await DocUpdaterClient.sendUpdate(
  510. this.project_id,
  511. this.doc_id,
  512. update
  513. )
  514. }
  515. await DocUpdaterClient.deleteDoc(this.project_id, this.doc_id)
  516. for (const update of this.updates.slice(5)) {
  517. await DocUpdaterClient.sendUpdate(
  518. this.project_id,
  519. this.doc_id,
  520. update
  521. )
  522. }
  523. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  524. doc.lines.should.deep.equal(this.my_result)
  525. })
  526. })
  527. })
  528. describe('with a broken update', function () {
  529. beforeEach(async function () {
  530. this.broken_update = {
  531. doc: this.doc_id,
  532. v: this.version,
  533. op: [{ d: 'not the correct content', p: 0 }],
  534. }
  535. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  536. lines: this.lines,
  537. version: this.version,
  538. })
  539. DocUpdaterClient.subscribeToAppliedOps(
  540. (this.messageCallback = sinon.stub())
  541. )
  542. await sendUpdateAndWait(this.project_id, this.doc_id, this.broken_update)
  543. })
  544. it('should not update the doc', async function () {
  545. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  546. doc.lines.should.deep.equal(this.lines)
  547. })
  548. it('should send a message with an error', function () {
  549. this.messageCallback.called.should.equal(true)
  550. const [channel, message] = this.messageCallback.args[0]
  551. channel.should.equal('applied-ops')
  552. JSON.parse(message).should.deep.include({
  553. project_id: this.project_id,
  554. doc_id: this.doc_id,
  555. error: 'Delete component does not match',
  556. })
  557. })
  558. })
  559. describe('with a broken update (history-ot)', function () {
  560. beforeEach(async function () {
  561. this.broken_update = {
  562. doc: this.doc_id,
  563. v: this.version,
  564. op: [{ textOperation: [99, -1] }],
  565. meta: { source: '42' },
  566. }
  567. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  568. lines: this.lines,
  569. version: this.version,
  570. otMigrationStage: 1,
  571. })
  572. DocUpdaterClient.subscribeToAppliedOps(
  573. (this.messageCallback = sinon.stub())
  574. )
  575. await sendUpdateAndWait(this.project_id, this.doc_id, this.broken_update)
  576. })
  577. it('should not update the doc', async function () {
  578. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  579. doc.lines.should.deep.equal(this.lines)
  580. })
  581. it('should send a message with an error', function () {
  582. this.messageCallback.called.should.equal(true)
  583. const [channel, message] = this.messageCallback.args[0]
  584. channel.should.equal('applied-ops')
  585. JSON.parse(message).should.deep.include({
  586. project_id: this.project_id,
  587. doc_id: this.doc_id,
  588. error:
  589. "The operation's base length must be equal to the string's length.",
  590. })
  591. })
  592. })
  593. describe('when mixing ot types (sharejs-text-ot -> history-ot)', function () {
  594. beforeEach(async function () {
  595. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  596. lines: this.lines,
  597. version: this.version,
  598. otMigrationStage: 0,
  599. })
  600. DocUpdaterClient.subscribeToAppliedOps(
  601. (this.messageCallback = sinon.stub())
  602. )
  603. await sendUpdateAndWait(
  604. this.project_id,
  605. this.doc_id,
  606. this.historyOTUpdate
  607. )
  608. })
  609. it('should not update the doc', async function () {
  610. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  611. doc.lines.should.deep.equal(this.lines)
  612. })
  613. it('should send a message with an error', function () {
  614. this.messageCallback.called.should.equal(true)
  615. const [channel, message] = this.messageCallback.args[0]
  616. channel.should.equal('applied-ops')
  617. JSON.parse(message).should.deep.include({
  618. project_id: this.project_id,
  619. doc_id: this.doc_id,
  620. error: 'ot type mismatch',
  621. })
  622. })
  623. })
  624. describe('when mixing ot types (history-ot -> sharejs-text-ot)', function () {
  625. beforeEach(async function () {
  626. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  627. lines: this.lines,
  628. version: this.version,
  629. otMigrationStage: 1,
  630. })
  631. DocUpdaterClient.subscribeToAppliedOps(
  632. (this.messageCallback = sinon.stub())
  633. )
  634. await sendUpdateAndWait(this.project_id, this.doc_id, this.update)
  635. })
  636. it('should not update the doc', async function () {
  637. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  638. doc.lines.should.deep.equal(this.lines)
  639. })
  640. it('should send a message with an error', function () {
  641. this.messageCallback.called.should.equal(true)
  642. const [channel, message] = this.messageCallback.args[0]
  643. channel.should.equal('applied-ops')
  644. JSON.parse(message).should.deep.include({
  645. project_id: this.project_id,
  646. doc_id: this.doc_id,
  647. error: 'ot type mismatch',
  648. })
  649. })
  650. })
  651. describe('when there is no version in Mongo', function () {
  652. beforeEach(async function () {
  653. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  654. lines: this.lines,
  655. })
  656. const update = {
  657. doc: this.doc_id,
  658. op: this.update.op,
  659. v: 0,
  660. }
  661. await sendUpdateAndWait(this.project_id, this.doc_id, update)
  662. })
  663. it('should update the doc (using version = 0)', async function () {
  664. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  665. doc.lines.should.deep.equal(this.result)
  666. })
  667. })
  668. describe('when the sending duplicate ops', function () {
  669. beforeEach(async function () {
  670. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  671. lines: this.lines,
  672. version: this.version,
  673. })
  674. DocUpdaterClient.subscribeToAppliedOps(
  675. (this.messageCallback = sinon.stub())
  676. )
  677. // One user delete 'one', the next turns it into 'once'. The second becomes a NOP.
  678. await sendUpdateAndWait(this.project_id, this.doc_id, {
  679. doc: this.doc_id,
  680. op: [
  681. {
  682. i: 'one and a half\n',
  683. p: 4,
  684. },
  685. ],
  686. v: this.version,
  687. meta: {
  688. source: 'ikHceq3yfAdQYzBo4-xZ',
  689. },
  690. })
  691. await sendUpdateAndWait(this.project_id, this.doc_id, {
  692. doc: this.doc_id,
  693. op: [
  694. {
  695. i: 'one and a half\n',
  696. p: 4,
  697. },
  698. ],
  699. v: this.version,
  700. dupIfSource: ['ikHceq3yfAdQYzBo4-xZ'],
  701. meta: {
  702. source: 'ikHceq3yfAdQYzBo4-xZ',
  703. },
  704. })
  705. })
  706. it('should update the doc', async function () {
  707. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  708. doc.lines.should.deep.equal(this.result)
  709. })
  710. it('should return a message about duplicate ops', function () {
  711. this.messageCallback.calledTwice.should.equal(true)
  712. this.messageCallback.args[0][0].should.equal('applied-ops')
  713. expect(JSON.parse(this.messageCallback.args[0][1]).op.dup).to.be.undefined
  714. this.messageCallback.args[1][0].should.equal('applied-ops')
  715. expect(JSON.parse(this.messageCallback.args[1][1]).op.dup).to.equal(true)
  716. })
  717. })
  718. describe('when sending duplicate ops (history-ot)', function () {
  719. beforeEach(async function () {
  720. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  721. lines: this.lines,
  722. version: this.version,
  723. otMigrationStage: 1,
  724. })
  725. DocUpdaterClient.subscribeToAppliedOps(
  726. (this.messageCallback = sinon.stub())
  727. )
  728. // One user delete 'one', the next turns it into 'once'. The second becomes a NOP.
  729. await sendUpdateAndWait(this.project_id, this.doc_id, {
  730. doc: this.doc_id,
  731. op: [{ textOperation: [4, 'one and a half\n', 9] }],
  732. v: this.version,
  733. meta: {
  734. source: 'ikHceq3yfAdQYzBo4-xZ',
  735. },
  736. })
  737. await sendUpdateAndWait(this.project_id, this.doc_id, {
  738. doc: this.doc_id,
  739. op: [
  740. {
  741. textOperation: [4, 'one and a half\n', 9],
  742. },
  743. ],
  744. v: this.version,
  745. dupIfSource: ['ikHceq3yfAdQYzBo4-xZ'],
  746. meta: {
  747. source: 'ikHceq3yfAdQYzBo4-xZ',
  748. },
  749. })
  750. })
  751. it('should update the doc', async function () {
  752. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  753. doc.lines.should.deep.equal(this.result)
  754. })
  755. it('should return a message about duplicate ops', function () {
  756. this.messageCallback.calledTwice.should.equal(true)
  757. this.messageCallback.args[0][0].should.equal('applied-ops')
  758. expect(JSON.parse(this.messageCallback.args[0][1]).op.dup).to.be.undefined
  759. this.messageCallback.args[1][0].should.equal('applied-ops')
  760. expect(JSON.parse(this.messageCallback.args[1][1]).op.dup).to.equal(true)
  761. })
  762. })
  763. describe('when sending updates for a non-existing doc id', function () {
  764. beforeEach(async function () {
  765. this.non_existing = {
  766. doc: this.doc_id,
  767. v: this.version,
  768. op: [{ d: 'content', p: 0 }],
  769. }
  770. DocUpdaterClient.subscribeToAppliedOps(
  771. (this.messageCallback = sinon.stub())
  772. )
  773. await sendUpdateAndWait(this.project_id, this.doc_id, this.non_existing)
  774. })
  775. it('should not update or create a doc', async function () {
  776. await expect(DocUpdaterClient.getDoc(this.project_id, this.doc_id))
  777. .to.be.rejectedWith(RequestFailedError)
  778. .and.eventually.have.nested.property('response.status', 404)
  779. })
  780. it('should send a message with an error', function () {
  781. this.messageCallback.called.should.equal(true)
  782. const [channel, message] = this.messageCallback.args[0]
  783. channel.should.equal('applied-ops')
  784. JSON.parse(message).should.deep.include({
  785. project_id: this.project_id,
  786. doc_id: this.doc_id,
  787. error: 'doc not found',
  788. })
  789. })
  790. })
  791. })