FixturesManager.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  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. let FixturesManager
  14. const RealTimeClient = require('./RealTimeClient')
  15. const MockWebServer = require('./MockWebServer')
  16. const MockDocUpdaterServer = require('./MockDocUpdaterServer')
  17. module.exports = FixturesManager = {
  18. setUpProject(options, callback) {
  19. if (options == null) {
  20. options = {}
  21. }
  22. if (callback == null) {
  23. callback = function (error, data) {}
  24. }
  25. if (!options.user_id) {
  26. options.user_id = FixturesManager.getRandomId()
  27. }
  28. if (!options.project_id) {
  29. options.project_id = FixturesManager.getRandomId()
  30. }
  31. if (!options.project) {
  32. options.project = { name: 'Test Project' }
  33. }
  34. const { project_id, user_id, privilegeLevel, project, publicAccess } =
  35. options
  36. const privileges = {}
  37. privileges[user_id] = privilegeLevel
  38. if (publicAccess) {
  39. privileges['anonymous-user'] = publicAccess
  40. }
  41. MockWebServer.createMockProject(project_id, privileges, project)
  42. return MockWebServer.run(error => {
  43. if (error != null) {
  44. throw error
  45. }
  46. return RealTimeClient.setSession(
  47. {
  48. user: {
  49. _id: user_id,
  50. first_name: 'Joe',
  51. last_name: 'Bloggs',
  52. },
  53. },
  54. error => {
  55. if (error != null) {
  56. throw error
  57. }
  58. return callback(null, {
  59. project_id,
  60. user_id,
  61. privilegeLevel,
  62. project,
  63. })
  64. }
  65. )
  66. })
  67. },
  68. setUpDoc(project_id, options, callback) {
  69. if (options == null) {
  70. options = {}
  71. }
  72. if (callback == null) {
  73. callback = function (error, data) {}
  74. }
  75. if (!options.doc_id) {
  76. options.doc_id = FixturesManager.getRandomId()
  77. }
  78. if (!options.lines) {
  79. options.lines = ['doc', 'lines']
  80. }
  81. if (!options.version) {
  82. options.version = 42
  83. }
  84. if (!options.ops) {
  85. options.ops = ['mock', 'ops']
  86. }
  87. const { doc_id, lines, version, ops, ranges } = options
  88. MockDocUpdaterServer.createMockDoc(project_id, doc_id, {
  89. lines,
  90. version,
  91. ops,
  92. ranges,
  93. })
  94. return MockDocUpdaterServer.run(error => {
  95. if (error != null) {
  96. throw error
  97. }
  98. return callback(null, { project_id, doc_id, lines, version, ops })
  99. })
  100. },
  101. setUpEditorSession(options, callback) {
  102. FixturesManager.setUpProject(options, (err, detailsProject) => {
  103. if (err) return callback(err)
  104. FixturesManager.setUpDoc(
  105. detailsProject.project_id,
  106. options,
  107. (err, detailsDoc) => {
  108. if (err) return callback(err)
  109. callback(null, Object.assign({}, detailsProject, detailsDoc))
  110. }
  111. )
  112. })
  113. },
  114. getRandomId() {
  115. return require('crypto')
  116. .createHash('sha1')
  117. .update(Math.random().toString())
  118. .digest('hex')
  119. .slice(0, 24)
  120. },
  121. }