1
+
1
2
const test = require ( 'node:test' ) ;
2
3
const assert = require ( 'assert' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+
6
+ // 模擬 fs.readFile ,返回假數據
7
+ test . mock . method ( fs , 'readFile' , ( file , options , callback ) => {
8
+ callback ( null , 'Alice\njohn\nBob' ) ;
9
+ } ) ;
10
+
11
+ // 從 main.js 導入 Application 和 MailSystem 類
3
12
const { Application, MailSystem } = require ( './main' ) ;
4
13
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
14
+ // testMailSystem_write()
15
+ test ( 'MailSystem_write()' , ( ) => {
16
+ const ms = new MailSystem ( ) ;
17
+ assert . equal ( ms . write ( 'Alice' ) , 'Congrats, Alice!' ) ;
18
+ assert . equal ( ms . write ( null ) , 'Congrats, null!' ) ;
19
+ assert . equal ( ms . write ( 512558020 ) , 'Congrats, 512558020!' ) ;
20
+ } ) ;
21
+
22
+ // test MailSystem_send()
23
+ test ( 'MailSystem_send()' , ( ) => {
24
+ const ms = new MailSystem ( ) ;
25
+ const name = 'Alice' ;
26
+ test . mock . method ( Math , 'random' , ( ) => 0.6 ) ;
27
+ assert . equal ( ms . send ( name , 'success' ) , true ) ;
28
+ test . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
29
+ assert . equal ( ms . send ( name , 'fail' ) , false ) ;
30
+ } ) ;
31
+
32
+ // test Application_getNames()
33
+ test ( 'Application_getNames()' , async ( ) => {
34
+ const app = new Application ( ) ;
35
+ const name_list = [ 'Alice' , 'john' , 'Bob' ] ;
36
+ const names = await app . getNames ( ) ;
37
+ assert . deepStrictEqual ( names , [ name_list , [ ] ] ) ;
38
+ } ) ;
39
+
40
+ // test Application_getRandomPerson()
41
+ test ( 'Application_getRandomPerson()' , async ( ) => {
42
+ const app = new Application ( ) ;
43
+ const names = await app . getNames ( ) ;
44
+ const randomPerson = app . getRandomPerson ( ) ;
45
+ assert . ok ( names [ 0 ] . includes ( randomPerson ) ) ;
46
+ } ) ;
47
+
48
+ // test Application_selectNextPerson()
49
+ test ( 'Application_selectNextPerson()' , async ( ) => {
50
+ const app = new Application ( ) ;
51
+ const names = await app . getNames ( ) ;
52
+ app . selected = [ 'Alice' ] ;
53
+ let cnt = 0 ;
54
+ test . mock . method ( app , 'getRandomPerson' , ( ) => {
55
+ if ( cnt <= names [ 0 ] . length ) {
56
+ return names [ 0 ] [ cnt ++ ] ;
57
+ }
58
+ } ) ;
59
+ assert . strictEqual ( app . selectNextPerson ( ) , 'john' ) ;
60
+ assert . deepStrictEqual ( app . selected , [ 'Alice' , 'john' ] ) ;
61
+ assert . strictEqual ( app . selectNextPerson ( ) , 'Bob' ) ;
62
+ assert . deepStrictEqual ( app . selected , [ 'Alice' , 'john' , 'Bob' ] ) ;
63
+ assert . strictEqual ( app . selectNextPerson ( ) , null ) ;
64
+ } ) ;
65
+
66
+ // test Application_notifySelected()
67
+ test ( 'Application_notifySelected()' , async ( ) => {
68
+ const app = new Application ( ) ;
69
+ app . people = [ 'Alice' , 'john' , 'Bob' ] ;
70
+ app . selected = [ 'Alice' , 'john' , 'Bob' ] ;
71
+ app . mailSystem . send = test . mock . fn ( app . mailSystem . send ) ;
72
+ app . mailSystem . write = test . mock . fn ( app . mailSystem . write ) ;
73
+ app . notifySelected ( ) ;
74
+ assert . strictEqual ( app . mailSystem . send . mock . calls . length , 3 ) ;
75
+ assert . strictEqual ( app . mailSystem . write . mock . calls . length , 3 ) ;
76
+ } ) ;
0 commit comments