1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+
4
+ const fs = require ( 'fs' ) ;
5
+ const { promisify } = require ( 'util' ) ;
6
+ const writeFile = promisify ( fs . writeFile ) ;
7
+
3
8
const { Application, MailSystem } = require ( './main' ) ;
4
9
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
10
+
11
+ test ( "Test MailSystem's write function" , ( ) => {
12
+ const mailSystem = new MailSystem ( ) ;
13
+ const context = mailSystem . write ( 'OuO' ) ;
14
+ assert . strictEqual ( context , 'Congrats, OuO!' ) ;
15
+ } ) ;
16
+
17
+ test ( "Test MailSystem's send sucess" , ( ) => {
18
+ const mailSystem = new MailSystem ( ) ;
19
+
20
+ // Do stub on Math.random to always return a value > 0.5
21
+ const MathRandomFunction = Math . random ;
22
+ Math . random = ( ) => 0.51 ;
23
+ const result = mailSystem . send ( 'OuO' , 'Hello World!' ) ;
24
+ assert . strictEqual ( result , true ) ;
25
+
26
+ // Recover the original Math.random function
27
+ Math . random = MathRandomFunction ;
28
+ } ) ;
29
+
30
+ test ( "Test MailSystem's send failure" , ( ) => {
31
+ const mailSystem = new MailSystem ( ) ;
32
+
33
+ // Do stub on Math.random to always return a
34
+ const MathRandomFunction = Math . random ;
35
+ Math . random = ( ) => 0.5 ;
36
+ const result = mailSystem . send ( 'OwO' , 'Goodbye World!' ) ;
37
+ assert . strictEqual ( result , false ) ;
38
+
39
+ // Recover the original Math.random function
40
+ Math . random = MathRandomFunction ;
41
+ } ) ;
42
+
43
+ test ( "Test Application's getNames" , async ( ) => {
44
+ const fakeData = `Holo\nGawr_Gura\nOuO\nOwO\nOOAAAOooooAAOOOAOZZzzzz` ;
45
+ const fakeFile = 'name_list.txt' ;
46
+ await writeFile ( fakeFile , fakeData , 'utf8' ) ;
47
+
48
+ const application = new Application ( ) ;
49
+ const [ people , selected ] = await application . getNames ( ) ;
50
+
51
+ assert . deepStrictEqual ( people , [ 'Holo' , 'Gawr_Gura' , 'OuO' , 'OwO' , 'OOAAAOooooAAOOOAOZZzzzz' ] ) ;
52
+ assert . deepStrictEqual ( selected , [ ] ) ;
53
+ fs . unlinkSync ( fakeFile ) ;
54
+ } ) ;
55
+
56
+ test ( "Test Application's getRandomPerson" , async ( ) => {
57
+ const fakeData = `Holo\nGawr_Gura\nOuO\nOwO\nOOAAAOooooAAOOOAOZZzzzz` ;
58
+ const fakeFile = 'name_list.txt' ;
59
+ await writeFile ( fakeFile , fakeData , 'utf8' ) ;
60
+
61
+ const application = new Application ( ) ;
62
+ const [ people , selected ] = await application . getNames ( ) ;
63
+
64
+ assert . deepStrictEqual ( people , [ 'Holo' , 'Gawr_Gura' , 'OuO' , 'OwO' , 'OOAAAOooooAAOOOAOZZzzzz' ] ) ;
65
+ assert . deepStrictEqual ( selected , [ ] ) ;
66
+
67
+ // See if the random person is in the list
68
+
69
+ const numberOfTests = 5 ;
70
+
71
+ for ( let i = 0 ; i < numberOfTests ; i ++ ) {
72
+ const person = application . getRandomPerson ( ) ;
73
+ assert . ok ( application . people . includes ( person ) ) ;
74
+ }
75
+ fs . unlinkSync ( fakeFile ) ;
76
+ } ) ;
77
+
78
+ test ( "Test Application's selectNextPerson" , async ( ) => {
79
+ const fakeData = `Gawr_Gura\nOOAAAOooooAAOOOAOZZzzzz\nOuO` ;
80
+ const fakeFile = 'name_list.txt' ;
81
+ await writeFile ( fakeFile , fakeData , 'utf8' ) ;
82
+
83
+ const application = new Application ( ) ;
84
+ const [ people , selected ] = await application . getNames ( ) ;
85
+
86
+ assert . deepStrictEqual ( people , [ 'Gawr_Gura' , 'OOAAAOooooAAOOOAOZZzzzz' , 'OuO' ] ) ;
87
+ assert . deepStrictEqual ( selected , [ ] ) ;
88
+
89
+ // Select the first person
90
+ application . getRandomPerson = ( ) => { return 'Gawr_Gura' } ;
91
+ let person = application . selectNextPerson ( ) ;
92
+ assert . strictEqual ( person , 'Gawr_Gura' ) ;
93
+ assert . deepStrictEqual ( application . selected , [ 'Gawr_Gura' ] ) ;
94
+
95
+ // Select already selected person
96
+ let control = 0 ;
97
+ application . getRandomPerson = ( ) => {
98
+ if ( control === 0 ) {
99
+ control = 1 ;
100
+ return 'Gawr_Gura' ;
101
+ } else {
102
+ return 'OuO' ;
103
+ }
104
+ } ;
105
+ person = application . selectNextPerson ( ) ;
106
+ assert . strictEqual ( person , 'OuO' ) ;
107
+ assert . deepStrictEqual ( application . selected , [ 'Gawr_Gura' , 'OuO' ] ) ;
108
+
109
+ // Select the third person
110
+ application . getRandomPerson = ( ) => { return 'OOAAAOooooAAOOOAOZZzzzz' } ;
111
+ person = application . selectNextPerson ( ) ;
112
+ assert . strictEqual ( person , 'OOAAAOooooAAOOOAOZZzzzz' ) ;
113
+ assert . deepStrictEqual ( application . selected , [ 'Gawr_Gura' , 'OuO' , 'OOAAAOooooAAOOOAOZZzzzz' ] ) ;
114
+
115
+ // All selected
116
+ person = application . selectNextPerson ( ) ;
117
+ assert . strictEqual ( person , null ) ;
118
+ assert . deepStrictEqual ( application . selected , [ 'Gawr_Gura' , 'OuO' , 'OOAAAOooooAAOOOAOZZzzzz' ] ) ;
119
+
120
+ fs . unlinkSync ( fakeFile ) ;
121
+ } ) ;
122
+
123
+ test ( "Test Application's notifySelected" , async ( ) => {
124
+ const fakeData = `Gawr_Gura\nTAT\nOuO` ;
125
+ const fakeFile = 'name_list.txt' ;
126
+ await writeFile ( fakeFile , fakeData , 'utf8' ) ;
127
+
128
+ const application = new Application ( ) ;
129
+ const [ people , selected ] = await application . getNames ( ) ;
130
+
131
+ assert . deepStrictEqual ( people , [ 'Gawr_Gura' , 'TAT' , 'OuO' ] ) ;
132
+ assert . deepStrictEqual ( selected , [ ] ) ;
133
+
134
+ // Create a spy for the MailSystem's write and send methods
135
+ const mailSystemSpy = {
136
+ write : test . mock . fn ( MailSystem . prototype . write ) ,
137
+ send : test . mock . fn ( MailSystem . prototype . send )
138
+ } ;
139
+ application . mailSystem = mailSystemSpy ;
140
+ application . selected = [ 'Gawr_Gura' , 'TAT' ] ;
141
+
142
+ application . notifySelected ( ) ;
143
+
144
+ // Check the number of calls and the arguments of the calls
145
+ assert . strictEqual ( mailSystemSpy . write . mock . callCount ( ) , 2 ) ;
146
+ assert . strictEqual ( mailSystemSpy . send . mock . callCount ( ) , 2 ) ;
147
+
148
+ // Check the arguments of the calls
149
+ assert . deepStrictEqual (
150
+ mailSystemSpy . write . mock . calls . map (
151
+ call => call . arguments [ 0 ]
152
+ ) , [ 'Gawr_Gura' , 'TAT' ]
153
+ ) ;
154
+ assert . deepStrictEqual (
155
+ mailSystemSpy . send . mock . calls . map (
156
+ call => call . arguments [ 0 ]
157
+ ) , [ 'Gawr_Gura' , 'TAT' ]
158
+ ) ;
159
+
160
+ // Remove the fake file
161
+ fs . unlinkSync ( fakeFile ) ;
162
+ } ) ;
0 commit comments