@@ -2,5 +2,90 @@ const test = require('node:test');
2
2
const assert = require ( 'assert' ) ;
3
3
const { Application, MailSystem } = require ( './main' ) ;
4
4
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
5
+ // // TODO: write your tests here
6
+ // // Remember to use Stub, Mock, and Spy when necessary
7
+ const fs = require ( 'fs' ) ;
8
+ const path = require ( 'path' ) ;
9
+
10
+ test ( 'MailSystem should return the correct message context.' , ( ) => {
11
+ const mailSystem = new MailSystem ( ) ;
12
+ const message = mailSystem . write ( 'Justin' ) ;
13
+
14
+ assert . strictEqual ( message , 'Congrats, Justin!' ) ;
15
+ } ) ;
16
+
17
+ test ( 'MailSystem should send mail corrctly' , ( ) => {
18
+ const mailSystem = new MailSystem ( ) ;
19
+
20
+ // SUCCESSFUL CASE
21
+ test . mock . method ( Math , 'random' , ( ) => 1 ) ;
22
+ let isMailSent = mailSystem . send ( 'Alice' , "Hello Message" ) ;
23
+ assert . strictEqual ( isMailSent , true , 'Mail should be sent successfully when Math.random() returns 1' ) ;
24
+
25
+ // FAIL CASE
26
+ test . mock . method ( Math , 'random' , ( ) => 0.5 ) ;
27
+ isMailSent = mailSystem . send ( 'Alice' , "Hello Message" ) ;
28
+ assert . strictEqual ( isMailSent , false , 'Mail should fail to send when Math.random() returns a value less than 0.5' ) ;
29
+ } ) ;
30
+
31
+ test ( 'Application should read names from file correctly' , async ( ) => {
32
+ const nameList = 'Alice\nBob\nCharlie\nSam' ;
33
+ const filePath = path . resolve ( 'name_list.txt' ) ;
34
+ fs . writeFileSync ( filePath , nameList ) ;
35
+
36
+ const app = new Application ( ) ;
37
+ const [ names , selected ] = await app . getNames ( filePath ) ;
38
+
39
+ assert . deepStrictEqual ( names , [ 'Alice' , 'Bob' , 'Charlie' , 'Sam' ] ) ;
40
+ assert . deepStrictEqual ( selected , [ ] ) ;
41
+ } ) ;
42
+
43
+ test ( 'Application should return null when all people are selected' , async ( t ) => {
44
+ const app = new Application ( ) ;
45
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
46
+ app . selected = [ 'Alice' , 'Bob' , 'Charlie' ] ;
47
+
48
+ const selectedPerson = app . selectNextPerson ( ) ;
49
+ assert . strictEqual ( selectedPerson , null ) ;
50
+ } ) ;
51
+
52
+ test ( 'Application should return a person randomly selected from the list' , ( ) => {
53
+ Math . random = ( ) => 0.2 ;
54
+
55
+ const app = new Application ( ) ;
56
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
57
+
58
+ const selectedPerson = app . getRandomPerson ( ) ;
59
+ assert ( app . people . includes ( selectedPerson ) ) ;
60
+ } ) ;
61
+
62
+ test ( 'Application should ensure no person is selected more than once' , ( ) => {
63
+ const app = new Application ( ) ;
64
+ app . people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
65
+
66
+ let randomCallCount = 0 ;
67
+ const people = [ 'Alice' , 'Bob' , 'Charlie' ] ;
68
+ app . getRandomPerson = ( ) => people [ randomCallCount ++ % people . length ] ;
69
+
70
+ app . selected = [ 'Alice' , 'Bob' ] ;
71
+
72
+ const nextSelectedPerson = app . selectNextPerson ( ) ;
73
+ assert . strictEqual ( nextSelectedPerson , 'Charlie' ) ;
74
+ assert . strictEqual ( randomCallCount , 3 ) ;
75
+ } ) ;
76
+
77
+ test ( 'Application should call write and send for each selected person' , ( ) => {
78
+ const writeMock = test . mock . fn ( ( ) => 'Message context' ) ;
79
+ const sendMock = test . mock . fn ( ( ) => true ) ;
80
+
81
+ const app = new Application ( ) ;
82
+ app . mailSystem . write = writeMock ;
83
+ app . mailSystem . send = sendMock ;
84
+
85
+ app . selected = [ 'Alice' , 'Bob' , 'Charlie' ] ;
86
+
87
+ app . notifySelected ( ) ;
88
+
89
+ assert . strictEqual ( writeMock . mock . callCount ( ) , 3 ) ;
90
+ assert . strictEqual ( sendMock . mock . callCount ( ) , 3 ) ;
91
+ } ) ;
0 commit comments