Skip to content

Commit

Permalink
Allow for advanced regex usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjpaz committed Jan 21, 2021
1 parent 55f7fb9 commit 29c04b0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class TwitchShufflerListener {
if(!isCoolingDown) {
this.lastCommandTimestamps['$$global$$'] = new Date().getTime();
this.lastCommandTimestamps[user] = new Date().getTime();

if(!message || message === '') {
const matches = command.match(chatCommandRegExp);


if(matches[1]) {
message = matches[1];
}
}

this.swap(message, `${user} via command`);
}
}
Expand Down
100 changes: 100 additions & 0 deletions src/twitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,84 @@ describe('twitch', () => {
listener = new TwitchShufflerListener();
});

describe('advanced regex', () => {
let listener;

beforeEach(() => {

listener = new TwitchShufflerListener({
chatCommand: 'swappy([0-9]*)'
});
});

it('should allow advanced regex usage', async () => {
listener.swap = sinon.spy();

listener.isCoolingDown = sinon.spy(() => false);

await listener.onCommand(
"fake_user",
"swappy",
"123",
{},
{
sinceLastCommand: {
any: 0
}
}
);

expect(listener.isCoolingDown.called).to.eql(true);
expect(listener.swap.called).to.eql(true);
expect(listener.swap.calledWith("123")).to.eql(true);
});

it('should allow the first match to be the argument for swap', async () => {
listener.swap = sinon.spy();

listener.isCoolingDown = sinon.spy(() => false);

await listener.onCommand(
"fake_user",
"swappy123",
"",
{},
{
sinceLastCommand: {
any: 0
}
}
);

expect(listener.isCoolingDown.called).to.eql(true);
expect(listener.swap.called).to.eql(true);
expect(listener.swap.calledWith("123")).to.eql(true);
});

it('should allow the first match to be the argument for swap', async () => {
listener.swap = sinon.spy();

listener.isCoolingDown = sinon.spy(() => false);

await listener.onCommand(
"fake_user",
"swappy",
null,
{},
{
sinceLastCommand: {
any: 0
}
}
);

expect(listener.isCoolingDown.called).to.eql(true);
expect(listener.swap.called).to.eql(true);
expect(listener.swap.calledWith(null)).to.eql(true);
});

});

describe('list', () => {
it('should list games on !list', () => {
listener.list = sinon.spy();
Expand Down Expand Up @@ -60,6 +138,28 @@ describe('twitch', () => {
expect(listener.swap.calledWith("fake_message")).to.eql(true);
});

it('should NOT swap on invalid', () => {
listener.swap = sinon.spy();

listener.isCoolingDown = sinon.spy(() => false);

listener.onCommand(
"fake_user",
"invalid", // TODO
"fake_message",
{},
{
sinceLastCommand: {
any: 0
}
}
);

expect(listener.isCoolingDown.called).to.eql(false);
expect(listener.swap.called).to.eql(false);
expect(listener.swap.calledWith("fake_message")).to.eql(false);
});

it('should swap on swap redemption', () => {
listener.swap = sinon.spy();

Expand Down

0 comments on commit 29c04b0

Please sign in to comment.