From 92f86100852b1ee93fdee5deae1016c324ac23be Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sat, 29 Jul 2023 21:41:42 +0200 Subject: [PATCH] tests: refactor to use async --- src/tests/backend/specs/api/chat.js | 71 +++++++++++++------------ src/tests/backend/specs/api/instance.js | 12 ++--- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/tests/backend/specs/api/chat.js b/src/tests/backend/specs/api/chat.js index fcc69a363..b86917dab 100644 --- a/src/tests/backend/specs/api/chat.js +++ b/src/tests/backend/specs/api/chat.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../../common'); +const assert = require('assert').strict; let agent; const apiKey = common.apiKey; @@ -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); }); }); @@ -38,20 +39,18 @@ 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'); @@ -59,47 +58,51 @@ describe(__filename, function () { 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); }); - }); - describe('appendChatMessage', function () { - it('Adds a chat message to the pad', function (done) { - agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` + + 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); + }); + + 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'); + }); }); }); }); diff --git a/src/tests/backend/specs/api/instance.js b/src/tests/backend/specs/api/instance.js index f0180e203..62202263e 100644 --- a/src/tests/backend/specs/api/instance.js +++ b/src/tests/backend/specs/api/instance.js @@ -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'); @@ -48,7 +48,7 @@ describe(__filename, function () { } }) .expect('Content-Type', /json/) - .expect(200, done); + .expect(200); }); }); });