|
1 |
| -const { expect } = require('chai'); |
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('assert'); |
| 3 | +const fs = require('fs'); |
| 4 | +const { Application, MailSystem } = require('./main'); |
2 | 5 | const sinon = require('sinon');
|
3 |
| -const fs = require('fs').promises; |
4 |
| -const { MailSystem, Application } = require('./main_test'); |
5 | 6 |
|
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 |
| - }); |
| 7 | +// Mock fs.readFile |
| 8 | +sinon.stub(fs, 'readFile').callsFake((file, options, callback) => { |
| 9 | + callback(null, 'Guan\nChen\nGala\n'); |
| 10 | +}); |
13 | 11 |
|
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); |
| 12 | +test("MailSystem write", () => { |
| 13 | + const mailSystem = new MailSystem(); |
| 14 | + assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!"); |
| 15 | +}); |
18 | 16 |
|
19 |
| - sinon.stub(Math, 'random').returns(0.6); // 假設成功 |
20 |
| - let result = mailSystem.send(name, context); |
21 |
| - expect(result).to.be.true; |
| 17 | +test("MailSystem send", () => { |
| 18 | + const mailSystem = new MailSystem(); |
| 19 | + const originalMathRandom = Math.random; |
22 | 20 |
|
23 |
| - Math.random.restore(); |
| 21 | + Math.random = () => 0.6; |
| 22 | + const context = mailSystem.write("Guan"); |
| 23 | + assert.strictEqual(mailSystem.send("Guan", context), true); |
24 | 24 |
|
25 |
| - sinon.stub(Math, 'random').returns(0.4); // 假設失敗 |
26 |
| - result = mailSystem.send(name, context); |
27 |
| - expect(result).to.be.false; |
| 25 | + Math.random = () => 0.4; |
| 26 | + assert.strictEqual(mailSystem.send("Chen", context), false); |
28 | 27 |
|
29 |
| - Math.random.restore(); |
30 |
| - }); |
| 28 | + Math.random = originalMathRandom; |
31 | 29 | });
|
32 | 30 |
|
33 |
| -describe('Application', () => { |
34 |
| - beforeEach(async () => { |
35 |
| - sinon.stub(fs, 'readFile').resolves('Alice\nBob\nCharlie'); |
36 |
| - this.app = new Application(); |
37 |
| - await this.app.getNames(); |
38 |
| - }); |
39 |
| - |
40 |
| - afterEach(() => { |
41 |
| - fs.readFile.restore(); |
42 |
| - }); |
| 31 | +test("Application getNames", async () => { |
| 32 | + const application = new Application(); |
| 33 | + const [people, selected] = await application.getNames(); |
| 34 | + // Remove any empty strings from the array |
| 35 | + assert.deepStrictEqual(people.filter(name => name), ["Guan", "Chen", "Gala"]); |
| 36 | + assert.deepStrictEqual(selected, []); |
| 37 | +}); |
43 | 38 |
|
44 |
| - it('should initialize with people and selected lists', async () => { |
45 |
| - expect(this.app.people).to.deep.equal(['Alice', 'Bob', 'Charlie']); |
46 |
| - expect(this.app.selected).to.deep.equal([]); |
47 |
| - }); |
| 39 | +test("Application getRandomPerson", () => { |
| 40 | + const application = new Application(); |
| 41 | + application.people = ["Guan", "Chen", "Gala"]; |
| 42 | + Math.random = () => 0; |
| 43 | + assert.deepStrictEqual(application.getRandomPerson(), "Guan"); |
| 44 | + Math.random = () => 0.6; |
| 45 | + assert.deepStrictEqual(application.getRandomPerson(), "Chen"); |
| 46 | + Math.random = () => 0.9; |
| 47 | + assert.deepStrictEqual(application.getRandomPerson(), "Gala"); |
| 48 | +}); |
48 | 49 |
|
49 |
| - it('should select a random person', () => { |
50 |
| - const person = this.app.selectNextPerson(); |
51 |
| - expect(this.app.people).to.include(person); |
52 |
| - expect(this.app.selected).to.include(person); |
| 50 | +test("Application selectNextPerson", async () => { |
| 51 | + const application = new Application(); |
| 52 | + await application.getNames(); |
| 53 | + application.selected = ["Guan"]; |
| 54 | + let count = 1; |
| 55 | + const getRandomPersonStub = sinon.stub(application, 'getRandomPerson').callsFake(() => { |
| 56 | + const name_list = application.people; |
| 57 | + if (count < name_list.length) { |
| 58 | + return name_list[count++]; |
| 59 | + } else { |
| 60 | + return null; |
| 61 | + } |
53 | 62 | });
|
54 | 63 |
|
55 |
| - it('should notify all selected people', () => { |
56 |
| - const mailSystemSpy = sinon.spy(this.app.mailSystem, 'send'); |
| 64 | + assert.strictEqual(application.selectNextPerson(), "Chen"); |
| 65 | + assert.strictEqual(application.selectNextPerson(), "Gala"); |
| 66 | + assert.strictEqual(application.selectNextPerson(), null); |
57 | 67 |
|
58 |
| - this.app.selectNextPerson(); |
59 |
| - this.app.selectNextPerson(); |
60 |
| - this.app.notifySelected(); |
| 68 | + getRandomPersonStub.restore(); |
| 69 | +}); |
61 | 70 |
|
62 |
| - expect(mailSystemSpy.calledTwice).to.be.true; |
63 |
| - }); |
| 71 | +test("Application notifySelected", () => { |
| 72 | + const application = new Application(); |
| 73 | + application.people = ["Guan", "Chen", "Gala"]; |
| 74 | + application.selected = ["Guan", "Chen", "Gala"]; |
| 75 | + sinon.spy(application.mailSystem, 'write'); |
| 76 | + sinon.spy(application.mailSystem, 'send'); |
| 77 | + application.notifySelected(); |
| 78 | + assert.strictEqual(application.mailSystem.write.callCount, application.people.length); |
| 79 | + assert.strictEqual(application.mailSystem.send.callCount, application.people.length); |
64 | 80 | });
|
0 commit comments