Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LAB2] 312551004 #120

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions lab2/main_test.js
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);
});
Loading