2019-05-06 19:15:33 +00:00
|
|
|
/* eslint-env node, mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
const mock = require('mock-require')
|
|
|
|
const sinon = require('sinon')
|
2019-05-20 08:04:35 +00:00
|
|
|
const { removeModuleFromRequireCache, makeMockSocket, removeLibModuleCache } = require('./utils')
|
2019-05-06 19:15:33 +00:00
|
|
|
|
|
|
|
describe('realtime#update note is dirty timer', function () {
|
|
|
|
let realtime
|
|
|
|
let clock
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-05-20 08:04:35 +00:00
|
|
|
removeLibModuleCache()
|
2019-05-06 19:15:33 +00:00
|
|
|
clock = sinon.useFakeTimers({
|
|
|
|
toFake: ['setInterval']
|
|
|
|
})
|
|
|
|
mock('../../lib/logger', {
|
|
|
|
error: () => {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mock('../../lib/history', {})
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Revision: {
|
|
|
|
saveAllNotesRevision: () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mock('../../lib/config', {})
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-06 19:15:33 +00:00
|
|
|
|
|
|
|
realtime.io = {
|
|
|
|
to: sinon.stub().callsFake(function () {
|
|
|
|
return {
|
|
|
|
emit: sinon.fake()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-01-04 22:14:03 +00:00
|
|
|
removeModuleFromRequireCache('../../lib/realtime/realtimeUpdateDirtyNoteJob')
|
|
|
|
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
2019-05-06 19:15:33 +00:00
|
|
|
mock.stopAll()
|
|
|
|
clock.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update note when note is dirty', (done) => {
|
|
|
|
sinon.stub(realtime, 'updateNote').callsFake(function (note, callback) {
|
|
|
|
callback(null, note)
|
|
|
|
})
|
|
|
|
|
2020-07-01 03:33:41 +00:00
|
|
|
realtime.notes.note1 = {
|
2019-05-06 19:15:33 +00:00
|
|
|
server: {
|
|
|
|
isDirty: false
|
|
|
|
},
|
|
|
|
socks: []
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:21:28 +00:00
|
|
|
const note2 = {
|
2019-05-06 19:15:33 +00:00
|
|
|
server: {
|
|
|
|
isDirty: true
|
|
|
|
},
|
|
|
|
socks: []
|
|
|
|
}
|
|
|
|
|
2020-07-01 03:33:41 +00:00
|
|
|
realtime.notes.note2 = note2
|
2019-05-06 19:15:33 +00:00
|
|
|
|
|
|
|
clock.tick(1000)
|
|
|
|
setTimeout(() => {
|
|
|
|
assert(note2.server.isDirty === false)
|
|
|
|
done()
|
2019-05-20 08:04:35 +00:00
|
|
|
}, 10)
|
2019-05-06 19:15:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not do anything when note missing', function (done) {
|
|
|
|
sinon.stub(realtime, 'updateNote').callsFake(function (note, callback) {
|
2020-07-01 03:33:41 +00:00
|
|
|
delete realtime.notes.note
|
2019-05-06 19:15:33 +00:00
|
|
|
callback(null, note)
|
|
|
|
})
|
|
|
|
|
2019-08-04 15:21:28 +00:00
|
|
|
const note = {
|
2019-05-06 19:15:33 +00:00
|
|
|
server: {
|
|
|
|
isDirty: true
|
|
|
|
},
|
|
|
|
socks: [makeMockSocket()]
|
|
|
|
}
|
2020-07-01 03:33:41 +00:00
|
|
|
realtime.notes.note = note
|
2019-05-06 19:15:33 +00:00
|
|
|
|
|
|
|
clock.tick(1000)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
assert(note.server.isDirty === false)
|
|
|
|
assert(note.socks[0].disconnect.called === false)
|
|
|
|
done()
|
|
|
|
}, 50)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect all clients when update note error', function (done) {
|
|
|
|
sinon.stub(realtime, 'updateNote').callsFake(function (note, callback) {
|
|
|
|
callback(new Error('some error'), null)
|
|
|
|
})
|
|
|
|
|
|
|
|
realtime.io = {
|
|
|
|
to: sinon.stub().callsFake(function () {
|
|
|
|
return {
|
|
|
|
emit: sinon.fake()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:21:28 +00:00
|
|
|
const note = {
|
2019-05-06 19:15:33 +00:00
|
|
|
server: {
|
|
|
|
isDirty: true
|
|
|
|
},
|
|
|
|
socks: [makeMockSocket(), undefined, makeMockSocket()]
|
|
|
|
}
|
2020-07-01 03:33:41 +00:00
|
|
|
realtime.notes.note = note
|
2019-05-06 19:15:33 +00:00
|
|
|
|
|
|
|
clock.tick(1000)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
assert(note.server.isDirty === false)
|
|
|
|
assert(note.socks[0].disconnect.called)
|
|
|
|
assert(note.socks[2].disconnect.called)
|
|
|
|
done()
|
|
|
|
}, 50)
|
|
|
|
})
|
|
|
|
})
|