ApplyingUpdatesToProjectStructureTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. chai.should()
  4. const Settings = require('settings-sharelatex')
  5. const rclientProjectHistory = require('redis-sharelatex').createClient(
  6. Settings.redis.project_history
  7. )
  8. const ProjectHistoryKeys = Settings.redis.project_history.key_schema
  9. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  10. const MockWebApi = require('./helpers/MockWebApi')
  11. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  12. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  13. describe("Applying updates to a project's structure", function () {
  14. before(function () {
  15. this.user_id = 'user-id-123'
  16. this.version = 1234
  17. })
  18. describe('renaming a file', function () {
  19. before(function (done) {
  20. this.project_id = DocUpdaterClient.randomId()
  21. this.fileUpdate = {
  22. id: DocUpdaterClient.randomId(),
  23. pathname: '/file-path',
  24. newPathname: '/new-file-path'
  25. }
  26. this.fileUpdates = [this.fileUpdate]
  27. DocUpdaterApp.ensureRunning((error) => {
  28. if (error) {
  29. return done(error)
  30. }
  31. DocUpdaterClient.sendProjectUpdate(
  32. this.project_id,
  33. this.user_id,
  34. [],
  35. this.fileUpdates,
  36. this.version,
  37. (error) => {
  38. if (error) {
  39. return done(error)
  40. }
  41. setTimeout(done, 200)
  42. }
  43. )
  44. })
  45. })
  46. it('should push the applied file renames to the project history api', function (done) {
  47. rclientProjectHistory.lrange(
  48. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  49. 0,
  50. -1,
  51. (error, updates) => {
  52. if (error) {
  53. return done(error)
  54. }
  55. const update = JSON.parse(updates[0])
  56. update.file.should.equal(this.fileUpdate.id)
  57. update.pathname.should.equal('/file-path')
  58. update.new_pathname.should.equal('/new-file-path')
  59. update.meta.user_id.should.equal(this.user_id)
  60. update.meta.ts.should.be.a('string')
  61. update.version.should.equal(`${this.version}.0`)
  62. done()
  63. }
  64. )
  65. })
  66. })
  67. describe('renaming a document', function () {
  68. before(function () {
  69. this.docUpdate = {
  70. id: DocUpdaterClient.randomId(),
  71. pathname: '/doc-path',
  72. newPathname: '/new-doc-path'
  73. }
  74. this.docUpdates = [this.docUpdate]
  75. })
  76. describe('when the document is not loaded', function () {
  77. before(function (done) {
  78. this.project_id = DocUpdaterClient.randomId()
  79. DocUpdaterClient.sendProjectUpdate(
  80. this.project_id,
  81. this.user_id,
  82. this.docUpdates,
  83. [],
  84. this.version,
  85. (error) => {
  86. if (error) {
  87. return done(error)
  88. }
  89. setTimeout(done, 200)
  90. }
  91. )
  92. })
  93. it('should push the applied doc renames to the project history api', function (done) {
  94. rclientProjectHistory.lrange(
  95. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  96. 0,
  97. -1,
  98. (error, updates) => {
  99. if (error) {
  100. return done(error)
  101. }
  102. const update = JSON.parse(updates[0])
  103. update.doc.should.equal(this.docUpdate.id)
  104. update.pathname.should.equal('/doc-path')
  105. update.new_pathname.should.equal('/new-doc-path')
  106. update.meta.user_id.should.equal(this.user_id)
  107. update.meta.ts.should.be.a('string')
  108. update.version.should.equal(`${this.version}.0`)
  109. done()
  110. }
  111. )
  112. })
  113. })
  114. describe('when the document is loaded', function () {
  115. before(function (done) {
  116. this.project_id = DocUpdaterClient.randomId()
  117. MockWebApi.insertDoc(this.project_id, this.docUpdate.id, {})
  118. DocUpdaterClient.preloadDoc(
  119. this.project_id,
  120. this.docUpdate.id,
  121. (error) => {
  122. if (error) {
  123. return done(error)
  124. }
  125. sinon.spy(MockWebApi, 'getDocument')
  126. DocUpdaterClient.sendProjectUpdate(
  127. this.project_id,
  128. this.user_id,
  129. this.docUpdates,
  130. [],
  131. this.version,
  132. (error) => {
  133. if (error) {
  134. return done(error)
  135. }
  136. setTimeout(done, 200)
  137. }
  138. )
  139. }
  140. )
  141. })
  142. after(function () {
  143. MockWebApi.getDocument.restore()
  144. })
  145. it('should update the doc', function (done) {
  146. DocUpdaterClient.getDoc(
  147. this.project_id,
  148. this.docUpdate.id,
  149. (error, res, doc) => {
  150. if (error) {
  151. return done(error)
  152. }
  153. doc.pathname.should.equal(this.docUpdate.newPathname)
  154. done()
  155. }
  156. )
  157. })
  158. it('should push the applied doc renames to the project history api', function (done) {
  159. rclientProjectHistory.lrange(
  160. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  161. 0,
  162. -1,
  163. (error, updates) => {
  164. if (error) {
  165. return done(error)
  166. }
  167. const update = JSON.parse(updates[0])
  168. update.doc.should.equal(this.docUpdate.id)
  169. update.pathname.should.equal('/doc-path')
  170. update.new_pathname.should.equal('/new-doc-path')
  171. update.meta.user_id.should.equal(this.user_id)
  172. update.meta.ts.should.be.a('string')
  173. update.version.should.equal(`${this.version}.0`)
  174. done()
  175. }
  176. )
  177. })
  178. })
  179. })
  180. describe('renaming multiple documents and files', function () {
  181. before(function () {
  182. this.docUpdate0 = {
  183. id: DocUpdaterClient.randomId(),
  184. pathname: '/doc-path0',
  185. newPathname: '/new-doc-path0'
  186. }
  187. this.docUpdate1 = {
  188. id: DocUpdaterClient.randomId(),
  189. pathname: '/doc-path1',
  190. newPathname: '/new-doc-path1'
  191. }
  192. this.docUpdates = [this.docUpdate0, this.docUpdate1]
  193. this.fileUpdate0 = {
  194. id: DocUpdaterClient.randomId(),
  195. pathname: '/file-path0',
  196. newPathname: '/new-file-path0'
  197. }
  198. this.fileUpdate1 = {
  199. id: DocUpdaterClient.randomId(),
  200. pathname: '/file-path1',
  201. newPathname: '/new-file-path1'
  202. }
  203. this.fileUpdates = [this.fileUpdate0, this.fileUpdate1]
  204. })
  205. describe('when the documents are not loaded', function () {
  206. before(function (done) {
  207. this.project_id = DocUpdaterClient.randomId()
  208. DocUpdaterClient.sendProjectUpdate(
  209. this.project_id,
  210. this.user_id,
  211. this.docUpdates,
  212. this.fileUpdates,
  213. this.version,
  214. (error) => {
  215. if (error) {
  216. return done(error)
  217. }
  218. setTimeout(done, 200)
  219. }
  220. )
  221. })
  222. it('should push the applied doc renames to the project history api', function (done) {
  223. rclientProjectHistory.lrange(
  224. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  225. 0,
  226. -1,
  227. (error, updates) => {
  228. if (error) {
  229. return done(error)
  230. }
  231. let update = JSON.parse(updates[0])
  232. update.doc.should.equal(this.docUpdate0.id)
  233. update.pathname.should.equal('/doc-path0')
  234. update.new_pathname.should.equal('/new-doc-path0')
  235. update.meta.user_id.should.equal(this.user_id)
  236. update.meta.ts.should.be.a('string')
  237. update.version.should.equal(`${this.version}.0`)
  238. update = JSON.parse(updates[1])
  239. update.doc.should.equal(this.docUpdate1.id)
  240. update.pathname.should.equal('/doc-path1')
  241. update.new_pathname.should.equal('/new-doc-path1')
  242. update.meta.user_id.should.equal(this.user_id)
  243. update.meta.ts.should.be.a('string')
  244. update.version.should.equal(`${this.version}.1`)
  245. update = JSON.parse(updates[2])
  246. update.file.should.equal(this.fileUpdate0.id)
  247. update.pathname.should.equal('/file-path0')
  248. update.new_pathname.should.equal('/new-file-path0')
  249. update.meta.user_id.should.equal(this.user_id)
  250. update.meta.ts.should.be.a('string')
  251. update.version.should.equal(`${this.version}.2`)
  252. update = JSON.parse(updates[3])
  253. update.file.should.equal(this.fileUpdate1.id)
  254. update.pathname.should.equal('/file-path1')
  255. update.new_pathname.should.equal('/new-file-path1')
  256. update.meta.user_id.should.equal(this.user_id)
  257. update.meta.ts.should.be.a('string')
  258. update.version.should.equal(`${this.version}.3`)
  259. done()
  260. }
  261. )
  262. })
  263. })
  264. })
  265. describe('adding a file', function () {
  266. before(function (done) {
  267. this.project_id = DocUpdaterClient.randomId()
  268. this.fileUpdate = {
  269. id: DocUpdaterClient.randomId(),
  270. pathname: '/file-path',
  271. url: 'filestore.example.com'
  272. }
  273. this.fileUpdates = [this.fileUpdate]
  274. DocUpdaterClient.sendProjectUpdate(
  275. this.project_id,
  276. this.user_id,
  277. [],
  278. this.fileUpdates,
  279. this.version,
  280. (error) => {
  281. if (error) {
  282. return done(error)
  283. }
  284. setTimeout(done, 200)
  285. }
  286. )
  287. })
  288. it('should push the file addition to the project history api', function (done) {
  289. rclientProjectHistory.lrange(
  290. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  291. 0,
  292. -1,
  293. (error, updates) => {
  294. if (error) {
  295. return done(error)
  296. }
  297. const update = JSON.parse(updates[0])
  298. update.file.should.equal(this.fileUpdate.id)
  299. update.pathname.should.equal('/file-path')
  300. update.url.should.equal('filestore.example.com')
  301. update.meta.user_id.should.equal(this.user_id)
  302. update.meta.ts.should.be.a('string')
  303. update.version.should.equal(`${this.version}.0`)
  304. done()
  305. }
  306. )
  307. })
  308. })
  309. describe('adding a doc', function () {
  310. before(function (done) {
  311. this.project_id = DocUpdaterClient.randomId()
  312. this.docUpdate = {
  313. id: DocUpdaterClient.randomId(),
  314. pathname: '/file-path',
  315. docLines: 'a\nb'
  316. }
  317. this.docUpdates = [this.docUpdate]
  318. DocUpdaterClient.sendProjectUpdate(
  319. this.project_id,
  320. this.user_id,
  321. this.docUpdates,
  322. [],
  323. this.version,
  324. (error) => {
  325. if (error) {
  326. return done(error)
  327. }
  328. setTimeout(done, 200)
  329. }
  330. )
  331. })
  332. it('should push the doc addition to the project history api', function (done) {
  333. rclientProjectHistory.lrange(
  334. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  335. 0,
  336. -1,
  337. (error, updates) => {
  338. if (error) {
  339. return done(error)
  340. }
  341. const update = JSON.parse(updates[0])
  342. update.doc.should.equal(this.docUpdate.id)
  343. update.pathname.should.equal('/file-path')
  344. update.docLines.should.equal('a\nb')
  345. update.meta.user_id.should.equal(this.user_id)
  346. update.meta.ts.should.be.a('string')
  347. update.version.should.equal(`${this.version}.0`)
  348. done()
  349. }
  350. )
  351. })
  352. })
  353. describe('with enough updates to flush to the history service', function () {
  354. before(function (done) {
  355. this.project_id = DocUpdaterClient.randomId()
  356. this.user_id = DocUpdaterClient.randomId()
  357. this.version0 = 12345
  358. this.version1 = this.version0 + 1
  359. const updates = []
  360. for (let v = 0; v <= 599; v++) {
  361. // Should flush after 500 ops
  362. updates.push({
  363. id: DocUpdaterClient.randomId(),
  364. pathname: '/file-' + v,
  365. docLines: 'a\nb'
  366. })
  367. }
  368. sinon.spy(MockProjectHistoryApi, 'flushProject')
  369. // Send updates in chunks to causes multiple flushes
  370. const projectId = this.project_id
  371. const userId = this.project_id
  372. DocUpdaterClient.sendProjectUpdate(
  373. projectId,
  374. userId,
  375. updates.slice(0, 250),
  376. [],
  377. this.version0,
  378. function (error) {
  379. if (error) {
  380. return done(error)
  381. }
  382. DocUpdaterClient.sendProjectUpdate(
  383. projectId,
  384. userId,
  385. updates.slice(250),
  386. [],
  387. this.version1,
  388. (error) => {
  389. if (error) {
  390. return done(error)
  391. }
  392. setTimeout(done, 2000)
  393. }
  394. )
  395. }
  396. )
  397. })
  398. after(function () {
  399. MockProjectHistoryApi.flushProject.restore()
  400. })
  401. it('should flush project history', function () {
  402. MockProjectHistoryApi.flushProject
  403. .calledWith(this.project_id)
  404. .should.equal(true)
  405. })
  406. })
  407. describe('with too few updates to flush to the history service', function () {
  408. before(function (done) {
  409. this.project_id = DocUpdaterClient.randomId()
  410. this.user_id = DocUpdaterClient.randomId()
  411. this.version0 = 12345
  412. this.version1 = this.version0 + 1
  413. const updates = []
  414. for (let v = 0; v <= 42; v++) {
  415. // Should flush after 500 ops
  416. updates.push({
  417. id: DocUpdaterClient.randomId(),
  418. pathname: '/file-' + v,
  419. docLines: 'a\nb'
  420. })
  421. }
  422. sinon.spy(MockProjectHistoryApi, 'flushProject')
  423. // Send updates in chunks
  424. const projectId = this.project_id
  425. const userId = this.project_id
  426. DocUpdaterClient.sendProjectUpdate(
  427. projectId,
  428. userId,
  429. updates.slice(0, 10),
  430. [],
  431. this.version0,
  432. function (error) {
  433. if (error) {
  434. return done(error)
  435. }
  436. DocUpdaterClient.sendProjectUpdate(
  437. projectId,
  438. userId,
  439. updates.slice(10),
  440. [],
  441. this.version1,
  442. (error) => {
  443. if (error) {
  444. return done(error)
  445. }
  446. setTimeout(done, 2000)
  447. }
  448. )
  449. }
  450. )
  451. })
  452. after(function () {
  453. MockProjectHistoryApi.flushProject.restore()
  454. })
  455. it('should not flush project history', function () {
  456. MockProjectHistoryApi.flushProject
  457. .calledWith(this.project_id)
  458. .should.equal(false)
  459. })
  460. })
  461. })