updateProjectTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. const sinon = require('sinon')
  2. const modulePath = '../../../../app/js/ProjectManager.js'
  3. const SandboxedModule = require('sandboxed-module')
  4. const _ = require('lodash')
  5. describe('ProjectManager', function () {
  6. beforeEach(function () {
  7. this.RedisManager = {}
  8. this.ProjectHistoryRedisManager = {
  9. queueRenameEntity: sinon.stub().yields(),
  10. queueAddEntity: sinon.stub().yields(),
  11. }
  12. this.DocumentManager = {
  13. renameDocWithLock: sinon.stub().yields(),
  14. }
  15. this.HistoryManager = {
  16. flushProjectChangesAsync: sinon.stub(),
  17. shouldFlushHistoryOps: sinon.stub().returns(false),
  18. }
  19. this.LockManager = {
  20. getLock: sinon.stub().yields(),
  21. releaseLock: sinon.stub().yields(),
  22. }
  23. this.Metrics = {
  24. Timer: class Timer {},
  25. }
  26. this.Metrics.Timer.prototype.done = sinon.stub()
  27. this.ProjectManager = SandboxedModule.require(modulePath, {
  28. requires: {
  29. './RedisManager': this.RedisManager,
  30. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  31. './DocumentManager': this.DocumentManager,
  32. './HistoryManager': this.HistoryManager,
  33. './LockManager': this.LockManager,
  34. './Metrics': this.Metrics,
  35. },
  36. })
  37. this.project_id = 'project-id-123'
  38. this.projectHistoryId = 'history-id-123'
  39. this.user_id = 'user-id-123'
  40. this.version = 1234567
  41. this.source = 'editor'
  42. this.callback = sinon.stub()
  43. })
  44. describe('updateProjectWithLocks', function () {
  45. describe('rename operations', function () {
  46. beforeEach(function () {
  47. this.firstDocUpdate = {
  48. type: 'rename-doc',
  49. id: 1,
  50. pathname: 'foo',
  51. newPathname: 'foo',
  52. }
  53. this.secondDocUpdate = {
  54. type: 'rename-doc',
  55. id: 2,
  56. pathname: 'bar',
  57. newPathname: 'bar2',
  58. }
  59. this.firstFileUpdate = {
  60. type: 'rename-file',
  61. id: 2,
  62. pathname: 'bar',
  63. newPathname: 'bar2',
  64. }
  65. this.updates = [
  66. this.firstDocUpdate,
  67. this.secondDocUpdate,
  68. this.firstFileUpdate,
  69. ]
  70. })
  71. describe('successfully', function () {
  72. beforeEach(function () {
  73. this.ProjectManager.updateProjectWithLocks(
  74. this.project_id,
  75. this.projectHistoryId,
  76. this.user_id,
  77. this.updates,
  78. this.version,
  79. this.source,
  80. this.callback
  81. )
  82. })
  83. it('should rename the docs in the updates', function () {
  84. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
  85. version: `${this.version}.0`,
  86. })
  87. const secondDocUpdateWithVersion = _.extend(
  88. {},
  89. this.secondDocUpdate,
  90. { version: `${this.version}.1` }
  91. )
  92. this.DocumentManager.renameDocWithLock
  93. .calledWith(
  94. this.project_id,
  95. this.firstDocUpdate.id,
  96. this.user_id,
  97. firstDocUpdateWithVersion,
  98. this.projectHistoryId
  99. )
  100. .should.equal(true)
  101. this.DocumentManager.renameDocWithLock
  102. .calledWith(
  103. this.project_id,
  104. this.secondDocUpdate.id,
  105. this.user_id,
  106. secondDocUpdateWithVersion,
  107. this.projectHistoryId
  108. )
  109. .should.equal(true)
  110. })
  111. it('should rename the files in the updates', function () {
  112. const firstFileUpdateWithVersion = _.extend(
  113. {},
  114. this.firstFileUpdate,
  115. { version: `${this.version}.2` }
  116. )
  117. this.ProjectHistoryRedisManager.queueRenameEntity
  118. .calledWith(
  119. this.project_id,
  120. this.projectHistoryId,
  121. 'file',
  122. this.firstFileUpdate.id,
  123. this.user_id,
  124. firstFileUpdateWithVersion,
  125. this.source
  126. )
  127. .should.equal(true)
  128. })
  129. it('should not flush the history', function () {
  130. this.HistoryManager.flushProjectChangesAsync
  131. .calledWith(this.project_id)
  132. .should.equal(false)
  133. })
  134. it('should call the callback', function () {
  135. this.callback.called.should.equal(true)
  136. })
  137. })
  138. describe('when renaming a doc fails', function () {
  139. beforeEach(function () {
  140. this.error = new Error('error')
  141. this.DocumentManager.renameDocWithLock.yields(this.error)
  142. this.ProjectManager.updateProjectWithLocks(
  143. this.project_id,
  144. this.projectHistoryId,
  145. this.user_id,
  146. this.updates,
  147. this.version,
  148. this.source,
  149. this.callback
  150. )
  151. })
  152. it('should call the callback with the error', function () {
  153. this.callback.calledWith(this.error).should.equal(true)
  154. })
  155. })
  156. describe('when renaming a file fails', function () {
  157. beforeEach(function () {
  158. this.error = new Error('error')
  159. this.ProjectHistoryRedisManager.queueRenameEntity.yields(this.error)
  160. this.ProjectManager.updateProjectWithLocks(
  161. this.project_id,
  162. this.projectHistoryId,
  163. this.user_id,
  164. this.updates,
  165. this.version,
  166. this.source,
  167. this.callback
  168. )
  169. })
  170. it('should call the callback with the error', function () {
  171. this.callback.calledWith(this.error).should.equal(true)
  172. })
  173. })
  174. describe('with enough ops to flush', function () {
  175. beforeEach(function () {
  176. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  177. this.ProjectManager.updateProjectWithLocks(
  178. this.project_id,
  179. this.projectHistoryId,
  180. this.user_id,
  181. this.updates,
  182. this.version,
  183. this.source,
  184. this.callback
  185. )
  186. })
  187. it('should flush the history', function () {
  188. this.HistoryManager.flushProjectChangesAsync
  189. .calledWith(this.project_id)
  190. .should.equal(true)
  191. })
  192. })
  193. })
  194. describe('add operations', function () {
  195. beforeEach(function () {
  196. this.firstDocUpdate = {
  197. type: 'add-doc',
  198. id: 1,
  199. docLines: 'a\nb',
  200. }
  201. this.secondDocUpdate = {
  202. type: 'add-doc',
  203. id: 2,
  204. docLines: 'a\nb',
  205. }
  206. this.firstFileUpdate = {
  207. type: 'add-file',
  208. id: 3,
  209. url: 'filestore.example.com/2',
  210. }
  211. this.secondFileUpdate = {
  212. type: 'add-file',
  213. id: 4,
  214. url: 'filestore.example.com/3',
  215. }
  216. this.updates = [
  217. this.firstDocUpdate,
  218. this.secondDocUpdate,
  219. this.firstFileUpdate,
  220. this.secondFileUpdate,
  221. ]
  222. })
  223. describe('successfully', function () {
  224. beforeEach(function () {
  225. this.ProjectManager.updateProjectWithLocks(
  226. this.project_id,
  227. this.projectHistoryId,
  228. this.user_id,
  229. this.updates,
  230. this.version,
  231. this.source,
  232. this.callback
  233. )
  234. })
  235. it('should add the docs in the updates', function () {
  236. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
  237. version: `${this.version}.0`,
  238. })
  239. const secondDocUpdateWithVersion = _.extend(
  240. {},
  241. this.secondDocUpdate,
  242. { version: `${this.version}.1` }
  243. )
  244. this.ProjectHistoryRedisManager.queueAddEntity
  245. .getCall(0)
  246. .calledWith(
  247. this.project_id,
  248. this.projectHistoryId,
  249. 'doc',
  250. this.firstDocUpdate.id,
  251. this.user_id,
  252. firstDocUpdateWithVersion,
  253. this.source
  254. )
  255. .should.equal(true)
  256. this.ProjectHistoryRedisManager.queueAddEntity
  257. .getCall(1)
  258. .calledWith(
  259. this.project_id,
  260. this.projectHistoryId,
  261. 'doc',
  262. this.secondDocUpdate.id,
  263. this.user_id,
  264. secondDocUpdateWithVersion,
  265. this.source
  266. )
  267. .should.equal(true)
  268. })
  269. it('should add the files in the updates', function () {
  270. const firstFileUpdateWithVersion = _.extend(
  271. {},
  272. this.firstFileUpdate,
  273. { version: `${this.version}.2` }
  274. )
  275. const secondFileUpdateWithVersion = _.extend(
  276. {},
  277. this.secondFileUpdate,
  278. { version: `${this.version}.3` }
  279. )
  280. this.ProjectHistoryRedisManager.queueAddEntity
  281. .getCall(2)
  282. .calledWith(
  283. this.project_id,
  284. this.projectHistoryId,
  285. 'file',
  286. this.firstFileUpdate.id,
  287. this.user_id,
  288. firstFileUpdateWithVersion,
  289. this.source
  290. )
  291. .should.equal(true)
  292. this.ProjectHistoryRedisManager.queueAddEntity
  293. .getCall(3)
  294. .calledWith(
  295. this.project_id,
  296. this.projectHistoryId,
  297. 'file',
  298. this.secondFileUpdate.id,
  299. this.user_id,
  300. secondFileUpdateWithVersion,
  301. this.source
  302. )
  303. .should.equal(true)
  304. })
  305. it('should not flush the history', function () {
  306. this.HistoryManager.flushProjectChangesAsync
  307. .calledWith(this.project_id)
  308. .should.equal(false)
  309. })
  310. it('should call the callback', function () {
  311. this.callback.called.should.equal(true)
  312. })
  313. })
  314. describe('when adding a doc fails', function () {
  315. beforeEach(function () {
  316. this.error = new Error('error')
  317. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  318. this.ProjectManager.updateProjectWithLocks(
  319. this.project_id,
  320. this.projectHistoryId,
  321. this.user_id,
  322. this.updates,
  323. this.version,
  324. this.source,
  325. this.callback
  326. )
  327. })
  328. it('should call the callback with the error', function () {
  329. this.callback.calledWith(this.error).should.equal(true)
  330. })
  331. })
  332. describe('when adding a file fails', function () {
  333. beforeEach(function () {
  334. this.error = new Error('error')
  335. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  336. this.ProjectManager.updateProjectWithLocks(
  337. this.project_id,
  338. this.projectHistoryId,
  339. this.user_id,
  340. this.updates,
  341. this.version,
  342. this.source,
  343. this.callback
  344. )
  345. })
  346. it('should call the callback with the error', function () {
  347. this.callback.calledWith(this.error).should.equal(true)
  348. })
  349. })
  350. describe('with enough ops to flush', function () {
  351. beforeEach(function () {
  352. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  353. this.ProjectManager.updateProjectWithLocks(
  354. this.project_id,
  355. this.projectHistoryId,
  356. this.user_id,
  357. this.updates,
  358. this.version,
  359. this.source,
  360. this.callback
  361. )
  362. })
  363. it('should flush the history', function () {
  364. this.HistoryManager.flushProjectChangesAsync
  365. .calledWith(this.project_id)
  366. .should.equal(true)
  367. })
  368. })
  369. })
  370. describe('when given an unknown operation type', function () {
  371. beforeEach(function () {
  372. this.updates = [{ type: 'brew-coffee' }]
  373. this.ProjectManager.updateProjectWithLocks(
  374. this.project_id,
  375. this.projectHistoryId,
  376. this.user_id,
  377. this.updates,
  378. this.version,
  379. this.source,
  380. this.callback
  381. )
  382. })
  383. it('should call back with an error', function () {
  384. this.callback.calledWith(sinon.match.instanceOf(Error)).should.be.true
  385. })
  386. })
  387. })
  388. })