ProjectDuplicator.test.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import mongodb from 'mongodb-legacy'
  4. const { ObjectId } = mongodb
  5. const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDuplicator.mjs'
  6. describe('ProjectDuplicator', function () {
  7. beforeEach(async function (ctx) {
  8. ctx.doc0 = { _id: 'doc0_id', name: 'rootDocHere' }
  9. ctx.doc1 = { _id: 'doc1_id', name: 'level1folderDocName' }
  10. ctx.doc2 = { _id: 'doc2_id', name: 'level2folderDocName' }
  11. ctx.doc0Lines = ['zero']
  12. ctx.doc1Lines = ['one']
  13. ctx.doc2Lines = ['two']
  14. ctx.file0 = { name: 'file0', _id: 'file0', hash: 'abcde' }
  15. ctx.file1 = { name: 'file1', _id: 'file1', hash: 'fffff' }
  16. ctx.file2 = {
  17. name: 'file2',
  18. _id: 'file2',
  19. created: '2024-07-05T14:18:31.401+00:00',
  20. linkedFileData: { provider: 'url' },
  21. hash: '123456',
  22. }
  23. ctx.level2folder = {
  24. name: 'level2folderName',
  25. _id: 'level2folderId',
  26. docs: [ctx.doc2, undefined],
  27. folders: [],
  28. fileRefs: [ctx.file2],
  29. }
  30. ctx.level1folder = {
  31. name: 'level1folder',
  32. _id: 'level1folderId',
  33. docs: [ctx.doc1],
  34. folders: [ctx.level2folder],
  35. fileRefs: [ctx.file1, null], // the null is intentional to test null docs/files
  36. }
  37. ctx.rootFolder = {
  38. name: 'rootFolder',
  39. _id: 'rootFolderId',
  40. docs: [ctx.doc0],
  41. folders: [ctx.level1folder, {}],
  42. fileRefs: [ctx.file0],
  43. }
  44. ctx.project = {
  45. _id: 'this_is_the_old_project_id',
  46. rootDoc_id: ctx.doc0._id,
  47. rootFolder: [ctx.rootFolder],
  48. compiler: 'this_is_a_Compiler',
  49. overleaf: { history: { id: 123456 } },
  50. }
  51. ctx.doc0Path = '/rootDocHere'
  52. ctx.doc1Path = '/level1folder/level1folderDocName'
  53. ctx.doc2Path = '/level1folder/level2folderName/level2folderDocName'
  54. ctx.file0Path = '/file0'
  55. ctx.file1Path = '/level1folder/file1'
  56. ctx.file2Path = '/level1folder/level2folderName/file2'
  57. ctx.docContents = [
  58. { _id: ctx.doc0._id, lines: ctx.doc0Lines },
  59. { _id: ctx.doc1._id, lines: ctx.doc1Lines },
  60. { _id: ctx.doc2._id, lines: ctx.doc2Lines },
  61. ]
  62. ctx.rootDoc = ctx.doc0
  63. ctx.rootDocPath = '/rootDocHere'
  64. ctx.owner = { _id: 'this_is_the_owner' }
  65. ctx.newBlankProject = {
  66. _id: 'new_project_id',
  67. overleaf: { history: { id: 339123 } },
  68. readOnly_refs: [],
  69. collaberator_refs: [],
  70. rootFolder: [{ _id: 'new_root_folder_id' }],
  71. }
  72. ctx.newFolder = { _id: 'newFolderId' }
  73. ctx.filestoreUrl = 'filestore-url'
  74. ctx.newProjectVersion = 2
  75. ctx.newDocId = new ObjectId()
  76. ctx.newFileId = new ObjectId()
  77. ctx.newDoc0 = { ...ctx.doc0, _id: ctx.newDocId }
  78. ctx.newDoc1 = { ...ctx.doc1, _id: ctx.newDocId }
  79. ctx.newDoc2 = { ...ctx.doc2, _id: ctx.newDocId }
  80. ctx.newFile0 = { ...ctx.file0, _id: ctx.newFileId }
  81. ctx.newFile1 = { ...ctx.file1, _id: ctx.newFileId }
  82. ctx.newFile2 = { ...ctx.file2, _id: ctx.newFileId }
  83. ctx.docEntries = [
  84. {
  85. path: ctx.doc0Path,
  86. doc: ctx.newDoc0,
  87. docLines: ctx.doc0Lines.join('\n'),
  88. },
  89. {
  90. path: ctx.doc1Path,
  91. doc: ctx.newDoc1,
  92. docLines: ctx.doc1Lines.join('\n'),
  93. },
  94. {
  95. path: ctx.doc2Path,
  96. doc: ctx.newDoc2,
  97. docLines: ctx.doc2Lines.join('\n'),
  98. },
  99. ]
  100. ctx.fileEntries = [
  101. {
  102. createdBlob: true,
  103. path: ctx.file0Path,
  104. file: ctx.newFile0,
  105. },
  106. {
  107. createdBlob: true,
  108. path: ctx.file1Path,
  109. file: ctx.newFile1,
  110. },
  111. {
  112. createdBlob: true,
  113. path: ctx.file2Path,
  114. file: ctx.newFile2,
  115. },
  116. ]
  117. ctx.Doc = sinon.stub().callsFake(props => ({ _id: ctx.newDocId, ...props }))
  118. ctx.File = sinon
  119. .stub()
  120. .callsFake(props => ({ _id: ctx.newFileId, ...props }))
  121. ctx.DocstoreManager = {
  122. promises: {
  123. updateDoc: sinon.stub().resolves(),
  124. getAllDocs: sinon.stub().resolves(ctx.docContents),
  125. },
  126. }
  127. ctx.DocumentUpdaterHandler = {
  128. promises: {
  129. flushProjectToMongo: sinon.stub().resolves(),
  130. updateProjectStructure: sinon.stub().resolves(),
  131. },
  132. }
  133. ctx.HistoryManager = {
  134. promises: {
  135. copyBlob: sinon.stub().callsFake((historyId, newHistoryId, hash) => {
  136. if (hash === '500') {
  137. return Promise.reject(new Error('copy blob error'))
  138. }
  139. return Promise.resolve()
  140. }),
  141. },
  142. }
  143. ctx.TagsHandler = {
  144. promises: {
  145. addProjectToTags: sinon.stub().resolves({
  146. _id: 'project-1',
  147. }),
  148. countTagsForProject: sinon.stub().resolves(1),
  149. },
  150. }
  151. ctx.ProjectCreationHandler = {
  152. promises: {
  153. createBlankProject: sinon.stub().resolves(ctx.newBlankProject),
  154. },
  155. }
  156. ctx.ProjectDeleter = {
  157. promises: {
  158. deleteProject: sinon.stub().resolves(),
  159. },
  160. }
  161. ctx.ProjectEntityMongoUpdateHandler = {
  162. promises: {
  163. createNewFolderStructure: sinon.stub().resolves(ctx.newProjectVersion),
  164. },
  165. }
  166. ctx.ProjectEntityUpdateHandler = {
  167. isPathValidForRootDoc: sinon.stub().returns(true),
  168. promises: {
  169. setRootDoc: sinon.stub().resolves(),
  170. },
  171. }
  172. ctx.ProjectGetter = {
  173. promises: {
  174. getProject: sinon
  175. .stub()
  176. .withArgs(ctx.project._id)
  177. .resolves(ctx.project),
  178. },
  179. }
  180. ctx.ProjectLocator = {
  181. promises: {
  182. findRootDoc: sinon.stub().resolves({
  183. element: ctx.rootDoc,
  184. path: { fileSystem: ctx.rootDocPath },
  185. }),
  186. findElementByPath: sinon
  187. .stub()
  188. .withArgs({
  189. project_id: ctx.newBlankProject._id,
  190. path: ctx.rootDocPath,
  191. exactCaseMatch: true,
  192. })
  193. .resolves({ element: ctx.doc0 }),
  194. },
  195. }
  196. ctx.ProjectOptionsHandler = {
  197. promises: {
  198. setCompiler: sinon.stub().resolves(),
  199. },
  200. }
  201. ctx.TpdsProjectFlusher = {
  202. promises: {
  203. flushProjectToTpds: sinon.stub().resolves(),
  204. },
  205. }
  206. ctx.Modules = {
  207. promises: {
  208. hooks: {
  209. fire: sinon.stub().resolves([]),
  210. },
  211. },
  212. }
  213. vi.doMock('../../../../app/src/models/Doc', () => ({
  214. Doc: ctx.Doc,
  215. }))
  216. vi.doMock('../../../../app/src/models/File', () => ({
  217. File: ctx.File,
  218. }))
  219. vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
  220. default: ctx.DocstoreManager,
  221. }))
  222. vi.doMock(
  223. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
  224. () => ({
  225. default: ctx.DocumentUpdaterHandler,
  226. })
  227. )
  228. vi.doMock(
  229. '../../../../app/src/Features/Project/ProjectCreationHandler',
  230. () => ({
  231. default: ctx.ProjectCreationHandler,
  232. })
  233. )
  234. vi.doMock('../../../../app/src/Features/Project/ProjectDeleter', () => ({
  235. default: ctx.ProjectDeleter,
  236. }))
  237. vi.doMock(
  238. '../../../../app/src/Features/Project/ProjectEntityMongoUpdateHandler',
  239. () => ({
  240. default: ctx.ProjectEntityMongoUpdateHandler,
  241. })
  242. )
  243. vi.doMock(
  244. '../../../../app/src/Features/Project/ProjectEntityUpdateHandler',
  245. () => ({
  246. default: ctx.ProjectEntityUpdateHandler,
  247. })
  248. )
  249. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  250. default: ctx.ProjectGetter,
  251. }))
  252. vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
  253. default: ctx.ProjectLocator,
  254. }))
  255. vi.doMock(
  256. '../../../../app/src/Features/Project/ProjectOptionsHandler',
  257. () => ({
  258. default: ctx.ProjectOptionsHandler,
  259. })
  260. )
  261. vi.doMock(
  262. '../../../../app/src/Features/ThirdPartyDataStore/TpdsProjectFlusher',
  263. () => ({
  264. default: ctx.TpdsProjectFlusher,
  265. })
  266. )
  267. vi.doMock('../../../../app/src/Features/Tags/TagsHandler', () => ({
  268. default: ctx.TagsHandler,
  269. }))
  270. vi.doMock('../../../../app/src/Features/History/HistoryManager', () => ({
  271. default: ctx.HistoryManager,
  272. }))
  273. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  274. default: ctx.Modules,
  275. }))
  276. vi.doMock('../../../../app/src/Features/Compile/ClsiCacheManager', () => ({
  277. default: {
  278. prepareClsiCache: sinon.stub().rejects(new Error('ignore this')),
  279. },
  280. }))
  281. ctx.ProjectDuplicator = (await import(MODULE_PATH)).default
  282. })
  283. describe('when the copy succeeds', function () {
  284. beforeEach(async function (ctx) {
  285. ctx.newProjectName = 'New project name'
  286. ctx.newProject = await ctx.ProjectDuplicator.promises.duplicate(
  287. ctx.owner,
  288. ctx.project._id,
  289. ctx.newProjectName
  290. )
  291. })
  292. it('should flush the original project to mongo', function (ctx) {
  293. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  294. ctx.project._id
  295. )
  296. })
  297. it('should copy docs to docstore', function (ctx) {
  298. for (const docLines of [ctx.doc0Lines, ctx.doc1Lines, ctx.doc2Lines]) {
  299. ctx.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  300. ctx.newProject._id.toString(),
  301. ctx.newDocId.toString(),
  302. docLines,
  303. 0,
  304. {}
  305. )
  306. }
  307. })
  308. it('should duplicate the files with hashes by copying the blobs in history v1', function (ctx) {
  309. for (const file of [ctx.file0, ctx.file1, ctx.file2]) {
  310. ctx.HistoryManager.promises.copyBlob.should.have.been.calledWith(
  311. ctx.project.overleaf.history.id,
  312. ctx.newProject.overleaf.history.id,
  313. file.hash
  314. )
  315. }
  316. })
  317. it('should create a blank project', function (ctx) {
  318. ctx.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  319. ctx.owner._id,
  320. ctx.newProjectName
  321. )
  322. ctx.newProject._id.should.equal(ctx.newBlankProject._id)
  323. })
  324. it('should use the same compiler', function (ctx) {
  325. ctx.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  326. ctx.newProject._id,
  327. ctx.project.compiler
  328. )
  329. })
  330. it('should use the same root doc', function (ctx) {
  331. ctx.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  332. ctx.newProject._id,
  333. ctx.rootFolder.docs[0]._id
  334. )
  335. })
  336. it('should not copy the collaborators or read only refs', function (ctx) {
  337. ctx.newProject.collaberator_refs.length.should.equal(0)
  338. ctx.newProject.readOnly_refs.length.should.equal(0)
  339. })
  340. it('should copy all documents and files', function (ctx) {
  341. ctx.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  342. ctx.newProject._id,
  343. ctx.docEntries,
  344. ctx.fileEntries
  345. )
  346. })
  347. it('should notify document updater of changes', function (ctx) {
  348. ctx.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  349. ctx.newProject._id,
  350. ctx.newProject.overleaf.history.id,
  351. ctx.owner._id,
  352. {
  353. newDocs: ctx.docEntries,
  354. newFiles: ctx.fileEntries,
  355. newProject: { version: ctx.newProjectVersion },
  356. },
  357. null
  358. )
  359. })
  360. it('should flush the project to TPDS', function (ctx) {
  361. ctx.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  362. ctx.newProject._id
  363. )
  364. })
  365. })
  366. describe('without a root doc', function () {
  367. beforeEach(async function (ctx) {
  368. ctx.ProjectLocator.promises.findRootDoc.resolves({
  369. element: null,
  370. path: null,
  371. })
  372. ctx.newProject = await ctx.ProjectDuplicator.promises.duplicate(
  373. ctx.owner,
  374. ctx.project._id,
  375. 'Copy of project'
  376. )
  377. })
  378. it('should not set the root doc on the copy', function (ctx) {
  379. ctx.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  380. .called
  381. })
  382. })
  383. describe('with an invalid root doc', function () {
  384. beforeEach(async function (ctx) {
  385. ctx.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  386. ctx.newProject = await ctx.ProjectDuplicator.promises.duplicate(
  387. ctx.owner,
  388. ctx.project._id,
  389. 'Copy of project'
  390. )
  391. })
  392. it('should not set the root doc on the copy', function (ctx) {
  393. ctx.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  394. .called
  395. })
  396. })
  397. describe('when cloning in history-v1 fails', function () {
  398. it('should fail the clone operation', async function (ctx) {
  399. ctx.file0.hash = '500'
  400. await expect(
  401. ctx.ProjectDuplicator.promises.duplicate(
  402. ctx.owner,
  403. ctx.project._id,
  404. 'name'
  405. )
  406. ).to.be.rejectedWith('copy blob error')
  407. })
  408. })
  409. describe('when there is an error', function () {
  410. beforeEach(async function (ctx) {
  411. ctx.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  412. await expect(
  413. ctx.ProjectDuplicator.promises.duplicate(ctx.owner, ctx.project._id, '')
  414. ).to.be.rejected
  415. })
  416. it('should delete the broken cloned project', function (ctx) {
  417. ctx.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  418. ctx.newBlankProject._id,
  419. { deletedReason: 'clone-failure' }
  420. )
  421. })
  422. it('should not delete the original project', function (ctx) {
  423. ctx.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  424. ctx.project._id
  425. )
  426. })
  427. })
  428. })