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 chai = require('chai')
  16. chai.should()
  17. const { expect } = chai
  18. const async = require('async')
  19. const Settings = require('settings-sharelatex')
  20. const rclient_history = require('@overleaf/redis-wrapper').createClient(
  21. Settings.redis.history
  22. ) // note: this is track changes, not project-history
  23. const rclient_project_history = require('@overleaf/redis-wrapper').createClient(
  24. Settings.redis.project_history
  25. )
  26. const rclient_du = require('@overleaf/redis-wrapper').createClient(
  27. Settings.redis.documentupdater
  28. )
  29. const Keys = Settings.redis.documentupdater.key_schema
  30. const HistoryKeys = Settings.redis.history.key_schema
  31. const ProjectHistoryKeys = Settings.redis.project_history.key_schema
  32. const MockTrackChangesApi = require('./helpers/MockTrackChangesApi')
  33. const MockWebApi = require('./helpers/MockWebApi')
  34. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  35. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  36. describe('Applying updates to a doc', function () {
  37. before(function (done) {
  38. this.lines = ['one', 'two', 'three']
  39. this.version = 42
  40. this.update = {
  41. doc: this.doc_id,
  42. op: [
  43. {
  44. i: 'one and a half\n',
  45. p: 4
  46. }
  47. ],
  48. v: this.version
  49. }
  50. this.result = ['one', 'one and a half', 'two', 'three']
  51. return DocUpdaterApp.ensureRunning(done)
  52. })
  53. describe('when the document is not loaded', function () {
  54. before(function (done) {
  55. ;[this.project_id, this.doc_id] = Array.from([
  56. DocUpdaterClient.randomId(),
  57. DocUpdaterClient.randomId()
  58. ])
  59. sinon.spy(MockWebApi, 'getDocument')
  60. this.startTime = Date.now()
  61. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  62. lines: this.lines,
  63. version: this.version
  64. })
  65. DocUpdaterClient.sendUpdate(
  66. this.project_id,
  67. this.doc_id,
  68. this.update,
  69. (error) => {
  70. if (error != null) {
  71. throw error
  72. }
  73. return setTimeout(done, 200)
  74. }
  75. )
  76. return null
  77. })
  78. after(function () {
  79. return MockWebApi.getDocument.restore()
  80. })
  81. it('should load the document from the web API', function () {
  82. return MockWebApi.getDocument
  83. .calledWith(this.project_id, this.doc_id)
  84. .should.equal(true)
  85. })
  86. it('should update the doc', function (done) {
  87. DocUpdaterClient.getDoc(
  88. this.project_id,
  89. this.doc_id,
  90. (error, res, doc) => {
  91. doc.lines.should.deep.equal(this.result)
  92. return done()
  93. }
  94. )
  95. return null
  96. })
  97. it('should push the applied updates to the track changes api', function (done) {
  98. rclient_history.lrange(
  99. HistoryKeys.uncompressedHistoryOps({ doc_id: this.doc_id }),
  100. 0,
  101. -1,
  102. (error, updates) => {
  103. if (error != null) {
  104. throw error
  105. }
  106. JSON.parse(updates[0]).op.should.deep.equal(this.update.op)
  107. return rclient_history.sismember(
  108. HistoryKeys.docsWithHistoryOps({ project_id: this.project_id }),
  109. this.doc_id,
  110. (error, result) => {
  111. if (error != null) {
  112. throw error
  113. }
  114. result.should.equal(1)
  115. return done()
  116. }
  117. )
  118. }
  119. )
  120. return null
  121. })
  122. it('should push the applied updates to the project history changes api', function (done) {
  123. rclient_project_history.lrange(
  124. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  125. 0,
  126. -1,
  127. (error, updates) => {
  128. if (error != null) {
  129. throw error
  130. }
  131. JSON.parse(updates[0]).op.should.deep.equal(this.update.op)
  132. return done()
  133. }
  134. )
  135. return null
  136. })
  137. it('should set the first op timestamp', function (done) {
  138. rclient_project_history.get(
  139. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  140. project_id: this.project_id
  141. }),
  142. (error, result) => {
  143. if (error != null) {
  144. throw error
  145. }
  146. result.should.be.within(this.startTime, Date.now())
  147. this.firstOpTimestamp = result
  148. return done()
  149. }
  150. )
  151. return null
  152. })
  153. return describe('when sending another update', function () {
  154. before(function (done) {
  155. this.timeout = 10000
  156. this.second_update = Object.create(this.update)
  157. this.second_update.v = this.version + 1
  158. DocUpdaterClient.sendUpdate(
  159. this.project_id,
  160. this.doc_id,
  161. this.second_update,
  162. (error) => {
  163. if (error != null) {
  164. throw error
  165. }
  166. return setTimeout(done, 200)
  167. }
  168. )
  169. return null
  170. })
  171. return it('should not change the first op timestamp', function (done) {
  172. rclient_project_history.get(
  173. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  174. project_id: this.project_id
  175. }),
  176. (error, result) => {
  177. if (error != null) {
  178. throw error
  179. }
  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. })