1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+ const fs = require ( 'fs' ) ;
3
4
const { Application, MailSystem } = require ( './main' ) ;
4
5
5
6
// TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
7
+ // Remember to use Stub, Mock, and Spy when necessary
8
+ // test("Test MailSystem's write", () => {
9
+ // const mailsystem = new MailSystem();
10
+ // assert.strictEqual(mailsystem.write("John"), 'Congrats, John!');
11
+ // });
12
+ // test("Test MailSystem's send",()=>{
13
+ // //test('should send mail successfully', () => {
14
+ // const mailsystem = new MailSystem();
15
+ // const name = 'John';
16
+ // Math.random = () => 0.7;
17
+ // const result = mailsystem.send(name,mailsystem.write(name));
18
+ // assert.strictEqual(result,true);
19
+ // Math.random = originalMathRandom;
20
+
21
+ // Math.random = () => 0.3;
22
+ // const result_ = mailsystem.send(name,mailsystem.write(name));
23
+ // assert.strictEqual(result_,false);
24
+ // });
25
+
26
+ const data = 'John\nJane\nDoe' ;
27
+ const originalMathRandom = Math . random ;
28
+ test ( "Test Application's getNames" , async ( ) => {
29
+ //mock fs.readFile just using test runner
30
+ fs . writeFileSync ( 'name_list.txt' , data , 'utf8' ) ;
31
+ // fs.readFile = (path, encoding, cb) => {
32
+ // cb(null, data);
33
+ // };
34
+ const app = new Application ( ) ;
35
+ const [ people , selected ] = await app . getNames ( ) ;
36
+ assert . deepStrictEqual ( people , [ 'John' , 'Jane' , 'Doe' ] ) ;
37
+ assert . deepStrictEqual ( selected , [ ] ) ;
38
+ //fs.unlink('name_list.txt', () => {});
39
+ } ) ;
40
+
41
+
42
+ test ( "Test Application's getRandomPerson" , async ( ) => {
43
+ let app = new Application ( ) ;
44
+ [ app . people , app . selected ] = await app . getNames ( ) ;
45
+ //const originalMathRandom = Math.random;
46
+ Math . random = ( ) => 0 ;
47
+ let result = await app . getRandomPerson ( ) ;
48
+ assert . deepStrictEqual ( result , 'John' ) ;
49
+ Math . random = ( ) => 0.5 ;
50
+ result = await app . getRandomPerson ( ) ;
51
+ assert . deepStrictEqual ( result , 'Jane' ) ;
52
+ Math . random = ( ) => 0.9 ;
53
+ result = await app . getRandomPerson ( ) ;
54
+ assert . deepStrictEqual ( result , 'Doe' ) ;
55
+ Math . random = originalMathRandom ;
56
+ } ) ;
57
+
58
+ test ( "Test Application's selectNextPerson" , async ( ) => {
59
+ let app = new Application ( ) ;
60
+ [ app . people , app . selected ] = await app . getNames ( ) ;
61
+ //const originalMathRandom = Math.random;
62
+ //Math.random = () => 0;
63
+ Math . random = originalMathRandom ;
64
+ Math . random = ( ) => 0 ;
65
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , 'John' ) ;
66
+ //Math.random = originalMathRandom;
67
+ Math . random = ( ) => 0.5 ;
68
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , 'Jane' ) ;
69
+ //Math.random = () => 0.9;
70
+ Math . random = originalMathRandom ;
71
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , 'Doe' ) ;
72
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , null ) ;
73
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , null ) ;
74
+ assert . deepStrictEqual ( app . selectNextPerson ( ) , null ) ;
75
+ //fs.unlink('name_list.txt', () => {});
76
+ } ) ;
77
+
78
+ test ( "Test Application's notifySelected" , async ( t ) => {
79
+ let app = new Application ( ) ;
80
+ [ app . people , app . selected ] = await app . getNames ( ) ;
81
+ const originalMathRandom = Math . random ;
82
+ Math . random = ( ) => 0 ;
83
+ app . selectNextPerson ( ) ;
84
+ Math . random = ( ) => 0.5 ;
85
+ app . selectNextPerson ( ) ;
86
+ Math . random = ( ) => 0.9 ;
87
+ app . selectNextPerson ( ) ;
88
+ Math . random = originalMathRandom ;
89
+
90
+ t . mock . method ( app . mailSystem , 'write' ) ;
91
+ t . mock . method ( app . mailSystem , 'send' ) ;
92
+ Math . random = ( ) => 0.7 ;
93
+ app . notifySelected ( ) ;
94
+
95
+ let call_write = app . mailSystem . write . mock . calls [ 0 ] ;
96
+ let call_send = app . mailSystem . send . mock . calls [ 0 ] ;
97
+
98
+ assert . deepStrictEqual ( call_send . arguments , [ 'John' , 'Congrats, John!' ] ) ;
99
+ assert . deepStrictEqual ( call_write . arguments , [ 'John' ] ) ;
100
+ assert . strictEqual ( call_send . result , true ) ;
101
+ assert . strictEqual ( call_write . result , 'Congrats, John!' ) ;
102
+
103
+ call_write = app . mailSystem . write . mock . calls [ 1 ] ;
104
+ call_send = app . mailSystem . send . mock . calls [ 1 ] ;
105
+
106
+ assert . deepStrictEqual ( call_send . arguments , [ 'Jane' , 'Congrats, Jane!' ] ) ;
107
+ assert . deepStrictEqual ( call_write . arguments , [ 'Jane' ] ) ;
108
+ assert . strictEqual ( call_send . result , true ) ;
109
+ assert . strictEqual ( call_write . result , 'Congrats, Jane!' ) ;
110
+
111
+ call_write = app . mailSystem . write . mock . calls [ 2 ] ;
112
+ call_send = app . mailSystem . send . mock . calls [ 2 ] ;
113
+
114
+ assert . deepStrictEqual ( call_send . arguments , [ 'Doe' , 'Congrats, Doe!' ] ) ;
115
+ assert . deepStrictEqual ( call_write . arguments , [ 'Doe' ] ) ;
116
+ assert . strictEqual ( call_send . result , true ) ;
117
+ assert . strictEqual ( call_write . result , 'Congrats, Doe!' ) ;
118
+
119
+ Math . random = ( ) => 0.3 ;
120
+ app . notifySelected ( ) ;
121
+
122
+ call_write = app . mailSystem . write . mock . calls [ 3 ] ;
123
+ call_send = app . mailSystem . send . mock . calls [ 3 ] ;
124
+
125
+ assert . deepStrictEqual ( call_send . arguments , [ 'John' , 'Congrats, John!' ] ) ;
126
+ assert . deepStrictEqual ( call_write . arguments , [ 'John' ] ) ;
127
+ assert . strictEqual ( call_send . result , false ) ;
128
+ assert . strictEqual ( call_write . result , 'Congrats, John!' ) ;
129
+
130
+ call_write = app . mailSystem . write . mock . calls [ 4 ] ;
131
+ call_send = app . mailSystem . send . mock . calls [ 4 ] ;
132
+
133
+ assert . deepStrictEqual ( call_send . arguments , [ 'Jane' , 'Congrats, Jane!' ] ) ;
134
+ assert . deepStrictEqual ( call_write . arguments , [ 'Jane' ] ) ;
135
+ assert . strictEqual ( call_send . result , false ) ;
136
+ assert . strictEqual ( call_write . result , 'Congrats, Jane!' ) ;
137
+
138
+ call_write = app . mailSystem . write . mock . calls [ 5 ] ;
139
+ call_send = app . mailSystem . send . mock . calls [ 5 ] ;
140
+
141
+ assert . deepStrictEqual ( call_send . arguments , [ 'Doe' , 'Congrats, Doe!' ] ) ;
142
+ assert . deepStrictEqual ( call_write . arguments , [ 'Doe' ] ) ;
143
+ assert . strictEqual ( call_send . result , false ) ;
144
+ assert . strictEqual ( call_write . result , 'Congrats, Doe!' ) ;
145
+ Math . random = originalMathRandom ;
146
+ fs . unlink ( 'name_list.txt' , ( ) => { } ) ; // cleanup
147
+ } ) ;
0 commit comments