From b5216af8f7821dc19a5d595bfbdeaa466ade9362 Mon Sep 17 00:00:00 2001 From: Andrew Rumble Date: Fri, 31 Oct 2025 09:55:40 +0000 Subject: [PATCH] Convert to ESM GitOrigin-RevId: 097bc45d500b4d2538ee49d60a597619579419a5 --- services/web/test/smoke/src/SmokeTests.mjs | 38 +++++++++---------- .../test/smoke/src/steps/000_getLoginCsrf.mjs | 2 +- .../smoke/src/steps/001_clearRateLimits.mjs | 10 ++--- .../web/test/smoke/src/steps/002_login.mjs | 4 +- .../src/steps/100_loadProjectDashboard.mjs | 2 +- .../test/smoke/src/steps/101_loadEditor.mjs | 4 +- services/web/test/smoke/src/support/Csrf.mjs | 12 ++---- .../web/test/smoke/src/support/Errors.mjs | 8 +--- .../test/smoke/src/support/requestHelper.mjs | 22 ++++------- .../test/smoke/src/support/timeoutHelper.mjs | 6 +-- 10 files changed, 45 insertions(+), 63 deletions(-) diff --git a/services/web/test/smoke/src/SmokeTests.mjs b/services/web/test/smoke/src/SmokeTests.mjs index ab25cc7a6f..7da26f8b9a 100644 --- a/services/web/test/smoke/src/SmokeTests.mjs +++ b/services/web/test/smoke/src/SmokeTests.mjs @@ -1,26 +1,26 @@ -const fs = require('fs') -const Path = require('path') - -const Settings = require('@overleaf/settings') -const { getCsrfTokenForFactory } = require('./support/Csrf') -const { SmokeTestFailure } = require('./support/Errors') -const { +import fs from 'fs' +import Path from 'path' +import Settings from '@overleaf/settings' +import { getCsrfTokenForFactory } from './support/Csrf.mjs' +import { SmokeTestFailure } from './support/Errors.mjs' +import { requestFactory, assertHasStatusCode, -} = require('./support/requestHelper') -const { processWithTimeout } = require('./support/timeoutHelper') +} from './support/requestHelper.mjs' +import { processWithTimeout } from './support/timeoutHelper.mjs' const STEP_TIMEOUT = Settings.smokeTest.stepTimeout -const PATH_STEPS = Path.join(__dirname, './steps') -const STEPS = fs - .readdirSync(PATH_STEPS) - .sort() - .map(name => { - const step = require(Path.join(PATH_STEPS, name)) - step.name = Path.basename(name, '.js') - return step - }) +const PATH_STEPS = Path.join(import.meta.dirname, './steps') +const sortedSteps = fs.readdirSync(PATH_STEPS).sort() + +const STEPS = [] + +for (const name of sortedSteps) { + const step = (await import(Path.join(PATH_STEPS, name))).default + step.name = Path.basename(name, '.mjs') + STEPS.push(step) +} async function runSmokeTests({ isAborted, stats }) { let lastStep = stats.start @@ -92,4 +92,4 @@ async function runSmokeTests({ isAborted, stats }) { } } -module.exports = { runSmokeTests, SmokeTestFailure } +export default { runSmokeTests, SmokeTestFailure } diff --git a/services/web/test/smoke/src/steps/000_getLoginCsrf.mjs b/services/web/test/smoke/src/steps/000_getLoginCsrf.mjs index cf1e5dc3e2..9b2bac49f7 100644 --- a/services/web/test/smoke/src/steps/000_getLoginCsrf.mjs +++ b/services/web/test/smoke/src/steps/000_getLoginCsrf.mjs @@ -4,4 +4,4 @@ async function run({ getCsrfTokenFor }) { return { loginCsrfToken } } -module.exports = { run } +export default { run } diff --git a/services/web/test/smoke/src/steps/001_clearRateLimits.mjs b/services/web/test/smoke/src/steps/001_clearRateLimits.mjs index 8ed81bd823..45e59f2b8c 100644 --- a/services/web/test/smoke/src/steps/001_clearRateLimits.mjs +++ b/services/web/test/smoke/src/steps/001_clearRateLimits.mjs @@ -1,9 +1,9 @@ -const Settings = require('@overleaf/settings') -const { +import Settings from '@overleaf/settings' +import { overleafLoginRateLimiter, openProjectRateLimiter, -} = require('../../../../app/src/infrastructure/RateLimiter') -const LoginRateLimiter = require('../../../../app/src/Features/Security/LoginRateLimiter') +} from '../../../../app/src/infrastructure/RateLimiter.js' +import LoginRateLimiter from '../../../../app/src/Features/Security/LoginRateLimiter.js' async function clearLoginRateLimit() { await LoginRateLimiter.promises.recordSuccessfulLogin(Settings.smokeTest.user) @@ -32,4 +32,4 @@ async function run({ processWithTimeout, timeout }) { }) } -module.exports = { run } +export default { run } diff --git a/services/web/test/smoke/src/steps/002_login.mjs b/services/web/test/smoke/src/steps/002_login.mjs index c92e38456b..5738c09ad2 100644 --- a/services/web/test/smoke/src/steps/002_login.mjs +++ b/services/web/test/smoke/src/steps/002_login.mjs @@ -1,4 +1,4 @@ -const Settings = require('@overleaf/settings') +import Settings from '@overleaf/settings' async function run({ assertHasStatusCode, loginCsrfToken, request }) { const response = await request('/login', { @@ -32,4 +32,4 @@ async function cleanup({ assertHasStatusCode, getCsrfTokenFor, request }) { assertHasStatusCode(response, 302) } -module.exports = { cleanup, run } +export default { cleanup, run } diff --git a/services/web/test/smoke/src/steps/100_loadProjectDashboard.mjs b/services/web/test/smoke/src/steps/100_loadProjectDashboard.mjs index aa471e0672..a281b23b1f 100644 --- a/services/web/test/smoke/src/steps/100_loadProjectDashboard.mjs +++ b/services/web/test/smoke/src/steps/100_loadProjectDashboard.mjs @@ -11,4 +11,4 @@ async function run({ request, assertHasStatusCode }) { } } -module.exports = { run } +export default { run } diff --git a/services/web/test/smoke/src/steps/101_loadEditor.mjs b/services/web/test/smoke/src/steps/101_loadEditor.mjs index 42d9af1999..d5b2d56821 100644 --- a/services/web/test/smoke/src/steps/101_loadEditor.mjs +++ b/services/web/test/smoke/src/steps/101_loadEditor.mjs @@ -1,4 +1,4 @@ -const Settings = require('@overleaf/settings') +import Settings from '@overleaf/settings' async function run({ assertHasStatusCode, request }) { const response = await request(`/project/${Settings.smokeTest.projectId}`) @@ -13,4 +13,4 @@ async function run({ assertHasStatusCode, request }) { } } -module.exports = { run } +export default { run } diff --git a/services/web/test/smoke/src/support/Csrf.mjs b/services/web/test/smoke/src/support/Csrf.mjs index 820ec7a3ab..d4b93dc4e1 100644 --- a/services/web/test/smoke/src/support/Csrf.mjs +++ b/services/web/test/smoke/src/support/Csrf.mjs @@ -1,8 +1,8 @@ -const OError = require('@overleaf/o-error') -const { assertHasStatusCode } = require('./requestHelper') +import OError from '@overleaf/o-error' +import { assertHasStatusCode } from './requestHelper.mjs' const CSRF_REGEX = // -function _parseCsrf(body) { +export function _parseCsrf(body) { const match = CSRF_REGEX.exec(body) if (!match) { throw new Error('Cannot find csrfToken in HTML') @@ -10,7 +10,7 @@ function _parseCsrf(body) { return match[1] } -function getCsrfTokenForFactory({ request }) { +export function getCsrfTokenForFactory({ request }) { return async function getCsrfTokenFor(endpoint) { try { const response = await request(endpoint) @@ -21,7 +21,3 @@ function getCsrfTokenForFactory({ request }) { } } } - -module.exports = { - getCsrfTokenForFactory, -} diff --git a/services/web/test/smoke/src/support/Errors.mjs b/services/web/test/smoke/src/support/Errors.mjs index a40586e225..577a1e4a0f 100644 --- a/services/web/test/smoke/src/support/Errors.mjs +++ b/services/web/test/smoke/src/support/Errors.mjs @@ -1,7 +1,3 @@ -const OError = require('@overleaf/o-error') +import OError from '@overleaf/o-error' -class SmokeTestFailure extends OError {} - -module.exports = { - SmokeTestFailure, -} +export class SmokeTestFailure extends OError {} diff --git a/services/web/test/smoke/src/support/requestHelper.mjs b/services/web/test/smoke/src/support/requestHelper.mjs index 2157a5fff0..811e5a3a4e 100644 --- a/services/web/test/smoke/src/support/requestHelper.mjs +++ b/services/web/test/smoke/src/support/requestHelper.mjs @@ -1,10 +1,9 @@ -const { Agent } = require('http') -const { createConnection } = require('net') -const { promisify } = require('util') - -const OError = require('@overleaf/o-error') -const request = require('request') -const Settings = require('@overleaf/settings') +import { Agent } from 'node:http' +import { createConnection } from 'node:net' +import { promisify } from 'node:util' +import OError from '@overleaf/o-error' +import request from 'request' +import Settings from '@overleaf/settings' // send requests to web router if this is the api process const OWN_PORT = Settings.port || Settings.internal.web.port || 3000 @@ -27,7 +26,7 @@ class InsecureCookieJar extends request.jar().constructor { } } -function requestFactory({ timeout }) { +export function requestFactory({ timeout }) { return promisify( request.defaults({ agent: new LocalhostAgent(), @@ -43,7 +42,7 @@ function requestFactory({ timeout }) { ) } -function assertHasStatusCode(response, expected) { +export function assertHasStatusCode(response, expected) { const { statusCode: actual } = response if (actual !== expected) { throw new OError('unexpected response code', { @@ -53,8 +52,3 @@ function assertHasStatusCode(response, expected) { }) } } - -module.exports = { - assertHasStatusCode, - requestFactory, -} diff --git a/services/web/test/smoke/src/support/timeoutHelper.mjs b/services/web/test/smoke/src/support/timeoutHelper.mjs index 6e7ea9c66e..46f7010bdb 100644 --- a/services/web/test/smoke/src/support/timeoutHelper.mjs +++ b/services/web/test/smoke/src/support/timeoutHelper.mjs @@ -1,4 +1,4 @@ -async function processWithTimeout({ work, timeout, message }) { +export async function processWithTimeout({ work, timeout, message }) { let workDeadLine function checkInResults() { clearTimeout(workDeadLine) @@ -12,7 +12,3 @@ async function processWithTimeout({ work, timeout, message }) { work.finally(checkInResults), ]) } - -module.exports = { - processWithTimeout, -}