first commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @import { Blob } from "../.."
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fake blob store for tests
|
||||
*/
|
||||
class FakeBlobStore {
|
||||
/**
|
||||
* Get a string from the blob store
|
||||
*
|
||||
* @param {string} hash
|
||||
* @return {Promise<string>}
|
||||
*/
|
||||
getString(hash) {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a string in the blob store
|
||||
*
|
||||
* @param {string} content
|
||||
* @return {Promise<Blob>}
|
||||
*/
|
||||
putString(content) {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FakeBlobStore
|
66
libraries/overleaf-editor-core/test/support/random.js
Normal file
66
libraries/overleaf-editor-core/test/support/random.js
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Randomised testing helpers from OT.js:
|
||||
// https://github.com/Operational-Transformation/ot.js/blob/
|
||||
// 8873b7e28e83f9adbf6c3a28ec639c9151a838ae/test/helpers.js
|
||||
//
|
||||
'use strict'
|
||||
|
||||
function randomInt(n) {
|
||||
return Math.floor(Math.random() * n)
|
||||
}
|
||||
|
||||
function randomString(n, newLine = true) {
|
||||
let str = ''
|
||||
while (n--) {
|
||||
if (newLine && Math.random() < 0.15) {
|
||||
str += '\n'
|
||||
} else {
|
||||
const chr = randomInt(26) + 97
|
||||
str += String.fromCharCode(chr)
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
function randomElement(arr) {
|
||||
return arr[randomInt(arr.length)]
|
||||
}
|
||||
|
||||
function randomTest(numTrials, test) {
|
||||
return function () {
|
||||
while (numTrials--) test()
|
||||
}
|
||||
}
|
||||
|
||||
function randomSubset(arr) {
|
||||
const n = randomInt(arr.length)
|
||||
const subset = []
|
||||
const indices = []
|
||||
for (let i = 0; i < arr.length; i++) indices.push(i)
|
||||
for (let i = 0; i < n; i++) {
|
||||
const index = randomInt(indices.length)
|
||||
subset.push(arr[indices[index]])
|
||||
indices.splice(index, 1)
|
||||
}
|
||||
return subset
|
||||
}
|
||||
|
||||
function randomComments(number) {
|
||||
const ids = new Set()
|
||||
const comments = []
|
||||
while (comments.length < number) {
|
||||
const id = randomString(10, false)
|
||||
if (!ids.has(id)) {
|
||||
comments.push({ id, ranges: [], resolved: false })
|
||||
ids.add(id)
|
||||
}
|
||||
}
|
||||
return { ids: Array.from(ids), comments }
|
||||
}
|
||||
|
||||
exports.int = randomInt
|
||||
exports.string = randomString
|
||||
exports.element = randomElement
|
||||
exports.test = randomTest
|
||||
exports.comments = randomComments
|
||||
exports.subset = randomSubset
|
@@ -0,0 +1,57 @@
|
||||
const TrackingProps = require('../../lib/file_data/tracking_props')
|
||||
const ClearTrackingProps = require('../../lib/file_data/clear_tracking_props')
|
||||
const TextOperation = require('../../lib/operation/text_operation')
|
||||
const random = require('./random')
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} str
|
||||
* @param {string[]} [commentIds]
|
||||
* @returns {TextOperation}
|
||||
*/
|
||||
function randomTextOperation(str, commentIds) {
|
||||
const operation = new TextOperation()
|
||||
let left
|
||||
while (true) {
|
||||
left = str.length - operation.baseLength
|
||||
if (left === 0) break
|
||||
const r = Math.random()
|
||||
const l = 1 + random.int(Math.min(left - 1, 20))
|
||||
const trackedChange =
|
||||
Math.random() < 0.1
|
||||
? new TrackingProps(
|
||||
random.element(['insert', 'delete']),
|
||||
random.element(['user1', 'user2', 'user3']),
|
||||
new Date(
|
||||
random.element([
|
||||
'2024-01-01T00:00:00.000Z',
|
||||
'2023-01-01T00:00:00.000Z',
|
||||
'2022-01-01T00:00:00.000Z',
|
||||
])
|
||||
)
|
||||
)
|
||||
: undefined
|
||||
if (r < 0.2) {
|
||||
let operationCommentIds
|
||||
if (commentIds?.length > 0 && Math.random() < 0.3) {
|
||||
operationCommentIds = random.subset(commentIds)
|
||||
}
|
||||
operation.insert(random.string(l), {
|
||||
tracking: trackedChange,
|
||||
commentIds: operationCommentIds,
|
||||
})
|
||||
} else if (r < 0.4) {
|
||||
operation.remove(l)
|
||||
} else if (r < 0.5) {
|
||||
operation.retain(l, { tracking: new ClearTrackingProps() })
|
||||
} else {
|
||||
operation.retain(l, { tracking: trackedChange })
|
||||
}
|
||||
}
|
||||
if (Math.random() < 0.3) {
|
||||
operation.insert(1 + random.string(10))
|
||||
}
|
||||
return operation
|
||||
}
|
||||
|
||||
module.exports = randomTextOperation
|
Reference in New Issue
Block a user