Skip to content

Commit

Permalink
fix: format-checkers regex to support :emoji: format (#97)
Browse files Browse the repository at this point in the history
* fix invalid regex https://github.com/ES-Community/bot/assets/5005154/02494c78-25d3-4a80-be93-77309e211816
* add some unit tests
* change `FunctionChecker` type
  use string instead Message for testing, all checks use `cleanContent` from `Message`

* test: disable WorkChronicles test if CI
  • Loading branch information
Purexo authored May 13, 2024
1 parent 11e0b5f commit b5fc4ee
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/format-checkers/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default new FormatChecker({
name: 'Job',
description: 'Force le formatage du canal #jobs.',
channelName: 'jobs',
checker({ cleanContent }, logger) {
checker(cleanContent, logger) {
const lines = cleanContent.split('\n');
const headerParts = lines[0].split(' - ');

Expand Down
2 changes: 1 addition & 1 deletion src/format-checkers/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import createRegExp from 'emoji-regex';
const unicodeEmojiRegexp = createRegExp().source;
const urlRegexp =
'<?https?:\\/\\/(?:www\\.)?[-\\w@:%.\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-\\w()@:%\\+.~#?&//=]*)>?';
const titleRegexp = `(?:[\\w- ]|(?:<a?:\\w{2,32}:\\d{17,20}>)|${unicodeEmojiRegexp})+`;
const titleRegexp = `(?:[a-zA-Z0-9_\\- ]|(?:<a?:\\w{2,32}:\\d{17,20}>)|(?::\\w{2,32}:)|${unicodeEmojiRegexp})+`;
const linkRegexp = `^\\[( )?(\\*\\*)?${titleRegexp}\\2\\1\\](?:[^\\n])+ - ${urlRegexp}$`;

export default new FormatChecker({
Expand Down
4 changes: 2 additions & 2 deletions src/format-checkers/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FormatChecker } from '../framework/index.js';
const unicodeEmojiRegexp = createRegExp().source;
const urlRegexp =
'<?https?:\\/\\/(?:www\\.)?[-\\w@:%.\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-\\w()@:%\\+.~#?&//=]*)>?';
const headerRegexp = `(?:[\\w- ]|(?:<a?:\\w{2,32}:\\d{17,20}>)|${unicodeEmojiRegexp})`;
const headerRegexp = `(?:[a-zA-Z0-9_\\- ]|(?:<a?:\\w{2,32}:\\d{17,20}>)|(?::\\w{2,32}:)|${unicodeEmojiRegexp})+`;
const projectRegexp = new RegExp(
`^\\*\\*${headerRegexp}+\\*\\*\\n\\n(?:.*\\n)+\n(?:(?:${headerRegexp}* )?${urlRegexp}\n)+$`,
);
Expand All @@ -14,7 +14,7 @@ export default new FormatChecker({
name: 'Project',
description: 'Force le formatage du canal #projets.',
channelName: 'projets',
checker: ({ cleanContent }) => projectRegexp.test(cleanContent + '\n'),
checker: (cleanContent) => projectRegexp.test(cleanContent + '\n'),
examples: [
`**Nom du projet**\n\nDescription du projet\n\nhttps://github.com`,
],
Expand Down
11 changes: 7 additions & 4 deletions src/framework/FormatChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Base, BaseConfig } from './Base.js';
import { Bot } from './Bot.js';
import { findTextChannelByName, isTextChannel } from './helpers.js';

type FunctionChecker = (message: Message, logger: Logger) => boolean;
type FunctionChecker = (cleanContent: string, logger: Logger) => boolean;

export interface FormatCheckerConfig extends BaseConfig {
channelName: string;
Expand Down Expand Up @@ -38,6 +38,11 @@ export class FormatChecker extends Base {
this._messageUpdateHandler = this._messageUpdateHandler.bind(this);
}

isMessageValid(cleanContent: string, logger: Logger) {
if (this.checker instanceof RegExp) return this.checker.test(cleanContent);
return this.checker(cleanContent, logger);
}

async _messageHandler(message: Message): Promise<void> {
if (this.bot === undefined) return;
if (
Expand All @@ -53,9 +58,7 @@ export class FormatChecker extends Base {
checkerName: this.name,
});

if (this.checker instanceof RegExp) {
if (this.checker.test(message.cleanContent)) return;
} else if (this.checker(message, logger)) return;
if (this.isMessageValid(message.cleanContent, logger)) return;

const { cleanContent, author } = message;

Expand Down
2 changes: 1 addition & 1 deletion tests/cron/WorkChronicles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from 'vitest';

import { getLastChronicle } from '../../src/crons/WorkChronicles.js';

test('getLastChronicle', async () => {
test.skipIf(process.env.CI)('getLastChronicle', async () => {
const chronicle = await getLastChronicle();
if (!chronicle) return;

Expand Down
75 changes: 75 additions & 0 deletions tests/format-checkers/Link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, test, expect } from 'vitest';
import Link from '../../src/format-checkers/Link.js';
import { pino, transport } from 'pino';
import { randomUUID } from 'node:crypto';

describe('Link', () => {
const logger = pino(transport({ target: 'pino-pretty' })).child({
id: randomUUID(),
type: 'FormatChecker',
checkerName: 'Link',
});

test('valid without emoji', () => {
const message = `[**SUJET**] Votre description ici - https://github.com/es-community`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with unicode emoji', () => {
const message = `[👍] Votre description ici - https://github.com/es-community`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with multiple unicode emojis', () => {
const message = `[👍👌] Votre description ici - https://github.com/es-community`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with discord emoji', () => {
const message = `[<:adonis:793993785712312361>] Breadcrumbs automatiques pour les routes sur Adonis V6 - https://adonis-breadcrumbs.pages.dev`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with multiple discord emojis', () => {
const message = `[<:adonis:793993785712312361><:firefox:793993785712312369>] Breadcrumbs automatiques pour les routes sur Adonis V6 - https://adonis-breadcrumbs.pages.dev`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with multiple discord emojis mixed with unicode emojis', () => {
const message = `[<:adonis:793993785712312361>🌈<:firefox:793993785712312369>👍👌] Breadcrumbs automatiques pour les routes sur Adonis V6 - https://adonis-breadcrumbs.pages.dev`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('valid with discord dismissed emoji', () => {
const message = `[:adonis:] Breadcrumbs automatiques pour les routes sur Adonis V6 - https://adonis-breadcrumbs.pages.dev`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(true);
});

test('invalid, link is missing', () => {
const message = `[**SUJET**] Votre description ici`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(false);
});

test('invalid, separator is missing', () => {
const message = `[**SUJET**] Votre description ici https://github.com/es-community`;
const isValid = Link.isMessageValid(message, logger);

expect(isValid).toBe(false);
});
});

0 comments on commit b5fc4ee

Please sign in to comment.