|
1 | 1 | const test = require('node:test');
|
2 | 2 | const assert = require('assert');
|
| 3 | +const fs = require('fs'); |
| 4 | +const os = require('os'); |
| 5 | +const path = require('path'); |
3 | 6 |
|
4 | 7 | const { Application, MailSystem } = require('./main');
|
5 | 8 |
|
@@ -31,12 +34,28 @@ test('should return true if mail is sent successfully', (t) => {
|
31 | 34 |
|
32 | 35 |
|
33 | 36 |
|
| 37 | + |
| 38 | + |
34 | 39 | test('should return name_list ', async()=>{
|
35 | 40 |
|
| 41 | + // Write mock name list to a temporary file |
| 42 | + const nameListContent = 'Alice\nBob\nCharlie'; |
| 43 | + const tempFilePath = path.join('name_list.txt'); |
| 44 | + fs.writeFileSync(tempFilePath, nameListContent); |
| 45 | + |
| 46 | + // Attach cleanup handler to the process exit event |
| 47 | + process.on('exit', () => { |
| 48 | + if (tempFilePath) { |
| 49 | + // Clean up: Delete the temporary directory |
| 50 | + fs.unlinkSync(tempFilePath); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + // Instantiate Application class and call getNames with the temporary file path |
36 | 55 | const app = new Application();
|
37 |
| - const [people, selected] = await app.getNames(); // Provide the path to the temporary file |
| 56 | + const [people, selected] = await app.getNames(tempFilePath); |
38 | 57 |
|
39 |
| - // Assert that the returned list matches the expected list |
| 58 | + // Assert the results |
40 | 59 | assert.deepStrictEqual(people, ['Alice', 'Bob', 'Charlie']);
|
41 | 60 | assert.deepStrictEqual(selected, []);
|
42 | 61 |
|
@@ -121,4 +140,7 @@ test('should call write and send methods of MailSystem for each selected person'
|
121 | 140 |
|
122 | 141 | assert.strictEqual(mailSystem.writeCallCount, 3);
|
123 | 142 | assert.strictEqual(mailSystem.sendCallCount, 3);
|
| 143 | + |
| 144 | + |
124 | 145 | });
|
| 146 | + |
0 commit comments