2022-12-13 07:37:49 -05:00
|
|
|
import { ObjectId } from '../../../app/js/mongodb.js'
|
|
|
|
import { expect } from 'chai'
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
import * as ChatClient from './helpers/ChatClient.js'
|
|
|
|
import * as ChatApp from './helpers/ChatApp.js'
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2023-01-05 06:42:34 -05:00
|
|
|
async function getCount() {
|
|
|
|
return await ChatClient.getMetric(line => {
|
|
|
|
return (
|
|
|
|
line.includes('timer_http_request_count') &&
|
|
|
|
line.includes('path="project_{projectId}_messages"') &&
|
|
|
|
line.includes('method="POST"')
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('Getting messages', async function () {
|
2023-10-17 08:48:51 -04:00
|
|
|
const userId1 = new ObjectId().toString()
|
|
|
|
const userId2 = new ObjectId().toString()
|
2022-01-10 04:50:25 -05:00
|
|
|
const content1 = 'foo bar'
|
|
|
|
const content2 = 'hello world'
|
2022-01-10 04:29:32 -05:00
|
|
|
before(async function () {
|
|
|
|
await ChatApp.ensureRunning()
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('globally', async function () {
|
2023-10-17 08:48:51 -04:00
|
|
|
const projectId = new ObjectId().toString()
|
2022-01-10 04:29:32 -05:00
|
|
|
before(async function () {
|
2023-01-05 06:42:34 -05:00
|
|
|
const previousCount = await getCount()
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response } = await ChatClient.sendGlobalMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
userId1,
|
|
|
|
content1
|
2022-01-10 04:29:32 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
const { response: response2 } = await ChatClient.sendGlobalMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
userId2,
|
|
|
|
content2
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response2.statusCode).to.equal(201)
|
2023-01-04 10:07:58 -05:00
|
|
|
const { response: response3, body } = await ChatClient.checkStatus()
|
|
|
|
expect(response3.statusCode).to.equal(200)
|
|
|
|
expect(body).to.equal('chat is alive')
|
2023-01-05 06:42:34 -05:00
|
|
|
expect(await getCount()).to.equal(previousCount + 2)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should contain the messages and populated users when getting the messages', async function () {
|
2024-03-25 06:51:40 -04:00
|
|
|
const { response, body: messages } =
|
|
|
|
await ChatClient.getGlobalMessages(projectId)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(messages.length).to.equal(2)
|
|
|
|
messages.reverse()
|
2022-01-10 04:50:25 -05:00
|
|
|
expect(messages[0].content).to.equal(content1)
|
|
|
|
expect(messages[0].user_id).to.equal(userId1)
|
|
|
|
expect(messages[1].content).to.equal(content2)
|
|
|
|
expect(messages[1].user_id).to.equal(userId2)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('from all the threads', async function () {
|
2023-10-17 08:48:51 -04:00
|
|
|
const projectId = new ObjectId().toString()
|
|
|
|
const threadId1 = new ObjectId().toString()
|
|
|
|
const threadId2 = new ObjectId().toString()
|
2022-01-10 04:50:25 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
before(async function () {
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response } = await ChatClient.sendMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
threadId1,
|
|
|
|
userId1,
|
2022-01-10 04:29:32 -05:00
|
|
|
'one'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
const { response: response2 } = await ChatClient.sendMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
threadId2,
|
|
|
|
userId2,
|
2022-01-10 04:29:32 -05:00
|
|
|
'two'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response2.statusCode).to.equal(201)
|
|
|
|
const { response: response3 } = await ChatClient.sendMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
threadId1,
|
|
|
|
userId1,
|
2022-01-10 04:29:32 -05:00
|
|
|
'three'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response3.statusCode).to.equal(201)
|
|
|
|
const { response: response4 } = await ChatClient.sendMessage(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId,
|
|
|
|
threadId2,
|
|
|
|
userId2,
|
2022-01-10 04:29:32 -05:00
|
|
|
'four'
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response4.statusCode).to.equal(201)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should contain a dictionary of threads with messages with populated users', async function () {
|
2022-01-10 04:50:25 -05:00
|
|
|
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(Object.keys(threads).length).to.equal(2)
|
2022-01-10 04:50:25 -05:00
|
|
|
const thread1 = threads[threadId1]
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread1.messages.length).to.equal(2)
|
2022-01-10 04:50:25 -05:00
|
|
|
const thread2 = threads[threadId2]
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread2.messages.length).to.equal(2)
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread1.messages[0].content).to.equal('one')
|
2022-01-10 04:50:25 -05:00
|
|
|
expect(thread1.messages[0].user_id).to.equal(userId1)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread1.messages[1].content).to.equal('three')
|
2022-01-10 04:50:25 -05:00
|
|
|
expect(thread1.messages[1].user_id).to.equal(userId1)
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread2.messages[0].content).to.equal('two')
|
2022-01-10 04:50:25 -05:00
|
|
|
expect(thread2.messages[0].user_id).to.equal(userId2)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread2.messages[1].content).to.equal('four')
|
2022-01-10 04:50:25 -05:00
|
|
|
expect(thread2.messages[1].user_id).to.equal(userId2)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|