FileTreeDiffTests.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* eslint-disable
  2. no-undef,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import sinon from 'sinon'
  14. import { expect } from 'chai'
  15. import Settings from '@overleaf/settings'
  16. import request from 'request'
  17. import assert from 'assert'
  18. import Path from 'path'
  19. import crypto from 'crypto'
  20. import { ObjectId } from 'mongodb'
  21. import nock from 'nock'
  22. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  23. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  24. import * as HistoryId from './helpers/HistoryId.js'
  25. const MockHistoryStore = () => nock('http://localhost:3100')
  26. const MockFileStore = () => nock('http://localhost:3009')
  27. const MockWeb = () => nock('http://localhost:3000')
  28. const sha = data => crypto.createHash('sha1').update(data).digest('hex')
  29. describe('FileTree Diffs', function () {
  30. beforeEach(function (done) {
  31. return ProjectHistoryApp.ensureRunning(error => {
  32. if (error != null) {
  33. throw error
  34. }
  35. this.historyId = ObjectId().toString()
  36. this.projectId = ObjectId().toString()
  37. MockHistoryStore().post('/api/projects').reply(200, {
  38. projectId: this.historyId,
  39. })
  40. MockWeb()
  41. .get(`/project/${this.projectId}/details`)
  42. .reply(200, {
  43. name: 'Test Project',
  44. overleaf: { history: { id: this.historyId } },
  45. })
  46. return ProjectHistoryClient.initializeProject(
  47. this.historyId,
  48. (error, olProject) => {
  49. if (error != null) {
  50. throw error
  51. }
  52. return done()
  53. }
  54. )
  55. })
  56. })
  57. afterEach(function () {
  58. return nock.cleanAll()
  59. })
  60. it('should return a diff of the updates to a doc from a single chunk', function (done) {
  61. MockHistoryStore()
  62. .get(`/api/projects/${this.historyId}/versions/7/history`)
  63. .reply(200, {
  64. chunk: {
  65. history: {
  66. snapshot: {
  67. files: {
  68. 'foo.tex': {
  69. hash: sha('mock-sha-foo'),
  70. stringLength: 42,
  71. },
  72. 'renamed.tex': {
  73. hash: sha('mock-sha-renamed'),
  74. stringLength: 42,
  75. },
  76. 'deleted.tex': {
  77. hash: sha('mock-sha-deleted'),
  78. stringLength: 42,
  79. },
  80. },
  81. },
  82. changes: [
  83. {
  84. operations: [
  85. {
  86. pathname: 'renamed.tex',
  87. newPathname: 'newName.tex',
  88. },
  89. ],
  90. timestamp: '2017-12-04T10:29:17.786Z',
  91. authors: [31],
  92. },
  93. {
  94. operations: [
  95. {
  96. pathname: 'foo.tex',
  97. textOperation: ['lorem ipsum'],
  98. },
  99. ],
  100. timestamp: '2017-12-04T10:29:17.786Z',
  101. authors: [31],
  102. },
  103. {
  104. operations: [
  105. {
  106. pathname: 'deleted.tex',
  107. newPathname: '',
  108. },
  109. ],
  110. timestamp: '2017-12-04T10:29:22.905Z',
  111. authors: [31],
  112. },
  113. {
  114. operations: [
  115. {
  116. file: {
  117. hash: sha('new-sha'),
  118. },
  119. pathname: 'added.tex',
  120. },
  121. ],
  122. timestamp: '2017-12-04T10:29:22.905Z',
  123. authors: [31],
  124. },
  125. ],
  126. },
  127. startVersion: 3,
  128. },
  129. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  130. })
  131. return ProjectHistoryClient.getFileTreeDiff(
  132. this.projectId,
  133. 3,
  134. 7,
  135. (error, diff) => {
  136. if (error != null) {
  137. throw error
  138. }
  139. expect(diff).to.deep.equal({
  140. diff: [
  141. {
  142. pathname: 'foo.tex',
  143. operation: 'edited',
  144. },
  145. {
  146. pathname: 'deleted.tex',
  147. operation: 'removed',
  148. deletedAtV: 5,
  149. },
  150. {
  151. newPathname: 'newName.tex',
  152. pathname: 'renamed.tex',
  153. operation: 'renamed',
  154. },
  155. {
  156. pathname: 'added.tex',
  157. operation: 'added',
  158. },
  159. ],
  160. })
  161. return done()
  162. }
  163. )
  164. })
  165. it('should return a diff of the updates to a doc across multiple chunks', function (done) {
  166. MockHistoryStore()
  167. .get(`/api/projects/${this.historyId}/versions/5/history`)
  168. .reply(200, {
  169. chunk: {
  170. history: {
  171. snapshot: {
  172. files: {
  173. 'foo.tex': {
  174. // Updated in this chunk
  175. hash: sha('mock-sha-foo'),
  176. stringLength: 42,
  177. },
  178. 'bar.tex': {
  179. // Updated in the next chunk
  180. hash: sha('mock-sha-bar'),
  181. stringLength: 42,
  182. },
  183. 'baz.tex': {
  184. // Not updated
  185. hash: sha('mock-sha-bar'),
  186. stringLength: 42,
  187. },
  188. 'renamed.tex': {
  189. hash: sha('mock-sha-renamed'),
  190. stringLength: 42,
  191. },
  192. 'deleted.tex': {
  193. hash: sha('mock-sha-deleted'),
  194. stringLength: 42,
  195. },
  196. },
  197. },
  198. changes: [
  199. {
  200. operations: [
  201. {
  202. pathname: 'renamed.tex',
  203. newPathname: 'newName.tex',
  204. },
  205. ],
  206. timestamp: '2017-12-04T10:29:17.786Z',
  207. authors: [31],
  208. },
  209. {
  210. operations: [
  211. {
  212. pathname: 'foo.tex',
  213. textOperation: ['lorem ipsum'],
  214. },
  215. ],
  216. timestamp: '2017-12-04T10:29:19.786Z',
  217. authors: [31],
  218. },
  219. {
  220. operations: [
  221. {
  222. pathname: 'deleted.tex',
  223. newPathname: '',
  224. },
  225. ],
  226. timestamp: '2017-12-04T10:29:22.905Z',
  227. authors: [31],
  228. },
  229. ],
  230. },
  231. startVersion: 2,
  232. },
  233. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  234. })
  235. MockHistoryStore()
  236. .get(`/api/projects/${this.historyId}/versions/7/history`)
  237. .reply(200, {
  238. chunk: {
  239. history: {
  240. snapshot: {
  241. files: {
  242. 'foo.tex': {
  243. hash: sha('mock-sha-foo'),
  244. stringLength: 42,
  245. },
  246. 'baz.tex': {
  247. hash: sha('mock-sha-bar'),
  248. stringLength: 42,
  249. },
  250. 'newName.tex': {
  251. hash: sha('mock-sha-renamed'),
  252. stringLength: 42,
  253. },
  254. },
  255. },
  256. changes: [
  257. {
  258. operations: [
  259. {
  260. file: {
  261. hash: sha('new-sha'),
  262. },
  263. pathname: 'added.tex',
  264. },
  265. ],
  266. timestamp: '2017-12-04T10:29:22.905Z',
  267. authors: [31],
  268. },
  269. {
  270. operations: [
  271. {
  272. pathname: 'bar.tex',
  273. textOperation: ['lorem ipsum'],
  274. },
  275. ],
  276. timestamp: '2017-12-04T10:29:23.786Z',
  277. authors: [31],
  278. },
  279. ],
  280. },
  281. startVersion: 5,
  282. },
  283. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  284. })
  285. return ProjectHistoryClient.getFileTreeDiff(
  286. this.projectId,
  287. 2,
  288. 7,
  289. (error, diff) => {
  290. if (error != null) {
  291. throw error
  292. }
  293. expect(diff).to.deep.equal({
  294. diff: [
  295. {
  296. pathname: 'foo.tex',
  297. operation: 'edited',
  298. },
  299. {
  300. pathname: 'bar.tex',
  301. operation: 'edited',
  302. },
  303. {
  304. pathname: 'baz.tex',
  305. },
  306. {
  307. pathname: 'deleted.tex',
  308. operation: 'removed',
  309. deletedAtV: 4,
  310. },
  311. {
  312. newPathname: 'newName.tex',
  313. pathname: 'renamed.tex',
  314. operation: 'renamed',
  315. },
  316. {
  317. pathname: 'added.tex',
  318. operation: 'added',
  319. },
  320. ],
  321. })
  322. return done()
  323. }
  324. )
  325. })
  326. it('should return a diff that includes multiple renames', function (done) {
  327. MockHistoryStore()
  328. .get(`/api/projects/${this.historyId}/versions/5/history`)
  329. .reply(200, {
  330. chunk: {
  331. history: {
  332. snapshot: {
  333. files: {
  334. 'one.tex': {
  335. hash: sha('mock-sha'),
  336. stringLength: 42,
  337. },
  338. },
  339. },
  340. changes: [
  341. {
  342. operations: [
  343. {
  344. pathname: 'one.tex',
  345. newPathname: 'two.tex',
  346. },
  347. ],
  348. timestamp: '2017-12-04T10:29:17.786Z',
  349. authors: [31],
  350. },
  351. {
  352. operations: [
  353. {
  354. pathname: 'two.tex',
  355. newPathname: 'three.tex',
  356. },
  357. ],
  358. timestamp: '2017-12-04T10:29:22.905Z',
  359. authors: [31],
  360. },
  361. ],
  362. },
  363. startVersion: 3,
  364. },
  365. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  366. })
  367. return ProjectHistoryClient.getFileTreeDiff(
  368. this.projectId,
  369. 3,
  370. 5,
  371. (error, diff) => {
  372. if (error != null) {
  373. throw error
  374. }
  375. expect(diff).to.deep.equal({
  376. diff: [
  377. {
  378. newPathname: 'three.tex',
  379. pathname: 'one.tex',
  380. operation: 'renamed',
  381. },
  382. ],
  383. })
  384. return done()
  385. }
  386. )
  387. })
  388. it('should handle deleting the re-adding a file', function (done) {
  389. MockHistoryStore()
  390. .get(`/api/projects/${this.historyId}/versions/5/history`)
  391. .reply(200, {
  392. chunk: {
  393. history: {
  394. snapshot: {
  395. files: {
  396. 'one.tex': {
  397. hash: sha('mock-sha'),
  398. stringLength: 42,
  399. },
  400. },
  401. },
  402. changes: [
  403. {
  404. operations: [
  405. {
  406. pathname: 'one.tex',
  407. newPathname: '',
  408. },
  409. ],
  410. timestamp: '2017-12-04T10:29:17.786Z',
  411. authors: [31],
  412. },
  413. {
  414. operations: [
  415. {
  416. pathname: 'one.tex',
  417. file: {
  418. hash: sha('mock-sha'),
  419. },
  420. },
  421. ],
  422. timestamp: '2017-12-04T10:29:22.905Z',
  423. authors: [31],
  424. },
  425. ],
  426. },
  427. startVersion: 3,
  428. },
  429. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  430. })
  431. return ProjectHistoryClient.getFileTreeDiff(
  432. this.projectId,
  433. 3,
  434. 5,
  435. (error, diff) => {
  436. if (error != null) {
  437. throw error
  438. }
  439. expect(diff).to.deep.equal({
  440. diff: [
  441. {
  442. pathname: 'one.tex',
  443. },
  444. ],
  445. })
  446. return done()
  447. }
  448. )
  449. })
  450. it('should handle deleting the renaming a file to the same place', function (done) {
  451. MockHistoryStore()
  452. .get(`/api/projects/${this.historyId}/versions/5/history`)
  453. .reply(200, {
  454. chunk: {
  455. history: {
  456. snapshot: {
  457. files: {
  458. 'one.tex': {
  459. hash: sha('mock-sha-one'),
  460. stringLength: 42,
  461. },
  462. 'two.tex': {
  463. hash: sha('mock-sha-two'),
  464. stringLength: 42,
  465. },
  466. },
  467. },
  468. changes: [
  469. {
  470. operations: [
  471. {
  472. pathname: 'one.tex',
  473. newPathname: '',
  474. },
  475. ],
  476. timestamp: '2017-12-04T10:29:17.786Z',
  477. authors: [31],
  478. },
  479. {
  480. operations: [
  481. {
  482. pathname: 'two.tex',
  483. newPathname: 'one.tex',
  484. },
  485. ],
  486. timestamp: '2017-12-04T10:29:22.905Z',
  487. authors: [31],
  488. },
  489. ],
  490. },
  491. startVersion: 3,
  492. },
  493. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  494. })
  495. return ProjectHistoryClient.getFileTreeDiff(
  496. this.projectId,
  497. 3,
  498. 5,
  499. (error, diff) => {
  500. if (error != null) {
  501. throw error
  502. }
  503. expect(diff).to.deep.equal({
  504. diff: [
  505. {
  506. pathname: 'two.tex',
  507. newPathname: 'one.tex',
  508. operation: 'renamed',
  509. },
  510. ],
  511. })
  512. return done()
  513. }
  514. )
  515. })
  516. it('should handle adding then renaming a file', function (done) {
  517. MockHistoryStore()
  518. .get(`/api/projects/${this.historyId}/versions/5/history`)
  519. .reply(200, {
  520. chunk: {
  521. history: {
  522. snapshot: {
  523. files: {},
  524. },
  525. changes: [
  526. {
  527. operations: [
  528. {
  529. pathname: 'one.tex',
  530. file: {
  531. hash: sha('mock-sha'),
  532. },
  533. },
  534. ],
  535. timestamp: '2017-12-04T10:29:17.786Z',
  536. authors: [31],
  537. },
  538. {
  539. operations: [
  540. {
  541. pathname: 'one.tex',
  542. newPathname: 'two.tex',
  543. },
  544. ],
  545. timestamp: '2017-12-04T10:29:22.905Z',
  546. authors: [31],
  547. },
  548. ],
  549. },
  550. startVersion: 3,
  551. },
  552. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  553. })
  554. return ProjectHistoryClient.getFileTreeDiff(
  555. this.projectId,
  556. 3,
  557. 5,
  558. (error, diff) => {
  559. if (error != null) {
  560. throw error
  561. }
  562. expect(diff).to.deep.equal({
  563. diff: [
  564. {
  565. pathname: 'two.tex',
  566. operation: 'added',
  567. },
  568. ],
  569. })
  570. return done()
  571. }
  572. )
  573. })
  574. it('should return 422 with a chunk with an invalid rename', function (done) {
  575. MockHistoryStore()
  576. .get(`/api/projects/${this.historyId}/versions/6/history`)
  577. .reply(200, {
  578. chunk: {
  579. history: {
  580. snapshot: {
  581. files: {
  582. 'foo.tex': {
  583. hash: sha('mock-sha-foo'),
  584. stringLength: 42,
  585. },
  586. 'bar.tex': {
  587. hash: sha('mock-sha-bar'),
  588. stringLength: 42,
  589. },
  590. },
  591. },
  592. changes: [
  593. {
  594. operations: [
  595. {
  596. pathname: 'foo.tex',
  597. newPathname: 'bar.tex',
  598. },
  599. ],
  600. timestamp: '2017-12-04T10:29:17.786Z',
  601. authors: [31],
  602. },
  603. ],
  604. },
  605. startVersion: 5,
  606. },
  607. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  608. })
  609. return ProjectHistoryClient.getFileTreeDiff(
  610. this.projectId,
  611. 5,
  612. 6,
  613. (error, diff, statusCode) => {
  614. if (error != null) {
  615. throw error
  616. }
  617. expect(statusCode).to.equal(422)
  618. return done()
  619. }
  620. )
  621. })
  622. it('should return 422 with a chunk with an invalid add', function (done) {
  623. MockHistoryStore()
  624. .get(`/api/projects/${this.historyId}/versions/6/history`)
  625. .reply(200, {
  626. chunk: {
  627. history: {
  628. snapshot: {
  629. files: {
  630. 'foo.tex': {
  631. hash: sha('mock-sha-foo'),
  632. stringLength: 42,
  633. },
  634. },
  635. },
  636. changes: [
  637. {
  638. operations: [
  639. {
  640. file: {
  641. hash: sha('new-sha'),
  642. },
  643. pathname: 'foo.tex',
  644. },
  645. ],
  646. timestamp: '2017-12-04T10:29:17.786Z',
  647. authors: [31],
  648. },
  649. ],
  650. },
  651. startVersion: 5,
  652. },
  653. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  654. })
  655. return ProjectHistoryClient.getFileTreeDiff(
  656. this.projectId,
  657. 5,
  658. 6,
  659. (error, diff, statusCode) => {
  660. if (error != null) {
  661. throw error
  662. }
  663. expect(statusCode).to.equal(422)
  664. return done()
  665. }
  666. )
  667. })
  668. it('should handle edits of missing/invalid files ', function (done) {
  669. MockHistoryStore()
  670. .get(`/api/projects/${this.historyId}/versions/5/history`)
  671. .reply(200, {
  672. chunk: {
  673. history: {
  674. snapshot: {
  675. files: {},
  676. },
  677. changes: [
  678. {
  679. operations: [
  680. {
  681. pathname: 'new.tex',
  682. textOperation: ['lorem ipsum'],
  683. },
  684. ],
  685. timestamp: '2017-12-04T10:29:18.786Z',
  686. authors: [31],
  687. },
  688. {
  689. operations: [
  690. {
  691. pathname: '',
  692. textOperation: ['lorem ipsum'],
  693. },
  694. ],
  695. timestamp: '2017-12-04T10:29:17.786Z',
  696. authors: [31],
  697. },
  698. ],
  699. },
  700. startVersion: 3,
  701. },
  702. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  703. })
  704. return ProjectHistoryClient.getFileTreeDiff(
  705. this.projectId,
  706. 3,
  707. 5,
  708. (error, diff) => {
  709. if (error != null) {
  710. throw error
  711. }
  712. expect(diff).to.deep.equal({
  713. diff: [
  714. {
  715. operation: 'edited',
  716. pathname: 'new.tex',
  717. },
  718. ],
  719. })
  720. return done()
  721. }
  722. )
  723. })
  724. it('should handle deletions of missing/invalid files ', function (done) {
  725. MockHistoryStore()
  726. .get(`/api/projects/${this.historyId}/versions/5/history`)
  727. .reply(200, {
  728. chunk: {
  729. history: {
  730. snapshot: {
  731. files: {},
  732. },
  733. changes: [
  734. {
  735. operations: [
  736. {
  737. pathname: 'missing.tex',
  738. newPathname: '',
  739. },
  740. ],
  741. timestamp: '2017-12-04T10:29:17.786Z',
  742. authors: [31],
  743. },
  744. {
  745. operations: [
  746. {
  747. pathname: '',
  748. newPathname: '',
  749. },
  750. ],
  751. timestamp: '2017-12-04T10:29:17.786Z',
  752. authors: [31],
  753. },
  754. ],
  755. },
  756. startVersion: 3,
  757. },
  758. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  759. })
  760. return ProjectHistoryClient.getFileTreeDiff(
  761. this.projectId,
  762. 3,
  763. 5,
  764. (error, diff) => {
  765. if (error != null) {
  766. throw error
  767. }
  768. expect(diff).to.deep.equal({
  769. diff: [],
  770. })
  771. return done()
  772. }
  773. )
  774. })
  775. return it('should handle renames of missing/invalid files ', function (done) {
  776. MockHistoryStore()
  777. .get(`/api/projects/${this.historyId}/versions/5/history`)
  778. .reply(200, {
  779. chunk: {
  780. history: {
  781. snapshot: {
  782. files: {},
  783. },
  784. changes: [
  785. {
  786. operations: [
  787. {
  788. pathname: 'missing.tex',
  789. newPathname: 'missing-renamed.tex',
  790. },
  791. ],
  792. timestamp: '2017-12-04T10:29:17.786Z',
  793. authors: [31],
  794. },
  795. {
  796. operations: [
  797. {
  798. pathname: '',
  799. newPathname: 'missing-renamed-other.tex',
  800. },
  801. ],
  802. timestamp: '2017-12-04T10:29:17.786Z',
  803. authors: [31],
  804. },
  805. ],
  806. },
  807. startVersion: 3,
  808. },
  809. authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
  810. })
  811. return ProjectHistoryClient.getFileTreeDiff(
  812. this.projectId,
  813. 3,
  814. 5,
  815. (error, diff) => {
  816. if (error != null) {
  817. throw error
  818. }
  819. expect(diff).to.deep.equal({
  820. diff: [],
  821. })
  822. return done()
  823. }
  824. )
  825. })
  826. })