|
1 |
| -const test = require('node:test'); |
2 |
| -const assert = require('assert'); |
3 |
| -const { Application, MailSystem } = require('./main'); |
| 1 | +const { expect } = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const fs = require('fs'); |
| 4 | +const { MailSystem, Application } = require('./main_test'); |
4 | 5 |
|
5 |
| -// TODO: write your tests here |
6 |
| -// Remember to use Stub, Mock, and Spy when necessary |
7 |
| -test.mock.method(fs, 'readFile', (file, options, callback) => { |
8 |
| - callback(null, 'Guan\nChen\nGala'); |
9 |
| -}); |
| 6 | +describe('MailSystem', () => { |
| 7 | + it('should write the correct mail content', () => { |
| 8 | + const mailSystem = new MailSystem(); |
| 9 | + const name = 'Alice'; |
| 10 | + const content = mailSystem.write(name); |
| 11 | + expect(content).to.equal('Congrats, Alice!'); |
| 12 | + }); |
10 | 13 |
|
11 |
| -test("MailSystem write", () => { |
12 |
| - const mailSystem = new MailSystem(); |
13 |
| - assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!"); |
14 |
| -}); |
| 14 | + it('should send mail with random success or failure', () => { |
| 15 | + const mailSystem = new MailSystem(); |
| 16 | + const name = 'Alice'; |
| 17 | + const context = mailSystem.write(name); |
15 | 18 |
|
16 |
| -test("MailSystem send", () => { |
17 |
| - const mailSystem = new MailSystem(); |
18 |
| - Math.random = () => 0.6; |
19 |
| - const context = mailSystem.write("Guan"); |
20 |
| - assert.strictEqual(mailSystem.send("Guan", context), true); |
21 |
| - Math.random = () => 0.5; |
22 |
| - assert.strictEqual(mailSystem.send("Chen", context), false); |
23 |
| -}); |
| 19 | + sinon.stub(Math, 'random').returns(0.6); |
| 20 | + let result = mailSystem.send(name, context); |
| 21 | + expect(result).to.be.true; |
24 | 22 |
|
25 |
| -test("Application getNames", async () => { |
26 |
| - const application = new Application(); |
27 |
| - const [people, selected] = await application.getNames(); |
28 |
| - assert.deepStrictEqual(people, ["Guan", "Chen", "Gala"]); |
29 |
| - assert.deepStrictEqual(selected, []); |
30 |
| -}); |
| 23 | + Math.random.restore(); |
31 | 24 |
|
32 |
| -test("Application getRandomPerson", () => { |
33 |
| - const application = new Application(); |
34 |
| - application.people = ["Guan", "Chen", "Gala"]; |
35 |
| - Math.random = () => 0; |
36 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Guan"); |
37 |
| - Math.random = () => 0.6; |
38 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Chen"); |
39 |
| - Math.random = () => 0.9; |
40 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Gala"); |
41 |
| -}); |
| 25 | + sinon.stub(Math, 'random').returns(0.4); |
| 26 | + result = mailSystem.send(name, context); |
| 27 | + expect(result).to.be.false; |
42 | 28 |
|
43 |
| -test("Application selectNextPerson", async () => { |
44 |
| - const application = new Application(); |
45 |
| - await application.getNames(); |
46 |
| - application.selected = ["Guan"]; |
47 |
| - let count = 0; |
48 |
| - test.mock.method(application, 'getRandomPerson', () => { |
49 |
| - const name_list = application.people; |
50 |
| - if (count <= name_list.length) { |
51 |
| - return name_list[count++]; |
52 |
| - } |
| 29 | + Math.random.restore(); |
53 | 30 | });
|
54 |
| - assert.strictEqual(application.selectNextPerson(), "Chen"); |
55 |
| - assert.strictEqual(application.selectNextPerson(), "Gala"); |
56 |
| - assert.strictEqual(application.selectNextPerson(), null); |
57 | 31 | });
|
58 | 32 |
|
59 |
| -test("Application notifySelected", () => { |
60 |
| - const application = new Application(); |
61 |
| - application.people = ["Guan", "Chen", "Gala"]; |
62 |
| - application.selected = ["Guan", "Chen", "Gala"]; |
63 |
| - application.mailSystem.write = test.mock.fn(application.mailSystem.write); |
64 |
| - application.mailSystem.send = test.mock.fn(application.mailSystem.send); |
65 |
| - application.notifySelected(); |
66 |
| - assert.strictEqual(application.mailSystem.send.mock.calls.length, application.people.length); |
67 |
| - assert.strictEqual(application.mailSystem.write.mock.calls.length, application.people.length); |
| 33 | +describe('Application', () => { |
| 34 | + beforeEach(() => { |
| 35 | + sinon.stub(fs, 'readFile').resolves('Alice\nBob\nCharlie'); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + fs.readFile.restore(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should initialize with people and selected lists', async () => { |
| 43 | + const app = new Application(); |
| 44 | + await app.getNames(); |
| 45 | + expect(app.people).to.deep.equal(['Alice', 'Bob', 'Charlie']); |
| 46 | + expect(app.selected).to.deep.equal([]); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should select a random person', async () => { |
| 50 | + const app = new Application(); |
| 51 | + await app.getNames(); |
| 52 | + const person = app.selectNextPerson(); |
| 53 | + expect(app.people).to.include(person); |
| 54 | + expect(app.selected).to.include(person); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should notify all selected people', async () => { |
| 58 | + const app = new Application(); |
| 59 | + await app.getNames(); |
| 60 | + const mailSystemSpy = sinon.spy(app.mailSystem, 'send'); |
| 61 | + |
| 62 | + app.selectNextPerson(); |
| 63 | + app.selectNextPerson(); |
| 64 | + app.notifySelected(); |
| 65 | + |
| 66 | + expect(mailSystemSpy.calledTwice).to.be.true; |
| 67 | + }); |
68 | 68 | });
|
0 commit comments