From 0a12c47b353fcb44c9b45b00c2087189da8201c9 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween Date: Tue, 13 Dec 2022 07:37:49 -0500 Subject: [PATCH] Merge pull request #10815 from overleaf/em-esm-chat Move chat service to ES modules GitOrigin-RevId: c08ae8328b8f3b539e6cfe052834b84bb3756330 --- libraries/settings/Settings.js | 3 + services/chat/app.js | 46 +++---- .../js/Features/Messages/MessageFormatter.js | 12 +- .../Messages/MessageHttpController.js | 65 ++++------ .../js/Features/Messages/MessageManager.js | 49 +++----- .../app/js/Features/Threads/ThreadManager.js | 41 ++---- services/chat/app/js/mongodb.js | 22 ++-- services/chat/app/js/router.js | 118 ++++++++---------- services/chat/app/js/server.js | 22 ++-- services/chat/app/js/util/promises.js | 4 +- ...ings.defaults.js => settings.defaults.cjs} | 0 services/chat/package.json | 1 + .../acceptance/js/DeletingAMessageTests.js | 8 +- .../acceptance/js/DeletingAThreadTests.js | 8 +- .../acceptance/js/DestroyingAProjectTests.js | 8 +- .../acceptance/js/EditingAMessageTests.js | 8 +- .../acceptance/js/GettingMessagesTests.js | 8 +- .../acceptance/js/ResolvingAThreadTests.js | 8 +- .../acceptance/js/SendingAMessageTests.js | 8 +- .../test/acceptance/js/helpers/ChatApp.js | 14 +-- .../test/acceptance/js/helpers/ChatClient.js | 40 +++--- 21 files changed, 198 insertions(+), 295 deletions(-) rename services/chat/config/{settings.defaults.js => settings.defaults.cjs} (100%) diff --git a/libraries/settings/Settings.js b/libraries/settings/Settings.js index e710a0d4f6..358b83b52a 100644 --- a/libraries/settings/Settings.js +++ b/libraries/settings/Settings.js @@ -13,7 +13,9 @@ const SHARELATEX_CONFIG = process.env.SHARELATEX_CONFIG let settings let settingsExist = false const defaultsPath = + pathIfExists(Path.join(CWD, 'config/settings.defaults.cjs')) || pathIfExists(Path.join(CWD, 'config/settings.defaults.js')) || + pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.cjs')) || pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.js')) if (defaultsPath) { console.log(`Using default settings from ${defaultsPath}`) @@ -25,6 +27,7 @@ if (defaultsPath) { const overridesPath = pathIfExists(SHARELATEX_CONFIG) || + pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.cjs`)) || pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.js`)) if (overridesPath) { console.log(`Using settings from ${overridesPath}`) diff --git a/services/chat/app.js b/services/chat/app.js index 80e58a7a8f..ca3ea09426 100644 --- a/services/chat/app.js +++ b/services/chat/app.js @@ -1,28 +1,22 @@ -const logger = require('@overleaf/logger') -const settings = require('@overleaf/settings') +import logger from '@overleaf/logger' +import settings from '@overleaf/settings' +import { mongoClient } from './app/js/mongodb.js' +import { server } from './app/js/server.js' -const { mongoClient } = require('./app/js/mongodb') -const Server = require('./app/js/server') - -if (!module.parent) { - // Called directly - const port = settings.internal.chat.port - const host = settings.internal.chat.host - mongoClient - .connect() - .then(() => { - Server.server.listen(port, host, function (err) { - if (err) { - logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`) - process.exit(1) - } - logger.debug(`Chat starting up, listening on ${host}:${port}`) - }) +const port = settings.internal.chat.port +const host = settings.internal.chat.host +mongoClient + .connect() + .then(() => { + server.listen(port, host, function (err) { + if (err) { + logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`) + process.exit(1) + } + logger.debug(`Chat starting up, listening on ${host}:${port}`) }) - .catch(err => { - logger.fatal({ err }, 'Cannot connect to mongo. Exiting.') - process.exit(1) - }) -} - -module.exports = Server.server + }) + .catch(err => { + logger.fatal({ err }, 'Cannot connect to mongo. Exiting.') + process.exit(1) + }) diff --git a/services/chat/app/js/Features/Messages/MessageFormatter.js b/services/chat/app/js/Features/Messages/MessageFormatter.js index 373087aea5..31e9300e9e 100644 --- a/services/chat/app/js/Features/Messages/MessageFormatter.js +++ b/services/chat/app/js/Features/Messages/MessageFormatter.js @@ -1,4 +1,4 @@ -function formatMessageForClientSide(message) { +export function formatMessageForClientSide(message) { if (message._id) { message.id = message._id.toString() delete message._id @@ -15,11 +15,11 @@ function formatMessageForClientSide(message) { return formattedMessage } -function formatMessagesForClientSide(messages) { +export function formatMessagesForClientSide(messages) { return messages.map(message => formatMessageForClientSide(message)) } -function groupMessagesByThreads(rooms, messages) { +export function groupMessagesByThreads(rooms, messages) { let room, thread const roomsById = {} for (room of rooms) { @@ -58,9 +58,3 @@ function groupMessagesByThreads(rooms, messages) { return threads } - -module.exports = { - formatMessagesForClientSide, - formatMessageForClientSide, - groupMessagesByThreads, -} diff --git a/services/chat/app/js/Features/Messages/MessageHttpController.js b/services/chat/app/js/Features/Messages/MessageHttpController.js index e05dacf611..7ccf63bb97 100644 --- a/services/chat/app/js/Features/Messages/MessageHttpController.js +++ b/services/chat/app/js/Features/Messages/MessageHttpController.js @@ -1,26 +1,26 @@ -const logger = require('@overleaf/logger') -const MessageManager = require('./MessageManager') -const MessageFormatter = require('./MessageFormatter') -const ThreadManager = require('../Threads/ThreadManager') -const { ObjectId } = require('../../mongodb') -const { expressify } = require('../../util/promises') +import logger from '@overleaf/logger' +import * as MessageManager from './MessageManager.js' +import * as MessageFormatter from './MessageFormatter.js' +import * as ThreadManager from '../Threads/ThreadManager.js' +import { ObjectId } from '../../mongodb.js' +import { expressify } from '../../util/promises.js' const DEFAULT_MESSAGE_LIMIT = 50 const MAX_MESSAGE_LENGTH = 10 * 1024 // 10kb, about 1,500 words -async function getGlobalMessages(req, res) { +export const getGlobalMessages = expressify(async (req, res) => { await _getMessages(ThreadManager.GLOBAL_THREAD, req, res) -} +}) -async function sendGlobalMessage(req, res) { +export const sendGlobalMessage = expressify(async (req, res) => { await _sendMessage(ThreadManager.GLOBAL_THREAD, req, res) -} +}) -async function sendThreadMessage(req, res) { +export const sendThreadMessage = expressify(async (req, res) => { await _sendMessage(req.params.threadId, req, res) -} +}) -async function getAllThreads(req, res) { +export const getAllThreads = expressify(async (req, res) => { const { projectId } = req.params logger.debug({ projectId }, 'getting all threads') const rooms = await ThreadManager.findAllThreadRooms(projectId) @@ -28,32 +28,32 @@ async function getAllThreads(req, res) { const messages = await MessageManager.findAllMessagesInRooms(roomIds) const threads = MessageFormatter.groupMessagesByThreads(rooms, messages) res.json(threads) -} +}) -async function resolveThread(req, res) { +export const resolveThread = expressify(async (req, res) => { const { projectId, threadId } = req.params const { user_id: userId } = req.body logger.debug({ userId, projectId, threadId }, 'marking thread as resolved') await ThreadManager.resolveThread(projectId, threadId, userId) res.sendStatus(204) -} +}) -async function reopenThread(req, res) { +export const reopenThread = expressify(async (req, res) => { const { projectId, threadId } = req.params logger.debug({ projectId, threadId }, 'reopening thread') await ThreadManager.reopenThread(projectId, threadId) res.sendStatus(204) -} +}) -async function deleteThread(req, res) { +export const deleteThread = expressify(async (req, res) => { const { projectId, threadId } = req.params logger.debug({ projectId, threadId }, 'deleting thread') const roomId = await ThreadManager.deleteThread(projectId, threadId) await MessageManager.deleteAllMessagesInRoom(roomId) res.sendStatus(204) -} +}) -async function editMessage(req, res) { +export const editMessage = expressify(async (req, res) => { const { content, userId } = req.body const { projectId, threadId, messageId } = req.params logger.debug({ projectId, threadId, messageId, content }, 'editing message') @@ -69,17 +69,17 @@ async function editMessage(req, res) { return res.sendStatus(404) } res.sendStatus(204) -} +}) -async function deleteMessage(req, res) { +export const deleteMessage = expressify(async (req, res) => { const { projectId, threadId, messageId } = req.params logger.debug({ projectId, threadId, messageId }, 'deleting message') const room = await ThreadManager.findOrCreateThread(projectId, threadId) await MessageManager.deleteMessage(room._id, messageId) res.sendStatus(204) -} +}) -async function destroyProject(req, res) { +export const destroyProject = expressify(async (req, res) => { const { projectId } = req.params logger.debug({ projectId }, 'destroying project') const rooms = await ThreadManager.findAllThreadRoomsAndGlobalThread(projectId) @@ -89,7 +89,7 @@ async function destroyProject(req, res) { logger.debug({ projectId }, 'deleting all threads in project') await ThreadManager.deleteAllThreadsInProject(projectId) res.sendStatus(204) -} +}) async function _sendMessage(clientThreadId, req, res) { const { user_id: userId, content } = req.body @@ -155,16 +155,3 @@ async function _getMessages(clientThreadId, req, res) { logger.debug({ projectId, messages }, 'got messages') res.status(200).send(messages) } - -module.exports = { - getGlobalMessages: expressify(getGlobalMessages), - sendGlobalMessage: expressify(sendGlobalMessage), - sendThreadMessage: expressify(sendThreadMessage), - getAllThreads: expressify(getAllThreads), - resolveThread: expressify(resolveThread), - reopenThread: expressify(reopenThread), - deleteThread: expressify(deleteThread), - editMessage: expressify(editMessage), - deleteMessage: expressify(deleteMessage), - destroyProject: expressify(destroyProject), -} diff --git a/services/chat/app/js/Features/Messages/MessageManager.js b/services/chat/app/js/Features/Messages/MessageManager.js index 30a5627e84..db521a7896 100644 --- a/services/chat/app/js/Features/Messages/MessageManager.js +++ b/services/chat/app/js/Features/Messages/MessageManager.js @@ -1,9 +1,6 @@ -let MessageManager -const { db, ObjectId } = require('../../mongodb') -const metrics = require('@overleaf/metrics') -const logger = require('@overleaf/logger') +import { db, ObjectId } from '../../mongodb.js' -async function createMessage(roomId, userId, content, timestamp) { +export async function createMessage(roomId, userId, content, timestamp) { let newMessageOpts = { content, room_id: roomId, @@ -16,7 +13,7 @@ async function createMessage(roomId, userId, content, timestamp) { return newMessageOpts } -async function getMessages(roomId, limit, before) { +export async function getMessages(roomId, limit, before) { let query = { room_id: roomId } if (before) { query.timestamp = { $lt: before } @@ -25,7 +22,7 @@ async function getMessages(roomId, limit, before) { return db.messages.find(query).sort({ timestamp: -1 }).limit(limit).toArray() } -async function findAllMessagesInRooms(roomIds) { +export async function findAllMessagesInRooms(roomIds) { return db.messages .find({ room_id: { $in: roomIds }, @@ -33,19 +30,25 @@ async function findAllMessagesInRooms(roomIds) { .toArray() } -async function deleteAllMessagesInRoom(roomId) { +export async function deleteAllMessagesInRoom(roomId) { await db.messages.deleteMany({ room_id: roomId, }) } -async function deleteAllMessagesInRooms(roomIds) { +export async function deleteAllMessagesInRooms(roomIds) { await db.messages.deleteMany({ room_id: { $in: roomIds }, }) } -async function updateMessage(roomId, messageId, userId, content, timestamp) { +export async function updateMessage( + roomId, + messageId, + userId, + content, + timestamp +) { const query = _ensureIdsAreObjectIds({ _id: messageId, room_id: roomId, @@ -62,7 +65,7 @@ async function updateMessage(roomId, messageId, userId, content, timestamp) { return res.modifiedCount === 1 } -async function deleteMessage(roomId, messageId) { +export async function deleteMessage(roomId, messageId) { const query = _ensureIdsAreObjectIds({ _id: messageId, room_id: roomId, @@ -82,27 +85,3 @@ function _ensureIdsAreObjectIds(query) { } return query } - -module.exports = MessageManager = { - createMessage, - getMessages, - findAllMessagesInRooms, - deleteAllMessagesInRoom, - deleteAllMessagesInRooms, - updateMessage, - deleteMessage, -} -;[ - 'createMessage', - 'getMessages', - 'findAllMessagesInRooms', - 'updateMessage', - 'deleteMessage', -].map(method => - metrics.timeAsyncMethod( - MessageManager, - method, - 'mongo.MessageManager', - logger - ) -) diff --git a/services/chat/app/js/Features/Threads/ThreadManager.js b/services/chat/app/js/Features/Threads/ThreadManager.js index 414b78b45a..432a1249f2 100644 --- a/services/chat/app/js/Features/Threads/ThreadManager.js +++ b/services/chat/app/js/Features/Threads/ThreadManager.js @@ -1,11 +1,8 @@ -let ThreadManager -const { db, ObjectId } = require('../../mongodb') -const logger = require('@overleaf/logger') -const metrics = require('@overleaf/metrics') +import { db, ObjectId } from '../../mongodb.js' -const GLOBAL_THREAD = 'GLOBAL' +export const GLOBAL_THREAD = 'GLOBAL' -async function findOrCreateThread(projectId, threadId) { +export async function findOrCreateThread(projectId, threadId) { let query, update projectId = ObjectId(projectId.toString()) if (threadId !== GLOBAL_THREAD) { @@ -39,7 +36,7 @@ async function findOrCreateThread(projectId, threadId) { return result.value } -async function findAllThreadRooms(projectId) { +export async function findAllThreadRooms(projectId) { return db.rooms .find( { @@ -54,7 +51,7 @@ async function findAllThreadRooms(projectId) { .toArray() } -async function findAllThreadRoomsAndGlobalThread(projectId) { +export async function findAllThreadRoomsAndGlobalThread(projectId) { return db.rooms .find( { @@ -68,7 +65,7 @@ async function findAllThreadRoomsAndGlobalThread(projectId) { .toArray() } -async function resolveThread(projectId, threadId, userId) { +export async function resolveThread(projectId, threadId, userId) { await db.rooms.updateOne( { project_id: ObjectId(projectId.toString()), @@ -85,7 +82,7 @@ async function resolveThread(projectId, threadId, userId) { ) } -async function reopenThread(projectId, threadId) { +export async function reopenThread(projectId, threadId) { await db.rooms.updateOne( { project_id: ObjectId(projectId.toString()), @@ -99,7 +96,7 @@ async function reopenThread(projectId, threadId) { ) } -async function deleteThread(projectId, threadId) { +export async function deleteThread(projectId, threadId) { const room = await findOrCreateThread(projectId, threadId) await db.rooms.deleteOne({ _id: room._id, @@ -107,28 +104,8 @@ async function deleteThread(projectId, threadId) { return room._id } -async function deleteAllThreadsInProject(projectId) { +export async function deleteAllThreadsInProject(projectId) { await db.rooms.deleteMany({ project_id: ObjectId(projectId.toString()), }) } - -module.exports = ThreadManager = { - GLOBAL_THREAD, - findOrCreateThread, - findAllThreadRooms, - findAllThreadRoomsAndGlobalThread, - resolveThread, - reopenThread, - deleteThread, - deleteAllThreadsInProject, -} -;[ - 'findOrCreateThread', - 'findAllThreadRooms', - 'resolveThread', - 'reopenThread', - 'deleteThread', -].map(method => - metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger) -) diff --git a/services/chat/app/js/mongodb.js b/services/chat/app/js/mongodb.js index 7ba98f40c2..927ba0e4a2 100644 --- a/services/chat/app/js/mongodb.js +++ b/services/chat/app/js/mongodb.js @@ -1,16 +1,12 @@ -const Settings = require('@overleaf/settings') -const { MongoClient, ObjectId } = require('mongodb') +import Settings from '@overleaf/settings' +import { MongoClient } from 'mongodb' -const client = new MongoClient(Settings.mongo.url) -const db = client.db() +export { ObjectId } from 'mongodb' -const collections = { - messages: db.collection('messages'), - rooms: db.collection('rooms'), -} - -module.exports = { - db: collections, - mongoClient: client, - ObjectId, +export const mongoClient = new MongoClient(Settings.mongo.url) +const mongoDb = mongoClient.db() + +export const db = { + messages: mongoDb.collection('messages'), + rooms: mongoDb.collection('rooms'), } diff --git a/services/chat/app/js/router.js b/services/chat/app/js/router.js index 73a1e6c0bd..d7d7a135b6 100644 --- a/services/chat/app/js/router.js +++ b/services/chat/app/js/router.js @@ -1,73 +1,65 @@ -const MessageHttpController = require('./Features/Messages/MessageHttpController') -const { ObjectId } = require('./mongodb') +import * as MessageHttpController from './Features/Messages/MessageHttpController.js' +import { ObjectId } from './mongodb.js' -module.exports = { - route(app) { - app.param('projectId', function (req, res, next, projectId) { - if (ObjectId.isValid(projectId)) { - next() - } else { - res.status(400).send('Invalid projectId') - } - }) +export function route(app) { + app.param('projectId', function (req, res, next, projectId) { + if (ObjectId.isValid(projectId)) { + next() + } else { + res.status(400).send('Invalid projectId') + } + }) - app.param('threadId', function (req, res, next, threadId) { - if (ObjectId.isValid(threadId)) { - next() - } else { - res.status(400).send('Invalid threadId') - } - }) + app.param('threadId', function (req, res, next, threadId) { + if (ObjectId.isValid(threadId)) { + next() + } else { + res.status(400).send('Invalid threadId') + } + }) - // These are for backwards compatibility - app.get( - '/room/:projectId/messages', - MessageHttpController.getGlobalMessages - ) - app.post( - '/room/:projectId/messages', - MessageHttpController.sendGlobalMessage - ) + // These are for backwards compatibility + app.get('/room/:projectId/messages', MessageHttpController.getGlobalMessages) + app.post('/room/:projectId/messages', MessageHttpController.sendGlobalMessage) - app.get( - '/project/:projectId/messages', - MessageHttpController.getGlobalMessages - ) - app.post( - '/project/:projectId/messages', - MessageHttpController.sendGlobalMessage - ) + app.get( + '/project/:projectId/messages', + MessageHttpController.getGlobalMessages + ) + app.post( + '/project/:projectId/messages', + MessageHttpController.sendGlobalMessage + ) - app.post( - '/project/:projectId/thread/:threadId/messages', - MessageHttpController.sendThreadMessage - ) - app.get('/project/:projectId/threads', MessageHttpController.getAllThreads) + app.post( + '/project/:projectId/thread/:threadId/messages', + MessageHttpController.sendThreadMessage + ) + app.get('/project/:projectId/threads', MessageHttpController.getAllThreads) - app.post( - '/project/:projectId/thread/:threadId/messages/:messageId/edit', - MessageHttpController.editMessage - ) - app.delete( - '/project/:projectId/thread/:threadId/messages/:messageId', - MessageHttpController.deleteMessage - ) + app.post( + '/project/:projectId/thread/:threadId/messages/:messageId/edit', + MessageHttpController.editMessage + ) + app.delete( + '/project/:projectId/thread/:threadId/messages/:messageId', + MessageHttpController.deleteMessage + ) - app.post( - '/project/:projectId/thread/:threadId/resolve', - MessageHttpController.resolveThread - ) - app.post( - '/project/:projectId/thread/:threadId/reopen', - MessageHttpController.reopenThread - ) - app.delete( - '/project/:projectId/thread/:threadId', - MessageHttpController.deleteThread - ) + app.post( + '/project/:projectId/thread/:threadId/resolve', + MessageHttpController.resolveThread + ) + app.post( + '/project/:projectId/thread/:threadId/reopen', + MessageHttpController.reopenThread + ) + app.delete( + '/project/:projectId/thread/:threadId', + MessageHttpController.deleteThread + ) - app.delete('/project/:projectId', MessageHttpController.destroyProject) + app.delete('/project/:projectId', MessageHttpController.destroyProject) - app.get('/status', (req, res, next) => res.send('chat is alive')) - }, + app.get('/status', (req, res, next) => res.send('chat is alive')) } diff --git a/services/chat/app/js/server.js b/services/chat/app/js/server.js index 49d6d391f9..21ebe2be58 100644 --- a/services/chat/app/js/server.js +++ b/services/chat/app/js/server.js @@ -1,12 +1,15 @@ -const metrics = require('@overleaf/metrics') +import http from 'http' +import metrics from '@overleaf/metrics' +import logger from '@overleaf/logger' +import express from 'express' +import bodyParser from 'body-parser' +import * as Router from './router.js' + metrics.initialize('chat') -const logger = require('@overleaf/logger') logger.initialize('chat') -const express = require('express') -const bodyParser = require('body-parser') -const app = express() -const server = require('http').createServer(app) -const Router = require('./router') + +export const app = express() +export const server = http.createServer(app) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true })) @@ -14,8 +17,3 @@ app.use(metrics.http.monitor(logger)) metrics.injectMetricsRoute(app) Router.route(app) - -module.exports = { - server, - app, -} diff --git a/services/chat/app/js/util/promises.js b/services/chat/app/js/util/promises.js index 5a6d88681f..fd46c8cf55 100644 --- a/services/chat/app/js/util/promises.js +++ b/services/chat/app/js/util/promises.js @@ -3,10 +3,8 @@ * * Any error will be passed to the error middlewares via `next()` */ -function expressify(fn) { +export function expressify(fn) { return (req, res, next) => { fn(req, res, next).catch(next) } } - -module.exports = { expressify } diff --git a/services/chat/config/settings.defaults.js b/services/chat/config/settings.defaults.cjs similarity index 100% rename from services/chat/config/settings.defaults.js rename to services/chat/config/settings.defaults.cjs diff --git a/services/chat/package.json b/services/chat/package.json index 5131c328b1..00da2fe1b8 100644 --- a/services/chat/package.json +++ b/services/chat/package.json @@ -3,6 +3,7 @@ "description": "The backend API that powers Overleaf chat", "private": true, "main": "app.js", + "type": "module", "scripts": { "start": "node $NODE_APP_OPTIONS app.js", "test:acceptance": "npm run test:acceptance:_run -- --grep=$MOCHA_GREP", diff --git a/services/chat/test/acceptance/js/DeletingAMessageTests.js b/services/chat/test/acceptance/js/DeletingAMessageTests.js index 1e6b3db5eb..dfe2ee0bd8 100644 --- a/services/chat/test/acceptance/js/DeletingAMessageTests.js +++ b/services/chat/test/acceptance/js/DeletingAMessageTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Deleting a message', async function () { const projectId = ObjectId().toString() diff --git a/services/chat/test/acceptance/js/DeletingAThreadTests.js b/services/chat/test/acceptance/js/DeletingAThreadTests.js index 74533c6f44..aa3c6a609a 100644 --- a/services/chat/test/acceptance/js/DeletingAThreadTests.js +++ b/services/chat/test/acceptance/js/DeletingAThreadTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Deleting a thread', async function () { const projectId = ObjectId().toString() diff --git a/services/chat/test/acceptance/js/DestroyingAProjectTests.js b/services/chat/test/acceptance/js/DestroyingAProjectTests.js index e4a45dbf00..2330db6149 100644 --- a/services/chat/test/acceptance/js/DestroyingAProjectTests.js +++ b/services/chat/test/acceptance/js/DestroyingAProjectTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' const db = ChatApp.db diff --git a/services/chat/test/acceptance/js/EditingAMessageTests.js b/services/chat/test/acceptance/js/EditingAMessageTests.js index f39a919c51..c806b4cc1b 100644 --- a/services/chat/test/acceptance/js/EditingAMessageTests.js +++ b/services/chat/test/acceptance/js/EditingAMessageTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Editing a message', async function () { let projectId, userId, threadId diff --git a/services/chat/test/acceptance/js/GettingMessagesTests.js b/services/chat/test/acceptance/js/GettingMessagesTests.js index fb400ecea6..83f0c8c137 100644 --- a/services/chat/test/acceptance/js/GettingMessagesTests.js +++ b/services/chat/test/acceptance/js/GettingMessagesTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Getting messages', async function () { const userId1 = ObjectId().toString() diff --git a/services/chat/test/acceptance/js/ResolvingAThreadTests.js b/services/chat/test/acceptance/js/ResolvingAThreadTests.js index d14c004943..afec51f375 100644 --- a/services/chat/test/acceptance/js/ResolvingAThreadTests.js +++ b/services/chat/test/acceptance/js/ResolvingAThreadTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Resolving a thread', async function () { const projectId = ObjectId().toString() diff --git a/services/chat/test/acceptance/js/SendingAMessageTests.js b/services/chat/test/acceptance/js/SendingAMessageTests.js index 9f3b87a26c..900ac32e64 100644 --- a/services/chat/test/acceptance/js/SendingAMessageTests.js +++ b/services/chat/test/acceptance/js/SendingAMessageTests.js @@ -1,8 +1,8 @@ -const { ObjectId } = require('../../../app/js/mongodb') -const { expect } = require('chai') +import { ObjectId } from '../../../app/js/mongodb.js' +import { expect } from 'chai' -const ChatClient = require('./helpers/ChatClient') -const ChatApp = require('./helpers/ChatApp') +import * as ChatClient from './helpers/ChatClient.js' +import * as ChatApp from './helpers/ChatApp.js' describe('Sending a message', async function () { before(async function () { diff --git a/services/chat/test/acceptance/js/helpers/ChatApp.js b/services/chat/test/acceptance/js/helpers/ChatApp.js index 163dc0db02..9286842e2e 100644 --- a/services/chat/test/acceptance/js/helpers/ChatApp.js +++ b/services/chat/test/acceptance/js/helpers/ChatApp.js @@ -1,9 +1,10 @@ -const { db } = require('../../../../app/js/mongodb') -const app = require('../../../../app') +import { server } from '../../../../app/js/server.js' + +export { db } from '../../../../app/js/mongodb.js' let serverPromise = null function startServer(resolve, reject) { - app.listen(3010, 'localhost', error => { + server.listen(3010, 'localhost', error => { if (error) { return reject(error) } @@ -11,14 +12,9 @@ function startServer(resolve, reject) { }) } -async function ensureRunning() { +export async function ensureRunning() { if (!serverPromise) { serverPromise = new Promise(startServer) } return serverPromise } - -module.exports = { - db, - ensureRunning, -} diff --git a/services/chat/test/acceptance/js/helpers/ChatClient.js b/services/chat/test/acceptance/js/helpers/ChatClient.js index e79cef3209..4827099cd5 100644 --- a/services/chat/test/acceptance/js/helpers/ChatClient.js +++ b/services/chat/test/acceptance/js/helpers/ChatClient.js @@ -1,4 +1,6 @@ -const request = require('request').defaults({ +import Request from 'request' + +const request = Request.defaults({ baseUrl: 'http://localhost:3010', }) @@ -14,7 +16,7 @@ async function asyncRequest(options) { }) } -async function sendGlobalMessage(projectId, userId, content) { +export async function sendGlobalMessage(projectId, userId, content) { return asyncRequest({ method: 'post', url: `/project/${projectId}/messages`, @@ -25,7 +27,7 @@ async function sendGlobalMessage(projectId, userId, content) { }) } -async function getGlobalMessages(projectId) { +export async function getGlobalMessages(projectId) { return asyncRequest({ method: 'get', url: `/project/${projectId}/messages`, @@ -33,7 +35,7 @@ async function getGlobalMessages(projectId) { }) } -async function sendMessage(projectId, threadId, userId, content) { +export async function sendMessage(projectId, threadId, userId, content) { return asyncRequest({ method: 'post', url: `/project/${projectId}/thread/${threadId}/messages`, @@ -44,7 +46,7 @@ async function sendMessage(projectId, threadId, userId, content) { }) } -async function getThreads(projectId) { +export async function getThreads(projectId) { return asyncRequest({ method: 'get', url: `/project/${projectId}/threads`, @@ -52,7 +54,7 @@ async function getThreads(projectId) { }) } -async function resolveThread(projectId, threadId, userId) { +export async function resolveThread(projectId, threadId, userId) { return asyncRequest({ method: 'post', url: `/project/${projectId}/thread/${threadId}/resolve`, @@ -62,21 +64,21 @@ async function resolveThread(projectId, threadId, userId) { }) } -async function reopenThread(projectId, threadId) { +export async function reopenThread(projectId, threadId) { return asyncRequest({ method: 'post', url: `/project/${projectId}/thread/${threadId}/reopen`, }) } -async function deleteThread(projectId, threadId) { +export async function deleteThread(projectId, threadId) { return asyncRequest({ method: 'delete', url: `/project/${projectId}/thread/${threadId}`, }) } -async function editMessage(projectId, threadId, messageId, content) { +export async function editMessage(projectId, threadId, messageId, content) { return asyncRequest({ method: 'post', url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`, @@ -86,7 +88,7 @@ async function editMessage(projectId, threadId, messageId, content) { }) } -async function editMessageWithUser( +export async function editMessageWithUser( projectId, threadId, messageId, @@ -103,30 +105,16 @@ async function editMessageWithUser( }) } -async function deleteMessage(projectId, threadId, messageId) { +export async function deleteMessage(projectId, threadId, messageId) { return asyncRequest({ method: 'delete', url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`, }) } -async function destroyProject(projectId) { +export async function destroyProject(projectId) { return asyncRequest({ method: 'delete', url: `/project/${projectId}`, }) } - -module.exports = { - sendGlobalMessage, - getGlobalMessages, - sendMessage, - getThreads, - resolveThread, - reopenThread, - deleteThread, - editMessage, - editMessageWithUser, - deleteMessage, - destroyProject, -}