11'use strict' ;
22
33const common = require ( '../../common' ) ;
4+ const assert = require ( 'assert' ) . strict ;
45
56let agent ;
67const apiKey = common . apiKey ;
@@ -15,14 +16,14 @@ describe(__filename, function () {
1516 before ( async function ( ) { agent = await common . init ( ) ; } ) ;
1617
1718 describe ( 'API Versioning' , function ( ) {
18- it ( 'errors if can not connect' , function ( done ) {
19- agent . get ( '/api/' )
19+ it ( 'errors if can not connect' , async function ( ) {
20+ await agent . get ( '/api/' )
2021 . expect ( ( res ) => {
2122 apiVersion = res . body . currentVersion ;
2223 if ( ! res . body . currentVersion ) throw new Error ( 'No version set in API' ) ;
2324 return ;
2425 } )
25- . expect ( 200 , done ) ;
26+ . expect ( 200 ) ;
2627 } ) ;
2728 } ) ;
2829
@@ -38,68 +39,70 @@ describe(__filename, function () {
3839 -> getChatHistory(padID)
3940 */
4041
41- describe ( 'createPad ' , function ( ) {
42- it ( 'creates a new Pad' , function ( done ) {
43- agent . get ( `${ endPoint ( 'createPad' ) } &padID=${ padID } ` )
42+ describe ( 'Chat functionality ' , function ( ) {
43+ it ( 'creates a new Pad' , async function ( ) {
44+ await agent . get ( `${ endPoint ( 'createPad' ) } &padID=${ padID } ` )
4445 . expect ( ( res ) => {
4546 if ( res . body . code !== 0 ) throw new Error ( 'Unable to create new Pad' ) ;
4647 } )
4748 . expect ( 'Content-Type' , / j s o n / )
48- . expect ( 200 , done ) ;
49+ . expect ( 200 ) ;
4950 } ) ;
50- } ) ;
5151
52- describe ( 'createAuthor' , function ( ) {
53- it ( 'Creates an author with a name set' , function ( done ) {
54- agent . get ( endPoint ( 'createAuthor' ) )
52+ it ( 'Creates an author with a name set' , async function ( ) {
53+ await agent . get ( endPoint ( 'createAuthor' ) )
5554 . expect ( ( res ) => {
5655 if ( res . body . code !== 0 || ! res . body . data . authorID ) {
5756 throw new Error ( 'Unable to create author' ) ;
5857 }
5958 authorID = res . body . data . authorID ; // we will be this author for the rest of the tests
6059 } )
6160 . expect ( 'Content-Type' , / j s o n / )
62- . expect ( 200 , done ) ;
61+ . expect ( 200 ) ;
62+ } ) ;
63+
64+ it ( 'Gets the head of chat before the first chat msg' , async function ( ) {
65+ await agent . get ( `${ endPoint ( 'getChatHead' ) } &padID=${ padID } ` )
66+ . expect ( ( res ) => {
67+ if ( res . body . data . chatHead !== - 1 ) throw new Error ( 'Chat Head Length is wrong' ) ;
68+ if ( res . body . code !== 0 ) throw new Error ( 'Unable to get chat head' ) ;
69+ } )
70+ . expect ( 'Content-Type' , / j s o n / )
71+ . expect ( 200 ) ;
6372 } ) ;
64- } ) ;
6573
66- describe ( 'appendChatMessage' , function ( ) {
67- it ( 'Adds a chat message to the pad' , function ( done ) {
68- agent . get ( `${ endPoint ( 'appendChatMessage' ) } &padID=${ padID } &text=blalblalbha` +
74+ it ( 'Adds a chat message to the pad' , async function ( ) {
75+ await agent . get ( `${ endPoint ( 'appendChatMessage' ) } &padID=${ padID } &text=blalblalbha` +
6976 `&authorID=${ authorID } &time=${ timestamp } ` )
7077 . expect ( ( res ) => {
7178 if ( res . body . code !== 0 ) throw new Error ( 'Unable to create chat message' ) ;
7279 } )
7380 . expect ( 'Content-Type' , / j s o n / )
74- . expect ( 200 , done ) ;
81+ . expect ( 200 ) ;
7582 } ) ;
76- } ) ;
77-
7883
79- describe ( 'getChatHead' , function ( ) {
80- it ( 'Gets the head of chat' , function ( done ) {
81- agent . get ( `${ endPoint ( 'getChatHead' ) } &padID=${ padID } ` )
84+ it ( 'Gets the head of chat' , async function ( ) {
85+ await agent . get ( `${ endPoint ( 'getChatHead' ) } &padID=${ padID } ` )
8286 . expect ( ( res ) => {
8387 if ( res . body . data . chatHead !== 0 ) throw new Error ( 'Chat Head Length is wrong' ) ;
8488
8589 if ( res . body . code !== 0 ) throw new Error ( 'Unable to get chat head' ) ;
8690 } )
8791 . expect ( 'Content-Type' , / j s o n / )
88- . expect ( 200 , done ) ;
92+ . expect ( 200 ) ;
8993 } ) ;
90- } ) ;
9194
92- describe ( 'getChatHistory' , function ( ) {
93- it ( 'Gets Chat History of a Pad' , function ( done ) {
94- agent . get ( `${ endPoint ( 'getChatHistory' ) } &padID=${ padID } ` )
95- . expect ( ( res ) => {
96- if ( res . body . data . messages . length !== 1 ) {
97- throw new Error ( 'Chat History Length is wrong' ) ;
98- }
99- if ( res . body . code !== 0 ) throw new Error ( 'Unable to get chat history' ) ;
100- } )
95+ it ( 'Gets Chat History of a Pad' , async function ( ) {
96+ await agent . get ( `${ endPoint ( 'getChatHistory' ) } &padID=${ padID } ` )
10197 . expect ( 'Content-Type' , / j s o n / )
102- . expect ( 200 , done ) ;
98+ . expect ( 200 )
99+ . expect ( ( res ) => {
100+ assert . equal ( res . body . code , 0 , 'Unable to get chat history' ) ;
101+ assert . equal ( res . body . data . messages . length , 1 , 'Chat History Length is wrong' ) ;
102+ assert . equal ( res . body . data . messages [ 0 ] . text , 'blalblalbha' , 'Chat text does not match' ) ;
103+ assert . equal ( res . body . data . messages [ 0 ] . userId , authorID , 'Message author does not match' ) ;
104+ assert . equal ( res . body . data . messages [ 0 ] . time , timestamp . toString ( ) , 'Message time does not match' ) ;
105+ } ) ;
103106 } ) ;
104107 } ) ;
105108} ) ;
0 commit comments