1
1
'use strict' ;
2
2
3
3
const common = require ( '../../common' ) ;
4
+ const assert = require ( 'assert' ) . strict ;
4
5
5
6
let agent ;
6
7
const apiKey = common . apiKey ;
@@ -15,14 +16,14 @@ describe(__filename, function () {
15
16
before ( async function ( ) { agent = await common . init ( ) ; } ) ;
16
17
17
18
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/' )
20
21
. expect ( ( res ) => {
21
22
apiVersion = res . body . currentVersion ;
22
23
if ( ! res . body . currentVersion ) throw new Error ( 'No version set in API' ) ;
23
24
return ;
24
25
} )
25
- . expect ( 200 , done ) ;
26
+ . expect ( 200 ) ;
26
27
} ) ;
27
28
} ) ;
28
29
@@ -38,68 +39,70 @@ describe(__filename, function () {
38
39
-> getChatHistory(padID)
39
40
*/
40
41
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 } ` )
44
45
. expect ( ( res ) => {
45
46
if ( res . body . code !== 0 ) throw new Error ( 'Unable to create new Pad' ) ;
46
47
} )
47
48
. expect ( 'Content-Type' , / j s o n / )
48
- . expect ( 200 , done ) ;
49
+ . expect ( 200 ) ;
49
50
} ) ;
50
- } ) ;
51
51
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' ) )
55
54
. expect ( ( res ) => {
56
55
if ( res . body . code !== 0 || ! res . body . data . authorID ) {
57
56
throw new Error ( 'Unable to create author' ) ;
58
57
}
59
58
authorID = res . body . data . authorID ; // we will be this author for the rest of the tests
60
59
} )
61
60
. 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 ) ;
63
72
} ) ;
64
- } ) ;
65
73
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` +
69
76
`&authorID=${ authorID } &time=${ timestamp } ` )
70
77
. expect ( ( res ) => {
71
78
if ( res . body . code !== 0 ) throw new Error ( 'Unable to create chat message' ) ;
72
79
} )
73
80
. expect ( 'Content-Type' , / j s o n / )
74
- . expect ( 200 , done ) ;
81
+ . expect ( 200 ) ;
75
82
} ) ;
76
- } ) ;
77
-
78
83
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 } ` )
82
86
. expect ( ( res ) => {
83
87
if ( res . body . data . chatHead !== 0 ) throw new Error ( 'Chat Head Length is wrong' ) ;
84
88
85
89
if ( res . body . code !== 0 ) throw new Error ( 'Unable to get chat head' ) ;
86
90
} )
87
91
. expect ( 'Content-Type' , / j s o n / )
88
- . expect ( 200 , done ) ;
92
+ . expect ( 200 ) ;
89
93
} ) ;
90
- } ) ;
91
94
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 } ` )
101
97
. 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
+ } ) ;
103
106
} ) ;
104
107
} ) ;
105
108
} ) ;
0 commit comments