HistoryV2ManagerTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import HistoryV2Manager from '../../../../frontend/js/ide/history/HistoryV2Manager'
  14. export default describe('HistoryV2Manager', function() {
  15. beforeEach(function(done) {
  16. this.defaultHistoryScope = {
  17. isV2: true,
  18. updates: [],
  19. viewMode: 'point_in_time',
  20. nextBeforeTimestamp: null,
  21. loading: false,
  22. atEnd: false,
  23. userHasFullFeature: undefined,
  24. freeHistoryLimitHit: false,
  25. selection: {
  26. docs: {},
  27. pathname: null,
  28. range: {
  29. fromV: null,
  30. toV: null
  31. },
  32. hoveredRange: {
  33. fromV: null,
  34. toV: null
  35. },
  36. diff: null,
  37. files: [],
  38. file: null
  39. },
  40. error: null,
  41. showOnlyLabels: false,
  42. labels: null,
  43. loadingFileTree: true
  44. }
  45. this.sampleUpdates = [
  46. {
  47. fromV: 4,
  48. toV: 5,
  49. meta: {
  50. users: [
  51. {
  52. first_name: 'john.doe',
  53. last_name: '',
  54. email: 'john.doe@domain.tld',
  55. id: '5b57299087712202fb599ab4',
  56. hue: 200
  57. }
  58. ],
  59. start_ts: 1544021278346,
  60. end_ts: 1544021278346
  61. },
  62. labels: [
  63. {
  64. id: '5c07e822042e67003b448f18',
  65. comment: 'My first label',
  66. version: 5,
  67. user_id: '5b57299087712202fb599ab4',
  68. created_at: '2018-12-05T15:00:50.688Z'
  69. }
  70. ],
  71. pathnames: [],
  72. project_ops: [
  73. {
  74. add: {
  75. pathname: 'chapters/chapter1.tex'
  76. },
  77. atV: 4
  78. }
  79. ]
  80. },
  81. {
  82. fromV: 3,
  83. toV: 4,
  84. meta: {
  85. users: [
  86. {
  87. first_name: 'john.doe',
  88. last_name: '',
  89. email: 'john.doe@domain.tld',
  90. id: '5b57299087712202fb599ab4',
  91. hue: 200
  92. }
  93. ],
  94. start_ts: 1544021262622,
  95. end_ts: 1544021262622
  96. },
  97. labels: [],
  98. pathnames: ['main.tex'],
  99. project_ops: []
  100. },
  101. {
  102. fromV: 0,
  103. toV: 3,
  104. meta: {
  105. users: [
  106. {
  107. first_name: 'john.doe',
  108. last_name: '',
  109. email: 'john.doe@domain.tld',
  110. id: '5b57299087712202fb599ab4',
  111. hue: 200
  112. }
  113. ],
  114. start_ts: 1544021213540,
  115. end_ts: 1544021213618
  116. },
  117. labels: [],
  118. pathnames: [],
  119. project_ops: [
  120. {
  121. add: {
  122. pathname: 'universe.jpg'
  123. },
  124. atV: 2
  125. },
  126. {
  127. add: {
  128. pathname: 'references.bib'
  129. },
  130. atV: 1
  131. },
  132. {
  133. add: {
  134. pathname: 'main.tex'
  135. },
  136. atV: 0
  137. }
  138. ]
  139. }
  140. ]
  141. inject(($q, $http, $filter, $rootScope) => {
  142. this.$scope = $rootScope.$new()
  143. this.$scope.project = {
  144. features: {
  145. versioning: true
  146. }
  147. }
  148. this.$scope.user = {
  149. isAdmin: false
  150. }
  151. this.ide = {
  152. globalEditorWatchdogManager: { attachToEditor() {} },
  153. $q: $q,
  154. $http: $http,
  155. $filter: $filter
  156. }
  157. this.localStorage = sinon.stub().returns(null)
  158. this.historyManager = new HistoryV2Manager(
  159. this.ide,
  160. this.$scope,
  161. this.localStorage
  162. )
  163. done()
  164. })
  165. })
  166. it('should setup the history scope on initialization', function() {
  167. expect(this.$scope.history).to.deep.equal(this.defaultHistoryScope)
  168. })
  169. it('should keep history updates after performing a soft reset', function() {
  170. let historyScopeWithUpdates = Object.assign({}, this.defaultHistoryScope, {
  171. updates: this.sampleUpdates
  172. })
  173. this.$scope.history.updates = this.sampleUpdates
  174. this.historyManager.softReset()
  175. expect(this.$scope.history).to.deep.equal(historyScopeWithUpdates)
  176. })
  177. it('should discard history updates after performing a hard reset', function() {
  178. this.$scope.history.updates = this.sampleUpdates
  179. this.historyManager.hardReset()
  180. expect(this.$scope.history).to.deep.equal(this.defaultHistoryScope)
  181. })
  182. it('should setup history with full access to the feature if the project has versioning', function() {
  183. this.$scope.$digest()
  184. expect(this.$scope.history.userHasFullFeature).to.equal(true)
  185. })
  186. it('should setup history without full access to the feature if the project does not have versioning', function() {
  187. this.$scope.project.features.versioning = false
  188. this.historyManager = new HistoryV2Manager(
  189. this.ide,
  190. this.$scope,
  191. this.localStorage
  192. )
  193. this.$scope.$digest()
  194. expect(this.$scope.history.userHasFullFeature).to.equal(false)
  195. })
  196. it('should setup history with full access to the feature for admin users even if the project does not have versioning', function() {
  197. this.$scope.project.features.versioning = false
  198. this.$scope.user.isAdmin = true
  199. this.historyManager = new HistoryV2Manager(
  200. this.ide,
  201. this.$scope,
  202. this.localStorage
  203. )
  204. this.$scope.$digest()
  205. expect(this.$scope.history.userHasFullFeature).to.equal(true)
  206. })
  207. describe('autoSelectFile', function() {
  208. describe('for compare mode', function() {
  209. beforeEach(function() {
  210. this.mockedFilesList = [
  211. {
  212. pathname: 'main.tex'
  213. },
  214. {
  215. pathname: 'references.bib'
  216. },
  217. {
  218. pathname: 'universe.jpg'
  219. },
  220. {
  221. pathname: 'chapters/chapter2.tex'
  222. },
  223. {
  224. pathname: 'chapters/draft.tex',
  225. operation: 'removed',
  226. deletedAtV: 47
  227. },
  228. {
  229. pathname: 'chapters/chapter3.tex',
  230. operation: 'added'
  231. },
  232. {
  233. pathname: 'chapters/chapter1.tex',
  234. operation: 'edited'
  235. },
  236. {
  237. pathname: 'chapters/foo.tex',
  238. oldPathname: 'chapters/bar.tex',
  239. operation: 'renamed'
  240. }
  241. ]
  242. this.mockedMainTex = this.mockedFilesList[0]
  243. this.mockedReferencesFile = this.mockedFilesList[1]
  244. this.mockedRemovedFile = this.mockedFilesList[4]
  245. this.mockedAddedFile = this.mockedFilesList[5]
  246. this.mockedEditedFile = this.mockedFilesList[6]
  247. this.mockedRenamedFile = this.mockedFilesList[7]
  248. this.$scope.history.viewMode = 'compare'
  249. this.$scope.history.selection.files = this.mockedFilesList
  250. })
  251. describe('with a previously selected file', function() {
  252. it('should prefer the previously selected file if it is available and has operations', function() {
  253. this.historyManager._previouslySelectedPathname = this.mockedAddedFile.pathname
  254. this.historyManager.autoSelectFile()
  255. expect(this.$scope.history.selection.file).to.deep.equal(
  256. this.mockedAddedFile
  257. )
  258. })
  259. it('should prefer a file with ops if the previously selected file is available but has no operations', function() {
  260. this.historyManager._previouslySelectedPathname = this.mockedReferencesFile.pathname
  261. this.historyManager.autoSelectFile()
  262. expect(this.$scope.history.selection.file.operation).to.exist
  263. })
  264. it('should ignore the previously selected file if it is not available', function() {
  265. this.historyManager._previouslySelectedPathname = 'non/existent.file'
  266. this.historyManager.autoSelectFile()
  267. expect(this.$scope.history.selection.file.pathname).to.not.equal(
  268. 'non/existent.file'
  269. )
  270. })
  271. })
  272. describe('without a previously selected file, with a list of files containing operations', function() {
  273. it('should prefer edited files', function() {
  274. this.historyManager.autoSelectFile()
  275. expect(this.$scope.history.selection.file).to.deep.equal(
  276. this.mockedEditedFile
  277. )
  278. })
  279. it('should prefer added files if no edited files are present', function() {
  280. const indexOfEditedFile = this.$scope.history.selection.files.indexOf(
  281. this.mockedEditedFile
  282. )
  283. this.$scope.history.selection.files.splice(indexOfEditedFile, 1)
  284. this.historyManager.autoSelectFile()
  285. expect(this.$scope.history.selection.file).to.deep.equal(
  286. this.mockedAddedFile
  287. )
  288. })
  289. it('should prefer renamed files if no edited or added files are present', function() {
  290. const indexOfEditedFile = this.$scope.history.selection.files.indexOf(
  291. this.mockedEditedFile
  292. )
  293. this.$scope.history.selection.files.splice(indexOfEditedFile, 1)
  294. const indexOfAddedFile = this.$scope.history.selection.files.indexOf(
  295. this.mockedAddedFile
  296. )
  297. this.$scope.history.selection.files.splice(indexOfAddedFile, 1)
  298. this.historyManager.autoSelectFile()
  299. expect(this.$scope.history.selection.file).to.deep.equal(
  300. this.mockedRenamedFile
  301. )
  302. })
  303. it('should prefer removed files if no edited, added or renamed files are present', function() {
  304. const indexOfEditedFile = this.$scope.history.selection.files.indexOf(
  305. this.mockedEditedFile
  306. )
  307. this.$scope.history.selection.files.splice(indexOfEditedFile, 1)
  308. const indexOfAddedFile = this.$scope.history.selection.files.indexOf(
  309. this.mockedAddedFile
  310. )
  311. this.$scope.history.selection.files.splice(indexOfAddedFile, 1)
  312. const indexOfRenamedFile = this.$scope.history.selection.files.indexOf(
  313. this.mockedRenamedFile
  314. )
  315. this.$scope.history.selection.files.splice(indexOfRenamedFile, 1)
  316. this.historyManager.autoSelectFile()
  317. expect(this.$scope.history.selection.file).to.deep.equal(
  318. this.mockedRemovedFile
  319. )
  320. })
  321. })
  322. describe('without a previously selected file, with a list of files without operations', function() {
  323. beforeEach(function() {
  324. this.mockedFilesListWithNoOps = [
  325. {
  326. pathname: 'main.tex'
  327. },
  328. {
  329. pathname: 'references.bib'
  330. },
  331. {
  332. pathname: 'other.tex'
  333. },
  334. {
  335. pathname: 'universe.jpg'
  336. }
  337. ]
  338. this.mockedMainTex = this.mockedFilesListWithNoOps[0]
  339. this.mockedReferencesFile = this.mockedFilesListWithNoOps[1]
  340. this.mockedOtherTexFile = this.mockedFilesListWithNoOps[2]
  341. this.$scope.history.selection.files = this.mockedFilesListWithNoOps
  342. })
  343. it('should prefer main.tex if it exists', function() {
  344. this.historyManager.autoSelectFile()
  345. expect(this.$scope.history.selection.file).to.deep.equal(
  346. this.mockedMainTex
  347. )
  348. })
  349. it('should prefer another tex file if main.tex does not exist', function() {
  350. const indexOfMainTex = this.$scope.history.selection.files.indexOf(
  351. this.mockedMainTex
  352. )
  353. this.$scope.history.selection.files.splice(indexOfMainTex, 1)
  354. this.historyManager.autoSelectFile()
  355. expect(this.$scope.history.selection.file).to.deep.equal(
  356. this.mockedOtherTexFile
  357. )
  358. })
  359. it('should pick the first available file if no tex files are available', function() {
  360. const indexOfMainTex = this.$scope.history.selection.files.indexOf(
  361. this.mockedMainTex
  362. )
  363. this.$scope.history.selection.files.splice(indexOfMainTex, 1)
  364. const indexOfOtherTexFile = this.$scope.history.selection.files.indexOf(
  365. this.mockedOtherTexFile
  366. )
  367. this.$scope.history.selection.files.splice(indexOfOtherTexFile, 1)
  368. this.historyManager.autoSelectFile()
  369. expect(this.$scope.history.selection.file).to.deep.equal(
  370. this.mockedReferencesFile
  371. )
  372. })
  373. })
  374. })
  375. describe('for point-in-time mode', function() {
  376. beforeEach(function() {
  377. this.$scope.history.viewMode = 'point_in_time'
  378. this.sampleUpdates[0] = {
  379. fromV: 4,
  380. toV: 5,
  381. meta: {
  382. users: [
  383. {
  384. first_name: 'john.doe',
  385. last_name: '',
  386. email: 'john.doe@domain.tld',
  387. id: '5b57299087712202fb599ab4',
  388. hue: 200
  389. }
  390. ],
  391. start_ts: 1544021278346,
  392. end_ts: 1544021278346
  393. },
  394. pathnames: ['main.tex'],
  395. project_ops: [
  396. {
  397. add: {
  398. pathname: 'chapters/chapter1.tex'
  399. },
  400. atV: 4
  401. },
  402. {
  403. rename: {
  404. pathname: 'foo.tex',
  405. newPathname: 'bar.tex'
  406. }
  407. }
  408. ]
  409. }
  410. this.sampleUpdateEditedFile = this.sampleUpdates[0].pathnames[0]
  411. this.sampleUpdateAddedFile = this.sampleUpdates[0].project_ops[0].add.pathname
  412. this.sampleUpdateRenamedFile = this.sampleUpdates[0].project_ops[1].rename.newPathname
  413. this.$scope.history.updates = this.sampleUpdates
  414. this.$scope.history.selection.range = {
  415. fromV: this.sampleUpdates[0].toV,
  416. toV: this.sampleUpdates[0].toV
  417. }
  418. this.mockedFilesList = [
  419. {
  420. pathname: 'main.tex'
  421. },
  422. {
  423. pathname: 'references.bib'
  424. },
  425. {
  426. pathname: 'universe.jpg'
  427. },
  428. {
  429. pathname: 'chapters/chapter2.tex'
  430. },
  431. {
  432. pathname: 'chapters/draft.tex'
  433. },
  434. {
  435. pathname: 'chapters/chapter3.tex'
  436. },
  437. {
  438. pathname: 'chapters/chapter1.tex'
  439. },
  440. {
  441. pathname: 'bar.tex'
  442. }
  443. ]
  444. this.$scope.history.selection.files = this.mockedFilesList
  445. })
  446. describe('with a previously selected file', function() {
  447. it('should prefer the previously selected file if it is available and has operations', function() {
  448. this.historyManager._previouslySelectedPathname = 'main.tex'
  449. this.historyManager.autoSelectFile()
  450. expect(this.$scope.history.selection.file.pathname).to.equal(
  451. this.sampleUpdateEditedFile
  452. )
  453. })
  454. it('should prefer a file with ops if the previously selected file is available but has no operations', function() {
  455. this.historyManager._previouslySelectedPathname = 'main.tex'
  456. this.sampleUpdates[0].pathnames = []
  457. this.historyManager.autoSelectFile()
  458. expect(this.$scope.history.selection.file.pathname).to.equal(
  459. this.sampleUpdateAddedFile
  460. )
  461. })
  462. it('should ignore the previously selected file if it is not available', function() {
  463. this.historyManager._previouslySelectedPathname = 'non/existent.file'
  464. this.historyManager.autoSelectFile()
  465. expect(this.$scope.history.selection.file.pathname).to.not.equal(
  466. 'non/existent.file'
  467. )
  468. })
  469. })
  470. describe('without a previously selected file, with a list of files containing operations', function() {
  471. it('should prefer edited files', function() {
  472. this.historyManager.autoSelectFile()
  473. expect(this.$scope.history.selection.file.pathname).to.equal(
  474. this.sampleUpdateEditedFile
  475. )
  476. })
  477. it('should prefer added files if no edited files are present', function() {
  478. this.sampleUpdates[0].pathnames = []
  479. this.historyManager.autoSelectFile()
  480. expect(this.$scope.history.selection.file.pathname).to.equal(
  481. this.sampleUpdateAddedFile
  482. )
  483. })
  484. it('should prefer renamed files if no edited or added files are present', function() {
  485. this.sampleUpdates[0].pathnames = []
  486. this.sampleUpdates[0].project_ops.shift()
  487. this.historyManager.autoSelectFile()
  488. expect(this.$scope.history.selection.file.pathname).to.equal(
  489. this.sampleUpdateRenamedFile
  490. )
  491. })
  492. })
  493. describe('without a previously selected file, with a list of files without operations', function() {
  494. beforeEach(function() {
  495. this.sampleUpdates[0].pathnames = []
  496. this.sampleUpdates[0].project_ops = []
  497. this.mockedMainTex = this.mockedFilesList[0]
  498. this.mockedReferencesFile = this.mockedFilesList[1]
  499. })
  500. it('should prefer main.tex if it exists', function() {
  501. this.historyManager.autoSelectFile()
  502. expect(this.$scope.history.selection.file.pathname).to.equal(
  503. this.mockedMainTex.pathname
  504. )
  505. })
  506. it('should prefer another tex file if main.tex does not exist', function() {
  507. const indexOfMainTex = this.$scope.history.selection.files.indexOf(
  508. this.mockedMainTex
  509. )
  510. this.$scope.history.selection.files.splice(indexOfMainTex, 1)
  511. this.historyManager.autoSelectFile()
  512. expect(this.$scope.history.selection.file.pathname).to.match(/.tex$/)
  513. })
  514. })
  515. })
  516. describe('_loadLabels', function() {
  517. it('should return labels list as is if there is a label for the last version', function() {
  518. const labels = [
  519. {
  520. id: '1',
  521. version: 1,
  522. comment: 'foo',
  523. created_at: new Date().toISOString()
  524. },
  525. {
  526. id: '2',
  527. version: 2,
  528. comment: 'bar',
  529. created_at: new Date().toISOString()
  530. },
  531. {
  532. id: '3',
  533. version: 3,
  534. comment: 'baz',
  535. created_at: new Date().toISOString()
  536. }
  537. ]
  538. const lastUpdate = 3
  539. const labelsResult = this.historyManager._loadLabels(labels, lastUpdate)
  540. expect(labelsResult).to.have.members(labels)
  541. })
  542. it('should return a labels list with a pseudo current state label if there is no label for the last version', function() {
  543. const labels = [
  544. {
  545. id: '1',
  546. version: 1,
  547. comment: 'foo',
  548. created_at: new Date().toISOString()
  549. },
  550. {
  551. id: '2',
  552. version: 2,
  553. comment: 'bar',
  554. created_at: new Date().toISOString()
  555. },
  556. {
  557. id: '3',
  558. version: 3,
  559. comment: 'baz',
  560. created_at: new Date().toISOString()
  561. }
  562. ]
  563. const lastUpdate = 5
  564. const labelsResult = this.historyManager._loadLabels(labels, lastUpdate)
  565. expect(labelsResult).to.include.members(labels)
  566. expect(labelsResult[0].isPseudoCurrentStateLabel).to.equal(true)
  567. expect(labelsResult[0].version).to.equal(5)
  568. })
  569. it('should keep pseudo label when deleting label', function() {
  570. this.historyManager.$scope.history.labels = [
  571. {
  572. id: '1',
  573. version: 1,
  574. comment: 'foo',
  575. created_at: new Date().toISOString()
  576. }
  577. ]
  578. const lastUpdate = 5
  579. this.historyManager.$scope.history.labels = this.historyManager._loadLabels(
  580. this.historyManager.$scope.history.labels,
  581. lastUpdate
  582. )
  583. expect(
  584. this.historyManager.$scope.history.labels[0].isPseudoCurrentStateLabel
  585. ).to.equal(true)
  586. this.historyManager.$scope.history.labels = this.historyManager._loadLabels(
  587. [],
  588. lastUpdate
  589. )
  590. expect(
  591. this.historyManager.$scope.history.labels[0].isPseudoCurrentStateLabel
  592. ).to.equal(true)
  593. expect(this.historyManager.$scope.history.labels[0].version).to.equal(5)
  594. })
  595. })
  596. })
  597. })