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