-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.js
44 lines (37 loc) · 1.13 KB
/
test_helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const fs = require('fs');
const yaml = require('js-yaml');
exports.writeMessage = function(body, reacts, senderID, is_fb) {
let messages = yaml.load(fs.readFileSync('db/messages.yaml', 'utf8'));
// Assign message attributes to dictionary
var newMsg = {id: messages.numMessages,
body: body,
reacts: reacts,
senderID: senderID,
is_fb: is_fb};
// Increment total number of messages stored
messages.numMessages++;
// Add new messsage
messages.messages.push(newMsg);
// Write to file
fs.writeFileSync('db/messages.yaml', yaml.dump(messages), (err) => {
if (err) {
console.log(err);
}
});
return;
}
exports.clearMessages = function() {
// Read messages file into js object
let messages = yaml.load(fs.readFileSync('db/messages.yaml', 'utf8'));
console.log(messages);
// Reset message db to empty
messages.messages = [];
messages.numMessages = 0;
// Write to file
fs.writeFileSync('db/messages.yaml', yaml.dump(messages), (err) => {
if (err) {
console.log(err);
}
});
console.log(messages);
}