RangesManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const { expect } = require('chai')
  17. const modulePath = '../../../../app/js/RangesManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('RangesManager', function () {
  20. beforeEach(function () {
  21. this.RangesManager = SandboxedModule.require(modulePath)
  22. this.doc_id = 'doc-id-123'
  23. this.project_id = 'project-id-123'
  24. this.user_id = 'user-id-123'
  25. return (this.callback = sinon.stub())
  26. })
  27. describe('applyUpdate', function () {
  28. beforeEach(function () {
  29. this.updates = [
  30. {
  31. meta: {
  32. user_id: this.user_id,
  33. },
  34. op: [
  35. {
  36. i: 'two ',
  37. p: 4,
  38. },
  39. ],
  40. },
  41. ]
  42. this.entries = {
  43. comments: [
  44. {
  45. op: {
  46. c: 'three ',
  47. p: 4,
  48. },
  49. metadata: {
  50. user_id: this.user_id,
  51. },
  52. },
  53. ],
  54. changes: [
  55. {
  56. op: {
  57. i: 'five',
  58. p: 15,
  59. },
  60. metadata: {
  61. user_id: this.user_id,
  62. },
  63. },
  64. ],
  65. }
  66. return (this.newDocLines = ['one two three four five'])
  67. }) // old is "one three four five"
  68. describe('successfully', function () {
  69. beforeEach(function () {
  70. return this.RangesManager.applyUpdate(
  71. this.project_id,
  72. this.doc_id,
  73. this.entries,
  74. this.updates,
  75. this.newDocLines,
  76. this.callback
  77. )
  78. })
  79. return it('should return the modified the comments and changes', function () {
  80. this.callback.called.should.equal(true)
  81. const [error, entries, ranges_were_collapsed] = Array.from(
  82. this.callback.args[0]
  83. )
  84. expect(error).to.be.null
  85. expect(ranges_were_collapsed).to.equal(false)
  86. entries.comments[0].op.should.deep.equal({
  87. c: 'three ',
  88. p: 8,
  89. })
  90. return entries.changes[0].op.should.deep.equal({
  91. i: 'five',
  92. p: 19,
  93. })
  94. })
  95. })
  96. describe('with empty comments', function () {
  97. beforeEach(function () {
  98. this.entries.comments = []
  99. return this.RangesManager.applyUpdate(
  100. this.project_id,
  101. this.doc_id,
  102. this.entries,
  103. this.updates,
  104. this.newDocLines,
  105. this.callback
  106. )
  107. })
  108. return it('should return an object with no comments', function () {
  109. // Save space in redis and don't store just {}
  110. this.callback.called.should.equal(true)
  111. const [error, entries] = Array.from(this.callback.args[0])
  112. expect(error).to.be.null
  113. return expect(entries.comments).to.be.undefined
  114. })
  115. })
  116. describe('with empty changes', function () {
  117. beforeEach(function () {
  118. this.entries.changes = []
  119. return this.RangesManager.applyUpdate(
  120. this.project_id,
  121. this.doc_id,
  122. this.entries,
  123. this.updates,
  124. this.newDocLines,
  125. this.callback
  126. )
  127. })
  128. return it('should return an object with no changes', function () {
  129. // Save space in redis and don't store just {}
  130. this.callback.called.should.equal(true)
  131. const [error, entries] = Array.from(this.callback.args[0])
  132. expect(error).to.be.null
  133. return expect(entries.changes).to.be.undefined
  134. })
  135. })
  136. describe('with too many comments', function () {
  137. beforeEach(function () {
  138. this.RangesManager.MAX_COMMENTS = 2
  139. this.updates = [
  140. {
  141. meta: {
  142. user_id: this.user_id,
  143. },
  144. op: [
  145. {
  146. c: 'one',
  147. p: 0,
  148. t: 'thread-id-1',
  149. },
  150. ],
  151. },
  152. ]
  153. this.entries = {
  154. comments: [
  155. {
  156. op: {
  157. c: 'three ',
  158. p: 4,
  159. t: 'thread-id-2',
  160. },
  161. metadata: {
  162. user_id: this.user_id,
  163. },
  164. },
  165. {
  166. op: {
  167. c: 'four ',
  168. p: 10,
  169. t: 'thread-id-3',
  170. },
  171. metadata: {
  172. user_id: this.user_id,
  173. },
  174. },
  175. ],
  176. changes: [],
  177. }
  178. return this.RangesManager.applyUpdate(
  179. this.project_id,
  180. this.doc_id,
  181. this.entries,
  182. this.updates,
  183. this.newDocLines,
  184. this.callback
  185. )
  186. })
  187. return it('should return an error', function () {
  188. this.callback.called.should.equal(true)
  189. const [error, entries] = Array.from(this.callback.args[0])
  190. expect(error).to.not.be.null
  191. return expect(error.message).to.equal(
  192. 'too many comments or tracked changes'
  193. )
  194. })
  195. })
  196. describe('with too many changes', function () {
  197. beforeEach(function () {
  198. this.RangesManager.MAX_CHANGES = 2
  199. this.updates = [
  200. {
  201. meta: {
  202. user_id: this.user_id,
  203. tc: 'track-changes-id-yes',
  204. },
  205. op: [
  206. {
  207. i: 'one ',
  208. p: 0,
  209. },
  210. ],
  211. },
  212. ]
  213. this.entries = {
  214. changes: [
  215. {
  216. op: {
  217. i: 'three',
  218. p: 4,
  219. },
  220. metadata: {
  221. user_id: this.user_id,
  222. },
  223. },
  224. {
  225. op: {
  226. i: 'four',
  227. p: 10,
  228. },
  229. metadata: {
  230. user_id: this.user_id,
  231. },
  232. },
  233. ],
  234. comments: [],
  235. }
  236. this.newDocLines = ['one two three four']
  237. return this.RangesManager.applyUpdate(
  238. this.project_id,
  239. this.doc_id,
  240. this.entries,
  241. this.updates,
  242. this.newDocLines,
  243. this.callback
  244. )
  245. })
  246. return it('should return an error', function () {
  247. // Save space in redis and don't store just {}
  248. this.callback.called.should.equal(true)
  249. const [error, entries] = Array.from(this.callback.args[0])
  250. expect(error).to.not.be.null
  251. return expect(error.message).to.equal(
  252. 'too many comments or tracked changes'
  253. )
  254. })
  255. })
  256. describe('inconsistent changes', function () {
  257. beforeEach(function () {
  258. this.updates = [
  259. {
  260. meta: {
  261. user_id: this.user_id,
  262. },
  263. op: [
  264. {
  265. c: "doesn't match",
  266. p: 0,
  267. },
  268. ],
  269. },
  270. ]
  271. return this.RangesManager.applyUpdate(
  272. this.project_id,
  273. this.doc_id,
  274. this.entries,
  275. this.updates,
  276. this.newDocLines,
  277. this.callback
  278. )
  279. })
  280. return it('should return an error', function () {
  281. // Save space in redis and don't store just {}
  282. this.callback.called.should.equal(true)
  283. const [error, entries] = Array.from(this.callback.args[0])
  284. expect(error).to.not.be.null
  285. return expect(error.message).to.equal(
  286. 'Change ({"op":{"i":"five","p":15},"metadata":{"user_id":"user-id-123"}}) doesn\'t match text ("our ")'
  287. )
  288. })
  289. })
  290. return describe('with an update that collapses a range', function () {
  291. beforeEach(function () {
  292. this.updates = [
  293. {
  294. meta: {
  295. user_id: this.user_id,
  296. },
  297. op: [
  298. {
  299. d: 'one',
  300. p: 0,
  301. t: 'thread-id-1',
  302. },
  303. ],
  304. },
  305. ]
  306. this.entries = {
  307. comments: [
  308. {
  309. op: {
  310. c: 'n',
  311. p: 1,
  312. t: 'thread-id-2',
  313. },
  314. metadata: {
  315. user_id: this.user_id,
  316. },
  317. },
  318. ],
  319. changes: [],
  320. }
  321. return this.RangesManager.applyUpdate(
  322. this.project_id,
  323. this.doc_id,
  324. this.entries,
  325. this.updates,
  326. this.newDocLines,
  327. this.callback
  328. )
  329. })
  330. return it('should return ranges_were_collapsed == true', function () {
  331. this.callback.called.should.equal(true)
  332. const [error, entries, ranges_were_collapsed] = Array.from(
  333. this.callback.args[0]
  334. )
  335. return expect(ranges_were_collapsed).to.equal(true)
  336. })
  337. })
  338. })
  339. return describe('acceptChanges', function () {
  340. beforeEach(function () {
  341. this.RangesManager = SandboxedModule.require(modulePath, {
  342. requires: {
  343. './RangesTracker': (this.RangesTracker = SandboxedModule.require(
  344. '../../../../app/js/RangesTracker.js'
  345. )),
  346. },
  347. })
  348. this.ranges = {
  349. comments: [],
  350. changes: [
  351. {
  352. id: 'a1',
  353. op: {
  354. i: 'lorem',
  355. p: 0,
  356. },
  357. },
  358. {
  359. id: 'a2',
  360. op: {
  361. i: 'ipsum',
  362. p: 10,
  363. },
  364. },
  365. {
  366. id: 'a3',
  367. op: {
  368. i: 'dolor',
  369. p: 20,
  370. },
  371. },
  372. {
  373. id: 'a4',
  374. op: {
  375. i: 'sit',
  376. p: 30,
  377. },
  378. },
  379. {
  380. id: 'a5',
  381. op: {
  382. i: 'amet',
  383. p: 40,
  384. },
  385. },
  386. ],
  387. }
  388. return (this.removeChangeIdsSpy = sinon.spy(
  389. this.RangesTracker.prototype,
  390. 'removeChangeIds'
  391. ))
  392. })
  393. describe('successfully with a single change', function () {
  394. beforeEach(function (done) {
  395. this.change_ids = [this.ranges.changes[1].id]
  396. return this.RangesManager.acceptChanges(
  397. this.change_ids,
  398. this.ranges,
  399. (err, ranges) => {
  400. this.rangesResponse = ranges
  401. return done()
  402. }
  403. )
  404. })
  405. it('should log the call with the correct number of changes', function () {
  406. return this.logger.log
  407. .calledWith('accepting 1 changes in ranges')
  408. .should.equal(true)
  409. })
  410. it('should delegate the change removal to the ranges tracker', function () {
  411. return this.removeChangeIdsSpy
  412. .calledWith(this.change_ids)
  413. .should.equal(true)
  414. })
  415. it('should remove the change', function () {
  416. return expect(
  417. this.rangesResponse.changes.find(
  418. change => change.id === this.ranges.changes[1].id
  419. )
  420. ).to.be.undefined
  421. })
  422. it('should return the original number of changes minus 1', function () {
  423. return this.rangesResponse.changes.length.should.equal(
  424. this.ranges.changes.length - 1
  425. )
  426. })
  427. return it('should not touch other changes', function () {
  428. return [0, 2, 3, 4].map(i =>
  429. expect(
  430. this.rangesResponse.changes.find(
  431. change => change.id === this.ranges.changes[i].id
  432. )
  433. ).to.deep.equal(this.ranges.changes[i])
  434. )
  435. })
  436. })
  437. return describe('successfully with multiple changes', function () {
  438. beforeEach(function (done) {
  439. this.change_ids = [
  440. this.ranges.changes[1].id,
  441. this.ranges.changes[3].id,
  442. this.ranges.changes[4].id,
  443. ]
  444. return this.RangesManager.acceptChanges(
  445. this.change_ids,
  446. this.ranges,
  447. (err, ranges) => {
  448. this.rangesResponse = ranges
  449. return done()
  450. }
  451. )
  452. })
  453. it('should log the call with the correct number of changes', function () {
  454. return this.logger.log
  455. .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
  456. .should.equal(true)
  457. })
  458. it('should delegate the change removal to the ranges tracker', function () {
  459. return this.removeChangeIdsSpy
  460. .calledWith(this.change_ids)
  461. .should.equal(true)
  462. })
  463. it('should remove the changes', function () {
  464. return [1, 3, 4].map(
  465. i =>
  466. expect(
  467. this.rangesResponse.changes.find(
  468. change => change.id === this.ranges.changes[1].id
  469. )
  470. ).to.be.undefined
  471. )
  472. })
  473. it('should return the original number of changes minus the number of accepted changes', function () {
  474. return this.rangesResponse.changes.length.should.equal(
  475. this.ranges.changes.length - 3
  476. )
  477. })
  478. return it('should not touch other changes', function () {
  479. return [0, 2].map(i =>
  480. expect(
  481. this.rangesResponse.changes.find(
  482. change => change.id === this.ranges.changes[i].id
  483. )
  484. ).to.deep.equal(this.ranges.changes[i])
  485. )
  486. })
  487. })
  488. })
  489. })