SnapshotManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* eslint-disable
  2. no-return-assign,
  3. no-undef,
  4. no-unused-vars,
  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 sinon from 'sinon'
  14. import { expect } from 'chai'
  15. import { strict as esmock } from 'esmock'
  16. import Core from 'overleaf-editor-core'
  17. import BPromise from 'bluebird'
  18. import * as Errors from '../../../../app/js/Errors.js'
  19. const MODULE_PATH = '../../../../app/js/SnapshotManager.js'
  20. describe('SnapshotManager', function () {
  21. beforeEach(async function () {
  22. this.HistoryStoreManager = {
  23. getBlobStore: sinon.stub(),
  24. getChunkAtVersion: sinon.stub(),
  25. getMostRecentChunk: sinon.stub(),
  26. getProjectBlobStream: sinon.stub(),
  27. }
  28. this.WebApiManager = {
  29. getHistoryId: sinon.stub(),
  30. }
  31. this.SnapshotManager = await esmock(MODULE_PATH, {
  32. 'overleaf-editor-core': Core,
  33. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  34. '../../../../app/js/WebApiManager.js': this.WebApiManager,
  35. '../../../../app/js/Errors.js': Errors,
  36. })
  37. this.projectId = 'project-id-123'
  38. this.historyId = 'ol-project-id-123'
  39. return (this.callback = sinon.stub())
  40. })
  41. describe('getFileSnapshotStream', function () {
  42. beforeEach(function () {
  43. this.WebApiManager.getHistoryId.yields(null, this.historyId)
  44. return this.HistoryStoreManager.getChunkAtVersion.yields(null, {
  45. chunk: {
  46. history: {
  47. snapshot: {
  48. files: {
  49. 'main.tex': {
  50. hash: '35c9bd86574d61dcadbce2fdd3d4a0684272c6ea',
  51. stringLength: 41,
  52. },
  53. 'binary.png': {
  54. hash: 'c6654ea913979e13e22022653d284444f284a172',
  55. byteLength: 41,
  56. },
  57. },
  58. },
  59. changes: [
  60. {
  61. operations: [
  62. {
  63. pathname: 'main.tex',
  64. textOperation: [41, '\n\nSeven eight'],
  65. },
  66. ],
  67. timestamp: '2017-12-04T10:29:17.786Z',
  68. authors: [31],
  69. },
  70. {
  71. operations: [
  72. {
  73. pathname: 'main.tex',
  74. textOperation: [54, ' nine'],
  75. },
  76. ],
  77. timestamp: '2017-12-04T10:29:22.905Z',
  78. authors: [31],
  79. },
  80. ],
  81. },
  82. startVersion: 3,
  83. authors: [
  84. {
  85. id: 31,
  86. email: 'james.allen@overleaf.com',
  87. name: 'James',
  88. },
  89. ],
  90. },
  91. })
  92. })
  93. describe('of a text file', function () {
  94. beforeEach(function (done) {
  95. this.HistoryStoreManager.getBlobStore.withArgs(this.historyId).returns({
  96. getString: BPromise.promisify(
  97. (this.getString = sinon.stub().yields(
  98. null,
  99. `\
  100. Hello world
  101. One two three
  102. Four five six\
  103. `.replace(/^\t/g, '')
  104. ))
  105. ),
  106. })
  107. this.SnapshotManager.getFileSnapshotStream(
  108. this.projectId,
  109. 5,
  110. 'main.tex',
  111. (error, stream) => {
  112. this.stream = stream
  113. return done(error)
  114. }
  115. )
  116. })
  117. it('should get the overleaf id', function () {
  118. return this.WebApiManager.getHistoryId
  119. .calledWith(this.projectId)
  120. .should.equal(true)
  121. })
  122. it('should get the chunk', function () {
  123. return this.HistoryStoreManager.getChunkAtVersion
  124. .calledWith(this.projectId, this.historyId, 5)
  125. .should.equal(true)
  126. })
  127. it('should get the blob of the starting snapshot', function () {
  128. return this.getString
  129. .calledWith('35c9bd86574d61dcadbce2fdd3d4a0684272c6ea')
  130. .should.equal(true)
  131. })
  132. it('should return a string stream with the text content', function () {
  133. return expect(this.stream.read().toString()).to.equal(
  134. `\
  135. Hello world
  136. One two three
  137. Four five six
  138. Seven eight nine\
  139. `.replace(/^\t/g, '')
  140. )
  141. })
  142. describe('on blob store error', function () {
  143. beforeEach(function () {
  144. this.error = new Error('ESOCKETTIMEDOUT')
  145. this.HistoryStoreManager.getBlobStore
  146. .withArgs(this.historyId)
  147. .returns({
  148. getString: BPromise.promisify(sinon.stub().throws(this.error)),
  149. })
  150. })
  151. it('should call back with error', function (done) {
  152. this.SnapshotManager.getFileSnapshotStream(
  153. this.projectId,
  154. 5,
  155. 'main.tex',
  156. error => {
  157. expect(error).to.exist
  158. expect(error.name).to.equal(this.error.name)
  159. done()
  160. }
  161. )
  162. })
  163. })
  164. })
  165. describe('of a binary file', function () {
  166. beforeEach(function (done) {
  167. this.HistoryStoreManager.getProjectBlobStream
  168. .withArgs(this.historyId)
  169. .yields(null, (this.stream = 'mock-stream'))
  170. return this.SnapshotManager.getFileSnapshotStream(
  171. this.projectId,
  172. 5,
  173. 'binary.png',
  174. (error, returnedStream) => {
  175. this.returnedStream = returnedStream
  176. return done(error)
  177. }
  178. )
  179. })
  180. it('should get the overleaf id', function () {
  181. return this.WebApiManager.getHistoryId
  182. .calledWith(this.projectId)
  183. .should.equal(true)
  184. })
  185. it('should get the chunk', function () {
  186. return this.HistoryStoreManager.getChunkAtVersion
  187. .calledWith(this.projectId, this.historyId, 5)
  188. .should.equal(true)
  189. })
  190. it('should get the blob of the starting snapshot', function () {
  191. return this.HistoryStoreManager.getProjectBlobStream
  192. .calledWith(
  193. this.historyId,
  194. 'c6654ea913979e13e22022653d284444f284a172'
  195. )
  196. .should.equal(true)
  197. })
  198. return it('should return a stream with the blob content', function () {
  199. return expect(this.returnedStream).to.equal(this.stream)
  200. })
  201. })
  202. return describe("when the file doesn't exist", function () {
  203. beforeEach(function (done) {
  204. return this.SnapshotManager.getFileSnapshotStream(
  205. this.projectId,
  206. 5,
  207. 'not-here.png',
  208. (error, returnedStream) => {
  209. this.error = error
  210. this.returnedStream = returnedStream
  211. return done()
  212. }
  213. )
  214. })
  215. return it('should return a NotFoundError', function () {
  216. expect(this.error).to.exist
  217. expect(this.error.message).to.equal('not-here.png not found')
  218. return expect(this.error).to.be.an.instanceof(Errors.NotFoundError)
  219. })
  220. })
  221. })
  222. describe('getProjectSnapshot', function () {
  223. beforeEach(function () {
  224. this.WebApiManager.getHistoryId.yields(null, this.historyId)
  225. return this.HistoryStoreManager.getChunkAtVersion.yields(null, {
  226. chunk: (this.chunk = {
  227. history: {
  228. snapshot: {
  229. files: {
  230. 'main.tex': {
  231. hash: '35c9bd86574d61dcadbce2fdd3d4a0684272c6ea',
  232. stringLength: 41,
  233. },
  234. 'unchanged.tex': {
  235. hash: '35c9bd86574d61dcadbce2fdd3d4a0684272c6ea',
  236. stringLength: 41,
  237. },
  238. 'binary.png': {
  239. hash: 'c6654ea913979e13e22022653d284444f284a172',
  240. byteLength: 41,
  241. },
  242. },
  243. },
  244. changes: [
  245. {
  246. operations: [
  247. {
  248. pathname: 'main.tex',
  249. textOperation: [41, '\n\nSeven eight'],
  250. },
  251. ],
  252. timestamp: '2017-12-04T10:29:17.786Z',
  253. authors: [31],
  254. },
  255. {
  256. operations: [
  257. {
  258. pathname: 'main.tex',
  259. textOperation: [54, ' nine'],
  260. },
  261. ],
  262. timestamp: '2017-12-04T10:29:22.905Z',
  263. authors: [31],
  264. },
  265. ],
  266. },
  267. startVersion: 3,
  268. authors: [
  269. {
  270. id: 31,
  271. email: 'james.allen@overleaf.com',
  272. name: 'James',
  273. },
  274. ],
  275. }),
  276. })
  277. })
  278. describe('of project', function () {
  279. beforeEach(function (done) {
  280. this.HistoryStoreManager.getBlobStore.withArgs(this.historyId).returns({
  281. getString: BPromise.promisify(
  282. (this.getString = sinon.stub().yields(
  283. null,
  284. `\
  285. Hello world
  286. One two three
  287. Four five six\
  288. `.replace(/^\t/g, '')
  289. ))
  290. ),
  291. })
  292. this.SnapshotManager.getProjectSnapshot(
  293. this.projectId,
  294. 5,
  295. (error, data) => {
  296. this.data = data
  297. done(error)
  298. }
  299. )
  300. })
  301. it('should get the overleaf id', function () {
  302. return this.WebApiManager.getHistoryId
  303. .calledWith(this.projectId)
  304. .should.equal(true)
  305. })
  306. it('should get the chunk', function () {
  307. return this.HistoryStoreManager.getChunkAtVersion
  308. .calledWith(this.projectId, this.historyId, 5)
  309. .should.equal(true)
  310. })
  311. return it('should produce the snapshot file data', function () {
  312. expect(this.data).to.have.all.keys(['files', 'projectId'])
  313. expect(this.data.projectId).to.equal('project-id-123')
  314. expect(this.data.files['main.tex']).to.exist
  315. expect(this.data.files['unchanged.tex']).to.exist
  316. expect(this.data.files['binary.png']).to.exist
  317. // files with operations in the chunk should return content only
  318. expect(this.data.files['main.tex'].data.content).to.equal(
  319. 'Hello world\n\nOne two three\n\nFour five six\n\nSeven eight nine'
  320. )
  321. expect(this.data.files['main.tex'].data.hash).to.not.exist
  322. // unchanged files in the chunk should return hash only
  323. expect(this.data.files['unchanged.tex'].data.hash).to.equal(
  324. '35c9bd86574d61dcadbce2fdd3d4a0684272c6ea'
  325. )
  326. expect(this.data.files['unchanged.tex'].data.content).to.not.exist
  327. return expect(this.data.files['binary.png'].data.hash).to.equal(
  328. 'c6654ea913979e13e22022653d284444f284a172'
  329. )
  330. })
  331. })
  332. describe('on blob store error', function () {
  333. beforeEach(function () {
  334. this.error = new Error('ESOCKETTIMEDOUT')
  335. this.HistoryStoreManager.getBlobStore.withArgs(this.historyId).returns({
  336. getString: BPromise.promisify(sinon.stub().yields(this.error)),
  337. })
  338. })
  339. it('should call back with error', function (done) {
  340. this.SnapshotManager.getProjectSnapshot(this.projectId, 5, error => {
  341. expect(error).to.exist
  342. expect(error.message).to.equal(this.error.message)
  343. done()
  344. })
  345. })
  346. })
  347. })
  348. return describe('getLatestSnapshot', function () {
  349. describe('for a project', function () {
  350. beforeEach(function (done) {
  351. this.HistoryStoreManager.getMostRecentChunk.yields(null, {
  352. chunk: (this.chunk = {
  353. history: {
  354. snapshot: {
  355. files: {
  356. 'main.tex': {
  357. hash: '35c9bd86574d61dcadbce2fdd3d4a0684272c6ea',
  358. stringLength: 41,
  359. },
  360. 'binary.png': {
  361. hash: 'c6654ea913979e13e22022653d284444f284a172',
  362. byteLength: 41,
  363. },
  364. },
  365. },
  366. changes: [
  367. {
  368. operations: [
  369. {
  370. pathname: 'main.tex',
  371. textOperation: [41, '\n\nSeven eight'],
  372. },
  373. ],
  374. timestamp: '2017-12-04T10:29:17.786Z',
  375. authors: [31],
  376. },
  377. {
  378. operations: [
  379. {
  380. pathname: 'main.tex',
  381. textOperation: [54, ' nine'],
  382. },
  383. ],
  384. timestamp: '2017-12-04T10:29:22.905Z',
  385. authors: [31],
  386. },
  387. ],
  388. },
  389. startVersion: 3,
  390. authors: [
  391. {
  392. id: 31,
  393. email: 'james.allen@overleaf.com',
  394. name: 'James',
  395. },
  396. ],
  397. }),
  398. })
  399. this.HistoryStoreManager.getBlobStore.withArgs(this.historyId).returns({
  400. getString: BPromise.promisify(
  401. (this.getString = sinon.stub().yields(
  402. null,
  403. `\
  404. Hello world
  405. One two three
  406. Four five six\
  407. `.replace(/^\t/g, '')
  408. ))
  409. ),
  410. })
  411. this.SnapshotManager.getLatestSnapshot(
  412. this.projectId,
  413. this.historyId,
  414. (error, data) => {
  415. this.data = data
  416. done(error)
  417. }
  418. )
  419. })
  420. it('should get the chunk', function () {
  421. return this.HistoryStoreManager.getMostRecentChunk
  422. .calledWith(this.projectId, this.historyId)
  423. .should.equal(true)
  424. })
  425. return it('should produce the snapshot file data', function () {
  426. expect(this.data).to.have.all.keys(['main.tex', 'binary.png'])
  427. expect(this.data['main.tex']).to.exist
  428. expect(this.data['binary.png']).to.exist
  429. expect(this.data['main.tex'].getStringLength()).to.equal(59)
  430. expect(this.data['binary.png'].getByteLength()).to.equal(41)
  431. return expect(this.data['binary.png'].getHash()).to.equal(
  432. 'c6654ea913979e13e22022653d284444f284a172'
  433. )
  434. })
  435. })
  436. return describe('when the chunk is empty', function () {
  437. beforeEach(function (done) {
  438. this.HistoryStoreManager.getMostRecentChunk.yields(null)
  439. return this.SnapshotManager.getLatestSnapshot(
  440. this.projectId,
  441. this.historyId,
  442. (error, data) => {
  443. this.error = error
  444. this.data = data
  445. return done()
  446. }
  447. )
  448. })
  449. it('should get the chunk', function () {
  450. return this.HistoryStoreManager.getMostRecentChunk
  451. .calledWith(this.projectId, this.historyId)
  452. .should.equal(true)
  453. })
  454. return it('return an error', function () {
  455. expect(this.error).to.exist
  456. return expect(this.error.message).to.equal('undefined chunk')
  457. })
  458. })
  459. })
  460. })