Skip to content

Commit

Permalink
Merge pull request #51 from r-argentina-programa/fix/wrong-class-subm…
Browse files Browse the repository at this point in the history
…issions

Fix: Incorrect channel submissions
  • Loading branch information
Leonelmarianog authored Jul 14, 2023
2 parents 36a842c + f4e49d6 commit ddeec9d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 50 deletions.
93 changes: 65 additions & 28 deletions src/events/reaction/__test__/handleRobotFaceReaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,17 @@ describe('handleRobotFaceReaction', () => {
});
});

it('should make Robotina respond with a message if the submission format is code but the channel is incorrect', async () => {
clientMock.conversations.history.mockResolvedValueOnce(
// @ts-ignore
conversationsHistoryResponse
);
it('should make Robotina respond with a message when there is multiple blocks of code', async () => {
clientMock.conversations.history.mockResolvedValueOnce({
...conversationsHistoryResponse,
messages: [
// @ts-ignore
{
...conversationsHistoryResponse.messages[0],
text: 'Hola, aca dejo la tarea 1 \n\n```console.log("Hello World 1!!!")``` \nY Y acá dejo la tarea 2 \n\n```console.log("Hello World 2!!!")```',
},
],
});

clientMock.users.info.mockResolvedValueOnce(
// @ts-ignore
Expand All @@ -350,10 +356,12 @@ describe('handleRobotFaceReaction', () => {

clientMock.conversations.info.mockResolvedValueOnce(
// @ts-ignore
{
...conversationsInfoResponse,
channel: { ...conversationsInfoResponse.channel, name: 'clase-10' },
}
conversationsInfoResponse
);

clientMock.chat.postMessage.mockResolvedValueOnce(
// @ts-ignore
chatPostMessageResponse
);

await handleRobotFaceReaction({
Expand All @@ -367,21 +375,15 @@ describe('handleRobotFaceReaction', () => {
expect(clientMock.chat.postMessage).toHaveBeenCalledWith({
channel: messageAuthorEvent.item.channel,
thread_ts: messageAuthorEvent.item.ts,
text: `<@${messageAuthorEvent.user}> el formato de la entrega no es válido, si estás en la clase 4 o menos tenés que enviar la tarea como bloque de código y a partir de la clase 5 tenés que enviar el link de github..`,
text: `<@${usersInfoResponse.user.id}> asegurate de mandar la tarea en un solo bloque de código, no en varios. Para ayudarte un poco, podés divirlo algo así: \`\`\`Tarea 1\n console.log("tarea 1")\n\n Tarea 2\n console.log("tarea 2")\`\`\``,
});
});

it('should make Robotina respond with a message when there is multiple blocks of code', async () => {
clientMock.conversations.history.mockResolvedValueOnce({
...conversationsHistoryResponse,
messages: [
// @ts-ignore
{
...conversationsHistoryResponse.messages[0],
text: 'Hola, aca dejo la tarea 1 \n\n```console.log("Hello World 1!!!")``` \nY Y acá dejo la tarea 2 \n\n```console.log("Hello World 2!!!")```',
},
],
});
it('should make Robotina respond with a message if the submission format is code but the channel is incorrect', async () => {
clientMock.conversations.history.mockResolvedValueOnce(
// @ts-ignore
conversationsHistoryResponse
);

clientMock.users.info.mockResolvedValueOnce(
// @ts-ignore
Expand All @@ -390,12 +392,10 @@ describe('handleRobotFaceReaction', () => {

clientMock.conversations.info.mockResolvedValueOnce(
// @ts-ignore
conversationsInfoResponse
);

clientMock.chat.postMessage.mockResolvedValueOnce(
// @ts-ignore
chatPostMessageResponse
{
...conversationsInfoResponse,
channel: { ...conversationsInfoResponse.channel, name: 'clase-10' },
}
);

await handleRobotFaceReaction({
Expand All @@ -409,7 +409,7 @@ describe('handleRobotFaceReaction', () => {
expect(clientMock.chat.postMessage).toHaveBeenCalledWith({
channel: messageAuthorEvent.item.channel,
thread_ts: messageAuthorEvent.item.ts,
text: `<@${usersInfoResponse.user.id}> asegurate de mandar la tarea en un solo bloque de código, no en varios. Para ayudarte un poco, podés divirlo algo así: \`\`\`Tarea 1\n console.log("tarea 1")\n\n Tarea 2\n console.log("tarea 2")\`\`\``,
text: `<@${messageAuthorEvent.user}> el formato de la entrega no es válido, si estás en la clase 4 o menos tenés que enviar la tarea como bloque de código y a partir de la clase 5 tenés que enviar el link de github..`,
});
});
});
Expand Down Expand Up @@ -523,6 +523,43 @@ describe('handleRobotFaceReaction', () => {
text: `<@${usersInfoResponse.user.id}> asegurate de mandar la tarea en un solo link de GitHub, no en varios.`,
});
});

it('should make Robotina respond with a message if the submission format is a GitHub link but the channel is incorrect', async () => {
clientMock.conversations.history.mockResolvedValueOnce({
...conversationsHistoryResponse,
messages: [
// @ts-ignore
{
...conversationsHistoryResponse.messages[0],
text: 'Hola, aca dejo la tarea https://github.com/r-argentina-programa/robotina',
},
],
});

clientMock.users.info.mockResolvedValueOnce(
// @ts-ignore
usersInfoResponse
);

clientMock.conversations.info.mockResolvedValueOnce(
// @ts-ignore
conversationsInfoResponse
);

await handleRobotFaceReaction({
client: clientMock,
// @ts-ignore
event: messageAuthorEvent,
logger: loggerMock,
});

expect(clientMock.chat.postMessage).toBeCalledTimes(1);
expect(clientMock.chat.postMessage).toHaveBeenCalledWith({
channel: messageAuthorEvent.item.channel,
thread_ts: messageAuthorEvent.item.ts,
text: `<@${messageAuthorEvent.user}> el formato de la entrega no es válido, si estás en la clase 4 o menos tenés que enviar la tarea como bloque de código y a partir de la clase 5 tenés que enviar el link de github..`,
});
});
});

describe('Formats exceptions', () => {
Expand Down
51 changes: 29 additions & 22 deletions src/utils/validateSubmissionDeliveryFormat.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
interface IValidateSubmissionDeliveryFormat {
classNumber: number;
delivery: string;
}

export const validateSubmissionDeliveryFormat = ({
classNumber,
delivery,
}: IValidateSubmissionDeliveryFormat) => {
const FIRST_LINK_FORMAT_LESSON_NUMBER = 5;
const linkFormatRegex = /github\.com\/[a-zA-Z]/;
if (classNumber < FIRST_LINK_FORMAT_LESSON_NUMBER) {
return true;
}
if (
classNumber >= FIRST_LINK_FORMAT_LESSON_NUMBER &&
linkFormatRegex.test(delivery)
) {
return true;
}
return false;
};
interface IValidateSubmissionDeliveryFormat {
classNumber: number;
delivery: string;
}

export const validateSubmissionDeliveryFormat = ({
classNumber,
delivery,
}: IValidateSubmissionDeliveryFormat) => {
const FIRST_LINK_FORMAT_LESSON_NUMBER = 5;
const codeFormatRegex = /```[^`]+```/;
const linkFormatRegex = /github\.com\/[a-zA-Z]/;

if (
classNumber < FIRST_LINK_FORMAT_LESSON_NUMBER &&
codeFormatRegex.test(delivery)
) {
return true;
}

if (
classNumber >= FIRST_LINK_FORMAT_LESSON_NUMBER &&
linkFormatRegex.test(delivery)
) {
return true;
}

return false;
};

0 comments on commit ddeec9d

Please sign in to comment.