Skip to content

Commit

Permalink
Merge pull request #3 from liquidnya/enhance-tests
Browse files Browse the repository at this point in the history
Enhance tests
  • Loading branch information
ToransuShoujo authored May 24, 2022
2 parents 2eb0aa9 + 3f220f5 commit 9d99b3c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 10 deletions.
36 changes: 26 additions & 10 deletions tests/chat-logs/chat-logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,20 @@ const advanceTime = async (ms, accuracy = 0) => {

const setTime = async (time, accuracy = 0) => {
let prevTime = new Date();
var newTime = new Date(`2022-04-21T${time}Z`);
var diff = newTime - prevTime;
if (diff < 0) {
let newTime = new Date();
let timeArray = time.split(':').map(x => parseInt(x, 10));
newTime.setUTCHours(timeArray[0]);
newTime.setUTCMinutes(timeArray[1]);
newTime.setUTCSeconds(timeArray[2]);
if (newTime < prevTime) {
// add one day in case of time going backwards
// TODO: do this better
newTime = new Date(`2022-04-22T${time}Z`);
diff = newTime - prevTime;
newTime.setUTCDate(newTime.getUTCDate() + 1);
}

const diff = newTime - prevTime;
if (diff > 0) {
await advanceTime(diff, accuracy);
} else if (diff < 0) {
fail(`Time went backwards, from ${mockTime} to ${newTime} (${time})`);
fail(`Time went backwards, from ${prevTime} to ${newTime} (${time})`);
}
}

Expand Down Expand Up @@ -113,7 +114,7 @@ beforeEach(() => {
});

// load index.js and test it being setup correctly
function requireIndex(mockFs = undefined, mockSettings = undefined) {
function requireIndex(mockFs = undefined, mockSettings = undefined, mockTime = undefined) {
let fs;
let settings;
let chatbot;
Expand All @@ -123,6 +124,16 @@ function requireIndex(mockFs = undefined, mockSettings = undefined) {
let handle_func;

jest.isolateModules(() => {
// remove timers
jest.clearAllTimers();

// setup time
jest.useFakeTimers();

if (mockTime !== undefined) {
jest.setSystemTime(mockTime);
}

// setup random mock
const chance = jestChance.getChance();
random = jest
Expand All @@ -137,6 +148,11 @@ function requireIndex(mockFs = undefined, mockSettings = undefined) {
mockFs.mkdirSync(path.resolve('.'), { recursive: true });
mockFs.writeFileSync(path.resolve('./waitingUsers.txt'), '[]');
mockFs.writeFileSync(path.resolve('./userWaitTime.txt'), '[]');
} else {
// copy files
const files = mockFs.toJSON();
mockFs = new Volume();
mockFs.fromJSON(files);
}
// setup virtual file system
jest.mock('fs', () => mockFs);
Expand Down Expand Up @@ -303,7 +319,7 @@ for (const file of testFiles) {
};
};
if (command == 'restart') {
test = requireIndex(test.fs, test.settings);
test = requireIndex(test.fs, test.settings, new Date());
test.chatbot_helper.say.mockImplementation(pushMessageWithStack);
} else if (command == 'accuracy') {
accuracy = parseInt(rest);
Expand Down
65 changes: 65 additions & 0 deletions tests/chat-logs/logs/weight-crash.test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# "username" will decide which of the messages are from the bot
settings {"username":"helperblock","password":"","channel":"liquidnya","max_size":50,"level_timeout":10,"level_selection":[],"message_cooldown":5}
# chatters
chatters {"_links":{},"chatter_count":4,"chatters":{"broadcaster":["liquidnya"],"vips":[],"moderators":["furretwalkbot","streamelements"],"staff":[],"admins":[],"global_mods":[],"viewers":["viewerlevels"]}}
# chatty chat log
# ~ broadcaster
# @ moderator
# % subscriber

# set test timer accuracy to 1 minute to accuratly test the weight
accuracy 60000

[02:15:35] ~%?liquidnya: !clear
[02:15:35] @^helperblock: The queue has been cleared!

[16:26:45] ~%?liquidnya: !open
[16:26:45] @^helperblock: The queue is now open!

[16:26:45] ~%?liquidnya: !add NB0-1MD-SLG
[16:26:45] @^helperblock: liquidnya, NB0-1MD-SLG has been added to the queue.

[16:26:45] @^FurretWalkBot: !add NB0-1MD-SLG
[16:26:46] @^helperblock: FurretWalkBot, NB0-1MD-SLG has been added to the queue.

queso.save [{"code":"NB0-1MD-SLG","submitter":"liquidnya","username":"liquidnya"},{"code":"NB0-1MD-SLG","submitter":"FurretWalkBot","username":"furretwalkbot"}]

[16:28:48] @^FurretWalkBot: !odds

# TODO: is this a bug?

restart

[16:28:48] @^helperblock: FurretWalkBot, you have a 50.0% chance of getting chosen in weighted random.

restart

[16:31:48] ~%?liquidnya: !odds
[16:31:48] @^helperblock: liquidnya, you have a 50.0% chance of getting chosen in weighted random.

random 0.999

[16:31:48] ~%?liquidnya: !random
[16:31:48] @^helperblock: Now playing NB0-1MD-SLG submitted by FurretWalkBot.

restart

[16:31:48] ~%?liquidnya: !punt
[16:31:48] @^helperblock: Ok, adding the current level back into the queue.

# TODO: BUG should have 0.0%
# -> this is because the queue restarted before the 1min timer was reached
# so the wait times were changed in memory, but have not been persisted yet

[16:31:48] @^FurretWalkBot: !odds
[16:31:48] @^helperblock: FurretWalkBot, you have a 50.0% chance of getting chosen in weighted random.

[16:31:48] ~%?liquidnya: !odds
[16:31:48] @^helperblock: liquidnya, you have a 50.0% chance of getting chosen in weighted random.

restart

[16:31:48] ~%?liquidnya: !odds
[16:31:48] @^helperblock: liquidnya, you have a 50.0% chance of getting chosen in weighted random.

restart
46 changes: 46 additions & 0 deletions tests/chat-logs/logs/weight-remove.test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# "username" will decide which of the messages are from the bot
settings {"username":"helperblock","password":"","channel":"liquidnya","max_size":50,"level_timeout":10,"level_selection":[],"message_cooldown":5}
# chatters
chatters {"_links":{},"chatter_count":4,"chatters":{"broadcaster":["liquidnya"],"vips":[],"moderators":["furretwalkbot","streamelements"],"staff":[],"admins":[],"global_mods":[],"viewers":["viewerlevels"]}}
# chatty chat log
# ~ broadcaster
# @ moderator
# % subscriber

# set test timer accuracy to 1 minute to accuratly test the weight
accuracy 60000

[02:15:35] ~%?liquidnya: !clear
[02:15:35] @^helperblock: The queue has been cleared!

[16:26:45] ~%?liquidnya: !open
[16:26:45] @^helperblock: The queue is now open!

[16:26:45] ~%?liquidnya: !add NB0-1MD-SLG
[16:26:45] @^helperblock: liquidnya, NB0-1MD-SLG has been added to the queue.

# one day passes
[16:24:45] ~%?liquidnya: uwu

[16:26:45] @^FurretWalkBot: !add NB0-1MD-SLG
[16:26:46] @^helperblock: FurretWalkBot, NB0-1MD-SLG has been added to the queue.

queso.save [{"code":"NB0-1MD-SLG","submitter":"liquidnya","username":"liquidnya"},{"code":"NB0-1MD-SLG","submitter":"FurretWalkBot","username":"furretwalkbot"}]

[16:26:47] @^FurretWalkBot: !odds

# TODO: is this a bug?

[16:26:48] @^helperblock: FurretWalkBot, you have a 0.0% chance of getting chosen in weighted random.

random 0.999

[16:26:48] ~%?liquidnya: !random
[16:26:48] @^helperblock: Now playing NB0-1MD-SLG submitted by FurretWalkBot.

[16:26:48] @^FurretWalkBot: !odds
[16:26:48] @^helperblock: Your level is being played right now!

[16:26:48] ~%?liquidnya: !odds

[16:26:48] @^helperblock: liquidnya, you have a 100.0% chance of getting chosen in weighted random.

0 comments on commit 9d99b3c

Please sign in to comment.