ApplyingUpdatesToADocTests.js 24 KB

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