| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- import RealTimeClient from './RealTimeClient.js'
- import MockWebServer from './MockWebServer.js'
- import MockDocUpdaterServer from './MockDocUpdaterServer.js'
- import crypto from 'node:crypto'
- let FixturesManager
- export default FixturesManager = {
- setUpProject(options, callback) {
- if (options == null) {
- options = {}
- }
- if (callback == null) {
- callback = function () {}
- }
- if (!options.user_id) {
- options.user_id = FixturesManager.getRandomId()
- }
- if (!options.project_id) {
- options.project_id = FixturesManager.getRandomId()
- }
- if (!options.project) {
- options.project = { name: 'Test Project' }
- }
- let {
- project_id: projectId,
- user_id: userId,
- privilegeLevel,
- project,
- publicAccess,
- userMetadata,
- anonymousAccessToken,
- } = options
- if (privilegeLevel === 'owner') {
- project.owner = { _id: userId }
- } else {
- project.owner = { _id: '404404404404404404404404' }
- }
- const privileges = {}
- privileges[userId] = privilegeLevel
- if (publicAccess) {
- anonymousAccessToken =
- anonymousAccessToken || FixturesManager.getRandomId()
- privileges[anonymousAccessToken] = publicAccess
- }
- const metadataByUser = {}
- metadataByUser[userId] = userMetadata
- MockWebServer.createMockProject(
- projectId,
- privileges,
- project,
- metadataByUser
- )
- return MockWebServer.run(error => {
- if (error != null) {
- throw error
- }
- return RealTimeClient.setSession(
- {
- user: {
- _id: userId,
- first_name: 'Joe',
- last_name: 'Bloggs',
- },
- },
- error => {
- if (error != null) {
- throw error
- }
- return callback(null, {
- project_id: projectId,
- user_id: userId,
- privilegeLevel,
- project,
- anonymousAccessToken,
- })
- }
- )
- })
- },
- setUpDoc(projectId, options, callback) {
- if (options == null) {
- options = {}
- }
- if (callback == null) {
- callback = function () {}
- }
- if (!options.doc_id) {
- options.doc_id = FixturesManager.getRandomId()
- }
- if (!options.lines) {
- options.lines = ['doc', 'lines']
- }
- if (!options.version) {
- options.version = 42
- }
- if (!options.ops) {
- options.ops = ['mock', 'ops']
- }
- if (!options.type) {
- options.type = 'sharejs-text-ot'
- }
- const { doc_id: docId, lines, version, ops, ranges, type } = options
- MockDocUpdaterServer.createMockDoc(projectId, docId, {
- lines,
- version,
- ops,
- ranges,
- type,
- })
- return MockDocUpdaterServer.run(error => {
- if (error != null) {
- throw error
- }
- return callback(null, {
- project_id: projectId,
- doc_id: docId,
- lines,
- version,
- ops,
- })
- })
- },
- setUpEditorSession(options, callback) {
- FixturesManager.setUpProject(options, (err, detailsProject) => {
- if (err) return callback(err)
- FixturesManager.setUpDoc(
- detailsProject.project_id,
- options,
- (err, detailsDoc) => {
- if (err) return callback(err)
- callback(null, Object.assign({}, detailsProject, detailsDoc))
- }
- )
- })
- },
- getRandomId() {
- return crypto
- .createHash('sha1')
- .update(Math.random().toString())
- .digest('hex')
- .slice(0, 24)
- },
- }
|