overleaf/services/real-time/test/acceptance/js/helpers/FixturesManager.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

156 lines
3.7 KiB
JavaScript

// 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
*/
let FixturesManager
const RealTimeClient = require('./RealTimeClient')
const MockWebServer = require('./MockWebServer')
const MockDocUpdaterServer = require('./MockDocUpdaterServer')
module.exports = 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']
}
const { doc_id: docId, lines, version, ops, ranges } = options
MockDocUpdaterServer.createMockDoc(projectId, docId, {
lines,
version,
ops,
ranges,
})
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 require('node:crypto')
.createHash('sha1')
.update(Math.random().toString())
.digest('hex')
.slice(0, 24)
},
}