1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const os = require ( 'os' ) ;
5
+ const path = require ( 'path' ) ;
6
+
3
7
const { Application, MailSystem } = require ( './main' ) ;
4
8
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
9
+ test ( 'should return a message context' , ( ) => {
10
+ const mailSystem = new MailSystem ( ) ;
11
+ const name = 'John' ;
12
+
13
+ const context = mailSystem . write ( name ) ;
14
+ assert . strictEqual ( context , 'Congrats, John!' ) ;
15
+
16
+ } ) ;
17
+
18
+ test ( 'should return true if mail is sent successfully' , ( t ) => {
19
+ const mailSystem = new MailSystem ( ) ;
20
+ const name = 'John' ;
21
+
22
+ // test send mail success return true
23
+ t . mock . method ( Math , 'random' , ( ) => 1 ) ;
24
+ is_send = mailSystem . send ( 'Joy' , "test mail" ) ;
25
+ assert . strictEqual ( is_send , true ) ;
26
+
27
+
28
+ // test send mail fail return false
29
+ t . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
30
+ is_send = mailSystem . send ( 'Joy' , "test mail" ) ;
31
+ assert . strictEqual ( is_send , false ) ;
32
+
33
+ } ) ;
34
+
35
+
36
+
37
+
38
+
39
+ test ( 'should return name_list ' , async ( ) => {
40
+
41
+ // Write mock name list to a temporary file
42
+ const nameListContent = 'Alice\nBob\nCharlie' ;
43
+ const tempFilePath = path . join ( 'name_list.txt' ) ;
44
+ fs . writeFileSync ( tempFilePath , nameListContent ) ;
45
+
46
+ // Attach cleanup handler to the process exit event
47
+ process . on ( 'exit' , ( ) => {
48
+ if ( tempFilePath ) {
49
+ // Clean up: Delete the temporary directory
50
+ fs . unlinkSync ( tempFilePath ) ;
51
+ }
52
+ } ) ;
53
+
54
+ // Instantiate Application class and call getNames with the temporary file path
55
+ const app = new Application ( ) ;
56
+ const [ people , selected ] = await app . getNames ( tempFilePath ) ;
57
+
58
+ // Assert the results
59
+ assert . deepStrictEqual ( people , [ 'Alice' , 'Bob' , 'Charlie' ] ) ;
60
+ assert . deepStrictEqual ( selected , [ ] ) ;
61
+
62
+ } ) ;
63
+
64
+
65
+ test ( 'should return null if all people are selected' , async ( t ) => {
66
+ const app = new Application ( ) ;
67
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
68
+ app . selected = [ 'Alice' , 'Bob' , 'Charlie' ] ;
69
+
70
+ const result = app . selectNextPerson ( ) ;
71
+ assert . strictEqual ( result , null ) ;
72
+ } ) ;
73
+
74
+ //Test case for getRandomPerson() method
75
+ test ( 'should return a random person' , ( ) => {
76
+ // Stub Math.random() to return a fixed value
77
+ Math . random = ( ) => 0.5 ; // Set Math.random() to always return 0.5
78
+ const randomNumber = Math . random ( ) ;
79
+
80
+ // Create an instance of the Application class
81
+ const app = new Application ( ) ;
82
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
83
+ // Call the getRandomPerson() method
84
+ const randomPerson = app . getRandomPerson ( ) ;
85
+
86
+ // Ensure that the random person is one of the people in the list
87
+ assert ( app . people . includes ( randomPerson ) ) ;
88
+
89
+ } ) ;
90
+
91
+ test ( 'should select and return a person who has not been selected yet' , ( ) => {
92
+ const app = new Application ( ) ;
93
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
94
+
95
+ let getRandomPersonCallCount = 0 ;
96
+ app . getRandomPerson = ( ) => {
97
+ switch ( getRandomPersonCallCount ++ ) {
98
+ case 0 :
99
+ return 'Alice' ;
100
+ case 1 :
101
+ return 'Bob' ;
102
+ case 2 :
103
+ return 'Charlie' ;
104
+ }
105
+ } ;
106
+
107
+ app . selected = [ 'Alice' , 'Bob' ] ;
108
+
109
+ const result = app . selectNextPerson ( ) ;
110
+
111
+ assert . strictEqual ( result , 'Charlie' ) ;
112
+ assert . strictEqual ( getRandomPersonCallCount , 3 ) ;
113
+ } ) ;
114
+
115
+ class MockMailSystem {
116
+ constructor ( ) {
117
+ this . writeCallCount = 0 ;
118
+ this . sendCallCount = 0 ;
119
+ }
120
+
121
+ write ( ) {
122
+ this . writeCallCount ++ ;
123
+ return 'Message context' ;
124
+ }
125
+
126
+ send ( ) {
127
+ this . sendCallCount ++ ;
128
+ return true ;
129
+ }
130
+ }
131
+
132
+ test ( 'should call write and send methods of MailSystem for each selected person' , ( ) => {
133
+ const mailSystem = new MockMailSystem ( ) ;
134
+
135
+ const app = new Application ( ) ;
136
+ app . mailSystem = mailSystem ;
137
+ app . selected = [ 'Alice' , 'Bob' , 'Charlie' ] ;
138
+
139
+ app . notifySelected ( ) ;
140
+
141
+ assert . strictEqual ( mailSystem . writeCallCount , 3 ) ;
142
+ assert . strictEqual ( mailSystem . sendCallCount , 3 ) ;
143
+
144
+
145
+ } ) ;
146
+
0 commit comments