-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lab2
- Loading branch information
Showing
1 changed file
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,100 @@ | ||
const test = require('node:test'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const { Application, MailSystem } = require('./main'); | ||
|
||
// TODO: write your tests here | ||
// Remember to use Stub, Mock, and Spy when necessary | ||
const writeFile = promisify(fs.writeFile); | ||
|
||
//test.mock.method(fs, 'readFile', async (file, encoding) => { | ||
|
||
test("Test MailSystem's write", () => { | ||
const mailSystem = new MailSystem(); | ||
const context = mailSystem.write('a'); | ||
assert.strictEqual(context, 'Congrats, a!'); | ||
}); | ||
|
||
test("Test MailSystem's send", () => { | ||
const mailSystem = new MailSystem(); | ||
const originalMathRandom = Math.random; | ||
Math.random = () => 0.51; | ||
const result = mailSystem.send('SUNGOD', 'Test context'); | ||
assert.strictEqual(result, true); | ||
Math.random = () => 0.49; | ||
const result2 = mailSystem.send('SUNGOD', 'Test context'); | ||
assert.strictEqual(result2, false); | ||
Math.random = originalMathRandom; | ||
}); | ||
|
||
test("Test Application's getRandom&selectNextPerson", async () => { | ||
const fakeData = 'John\nJane'; | ||
const fakeFile = 'name_list.txt'; | ||
await writeFile(fakeFile, fakeData, 'utf8'); | ||
|
||
const application = new Application(); | ||
const [people, selected] = await application.getNames(); | ||
|
||
assert.deepStrictEqual(people, ['John', 'Jane']); | ||
assert.deepStrictEqual(selected, []); | ||
|
||
const rd_person = application.getRandomPerson(); | ||
assert.ok(people.includes(rd_person)); | ||
|
||
application.getRandomPerson = () => 'John'; | ||
let person = application.selectNextPerson(); | ||
assert.deepStrictEqual(application.selected, ['John']); | ||
|
||
// Select the next person | ||
application.getRandomPerson = () => 'Jane'; | ||
person = application.selectNextPerson(); | ||
assert.deepStrictEqual(application.selected, ['John', 'Jane']); | ||
|
||
// All people are selected | ||
person = application.selectNextPerson(); | ||
assert.strictEqual(person, null); | ||
|
||
let cnt = 0; | ||
application.getRandomPerson = () => { | ||
if (cnt++ % 2 === 0) { | ||
return 'John'; | ||
} else { | ||
return 'Jane'; | ||
} | ||
} | ||
application.selected = ['John']; | ||
person = application.selectNextPerson(); | ||
assert.strictEqual(person, 'Jane'); | ||
|
||
fs.unlinkSync(fakeFile); | ||
}); | ||
|
||
test("Test Application's notifySelected", async () => { | ||
const fakeData = 'John\nJane\nDoe\nSmith'; | ||
const fakeFile = 'name_list.txt'; | ||
await writeFile(fakeFile, fakeData, 'utf8'); | ||
|
||
const application = new Application(); | ||
const [people, selected] = await application.getNames(); | ||
|
||
assert.deepStrictEqual(people, ['John', 'Jane', 'Doe', 'Smith']); | ||
assert.deepStrictEqual(selected, []); | ||
|
||
// Create a spy for the MailSystem's write and send methods | ||
const mailSystemSpy = { | ||
write: test.mock.fn(MailSystem.prototype.write), | ||
send: test.mock.fn(MailSystem.prototype.send) | ||
}; | ||
application.mailSystem = mailSystemSpy; | ||
application.selected = ['John', 'Jane', 'Doe']; | ||
|
||
application.notifySelected(); | ||
|
||
// Ensure that write and send methods are called for each selected person | ||
assert.strictEqual(mailSystemSpy.write.mock.callCount(), 3); | ||
assert.strictEqual(mailSystemSpy.send.mock.callCount(), 3); | ||
|
||
assert.deepStrictEqual(mailSystemSpy.write.mock.calls.map(call => call.arguments[0]), ['John', 'Jane', 'Doe']); | ||
assert.deepStrictEqual(mailSystemSpy.send.mock.calls.map(call => call.arguments[0]), ['John', 'Jane', 'Doe']); | ||
|
||
fs.unlinkSync(fakeFile); | ||
}); |