updateProjectTests.js 12 KB

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