11/* node:coverage disable */
22
3- const { LoggedServer} = require ( '../../../js/index' ) . server ;
43const { describe, it, mock, beforeEach, afterEach} = require ( 'node:test' ) ;
54const assert = require ( 'node:assert' ) ;
65
6+ const { LoggedServer} = require ( '../../../js' ) . server ;
7+
8+ const diagnosticLogger = {
9+ debug ( ) {
10+ }
11+ }
12+
13+ const diagnosticOrigin = {
14+ start ( ) {
15+ } ,
16+ stop ( ) {
17+ } ,
18+ options ( ) {
19+ }
20+ }
721
822function prepareDiagnostic ( ) {
23+ diagnosticLogger . options = { } ;
24+ diagnosticLogger . debug = ( message ) => {
25+ diagnosticLogger . options . message = message ;
26+ return diagnosticLogger ;
27+ }
28+
29+ mock . method ( diagnosticLogger , 'debug' ) ;
30+
31+ diagnosticOrigin . start = ( ) => {
32+ return diagnosticOrigin ;
33+ }
34+ diagnosticOrigin . stop = ( ) => {
35+ return diagnosticOrigin ;
36+ }
37+ diagnosticOrigin . options = ( ) => {
38+ return { port : 80 } ;
39+ }
40+
41+ mock . method ( diagnosticOrigin , 'start' ) ;
42+ mock . method ( diagnosticOrigin , 'stop' ) ;
43+ mock . method ( diagnosticOrigin , 'options' ) ;
944}
1045
1146function resetDiagnostic ( ) {
@@ -20,7 +55,158 @@ describe('LoggedServer', () => {
2055 it ( 'should not call anything' , ( ) => {
2156 assert . doesNotThrow ( ( ) => {
2257 new LoggedServer ( ) ;
58+ new LoggedServer ( diagnosticOrigin , diagnosticLogger ) ;
2359 } ) ;
2460 } ) ;
2561 } ) ;
62+
63+ describe ( 'start' , ( ) => {
64+ it ( 'should fall, cause origin is null' , async ( ) => {
65+ await assert . rejects ( ( ) => new LoggedServer ( ) . start ( ) ,
66+ { name : 'TypeError' } ) ;
67+ } ) ;
68+
69+ it ( 'should fall, cause origin start throws error' , async ( ) => {
70+ diagnosticOrigin . start = ( ) => { throw new Error ( 'start error' ) } ;
71+ mock . method ( diagnosticOrigin , 'start' ) ;
72+
73+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin ) . start ( ) ,
74+ { message : 'start error' } ) ;
75+
76+ assert . strictEqual ( diagnosticOrigin . start . mock . calls . length , 1 ) ;
77+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 0 ) ;
78+ } ) ;
79+
80+ it ( 'should fall, cause origin options throws error' , async ( ) => {
81+ diagnosticOrigin . options = ( ) => { throw new Error ( 'options error' ) } ;
82+ mock . method ( diagnosticOrigin , 'options' ) ;
83+
84+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . start ( ) ,
85+ { message : 'options error' } ) ;
86+
87+ assert . strictEqual ( diagnosticOrigin . start . mock . calls . length , 1 ) ;
88+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
89+ } ) ;
90+
91+ it ( 'should fall, cause logger is null' , async ( ) => {
92+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin ) . start ( ) ,
93+ { name : 'TypeError' } ) ;
94+
95+ assert . strictEqual ( diagnosticOrigin . start . mock . calls . length , 1 ) ;
96+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 0 ) ;
97+ } ) ;
98+
99+ it ( 'should fall, cause logger debug throws error' , async ( ) => {
100+ diagnosticLogger . debug = ( ) => { throw new Error ( 'debug error' ) } ;
101+ mock . method ( diagnosticLogger , 'debug' ) ;
102+
103+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . start ( ) ,
104+ { message : 'debug error' } ) ;
105+
106+ assert . strictEqual ( diagnosticOrigin . start . mock . calls . length , 1 ) ;
107+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
108+ assert . strictEqual ( diagnosticLogger . debug . mock . calls . length , 1 ) ;
109+ } ) ;
110+
111+ it ( 'should not fall' , async ( ) => {
112+ await assert . doesNotReject ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . start ( ) , ) ;
113+
114+ assert . strictEqual ( diagnosticOrigin . start . mock . calls . length , 1 ) ;
115+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
116+ assert . strictEqual ( diagnosticLogger . debug . mock . calls . length , 1 ) ;
117+ } ) ;
118+
119+ it ( 'should logg message' , async ( ) => {
120+ await assert . doesNotReject ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . start ( ) ) ;
121+
122+ assert . strictEqual ( diagnosticLogger . options . message , 'HttpServer is running at port: 80' ) ;
123+ } ) ;
124+
125+ it ( 'should return new Server instance' , async ( ) => {
126+ const server = new LoggedServer ( diagnosticOrigin , diagnosticLogger ) ;
127+ const startedServer = await server . start ( ) ;
128+
129+ assert . notEqual ( server , startedServer ) ;
130+ assert . strictEqual ( typeof server , typeof startedServer )
131+ } ) ;
132+ } ) ;
133+
134+ describe ( 'stop' , ( ) => {
135+ it ( 'should fall, cause origin is null' , async ( ) => {
136+ await assert . rejects ( ( ) => new LoggedServer ( ) . stop ( ) ,
137+ { name : 'TypeError' } ) ;
138+ } ) ;
139+
140+ it ( 'should fall, cause origin stop throws error' , async ( ) => {
141+ diagnosticOrigin . stop = ( ) => { throw new Error ( 'stop error' ) } ;
142+ mock . method ( diagnosticOrigin , 'stop' ) ;
143+
144+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin ) . stop ( ) ,
145+ { message : 'stop error' } ) ;
146+
147+ assert . strictEqual ( diagnosticOrigin . stop . mock . calls . length , 1 ) ;
148+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 0 ) ;
149+ } ) ;
150+
151+ it ( 'should fall, cause origin options throws error' , async ( ) => {
152+ diagnosticOrigin . options = ( ) => { throw new Error ( 'options error' ) } ;
153+ mock . method ( diagnosticOrigin , 'options' ) ;
154+
155+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . stop ( ) ,
156+ { message : 'options error' } ) ;
157+
158+ assert . strictEqual ( diagnosticOrigin . stop . mock . calls . length , 1 ) ;
159+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
160+ } ) ;
161+
162+ it ( 'should fall, cause logger is null' , async ( ) => {
163+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin ) . stop ( ) ,
164+ { name : 'TypeError' } ) ;
165+
166+ assert . strictEqual ( diagnosticOrigin . stop . mock . calls . length , 1 ) ;
167+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 0 ) ;
168+ } ) ;
169+
170+ it ( 'should fall, cause logger debug throws error' , async ( ) => {
171+ diagnosticLogger . debug = ( ) => { throw new Error ( 'debug error' ) } ;
172+ mock . method ( diagnosticLogger , 'debug' ) ;
173+
174+ await assert . rejects ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . stop ( ) ,
175+ { message : 'debug error' } ) ;
176+
177+ assert . strictEqual ( diagnosticOrigin . stop . mock . calls . length , 1 ) ;
178+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
179+ assert . strictEqual ( diagnosticLogger . debug . mock . calls . length , 1 ) ;
180+ } ) ;
181+
182+ it ( 'should not fall' , async ( ) => {
183+ await assert . doesNotReject ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . stop ( ) , ) ;
184+
185+ assert . strictEqual ( diagnosticOrigin . stop . mock . calls . length , 1 ) ;
186+ assert . strictEqual ( diagnosticOrigin . options . mock . calls . length , 1 ) ;
187+ assert . strictEqual ( diagnosticLogger . debug . mock . calls . length , 1 ) ;
188+ } ) ;
189+
190+ it ( 'should logg message' , async ( ) => {
191+ await assert . doesNotReject ( ( ) => new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . stop ( ) ) ;
192+
193+ assert . strictEqual ( diagnosticLogger . options . message , 'HttpServer at port: 80 is stopped' ) ;
194+ } ) ;
195+
196+ it ( 'should return new Server instance' , async ( ) => {
197+ const server = new LoggedServer ( diagnosticOrigin , diagnosticLogger ) ;
198+ const stoppedServer = await server . stop ( ) ;
199+
200+ assert . notEqual ( server , stoppedServer ) ;
201+ assert . strictEqual ( typeof server , typeof stoppedServer )
202+ } ) ;
203+ } ) ;
204+
205+ describe ( 'options' , ( ) => {
206+ it ( 'should return same options' , ( ) => {
207+ const resultOptions = new LoggedServer ( diagnosticOrigin , diagnosticLogger ) . options ( ) ;
208+
209+ assert . deepStrictEqual ( resultOptions , diagnosticOrigin . options ( ) ) ;
210+ } ) ;
211+ } ) ;
26212} ) ;
0 commit comments