ApplyingUpdatesToProjectStructureTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  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. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require("sinon");
  15. const chai = require("chai");
  16. chai.should();
  17. const Settings = require('settings-sharelatex');
  18. const rclient_project_history = require("redis-sharelatex").createClient(Settings.redis.project_history);
  19. const ProjectHistoryKeys = Settings.redis.project_history.key_schema;
  20. const MockProjectHistoryApi = require("./helpers/MockProjectHistoryApi");
  21. const MockWebApi = require("./helpers/MockWebApi");
  22. const DocUpdaterClient = require("./helpers/DocUpdaterClient");
  23. const DocUpdaterApp = require("./helpers/DocUpdaterApp");
  24. describe("Applying updates to a project's structure", function() {
  25. before(function() {
  26. this.user_id = 'user-id-123';
  27. return this.version = 1234;
  28. });
  29. describe("renaming a file", function() {
  30. before(function(done) {
  31. this.project_id = DocUpdaterClient.randomId();
  32. this.fileUpdate = {
  33. id: DocUpdaterClient.randomId(),
  34. pathname: '/file-path',
  35. newPathname: '/new-file-path'
  36. };
  37. this.fileUpdates = [ this.fileUpdate ];
  38. return DocUpdaterApp.ensureRunning(error => {
  39. if (error != null) { throw error; }
  40. return DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, [], this.fileUpdates, this.version, (error) => {
  41. if (error != null) { throw error; }
  42. return setTimeout(done, 200);
  43. });
  44. });
  45. });
  46. return it("should push the applied file renames to the project history api", function(done) {
  47. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  48. if (error != null) { throw error; }
  49. const update = JSON.parse(updates[0]);
  50. update.file.should.equal(this.fileUpdate.id);
  51. update.pathname.should.equal('/file-path');
  52. update.new_pathname.should.equal('/new-file-path');
  53. update.meta.user_id.should.equal(this.user_id);
  54. update.meta.ts.should.be.a('string');
  55. update.version.should.equal(`${this.version}.0`);
  56. return done();
  57. });
  58. return null;
  59. });
  60. });
  61. describe("renaming a document", function() {
  62. before(function() {
  63. this.docUpdate = {
  64. id: DocUpdaterClient.randomId(),
  65. pathname: '/doc-path',
  66. newPathname: '/new-doc-path'
  67. };
  68. return this.docUpdates = [ this.docUpdate ];});
  69. describe("when the document is not loaded", function() {
  70. before(function(done) {
  71. this.project_id = DocUpdaterClient.randomId();
  72. DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, this.docUpdates, [], this.version, (error) => {
  73. if (error != null) { throw error; }
  74. return setTimeout(done, 200);
  75. });
  76. return null;
  77. });
  78. return it("should push the applied doc renames to the project history api", function(done) {
  79. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  80. if (error != null) { throw error; }
  81. const update = JSON.parse(updates[0]);
  82. update.doc.should.equal(this.docUpdate.id);
  83. update.pathname.should.equal('/doc-path');
  84. update.new_pathname.should.equal('/new-doc-path');
  85. update.meta.user_id.should.equal(this.user_id);
  86. update.meta.ts.should.be.a('string');
  87. update.version.should.equal(`${this.version}.0`);
  88. return done();
  89. });
  90. return null;
  91. });
  92. });
  93. return describe("when the document is loaded", function() {
  94. before(function(done) {
  95. this.project_id = DocUpdaterClient.randomId();
  96. MockWebApi.insertDoc(this.project_id, this.docUpdate.id, {});
  97. DocUpdaterClient.preloadDoc(this.project_id, this.docUpdate.id, error => {
  98. if (error != null) { throw error; }
  99. sinon.spy(MockWebApi, "getDocument");
  100. return DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, this.docUpdates, [], this.version, (error) => {
  101. if (error != null) { throw error; }
  102. return setTimeout(done, 200);
  103. });
  104. });
  105. return null;
  106. });
  107. after(function() { return MockWebApi.getDocument.restore(); });
  108. it("should update the doc", function(done) {
  109. DocUpdaterClient.getDoc(this.project_id, this.docUpdate.id, (error, res, doc) => {
  110. doc.pathname.should.equal(this.docUpdate.newPathname);
  111. return done();
  112. });
  113. return null;
  114. });
  115. return it("should push the applied doc renames to the project history api", function(done) {
  116. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  117. if (error != null) { throw error; }
  118. const update = JSON.parse(updates[0]);
  119. update.doc.should.equal(this.docUpdate.id);
  120. update.pathname.should.equal('/doc-path');
  121. update.new_pathname.should.equal('/new-doc-path');
  122. update.meta.user_id.should.equal(this.user_id);
  123. update.meta.ts.should.be.a('string');
  124. update.version.should.equal(`${this.version}.0`);
  125. return done();
  126. });
  127. return null;
  128. });
  129. });
  130. });
  131. describe("renaming multiple documents and files", function() {
  132. before(function() {
  133. this.docUpdate0 = {
  134. id: DocUpdaterClient.randomId(),
  135. pathname: '/doc-path0',
  136. newPathname: '/new-doc-path0'
  137. };
  138. this.docUpdate1 = {
  139. id: DocUpdaterClient.randomId(),
  140. pathname: '/doc-path1',
  141. newPathname: '/new-doc-path1'
  142. };
  143. this.docUpdates = [ this.docUpdate0, this.docUpdate1 ];
  144. this.fileUpdate0 = {
  145. id: DocUpdaterClient.randomId(),
  146. pathname: '/file-path0',
  147. newPathname: '/new-file-path0'
  148. };
  149. this.fileUpdate1 = {
  150. id: DocUpdaterClient.randomId(),
  151. pathname: '/file-path1',
  152. newPathname: '/new-file-path1'
  153. };
  154. return this.fileUpdates = [ this.fileUpdate0, this.fileUpdate1 ];});
  155. return describe("when the documents are not loaded", function() {
  156. before(function(done) {
  157. this.project_id = DocUpdaterClient.randomId();
  158. DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, this.docUpdates, this.fileUpdates, this.version, (error) => {
  159. if (error != null) { throw error; }
  160. return setTimeout(done, 200);
  161. });
  162. return null;
  163. });
  164. return it("should push the applied doc renames to the project history api", function(done) {
  165. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  166. if (error != null) { throw error; }
  167. let update = JSON.parse(updates[0]);
  168. update.doc.should.equal(this.docUpdate0.id);
  169. update.pathname.should.equal('/doc-path0');
  170. update.new_pathname.should.equal('/new-doc-path0');
  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. update = JSON.parse(updates[1]);
  175. update.doc.should.equal(this.docUpdate1.id);
  176. update.pathname.should.equal('/doc-path1');
  177. update.new_pathname.should.equal('/new-doc-path1');
  178. update.meta.user_id.should.equal(this.user_id);
  179. update.meta.ts.should.be.a('string');
  180. update.version.should.equal(`${this.version}.1`);
  181. update = JSON.parse(updates[2]);
  182. update.file.should.equal(this.fileUpdate0.id);
  183. update.pathname.should.equal('/file-path0');
  184. update.new_pathname.should.equal('/new-file-path0');
  185. update.meta.user_id.should.equal(this.user_id);
  186. update.meta.ts.should.be.a('string');
  187. update.version.should.equal(`${this.version}.2`);
  188. update = JSON.parse(updates[3]);
  189. update.file.should.equal(this.fileUpdate1.id);
  190. update.pathname.should.equal('/file-path1');
  191. update.new_pathname.should.equal('/new-file-path1');
  192. update.meta.user_id.should.equal(this.user_id);
  193. update.meta.ts.should.be.a('string');
  194. update.version.should.equal(`${this.version}.3`);
  195. return done();
  196. });
  197. return null;
  198. });
  199. });
  200. });
  201. describe("adding a file", function() {
  202. before(function(done) {
  203. this.project_id = DocUpdaterClient.randomId();
  204. this.fileUpdate = {
  205. id: DocUpdaterClient.randomId(),
  206. pathname: '/file-path',
  207. url: 'filestore.example.com'
  208. };
  209. this.fileUpdates = [ this.fileUpdate ];
  210. DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, [], this.fileUpdates, this.version, (error) => {
  211. if (error != null) { throw error; }
  212. return setTimeout(done, 200);
  213. });
  214. return null;
  215. });
  216. return it("should push the file addition to the project history api", function(done) {
  217. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  218. if (error != null) { throw error; }
  219. const update = JSON.parse(updates[0]);
  220. update.file.should.equal(this.fileUpdate.id);
  221. update.pathname.should.equal('/file-path');
  222. update.url.should.equal('filestore.example.com');
  223. update.meta.user_id.should.equal(this.user_id);
  224. update.meta.ts.should.be.a('string');
  225. update.version.should.equal(`${this.version}.0`);
  226. return done();
  227. });
  228. return null;
  229. });
  230. });
  231. describe("adding a doc", function() {
  232. before(function(done) {
  233. this.project_id = DocUpdaterClient.randomId();
  234. this.docUpdate = {
  235. id: DocUpdaterClient.randomId(),
  236. pathname: '/file-path',
  237. docLines: 'a\nb'
  238. };
  239. this.docUpdates = [ this.docUpdate ];
  240. DocUpdaterClient.sendProjectUpdate(this.project_id, this.user_id, this.docUpdates, [], this.version, (error) => {
  241. if (error != null) { throw error; }
  242. return setTimeout(done, 200);
  243. });
  244. return null;
  245. });
  246. return it("should push the doc addition to the project history api", function(done) {
  247. rclient_project_history.lrange(ProjectHistoryKeys.projectHistoryOps({project_id: this.project_id}), 0, -1, (error, updates) => {
  248. if (error != null) { throw error; }
  249. const update = JSON.parse(updates[0]);
  250. update.doc.should.equal(this.docUpdate.id);
  251. update.pathname.should.equal('/file-path');
  252. update.docLines.should.equal('a\nb');
  253. update.meta.user_id.should.equal(this.user_id);
  254. update.meta.ts.should.be.a('string');
  255. update.version.should.equal(`${this.version}.0`);
  256. return done();
  257. });
  258. return null;
  259. });
  260. });
  261. describe("with enough updates to flush to the history service", function() {
  262. before(function(done) {
  263. this.project_id = DocUpdaterClient.randomId();
  264. this.user_id = DocUpdaterClient.randomId();
  265. this.version0 = 12345;
  266. this.version1 = this.version0 + 1;
  267. const updates = [];
  268. for (let v = 0; v <= 599; v++) { // Should flush after 500 ops
  269. updates.push({
  270. id: DocUpdaterClient.randomId(),
  271. pathname: '/file-' + v,
  272. docLines: 'a\nb'
  273. });
  274. }
  275. sinon.spy(MockProjectHistoryApi, "flushProject");
  276. // Send updates in chunks to causes multiple flushes
  277. const projectId = this.project_id;
  278. const userId = this.project_id;
  279. DocUpdaterClient.sendProjectUpdate(projectId, userId, updates.slice(0, 250), [], this.version0, function(error) {
  280. if (error != null) { throw error; }
  281. return DocUpdaterClient.sendProjectUpdate(projectId, userId, updates.slice(250), [], this.version1, (error) => {
  282. if (error != null) { throw error; }
  283. return setTimeout(done, 2000);
  284. });
  285. });
  286. return null;
  287. });
  288. after(function() { return MockProjectHistoryApi.flushProject.restore(); });
  289. return it("should flush project history", function() {
  290. return MockProjectHistoryApi.flushProject.calledWith(this.project_id).should.equal(true);
  291. });
  292. });
  293. return describe("with too few updates to flush to the history service", function() {
  294. before(function(done) {
  295. this.project_id = DocUpdaterClient.randomId();
  296. this.user_id = DocUpdaterClient.randomId();
  297. this.version0 = 12345;
  298. this.version1 = this.version0 + 1;
  299. const updates = [];
  300. for (let v = 0; v <= 42; v++) { // Should flush after 500 ops
  301. updates.push({
  302. id: DocUpdaterClient.randomId(),
  303. pathname: '/file-' + v,
  304. docLines: 'a\nb'
  305. });
  306. }
  307. sinon.spy(MockProjectHistoryApi, "flushProject");
  308. // Send updates in chunks
  309. const projectId = this.project_id;
  310. const userId = this.project_id;
  311. DocUpdaterClient.sendProjectUpdate(projectId, userId, updates.slice(0, 10), [], this.version0, function(error) {
  312. if (error != null) { throw error; }
  313. return DocUpdaterClient.sendProjectUpdate(projectId, userId, updates.slice(10), [], this.version1, (error) => {
  314. if (error != null) { throw error; }
  315. return setTimeout(done, 2000);
  316. });
  317. });
  318. return null;
  319. });
  320. after(function() { return MockProjectHistoryApi.flushProject.restore(); });
  321. return it("should not flush project history", function() {
  322. return MockProjectHistoryApi.flushProject.calledWith(this.project_id).should.equal(false);
  323. });
  324. });
  325. });