Skip to content

Commit cd95aa0

Browse files
authored
Update main_test.js
1 parent 8aff5cd commit cd95aa0

File tree

1 file changed

+79
-24
lines changed

1 file changed

+79
-24
lines changed

lab2/main_test.js

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,79 @@
1-
const test = require('node:test');
2-
const sinon = require('sinon');
3-
const assert = require('assert');
4-
const { Application, MailSystem } = require('./main');
5-
6-
test('Application selects and notifies a person', () => {
7-
// Create a stub for MailSystem class
8-
const mailSystemStub = {
9-
write: sinon.stub().returns('Mocked context'),
10-
send: sinon.stub().returns(true),
11-
};
12-
13-
const app = new Application();
14-
app.mailSystem = mailSystemStub;
15-
16-
const selectedPerson = app.selectNextPerson();
17-
assert(selectedPerson !== null); // Assuming a person is selected
18-
19-
app.notifySelected();
20-
21-
// Assert that write and send methods of mailSystemStub were called
22-
assert(mailSystemStub.write.calledOnceWith(selectedPerson)); // Check if write method was called with selected person
23-
assert(mailSystemStub.send.calledOnceWith(selectedPerson, 'Mocked context')); // Check if send method was called with correct arguments
24-
});
1+
const fs = require('fs');
2+
const util = require('util');
3+
const readFile = util.promisify(fs.readFile);
4+
5+
class MailSystem {
6+
write(name) {
7+
// 修改 MailSystem 的 write 方法,使其返回包含名稱的特定內容
8+
console.log('--write mail for ' + name + '--');
9+
const context = 'Congrats, ' + name + '!';
10+
return context;
11+
}
12+
13+
send(name, context) {
14+
// 修改 MailSystem 的 send 方法,模擬發送郵件並隨機返回成功或失敗
15+
console.log('--send mail to ' + name + '--');
16+
const success = Math.random() > 0.5; // 模擬郵件發送的隨機成功或失敗
17+
if (success) {
18+
console.log('mail sent');
19+
} else {
20+
console.log('mail failed');
21+
}
22+
return success; // 返回發送成功或失敗的結果
23+
}
24+
}
25+
26+
class Application {
27+
constructor() {
28+
this.people = [];
29+
this.selected = [];
30+
this.mailSystem = new MailSystem();
31+
this.getNames().then(([people, selected]) => {
32+
this.people = people;
33+
this.selected = selected;
34+
});
35+
}
36+
37+
async getNames() {
38+
// 修改 getNames 方法,使其從文件中讀取名稱列表
39+
const data = await readFile('name_list.txt', 'utf8');
40+
const people = data.split('\n');
41+
const selected = [];
42+
return [people, selected];
43+
}
44+
45+
getRandomPerson() {
46+
// 修改 getRandomPerson 方法,隨機返回一個名稱
47+
const i = Math.floor(Math.random() * this.people.length);
48+
return this.people[i];
49+
}
50+
51+
selectNextPerson() {
52+
// 修改 selectNextPerson 方法,隨機選擇並返回一個未被選擇過的名稱
53+
console.log('--select next person--');
54+
if (this.people.length === this.selected.length) {
55+
console.log('all selected');
56+
return null;
57+
}
58+
let person = this.getRandomPerson();
59+
while (this.selected.includes(person)) {
60+
person = this.getRandomPerson();
61+
}
62+
this.selected.push(person);
63+
return person;
64+
}
65+
66+
notifySelected() {
67+
// 修改 notifySelected 方法,通知所有已選擇的人
68+
console.log('--notify selected--');
69+
for (const x of this.selected) {
70+
const context = this.mailSystem.write(x);
71+
this.mailSystem.send(x, context);
72+
}
73+
}
74+
}
75+
76+
module.exports = {
77+
Application,
78+
MailSystem,
79+
};

0 commit comments

Comments
 (0)