2020-02-16 14:02:49 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 14:02:48 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-16 14:02:55 +00:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2021-03-12 22:16:01 +00:00
|
|
|
const { assert, expect } = require('chai')
|
2020-02-16 14:02:55 +00:00
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/RangeManager'
|
|
|
|
)
|
2020-08-21 16:50:01 +00:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-16 14:02:55 +00:00
|
|
|
const _ = require('underscore')
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('RangeManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
return (this.RangeManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-08-21 16:50:01 +00:00
|
|
|
'./mongodb': {
|
2021-07-13 11:04:48 +00:00
|
|
|
ObjectId,
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
}))
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('jsonRangesToMongo', function () {
|
|
|
|
it('should convert ObjectIds and dates to proper objects', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
const change_id = ObjectId().toString()
|
|
|
|
const comment_id = ObjectId().toString()
|
|
|
|
const user_id = ObjectId().toString()
|
|
|
|
const thread_id = ObjectId().toString()
|
|
|
|
const ts = new Date().toJSON()
|
|
|
|
return this.RangeManager.jsonRangesToMongo({
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: change_id,
|
|
|
|
op: { i: 'foo', p: 3 },
|
|
|
|
metadata: {
|
|
|
|
user_id,
|
2021-07-13 11:04:48 +00:00
|
|
|
ts,
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
id: comment_id,
|
2021-07-13 11:04:48 +00:00
|
|
|
op: { c: 'foo', p: 3, t: thread_id },
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
}).should.deep.equal({
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: ObjectId(change_id),
|
|
|
|
op: { i: 'foo', p: 3 },
|
|
|
|
metadata: {
|
|
|
|
user_id: ObjectId(user_id),
|
2021-07-13 11:04:48 +00:00
|
|
|
ts: new Date(ts),
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
id: ObjectId(comment_id),
|
2021-07-13 11:04:48 +00:00
|
|
|
op: { c: 'foo', p: 3, t: ObjectId(thread_id) },
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should leave malformed ObjectIds as they are', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
const change_id = 'foo'
|
|
|
|
const comment_id = 'bar'
|
|
|
|
const user_id = 'baz'
|
|
|
|
return this.RangeManager.jsonRangesToMongo({
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: change_id,
|
|
|
|
metadata: {
|
2021-07-13 11:04:48 +00:00
|
|
|
user_id,
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
id: comment_id,
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
}).should.deep.equal({
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: change_id,
|
|
|
|
metadata: {
|
2021-07-13 11:04:48 +00:00
|
|
|
user_id,
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
id: comment_id,
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
return it('should be consistent when transformed through json -> mongo -> json', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
const change_id = ObjectId().toString()
|
|
|
|
const comment_id = ObjectId().toString()
|
|
|
|
const user_id = ObjectId().toString()
|
|
|
|
const thread_id = ObjectId().toString()
|
|
|
|
const ts = new Date().toJSON()
|
|
|
|
const ranges1 = {
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: change_id,
|
|
|
|
op: { i: 'foo', p: 3 },
|
|
|
|
metadata: {
|
|
|
|
user_id,
|
2021-07-13 11:04:48 +00:00
|
|
|
ts,
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
id: comment_id,
|
2021-07-13 11:04:48 +00:00
|
|
|
op: { c: 'foo', p: 3, t: thread_id },
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
}
|
|
|
|
const ranges1_copy = JSON.parse(JSON.stringify(ranges1)) // jsonRangesToMongo modifies in place
|
|
|
|
const ranges2 = JSON.parse(
|
|
|
|
JSON.stringify(this.RangeManager.jsonRangesToMongo(ranges1_copy))
|
|
|
|
)
|
|
|
|
return ranges1.should.deep.equal(ranges2)
|
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
return describe('shouldUpdateRanges', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges = {
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
id: ObjectId(),
|
|
|
|
op: { i: 'foo', p: 3 },
|
|
|
|
metadata: {
|
|
|
|
user_id: ObjectId(),
|
2021-07-13 11:04:48 +00:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
],
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
id: ObjectId(),
|
2021-07-13 11:04:48 +00:00
|
|
|
op: { c: 'foo', p: 3, t: ObjectId() },
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 14:02:55 +00:00
|
|
|
}
|
|
|
|
return (this.ranges_copy = this.RangeManager.jsonRangesToMongo(
|
|
|
|
JSON.parse(JSON.stringify(this.ranges))
|
|
|
|
))
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('with a blank new range', function () {
|
|
|
|
return it('should throw an error', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
return expect(() => {
|
|
|
|
return this.RangeManager.shouldUpdateRanges(this.ranges, null)
|
|
|
|
}).to.throw(Error)
|
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('with a blank old range', function () {
|
|
|
|
return it('should treat it like {}', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.RangeManager.shouldUpdateRanges(null, {}).should.equal(false)
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
null,
|
|
|
|
this.ranges
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('with no changes', function () {
|
|
|
|
return it('should return false', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
2016-12-05 15:38:38 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
return describe('with changes', function () {
|
|
|
|
it('should return true when the change id changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.changes[0].id = ObjectId()
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should return true when the change user id changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.changes[0].metadata.user_id = ObjectId()
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should return true when the change ts changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.changes[0].metadata.ts = new Date(Date.now() + 1000)
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should return true when the change op changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.changes[0].op.i = 'bar'
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should return true when the comment id changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.comments[0].id = ObjectId()
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should return true when the comment offset changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.comments[0].op.p = 17
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
return it('should return true when the comment content changes', function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.ranges_copy.comments[0].op.c = 'bar'
|
|
|
|
return this.RangeManager.shouldUpdateRanges(
|
|
|
|
this.ranges,
|
|
|
|
this.ranges_copy
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|