From a1035ea752222ed5aae37c4cd509826c338059e5 Mon Sep 17 00:00:00 2001 From: Alice Jones <21381891+Pactionly@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:46:27 -0700 Subject: [PATCH] fix: group mentions without handle (#583) * test: update group tests to check regex; add test for group short syntax * fix: update group regex to capture without optional handle --- service/recognition.js | 12 ++----- test/service/recognition.js | 72 ++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/service/recognition.js b/service/recognition.js index 0fd77080..74b59574 100644 --- a/service/recognition.js +++ b/service/recognition.js @@ -15,7 +15,7 @@ const { } = config; const userRegex = /<@([a-zA-Z0-9]+)>/g; -const groupRegex = //g; +const groupRegex = //g; const tagRegex = /#(\S+)/g; const generalEmojiRegex = /:([a-z-_']+):/g; const gratitudeEmojiRegex = new RegExp(config.recognizeEmoji, "g"); @@ -171,14 +171,8 @@ async function gratitudeReceiverIdsIn(client, text) { let users = (text.match(userRegex) || []).map((userMention) => userMention.slice(2, -1), ); - let groups = (text.match(groupRegex) || []).map((groupMention) => - groupMention.substring( - groupMention.indexOf("^") + 1, - groupMention.lastIndexOf("|"), - ), - ); - for (let i = 0; i < groups.length; i++) { - users = users.concat(await groupUsers(client, groups[i])); + for (const groupMatch of text.matchAll(groupRegex)) { + users = users.concat(await groupUsers(client, groupMatch[1])); } return users; } diff --git a/test/service/recognition.js b/test/service/recognition.js index 6e4ca80b..79616280 100644 --- a/test/service/recognition.js +++ b/test/service/recognition.js @@ -387,13 +387,36 @@ describe("service/recognition", () => { it("should return the users within the group when a group is mentioned", async () => { const group = ""; const text = ":fistbump: " + group + " Test Message"; + const listStub = sinon.stub(); + listStub.resolves({}); + listStub.withArgs({ usergroup: "S1234567890" }).resolves({ + ok: true, + users: ["TestUserOne", "TestUserTwo"], + }); const client = { usergroups: { users: { - list: sinon.stub().resolves({ - ok: true, - users: ["TestUserOne", "TestUserTwo"], - }), + list: listStub, + }, + }, + }; + const results = await recognition.gratitudeReceiverIdsIn(client, text); + expect(results).to.deep.equal(["TestUserOne", "TestUserTwo"]); + }); + + it("should return the users within the group when a the short group syntax is used", async () => { + const group = ""; + const text = ":fistbump: " + group + " Test Message"; + const listStub = sinon.stub(); + listStub.resolves({}); + listStub.withArgs({ usergroup: "S1234567890" }).resolves({ + ok: true, + users: ["TestUserOne", "TestUserTwo"], + }); + const client = { + usergroups: { + users: { + list: listStub, }, }, }; @@ -404,13 +427,16 @@ describe("service/recognition", () => { it("should return the users within the group when a group is mentioned and other users", async () => { const group = ""; const text = ":fistbump: " + group + " <@TestUserThree> Test Message"; + const listStub = sinon.stub(); + listStub.resolves({}); + listStub.withArgs({ usergroup: "S1234567890" }).resolves({ + ok: true, + users: ["TestUserOne", "TestUserTwo"], + }); const client = { usergroups: { users: { - list: sinon.stub().resolves({ - ok: true, - users: ["TestUserOne", "TestUserTwo"], - }), + list: listStub, }, }, }; @@ -421,6 +447,36 @@ describe("service/recognition", () => { "TestUserTwo", ]); }); + + it("should return the users within all groups mentioned", async () => { + const group1 = ""; + const group2 = ""; + const text = ":fistbump: " + group1 + " " + group2 + " Test Message"; + const listStub = sinon.stub(); + listStub.resolves({}); + listStub.withArgs({ usergroup: "S111111111" }).resolves({ + ok: true, + users: ["TestUserOne", "TestUserTwo"], + }); + listStub.withArgs({ usergroup: "S222222222" }).resolves({ + ok: true, + users: ["TestUserThree", "TestUserFour"], + }); + const client = { + usergroups: { + users: { + list: listStub, + }, + }, + }; + const results = await recognition.gratitudeReceiverIdsIn(client, text); + expect(results).to.deep.equal([ + "TestUserOne", + "TestUserTwo", + "TestUserThree", + "TestUserFour", + ]); + }); }); describe("gratitudeCountIn", () => {