2020-06-23 13:30:34 -04:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2020-06-23 13:30:45 -04:00
|
|
|
let FixturesManager
|
|
|
|
const RealTimeClient = require('./RealTimeClient')
|
|
|
|
const MockWebServer = require('./MockWebServer')
|
|
|
|
const MockDocUpdaterServer = require('./MockDocUpdaterServer')
|
2014-11-12 10:54:55 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
module.exports = FixturesManager = {
|
|
|
|
setUpProject(options, callback) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
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' }
|
|
|
|
}
|
2023-06-13 04:35:39 -04:00
|
|
|
let {
|
2023-03-20 10:10:40 -04:00
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
privilegeLevel,
|
|
|
|
project,
|
|
|
|
publicAccess,
|
2023-05-30 08:21:55 -04:00
|
|
|
userMetadata,
|
2023-06-13 04:35:39 -04:00
|
|
|
anonymousAccessToken,
|
2023-03-20 10:10:40 -04:00
|
|
|
} = options
|
2020-06-23 13:30:45 -04:00
|
|
|
|
|
|
|
const privileges = {}
|
2023-03-20 10:10:40 -04:00
|
|
|
privileges[userId] = privilegeLevel
|
2020-06-23 13:30:45 -04:00
|
|
|
if (publicAccess) {
|
2023-06-13 04:35:39 -04:00
|
|
|
anonymousAccessToken =
|
|
|
|
anonymousAccessToken || FixturesManager.getRandomId()
|
|
|
|
privileges[anonymousAccessToken] = publicAccess
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
|
2023-05-30 08:21:55 -04:00
|
|
|
const metadataByUser = {}
|
|
|
|
metadataByUser[userId] = userMetadata
|
|
|
|
|
|
|
|
MockWebServer.createMockProject(
|
|
|
|
projectId,
|
|
|
|
privileges,
|
|
|
|
project,
|
|
|
|
metadataByUser
|
|
|
|
)
|
2021-07-13 07:04:45 -04:00
|
|
|
return MockWebServer.run(error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return RealTimeClient.setSession(
|
|
|
|
{
|
|
|
|
user: {
|
2023-03-20 10:10:40 -04:00
|
|
|
_id: userId,
|
2020-06-23 13:30:45 -04:00
|
|
|
first_name: 'Joe',
|
2021-07-13 07:04:45 -04:00
|
|
|
last_name: 'Bloggs',
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2021-07-13 07:04:45 -04:00
|
|
|
error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return callback(null, {
|
2023-03-20 10:10:40 -04:00
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
2020-06-23 13:30:45 -04:00
|
|
|
privilegeLevel,
|
2021-07-13 07:04:45 -04:00
|
|
|
project,
|
2023-06-13 04:35:39 -04:00
|
|
|
anonymousAccessToken,
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
setUpDoc(projectId, options, callback) {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
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']
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
const { doc_id: docId, lines, version, ops, ranges } = options
|
2020-06-23 13:30:45 -04:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
MockDocUpdaterServer.createMockDoc(projectId, docId, {
|
2020-06-23 13:30:45 -04:00
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ops,
|
2021-07-13 07:04:45 -04:00
|
|
|
ranges,
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
return MockDocUpdaterServer.run(error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
return callback(null, {
|
|
|
|
project_id: projectId,
|
|
|
|
doc_id: docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ops,
|
|
|
|
})
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-10-01 07:33:15 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
getRandomId() {
|
|
|
|
return require('crypto')
|
|
|
|
.createHash('sha1')
|
|
|
|
.update(Math.random().toString())
|
|
|
|
.digest('hex')
|
|
|
|
.slice(0, 24)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|