|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('assert'); |
1 | 3 | const fs = require('fs');
|
2 |
| -const util = require('util'); |
3 |
| -const readFile = util.promisify(fs.readFile); |
4 | 4 |
|
5 |
| -class MailSystem { |
6 |
| - write(name) { |
7 |
| - console.log('--write mail for ' + name + '--'); |
8 |
| - const context = 'Congrats, ' + name + '!'; |
9 |
| - return context; |
10 |
| - } |
| 5 | +// Mock the file containing names |
| 6 | +test.mock.method(fs, 'readFile', (file, options, callback) => { |
| 7 | + callback(null, 'Aqur\nBobi\ncat'); |
| 8 | +}); |
11 | 9 |
|
12 |
| - send(name, context) { |
13 |
| - console.log('--send mail to ' + name + '--'); |
14 |
| - const success = Math.random() > 0.5; |
15 |
| - if (success) { |
16 |
| - console.log('mail sent'); |
17 |
| - } else { |
18 |
| - console.log('mail failed'); |
19 |
| - } |
20 |
| - return success; |
21 |
| - } |
22 |
| -} |
| 10 | +const { Application, MailSystem } = require('./main'); |
23 | 11 |
|
24 |
| -class Application { |
25 |
| - constructor() { |
26 |
| - this.people = []; |
27 |
| - this.selected = []; |
28 |
| - this.mailSystem = new MailSystem(); |
29 |
| - this.getNames().then(([people, selected]) => { |
30 |
| - this.people = people; |
31 |
| - this.selected = selected; |
32 |
| - }); |
33 |
| - } |
| 12 | +test('MailSystem_write()', () => { |
| 13 | + const mailSystem = new MailSystem(); |
| 14 | + assert.strictEqual(mailSystem.write('Aqur'), 'Congrats, Aqur!'); |
| 15 | + assert.strictEqual(mailSystem.write(202), 'Congrats, 202!'); |
| 16 | + assert.strictEqual(mailSystem.write(null), 'Congrats, null!'); |
| 17 | +}); |
34 | 18 |
|
35 |
| - async getNames() { |
36 |
| - const data = await readFile('name_list.txt', 'utf8'); |
37 |
| - const people = data.split('\n'); |
38 |
| - const selected = []; |
39 |
| - return [people, selected]; |
40 |
| - } |
| 19 | +test('MailSystem_send()', () => { |
| 20 | + const mailSystem = new MailSystem(); |
| 21 | + const name = 'Aqur'; |
| 22 | + test.mock.method(Math, 'random', () => 0.9); |
| 23 | + assert.strictEqual(mailSystem.send(name, 'success'), true); |
| 24 | + test.mock.method(Math, 'random', () => 0.2); |
| 25 | + assert.strictEqual(mailSystem.send(name, 'fail'), false); |
| 26 | +}); |
41 | 27 |
|
42 |
| - getRandomPerson() { |
43 |
| - const i = Math.floor(Math.random() * this.people.length); |
44 |
| - return this.people[i]; |
45 |
| - } |
| 28 | +test('Application_getNames()', async () => { |
| 29 | + const app = new Application(); |
| 30 | + const nameList = ['Aqur', 'Bobi', 'cat']; |
| 31 | + const [names, selected] = await app.getNames(); |
| 32 | + assert.deepStrictEqual(names, nameList); |
| 33 | + assert.deepStrictEqual(selected, []); |
| 34 | +}); |
46 | 35 |
|
47 |
| - selectNextPerson() { |
48 |
| - console.log('--select next person--'); |
49 |
| - if (this.people.length === this.selected.length) { |
50 |
| - console.log('all selected'); |
51 |
| - return null; |
52 |
| - } |
53 |
| - let person = this.getRandomPerson(); |
54 |
| - while (this.selected.includes(person)) { |
55 |
| - person = this.getRandomPerson(); |
56 |
| - } |
57 |
| - this.selected.push(person); |
58 |
| - return person; |
59 |
| - } |
| 36 | +test('Application_getRandomPerson()', async () => { |
| 37 | + const app = new Application(); |
| 38 | + const [names] = await app.getNames(); |
| 39 | + const randomPerson = app.getRandomPerson(); |
| 40 | +}); |
60 | 41 |
|
61 |
| - notifySelected() { |
62 |
| - console.log('--notify selected--'); |
63 |
| - for (const x of this.selected) { |
64 |
| - const context = this.mailSystem.write(x); |
65 |
| - this.mailSystem.send(x, context); |
66 |
| - } |
67 |
| - } |
68 |
| -} |
| 42 | +test('Application_selectNextPerson()', async () => { |
| 43 | + const app = new Application(); |
| 44 | + const [names] = await app.getNames(); |
| 45 | + app.selected = ['Aqur']; |
| 46 | + let count = 0; |
| 47 | + test.mock.method(app, 'getRandomPerson', () => names[count++]); |
| 48 | + assert.strictEqual(app.selectNextPerson(), 'Bobi'); |
| 49 | + assert.deepStrictEqual(app.selected, ['Aqur', 'Bobi']); |
| 50 | + assert.strictEqual(app.selectNextPerson(), 'cat'); |
| 51 | + assert.deepStrictEqual(app.selected, ['Aqur', 'Bobi', 'cat']); |
| 52 | + assert.strictEqual(app.selectNextPerson(), null); |
| 53 | +}); |
69 | 54 |
|
70 |
| -module.exports = { |
71 |
| - Application, |
72 |
| - MailSystem, |
73 |
| -}; |
| 55 | + |
| 56 | +test('Application_notifySelected()', async () => { |
| 57 | + const app = new Application(); |
| 58 | + const [names] = await app.getNames(); |
| 59 | + app.selected = names.slice(); // Select all names initially |
| 60 | + app.mailSystem.send = test.mock.fn(app.mailSystem.send); |
| 61 | + app.mailSystem.write = test.mock.fn(app.mailSystem.write); |
| 62 | + app.notifySelected(); |
| 63 | + assert.strictEqual(app.mailSystem.send.mock.calls.length, names.length); |
| 64 | + assert.strictEqual(app.mailSystem.write.mock.calls.length, names.length); |
| 65 | +}); |
0 commit comments