From 8df3f90d943c18af1441ff062ecc709e2b1a0dac Mon Sep 17 00:00:00 2001 From: Annheij Date: Sat, 7 Jun 2025 03:30:13 +0800 Subject: [PATCH] Resolving git branch problems for lab --- lab2/main.js | 19 ++++++---------- lab2/main_test.js | 56 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/lab2/main.js b/lab2/main.js index 2e159e7..c897d99 100644 --- a/lab2/main.js +++ b/lab2/main.js @@ -25,20 +25,15 @@ class MailSystem { class Application { constructor() { - this.people = []; - this.selected = []; - this.mailSystem = new MailSystem(); - this.getNames().then(([people, selected]) => { - this.people = people; - this.selected = selected; - }); + this.people = []; + this.selected = []; + this.mailSystem = new MailSystem(); } - + async getNames() { - const data = await readFile('name_list.txt', 'utf8'); - const people = data.split('\n'); - const selected = []; - return [people, selected]; + const data = await readFile('name_list.txt', 'utf8'); + this.people = data.split('\n'); + this.selected = []; } getRandomPerson() { diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468..a865d42 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,58 @@ const test = require('node:test'); const assert = require('assert'); const { Application, MailSystem } = require('./main'); +const sinon = require('sinon'); -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file +test('test getRandomPerson in main.js', async () => { + const app = new Application(); + + app.getNames = async () => { + app.people = ['Annika', 'Billy', 'Cecilia']; + }; + + await app.getNames(); + + const person = app.getRandomPerson(); + assert.ok(app.people.includes(person), 'the person is not in list'); +}); + +test('test mains selectNextPerson to not select same person twice', async () => { + const app = new Application(); + + app.getNames = async () => { + app.people = ['Annika', 'Billy', 'Cecilia']; + app.selected = []; + }; + + await app.getNames(); + + const person1 = app.selectNextPerson(); + const person2 = app.selectNextPerson(); + + assert.notStrictEqual(person1, person2, 'Same person was selected twice'); +}); + +test('test mains notifySelected that it calls send()', async () => { + const mailSystem = new MailSystem(); + const sendSpy = sinon.spy(mailSystem, 'send'); + + const app = new Application(); + app.mailSystem = mailSystem; + + app.getNames = async () => { + app.people = ['Annika', 'Billy', 'Cecilia']; + app.selected = []; + }; + + await app.getNames(); + + const person = app.selectNextPerson(); + app.selected = [person]; + + await app.notifySelected(); + + assert.ok( + sendSpy.calledWith(person, `Congrats, ${person}!`), + `send() didn't work with args. Captured calls: ${JSON.stringify(sendSpy.args)}` + ); +});