FixturesManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. import RealTimeClient from './RealTimeClient.js'
  10. import MockWebServer from './MockWebServer.js'
  11. import MockDocUpdaterServer from './MockDocUpdaterServer.js'
  12. import crypto from 'node:crypto'
  13. let FixturesManager
  14. export default FixturesManager = {
  15. setUpProject(options, callback) {
  16. if (options == null) {
  17. options = {}
  18. }
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. if (!options.user_id) {
  23. options.user_id = FixturesManager.getRandomId()
  24. }
  25. if (!options.project_id) {
  26. options.project_id = FixturesManager.getRandomId()
  27. }
  28. if (!options.project) {
  29. options.project = { name: 'Test Project' }
  30. }
  31. let {
  32. project_id: projectId,
  33. user_id: userId,
  34. privilegeLevel,
  35. project,
  36. publicAccess,
  37. userMetadata,
  38. anonymousAccessToken,
  39. } = options
  40. if (privilegeLevel === 'owner') {
  41. project.owner = { _id: userId }
  42. } else {
  43. project.owner = { _id: '404404404404404404404404' }
  44. }
  45. const privileges = {}
  46. privileges[userId] = privilegeLevel
  47. if (publicAccess) {
  48. anonymousAccessToken =
  49. anonymousAccessToken || FixturesManager.getRandomId()
  50. privileges[anonymousAccessToken] = publicAccess
  51. }
  52. const metadataByUser = {}
  53. metadataByUser[userId] = userMetadata
  54. MockWebServer.createMockProject(
  55. projectId,
  56. privileges,
  57. project,
  58. metadataByUser
  59. )
  60. return MockWebServer.run(error => {
  61. if (error != null) {
  62. throw error
  63. }
  64. return RealTimeClient.setSession(
  65. {
  66. user: {
  67. _id: userId,
  68. first_name: 'Joe',
  69. last_name: 'Bloggs',
  70. },
  71. },
  72. error => {
  73. if (error != null) {
  74. throw error
  75. }
  76. return callback(null, {
  77. project_id: projectId,
  78. user_id: userId,
  79. privilegeLevel,
  80. project,
  81. anonymousAccessToken,
  82. })
  83. }
  84. )
  85. })
  86. },
  87. setUpDoc(projectId, options, callback) {
  88. if (options == null) {
  89. options = {}
  90. }
  91. if (callback == null) {
  92. callback = function () {}
  93. }
  94. if (!options.doc_id) {
  95. options.doc_id = FixturesManager.getRandomId()
  96. }
  97. if (!options.lines) {
  98. options.lines = ['doc', 'lines']
  99. }
  100. if (!options.version) {
  101. options.version = 42
  102. }
  103. if (!options.ops) {
  104. options.ops = ['mock', 'ops']
  105. }
  106. if (!options.type) {
  107. options.type = 'sharejs-text-ot'
  108. }
  109. const { doc_id: docId, lines, version, ops, ranges, type } = options
  110. MockDocUpdaterServer.createMockDoc(projectId, docId, {
  111. lines,
  112. version,
  113. ops,
  114. ranges,
  115. type,
  116. })
  117. return MockDocUpdaterServer.run(error => {
  118. if (error != null) {
  119. throw error
  120. }
  121. return callback(null, {
  122. project_id: projectId,
  123. doc_id: docId,
  124. lines,
  125. version,
  126. ops,
  127. })
  128. })
  129. },
  130. setUpEditorSession(options, callback) {
  131. FixturesManager.setUpProject(options, (err, detailsProject) => {
  132. if (err) return callback(err)
  133. FixturesManager.setUpDoc(
  134. detailsProject.project_id,
  135. options,
  136. (err, detailsDoc) => {
  137. if (err) return callback(err)
  138. callback(null, Object.assign({}, detailsProject, detailsDoc))
  139. }
  140. )
  141. })
  142. },
  143. getRandomId() {
  144. return crypto
  145. .createHash('sha1')
  146. .update(Math.random().toString())
  147. .digest('hex')
  148. .slice(0, 24)
  149. },
  150. }