Convert tests to ESM

GitOrigin-RevId: 03bd4db8cddc548706439edd7f6db0bc3e7ed9d3
This commit is contained in:
Andrew Rumble
2025-10-22 13:52:08 +01:00
committed by Copybot
parent 912324f560
commit 93b7274ea6
61 changed files with 8428 additions and 8063 deletions
@@ -1,84 +1,89 @@
/* eslint-disable
max-len,
no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
import { vi } from 'vitest'
import path from 'path'
import sinon from 'sinon'
const modulePath = path.join(
import.meta.dirname,
'../../../../app/src/Features/Editor/EditorRealTimeController'
)
describe('EditorRealTimeController', function () {
beforeEach(function () {
this.rclient = { publish: sinon.stub() }
this.Metrics = { summary: sinon.stub() }
this.EditorRealTimeController = SandboxedModule.require(modulePath, {
requires: {
'../../infrastructure/RedisWrapper': {
client: () => this.rclient,
},
'../../infrastructure/Server': {
io: (this.io = {}),
},
'@overleaf/settings': { redis: {} },
'@overleaf/metrics': this.Metrics,
crypto: (this.crypto = {
randomBytes: sinon
.stub()
.withArgs(4)
.returns(Buffer.from([0x1, 0x2, 0x3, 0x4])),
}),
os: (this.os = { hostname: sinon.stub().returns('somehost') }),
},
})
beforeEach(async function (ctx) {
ctx.rclient = { publish: sinon.stub() }
ctx.Metrics = { summary: sinon.stub() }
this.room_id = 'room-id'
this.message = 'message-to-editor'
return (this.payload = ['argument one', 42])
vi.doMock('../../../../app/src/infrastructure/RedisWrapper', () => ({
default: {
client: () => ctx.rclient,
},
}))
vi.doMock('../../../../app/src/infrastructure/Server', () => ({
default: {
io: (ctx.io = {}),
},
}))
vi.doMock('@overleaf/settings', () => ({
default: { redis: {} },
}))
vi.doMock('@overleaf/metrics', () => ({
default: ctx.Metrics,
}))
vi.doMock('node:crypto', () => ({
default: (ctx.crypto = {
randomBytes: sinon
.stub()
.withArgs(4)
.returns(Buffer.from([0x1, 0x2, 0x3, 0x4])),
}),
}))
vi.doMock('node:os', () => ({
default: (ctx.os = { hostname: sinon.stub().returns('somehost') }),
}))
ctx.EditorRealTimeController = (await import(modulePath)).default
ctx.room_id = 'room-id'
ctx.message = 'message-to-editor'
return (ctx.payload = ['argument one', 42])
})
describe('emitToRoom', function () {
beforeEach(function () {
this.message_id = 'web:somehost:01020304-0'
return this.EditorRealTimeController.emitToRoom(
this.room_id,
this.message,
...Array.from(this.payload)
beforeEach(function (ctx) {
ctx.message_id = 'web:somehost:01020304-0'
return ctx.EditorRealTimeController.emitToRoom(
ctx.room_id,
ctx.message,
...Array.from(ctx.payload)
)
})
it('should publish the message to redis', function () {
return this.rclient.publish
it('should publish the message to redis', function (ctx) {
return ctx.rclient.publish
.calledWith(
'editor-events',
JSON.stringify({
room_id: this.room_id,
message: this.message,
payload: this.payload,
_id: this.message_id,
room_id: ctx.room_id,
message: ctx.message,
payload: ctx.payload,
_id: ctx.message_id,
})
)
.should.equal(true)
})
it('should track the payload size', function () {
this.Metrics.summary
it('should track the payload size', function (ctx) {
ctx.Metrics.summary
.calledWith(
'redis.publish.editor-events',
JSON.stringify({
room_id: this.room_id,
message: this.message,
payload: this.payload,
_id: this.message_id,
room_id: ctx.room_id,
message: ctx.message,
payload: ctx.payload,
_id: ctx.message_id,
}).length
)
.should.equal(true)
@@ -86,17 +91,17 @@ describe('EditorRealTimeController', function () {
})
describe('emitToAll', function () {
beforeEach(function () {
this.EditorRealTimeController.emitToRoom = sinon.stub()
return this.EditorRealTimeController.emitToAll(
this.message,
...Array.from(this.payload)
beforeEach(function (ctx) {
ctx.EditorRealTimeController.emitToRoom = sinon.stub()
return ctx.EditorRealTimeController.emitToAll(
ctx.message,
...Array.from(ctx.payload)
)
})
it("should emit to the room 'all'", function () {
return this.EditorRealTimeController.emitToRoom
.calledWith('all', this.message, ...Array.from(this.payload))
it("should emit to the room 'all'", function (ctx) {
return ctx.EditorRealTimeController.emitToRoom
.calledWith('all', ctx.message, ...Array.from(ctx.payload))
.should.equal(true)
})
})