| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855 |
- /* eslint-disable
- no-undef,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- import sinon from 'sinon'
- import { expect } from 'chai'
- import Settings from '@overleaf/settings'
- import request from 'request'
- import assert from 'assert'
- import Path from 'path'
- import crypto from 'crypto'
- import { ObjectId } from 'mongodb'
- import nock from 'nock'
- import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
- import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
- import * as HistoryId from './helpers/HistoryId.js'
- const MockHistoryStore = () => nock('http://localhost:3100')
- const MockFileStore = () => nock('http://localhost:3009')
- const MockWeb = () => nock('http://localhost:3000')
- const sha = data => crypto.createHash('sha1').update(data).digest('hex')
- describe('FileTree Diffs', function () {
- beforeEach(function (done) {
- return ProjectHistoryApp.ensureRunning(error => {
- if (error != null) {
- throw error
- }
- this.historyId = ObjectId().toString()
- this.projectId = ObjectId().toString()
- MockHistoryStore().post('/api/projects').reply(200, {
- projectId: this.historyId,
- })
- MockWeb()
- .get(`/project/${this.projectId}/details`)
- .reply(200, {
- name: 'Test Project',
- overleaf: { history: { id: this.historyId } },
- })
- return ProjectHistoryClient.initializeProject(
- this.historyId,
- (error, olProject) => {
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- })
- })
- afterEach(function () {
- return nock.cleanAll()
- })
- it('should return a diff of the updates to a doc from a single chunk', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/7/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'foo.tex': {
- hash: sha('mock-sha-foo'),
- stringLength: 42,
- },
- 'renamed.tex': {
- hash: sha('mock-sha-renamed'),
- stringLength: 42,
- },
- 'deleted.tex': {
- hash: sha('mock-sha-deleted'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'renamed.tex',
- newPathname: 'newName.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'foo.tex',
- textOperation: ['lorem ipsum'],
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'deleted.tex',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- {
- operations: [
- {
- file: {
- hash: sha('new-sha'),
- },
- pathname: 'added.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 7,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- pathname: 'foo.tex',
- operation: 'edited',
- },
- {
- pathname: 'deleted.tex',
- operation: 'removed',
- deletedAtV: 5,
- },
- {
- newPathname: 'newName.tex',
- pathname: 'renamed.tex',
- operation: 'renamed',
- },
- {
- pathname: 'added.tex',
- operation: 'added',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should return a diff of the updates to a doc across multiple chunks', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'foo.tex': {
- // Updated in this chunk
- hash: sha('mock-sha-foo'),
- stringLength: 42,
- },
- 'bar.tex': {
- // Updated in the next chunk
- hash: sha('mock-sha-bar'),
- stringLength: 42,
- },
- 'baz.tex': {
- // Not updated
- hash: sha('mock-sha-bar'),
- stringLength: 42,
- },
- 'renamed.tex': {
- hash: sha('mock-sha-renamed'),
- stringLength: 42,
- },
- 'deleted.tex': {
- hash: sha('mock-sha-deleted'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'renamed.tex',
- newPathname: 'newName.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'foo.tex',
- textOperation: ['lorem ipsum'],
- },
- ],
- timestamp: '2017-12-04T10:29:19.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'deleted.tex',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 2,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/7/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'foo.tex': {
- hash: sha('mock-sha-foo'),
- stringLength: 42,
- },
- 'baz.tex': {
- hash: sha('mock-sha-bar'),
- stringLength: 42,
- },
- 'newName.tex': {
- hash: sha('mock-sha-renamed'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- file: {
- hash: sha('new-sha'),
- },
- pathname: 'added.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'bar.tex',
- textOperation: ['lorem ipsum'],
- },
- ],
- timestamp: '2017-12-04T10:29:23.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 5,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 2,
- 7,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- pathname: 'foo.tex',
- operation: 'edited',
- },
- {
- pathname: 'bar.tex',
- operation: 'edited',
- },
- {
- pathname: 'baz.tex',
- },
- {
- pathname: 'deleted.tex',
- operation: 'removed',
- deletedAtV: 4,
- },
- {
- newPathname: 'newName.tex',
- pathname: 'renamed.tex',
- operation: 'renamed',
- },
- {
- pathname: 'added.tex',
- operation: 'added',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should return a diff that includes multiple renames', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'one.tex': {
- hash: sha('mock-sha'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'one.tex',
- newPathname: 'two.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'two.tex',
- newPathname: 'three.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- newPathname: 'three.tex',
- pathname: 'one.tex',
- operation: 'renamed',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should handle deleting the re-adding a file', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'one.tex': {
- hash: sha('mock-sha'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'one.tex',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'one.tex',
- file: {
- hash: sha('mock-sha'),
- },
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- pathname: 'one.tex',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should handle deleting the renaming a file to the same place', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'one.tex': {
- hash: sha('mock-sha-one'),
- stringLength: 42,
- },
- 'two.tex': {
- hash: sha('mock-sha-two'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'one.tex',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'two.tex',
- newPathname: 'one.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- pathname: 'two.tex',
- newPathname: 'one.tex',
- operation: 'renamed',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should handle adding then renaming a file', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {},
- },
- changes: [
- {
- operations: [
- {
- pathname: 'one.tex',
- file: {
- hash: sha('mock-sha'),
- },
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: 'one.tex',
- newPathname: 'two.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:22.905Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- pathname: 'two.tex',
- operation: 'added',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should return 422 with a chunk with an invalid rename', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/6/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'foo.tex': {
- hash: sha('mock-sha-foo'),
- stringLength: 42,
- },
- 'bar.tex': {
- hash: sha('mock-sha-bar'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- pathname: 'foo.tex',
- newPathname: 'bar.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 5,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 5,
- 6,
- (error, diff, statusCode) => {
- if (error != null) {
- throw error
- }
- expect(statusCode).to.equal(422)
- return done()
- }
- )
- })
- it('should return 422 with a chunk with an invalid add', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/6/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {
- 'foo.tex': {
- hash: sha('mock-sha-foo'),
- stringLength: 42,
- },
- },
- },
- changes: [
- {
- operations: [
- {
- file: {
- hash: sha('new-sha'),
- },
- pathname: 'foo.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 5,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 5,
- 6,
- (error, diff, statusCode) => {
- if (error != null) {
- throw error
- }
- expect(statusCode).to.equal(422)
- return done()
- }
- )
- })
- it('should handle edits of missing/invalid files ', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {},
- },
- changes: [
- {
- operations: [
- {
- pathname: 'new.tex',
- textOperation: ['lorem ipsum'],
- },
- ],
- timestamp: '2017-12-04T10:29:18.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: '',
- textOperation: ['lorem ipsum'],
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [
- {
- operation: 'edited',
- pathname: 'new.tex',
- },
- ],
- })
- return done()
- }
- )
- })
- it('should handle deletions of missing/invalid files ', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {},
- },
- changes: [
- {
- operations: [
- {
- pathname: 'missing.tex',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: '',
- newPathname: '',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [],
- })
- return done()
- }
- )
- })
- return it('should handle renames of missing/invalid files ', function (done) {
- MockHistoryStore()
- .get(`/api/projects/${this.historyId}/versions/5/history`)
- .reply(200, {
- chunk: {
- history: {
- snapshot: {
- files: {},
- },
- changes: [
- {
- operations: [
- {
- pathname: 'missing.tex',
- newPathname: 'missing-renamed.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- {
- operations: [
- {
- pathname: '',
- newPathname: 'missing-renamed-other.tex',
- },
- ],
- timestamp: '2017-12-04T10:29:17.786Z',
- authors: [31],
- },
- ],
- },
- startVersion: 3,
- },
- authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }],
- })
- return ProjectHistoryClient.getFileTreeDiff(
- this.projectId,
- 3,
- 5,
- (error, diff) => {
- if (error != null) {
- throw error
- }
- expect(diff).to.deep.equal({
- diff: [],
- })
- return done()
- }
- )
- })
- })
|