2020-08-19 08:00:01 -04:00
|
|
|
const { ObjectId } = require('../../../app/js/mongodb')
|
2018-12-20 14:14:11 -05:00
|
|
|
const { expect } = require('chai')
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2018-12-20 14:14:11 -05:00
|
|
|
const ChatClient = require('./helpers/ChatClient')
|
|
|
|
const ChatApp = require('./helpers/ChatApp')
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('Getting messages', async function () {
|
2022-01-10 04:50:25 -05:00
|
|
|
const userId1 = ObjectId().toString()
|
|
|
|
const userId2 = ObjectId().toString()
|
|
|
|
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 () {
|
2022-01-10 04:50:25 -05:00
|
|
|
const projectId = ObjectId().toString()
|
2022-01-10 04:29:32 -05:00
|
|
|
before(async function () {
|
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)
|
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 () {
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response, body: messages } = await ChatClient.getGlobalMessages(
|
2022-01-10 04:50:25 -05:00
|
|
|
projectId
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
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 () {
|
2022-01-10 04:50:25 -05:00
|
|
|
const projectId = ObjectId().toString()
|
|
|
|
const threadId1 = ObjectId().toString()
|
|
|
|
const threadId2 = ObjectId().toString()
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|