| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath = '../../../../app/js/RangesManager.js'
- const SandboxedModule = require('sandboxed-module')
- describe('RangesManager', function () {
- beforeEach(function () {
- this.RangesManager = SandboxedModule.require(modulePath)
- this.doc_id = 'doc-id-123'
- this.project_id = 'project-id-123'
- this.user_id = 'user-id-123'
- return (this.callback = sinon.stub())
- })
- describe('applyUpdate', function () {
- beforeEach(function () {
- this.updates = [
- {
- meta: {
- user_id: this.user_id,
- },
- op: [
- {
- i: 'two ',
- p: 4,
- },
- ],
- },
- ]
- this.entries = {
- comments: [
- {
- op: {
- c: 'three ',
- p: 4,
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- ],
- changes: [
- {
- op: {
- i: 'five',
- p: 15,
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- ],
- }
- return (this.newDocLines = ['one two three four five'])
- }) // old is "one three four five"
- describe('successfully', function () {
- beforeEach(function () {
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return the modified the comments and changes', function () {
- this.callback.called.should.equal(true)
- const [error, entries, ranges_were_collapsed] = Array.from(
- this.callback.args[0]
- )
- expect(error).to.be.null
- expect(ranges_were_collapsed).to.equal(false)
- entries.comments[0].op.should.deep.equal({
- c: 'three ',
- p: 8,
- })
- return entries.changes[0].op.should.deep.equal({
- i: 'five',
- p: 19,
- })
- })
- })
- describe('with empty comments', function () {
- beforeEach(function () {
- this.entries.comments = []
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return an object with no comments', function () {
- // Save space in redis and don't store just {}
- this.callback.called.should.equal(true)
- const [error, entries] = Array.from(this.callback.args[0])
- expect(error).to.be.null
- return expect(entries.comments).to.be.undefined
- })
- })
- describe('with empty changes', function () {
- beforeEach(function () {
- this.entries.changes = []
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return an object with no changes', function () {
- // Save space in redis and don't store just {}
- this.callback.called.should.equal(true)
- const [error, entries] = Array.from(this.callback.args[0])
- expect(error).to.be.null
- return expect(entries.changes).to.be.undefined
- })
- })
- describe('with too many comments', function () {
- beforeEach(function () {
- this.RangesManager.MAX_COMMENTS = 2
- this.updates = [
- {
- meta: {
- user_id: this.user_id,
- },
- op: [
- {
- c: 'one',
- p: 0,
- t: 'thread-id-1',
- },
- ],
- },
- ]
- this.entries = {
- comments: [
- {
- op: {
- c: 'three ',
- p: 4,
- t: 'thread-id-2',
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- {
- op: {
- c: 'four ',
- p: 10,
- t: 'thread-id-3',
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- ],
- changes: [],
- }
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return an error', function () {
- this.callback.called.should.equal(true)
- const [error, entries] = Array.from(this.callback.args[0])
- expect(error).to.not.be.null
- return expect(error.message).to.equal(
- 'too many comments or tracked changes'
- )
- })
- })
- describe('with too many changes', function () {
- beforeEach(function () {
- this.RangesManager.MAX_CHANGES = 2
- this.updates = [
- {
- meta: {
- user_id: this.user_id,
- tc: 'track-changes-id-yes',
- },
- op: [
- {
- i: 'one ',
- p: 0,
- },
- ],
- },
- ]
- this.entries = {
- changes: [
- {
- op: {
- i: 'three',
- p: 4,
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- {
- op: {
- i: 'four',
- p: 10,
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- ],
- comments: [],
- }
- this.newDocLines = ['one two three four']
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return an error', function () {
- // Save space in redis and don't store just {}
- this.callback.called.should.equal(true)
- const [error, entries] = Array.from(this.callback.args[0])
- expect(error).to.not.be.null
- return expect(error.message).to.equal(
- 'too many comments or tracked changes'
- )
- })
- })
- describe('inconsistent changes', function () {
- beforeEach(function () {
- this.updates = [
- {
- meta: {
- user_id: this.user_id,
- },
- op: [
- {
- c: "doesn't match",
- p: 0,
- },
- ],
- },
- ]
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return an error', function () {
- // Save space in redis and don't store just {}
- this.callback.called.should.equal(true)
- const [error, entries] = Array.from(this.callback.args[0])
- expect(error).to.not.be.null
- return expect(error.message).to.equal(
- 'Change ({"op":{"i":"five","p":15},"metadata":{"user_id":"user-id-123"}}) doesn\'t match text ("our ")'
- )
- })
- })
- return describe('with an update that collapses a range', function () {
- beforeEach(function () {
- this.updates = [
- {
- meta: {
- user_id: this.user_id,
- },
- op: [
- {
- d: 'one',
- p: 0,
- t: 'thread-id-1',
- },
- ],
- },
- ]
- this.entries = {
- comments: [
- {
- op: {
- c: 'n',
- p: 1,
- t: 'thread-id-2',
- },
- metadata: {
- user_id: this.user_id,
- },
- },
- ],
- changes: [],
- }
- return this.RangesManager.applyUpdate(
- this.project_id,
- this.doc_id,
- this.entries,
- this.updates,
- this.newDocLines,
- this.callback
- )
- })
- return it('should return ranges_were_collapsed == true', function () {
- this.callback.called.should.equal(true)
- const [error, entries, ranges_were_collapsed] = Array.from(
- this.callback.args[0]
- )
- return expect(ranges_were_collapsed).to.equal(true)
- })
- })
- })
- return describe('acceptChanges', function () {
- beforeEach(function () {
- this.RangesManager = SandboxedModule.require(modulePath, {
- requires: {
- './RangesTracker': (this.RangesTracker = SandboxedModule.require(
- '../../../../app/js/RangesTracker.js'
- )),
- },
- })
- this.ranges = {
- comments: [],
- changes: [
- {
- id: 'a1',
- op: {
- i: 'lorem',
- p: 0,
- },
- },
- {
- id: 'a2',
- op: {
- i: 'ipsum',
- p: 10,
- },
- },
- {
- id: 'a3',
- op: {
- i: 'dolor',
- p: 20,
- },
- },
- {
- id: 'a4',
- op: {
- i: 'sit',
- p: 30,
- },
- },
- {
- id: 'a5',
- op: {
- i: 'amet',
- p: 40,
- },
- },
- ],
- }
- return (this.removeChangeIdsSpy = sinon.spy(
- this.RangesTracker.prototype,
- 'removeChangeIds'
- ))
- })
- describe('successfully with a single change', function () {
- beforeEach(function (done) {
- this.change_ids = [this.ranges.changes[1].id]
- return this.RangesManager.acceptChanges(
- this.change_ids,
- this.ranges,
- (err, ranges) => {
- this.rangesResponse = ranges
- return done()
- }
- )
- })
- it('should log the call with the correct number of changes', function () {
- return this.logger.log
- .calledWith('accepting 1 changes in ranges')
- .should.equal(true)
- })
- it('should delegate the change removal to the ranges tracker', function () {
- return this.removeChangeIdsSpy
- .calledWith(this.change_ids)
- .should.equal(true)
- })
- it('should remove the change', function () {
- return expect(
- this.rangesResponse.changes.find(
- change => change.id === this.ranges.changes[1].id
- )
- ).to.be.undefined
- })
- it('should return the original number of changes minus 1', function () {
- return this.rangesResponse.changes.length.should.equal(
- this.ranges.changes.length - 1
- )
- })
- return it('should not touch other changes', function () {
- return [0, 2, 3, 4].map(i =>
- expect(
- this.rangesResponse.changes.find(
- change => change.id === this.ranges.changes[i].id
- )
- ).to.deep.equal(this.ranges.changes[i])
- )
- })
- })
- return describe('successfully with multiple changes', function () {
- beforeEach(function (done) {
- this.change_ids = [
- this.ranges.changes[1].id,
- this.ranges.changes[3].id,
- this.ranges.changes[4].id,
- ]
- return this.RangesManager.acceptChanges(
- this.change_ids,
- this.ranges,
- (err, ranges) => {
- this.rangesResponse = ranges
- return done()
- }
- )
- })
- it('should log the call with the correct number of changes', function () {
- return this.logger.log
- .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
- .should.equal(true)
- })
- it('should delegate the change removal to the ranges tracker', function () {
- return this.removeChangeIdsSpy
- .calledWith(this.change_ids)
- .should.equal(true)
- })
- it('should remove the changes', function () {
- return [1, 3, 4].map(
- i =>
- expect(
- this.rangesResponse.changes.find(
- change => change.id === this.ranges.changes[1].id
- )
- ).to.be.undefined
- )
- })
- it('should return the original number of changes minus the number of accepted changes', function () {
- return this.rangesResponse.changes.length.should.equal(
- this.ranges.changes.length - 3
- )
- })
- return it('should not touch other changes', function () {
- return [0, 2].map(i =>
- expect(
- this.rangesResponse.changes.find(
- change => change.id === this.ranges.changes[i].id
- )
- ).to.deep.equal(this.ranges.changes[i])
- )
- })
- })
- })
- })
|