updateProjectTests.js 11 KB

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