Skip to content

Commit

Permalink
tests: refactor to use async
Browse files Browse the repository at this point in the history
  • Loading branch information
webzwo0i committed Jul 29, 2023
1 parent 534b794 commit 2eb02e5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
71 changes: 37 additions & 34 deletions src/tests/backend/specs/api/chat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const common = require('../../common');
const assert = require('assert').strict;

let agent;
const apiKey = common.apiKey;
Expand All @@ -15,14 +16,14 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); });

describe('API Versioning', function () {
it('errors if can not connect', function (done) {
agent.get('/api/')
it('errors if can not connect', async function () {
await agent.get('/api/')
.expect((res) => {
apiVersion = res.body.currentVersion;
if (!res.body.currentVersion) throw new Error('No version set in API');
return;
})
.expect(200, done);
.expect(200);
});
});

Expand All @@ -38,68 +39,70 @@ describe(__filename, function () {
-> getChatHistory(padID)
*/

describe('createPad', function () {
it('creates a new Pad', function (done) {
agent.get(`${endPoint('createPad')}&padID=${padID}`)
describe('Chat functionality', function () {
it('creates a new Pad', async function () {
await agent.get(`${endPoint('createPad')}&padID=${padID}`)
.expect((res) => {
if (res.body.code !== 0) throw new Error('Unable to create new Pad');
})
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});
});

describe('createAuthor', function () {
it('Creates an author with a name set', function (done) {
agent.get(endPoint('createAuthor'))
it('Creates an author with a name set', async function () {
await agent.get(endPoint('createAuthor'))
.expect((res) => {
if (res.body.code !== 0 || !res.body.data.authorID) {
throw new Error('Unable to create author');
}
authorID = res.body.data.authorID; // we will be this author for the rest of the tests
})
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});

it('Gets the head of chat before the first chat msg', async function () {
await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
.expect((res) => {
if (res.body.data.chatHead !== -1) throw new Error('Chat Head Length is wrong');
if (res.body.code !== 0) throw new Error('Unable to get chat head');
})
.expect('Content-Type', /json/)
.expect(200);
});
});

describe('appendChatMessage', function () {
it('Adds a chat message to the pad', function (done) {
agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` +
it('Adds a chat message to the pad', async function () {
await agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` +
`&authorID=${authorID}&time=${timestamp}`)
.expect((res) => {
if (res.body.code !== 0) throw new Error('Unable to create chat message');
})
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});
});


describe('getChatHead', function () {
it('Gets the head of chat', function (done) {
agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
it('Gets the head of chat', async function () {
await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
.expect((res) => {
if (res.body.data.chatHead !== 0) throw new Error('Chat Head Length is wrong');

if (res.body.code !== 0) throw new Error('Unable to get chat head');
})
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});
});

describe('getChatHistory', function () {
it('Gets Chat History of a Pad', function (done) {
agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
.expect((res) => {
if (res.body.data.messages.length !== 1) {
throw new Error('Chat History Length is wrong');
}
if (res.body.code !== 0) throw new Error('Unable to get chat history');
})
it('Gets Chat History of a Pad', async function () {
await agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200)
.expect((res) => {
assert.equal(res.body.code, 0, 'Unable to get chat history');
assert.equal(res.body.data.messages.length, 1, 'Chat History Length is wrong');
assert.equal(res.body.data.messages[0].text, 'blalblalbha', 'Chat text does not match');
assert.equal(res.body.data.messages[0].userId, authorID, 'Message author does not match');
assert.equal(res.body.data.messages[0].time, timestamp.toString(), 'Message time does not match');
});
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/tests/backend/specs/api/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ describe(__filename, function () {
before(async function () { agent = await common.init(); });

describe('Connectivity for instance-level API tests', function () {
it('can connect', function (done) {
agent.get('/api/')
it('can connect', async function () {
await agent.get('/api/')
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});
});

describe('getStats', function () {
it('Gets the stats of a running instance', function (done) {
agent.get(endPoint('getStats'))
it('Gets the stats of a running instance', async function () {
await agent.get(endPoint('getStats'))
.expect((res) => {
if (res.body.code !== 0) throw new Error('getStats() failed');

Expand All @@ -48,7 +48,7 @@ describe(__filename, function () {
}
})
.expect('Content-Type', /json/)
.expect(200, done);
.expect(200);
});
});
});

0 comments on commit 2eb02e5

Please sign in to comment.