Merge pull request #32688 from overleaf/mj-chai-messages

[overleaf-editor-core] Use chai messages instead of try-catch in fuzzing tests

GitOrigin-RevId: b6db81e2fdbaac730ddca2bfb555983685396b43
This commit is contained in:
Mathias Jakobsen
2026-05-07 09:46:46 +01:00
committed by Copybot
parent bd604063e6
commit c2c8b1d1f1

View File

@@ -17,6 +17,10 @@ const { RetainOp, InsertOp, RemoveOp } = require('../../lib/operation/scan_op')
const TrackingProps = require('../../lib/file_data/tracking_props') const TrackingProps = require('../../lib/file_data/tracking_props')
const ClearTrackingProps = require('../../lib/file_data/clear_tracking_props') const ClearTrackingProps = require('../../lib/file_data/clear_tracking_props')
function fuzzingErrorMessage(obj) {
return `Failed randomized test with input: ${JSON.stringify(obj)}`
}
describe('TextOperation', function () { describe('TextOperation', function () {
const numTrials = 500 const numTrials = 500
@@ -145,18 +149,12 @@ describe('TextOperation', function () {
const str = random.string(50) const str = random.string(50)
const comments = random.comments(6) const comments = random.comments(6)
const o = randomOperation(str, comments.ids) const o = randomOperation(str, comments.ids)
try { const fuzzingError = fuzzingErrorMessage({ str, comments, o: o.toJSON() })
expect(str.length).to.equal(o.baseLength) expect(str.length).to.equal(o.baseLength, fuzzingError)
const file = new StringFileData(str, comments.comments) const file = new StringFileData(str, comments.comments)
o.apply(file) o.apply(file)
const result = file.getContent() const result = file.getContent()
expect(result.length).to.equal(o.targetLength) expect(result.length).to.equal(o.targetLength, fuzzingError)
} catch (err) {
if (err instanceof Error) {
err.message = `Failing inputs:\n str: ${JSON.stringify(str)}\n comments: ${JSON.stringify(comments)}\n o: ${JSON.stringify(o.toJSON())}\n\n${err.message}`
}
throw err
}
}) })
) )
@@ -166,15 +164,11 @@ describe('TextOperation', function () {
const doc = random.string(50) const doc = random.string(50)
const comments = random.comments(2) const comments = random.comments(2)
const operation = randomOperation(doc, comments.ids) const operation = randomOperation(doc, comments.ids)
try { const roundTripOperation = TextOperation.fromJSON(operation.toJSON())
const roundTripOperation = TextOperation.fromJSON(operation.toJSON()) expect(operation.equals(roundTripOperation)).to.equal(
expect(operation.equals(roundTripOperation)).to.be.true true,
} catch (err) { fuzzingErrorMessage({ operation })
if (err instanceof Error) { )
err.message = `Failing inputs:\n doc: ${JSON.stringify(doc)}\n comments: ${JSON.stringify(comments)}\n operation: ${JSON.stringify(operation.toJSON())}\n\n${err.message}`
}
throw err
}
}) })
) )
@@ -227,22 +221,20 @@ describe('TextOperation', function () {
const str = random.string(50) const str = random.string(50)
const comments = random.comments(6) const comments = random.comments(6)
const o = randomOperation(str, comments.ids) const o = randomOperation(str, comments.ids)
try { const originalFile = new StringFileData(str, comments.comments)
const originalFile = new StringFileData(str, comments.comments) const p = o.invert(originalFile)
const p = o.invert(originalFile) const fuzzingError = fuzzingErrorMessage({
expect(o.baseLength).to.equal(p.targetLength) str,
expect(o.targetLength).to.equal(p.baseLength) comments,
const file = new StringFileData(str, comments.comments) o: o.toJSON(),
o.apply(file) })
p.apply(file) expect(o.baseLength).to.equal(p.targetLength, fuzzingError)
const result = file.toRaw() expect(o.targetLength).to.equal(p.baseLength, fuzzingError)
expect(result).to.deep.equal(originalFile.toRaw()) const file = new StringFileData(str, comments.comments)
} catch (err) { o.apply(file)
if (err instanceof Error) { p.apply(file)
err.message = `Failing inputs:\n str: ${JSON.stringify(str)}\n comments: ${JSON.stringify(comments)}\n o: ${JSON.stringify(o.toJSON())}\n\n${err.message}` const result = file.toRaw()
} expect(result).to.deep.equal(originalFile.toRaw(), fuzzingError)
throw err
}
}) })
) )
@@ -394,26 +386,33 @@ describe('TextOperation', function () {
const str = random.string(20) const str = random.string(20)
const comments = random.comments(6) const comments = random.comments(6)
const a = randomOperation(str, comments.ids) const a = randomOperation(str, comments.ids)
const fuzzingError = fuzzingErrorMessage({
str,
comments,
a: a.toJSON(),
})
const file = new StringFileData(str, comments.comments) const file = new StringFileData(str, comments.comments)
a.apply(file) a.apply(file)
const afterA = file.toRaw() const afterA = file.toRaw()
expect(afterA.content.length).to.equal(a.targetLength, fuzzingError)
const b = randomOperation(afterA.content, comments.ids) const b = randomOperation(afterA.content, comments.ids)
try { const fuzzingErrorWithB = fuzzingErrorMessage({
expect(afterA.content.length).to.equal(a.targetLength) str,
b.apply(file) comments,
const afterB = file.toRaw() a: a.toJSON(),
expect(afterB.content.length).to.equal(b.targetLength) b: b.toJSON(),
const ab = a.compose(b) })
expect(ab.targetLength).to.equal(b.targetLength) b.apply(file)
ab.apply(new StringFileData(str, comments.comments)) const afterB = file.toRaw()
const afterAB = file.toRaw() expect(afterB.content.length).to.equal(
expect(afterAB).to.deep.equal(afterB) b.targetLength,
} catch (err) { fuzzingErrorWithB
if (err instanceof Error) { )
err.message = `Failing inputs:\n str: ${JSON.stringify(str)}\n comments: ${JSON.stringify(comments)}\n a: ${JSON.stringify(a.toJSON())}\n b: ${JSON.stringify(b.toJSON())}\n\n${err.message}` const ab = a.compose(b)
} expect(ab.targetLength).to.equal(b.targetLength, fuzzingErrorWithB)
throw err ab.apply(new StringFileData(str, comments.comments))
} const afterAB = file.toRaw()
expect(afterAB).to.deep.equal(afterB, fuzzingErrorWithB)
}) })
) )
@@ -622,24 +621,23 @@ describe('TextOperation', function () {
const comments = random.comments(6) const comments = random.comments(6)
const a = randomOperation(str, comments.ids) const a = randomOperation(str, comments.ids)
const b = randomOperation(str, comments.ids) const b = randomOperation(str, comments.ids)
try { const primes = TextOperation.transform(a, b)
const primes = TextOperation.transform(a, b) const aPrime = primes[0]
const aPrime = primes[0] const bPrime = primes[1]
const bPrime = primes[1] const abPrime = a.compose(bPrime)
const abPrime = a.compose(bPrime) const baPrime = b.compose(aPrime)
const baPrime = b.compose(aPrime) const abFile = new StringFileData(str, comments.comments)
const abFile = new StringFileData(str, comments.comments) const baFile = new StringFileData(str, comments.comments)
const baFile = new StringFileData(str, comments.comments) abPrime.apply(abFile)
abPrime.apply(abFile) baPrime.apply(baFile)
baPrime.apply(baFile) const fuzzingError = fuzzingErrorMessage({
expect(abPrime.equals(baPrime)).to.be.true str,
expect(abFile.toRaw()).to.deep.equal(baFile.toRaw()) comments,
} catch (err) { a: a.toJSON(),
if (err instanceof Error) { b: b.toJSON(),
err.message = `Failing inputs:\n str: ${JSON.stringify(str)}\n comments: ${JSON.stringify(comments)}\n a: ${JSON.stringify(a.toJSON())}\n b: ${JSON.stringify(b.toJSON())}\n\n${err.message}` })
} expect(abPrime.equals(baPrime)).to.be.equal(true, fuzzingError)
throw err expect(abFile.toRaw()).to.deep.equal(baFile.toRaw(), fuzzingError)
}
}) })
) )