diff --git a/apps/worker/src/whatsapp/tag-detector.test.ts b/apps/worker/src/whatsapp/tag-detector.test.ts new file mode 100644 index 0000000..f0d0996 --- /dev/null +++ b/apps/worker/src/whatsapp/tag-detector.test.ts @@ -0,0 +1,55 @@ +import { detectTags, isFlagged } from './tag-detector'; + +const ADMINS = ['1234567890@s.whatsapp.net', '0987654321@s.whatsapp.net']; + +describe('detectTags', () => { + it('detects #important hashtag (case-insensitive)', () => { + expect(detectTags('Check this #IMPORTANT update', 'user@s.whatsapp.net', ADMINS)) + .toContain('#important'); + }); + + it('detects #upparivar hashtag (case-insensitive)', () => { + expect(detectTags('Welcome to #UPParivar community', 'user@s.whatsapp.net', ADMINS)) + .toContain('#upparivar'); + }); + + it('detects #event hashtag', () => { + expect(detectTags('Join our #event on Saturday', 'user@s.whatsapp.net', ADMINS)) + .toContain('#event'); + }); + + it('detects /tower command prefix', () => { + expect(detectTags('/tower save this message', 'user@s.whatsapp.net', ADMINS)) + .toContain('#tower-command'); + }); + + it('detects multiple tags in one message', () => { + const tags = detectTags('#important #event happening', 'user@s.whatsapp.net', ADMINS); + expect(tags).toContain('#important'); + expect(tags).toContain('#event'); + }); + + it('detects admin sender', () => { + expect(detectTags('Regular message', '1234567890@s.whatsapp.net', ADMINS)) + .toContain('#admin'); + }); + + it('returns empty array for untagged message from non-admin', () => { + expect(detectTags('Just a regular chat message', 'nobody@s.whatsapp.net', ADMINS)) + .toEqual([]); + }); + + it('returns empty array for empty text', () => { + expect(detectTags('', 'nobody@s.whatsapp.net', ADMINS)).toEqual([]); + }); +}); + +describe('isFlagged', () => { + it('returns true when tags array is non-empty', () => { + expect(isFlagged(['#important'])).toBe(true); + }); + + it('returns false when tags array is empty', () => { + expect(isFlagged([])).toBe(false); + }); +}); diff --git a/apps/worker/src/whatsapp/tag-detector.ts b/apps/worker/src/whatsapp/tag-detector.ts new file mode 100644 index 0000000..0855ab3 --- /dev/null +++ b/apps/worker/src/whatsapp/tag-detector.ts @@ -0,0 +1,23 @@ +const HASHTAGS: Array<{ pattern: RegExp; tag: string }> = [ + { pattern: /#important/i, tag: '#important' }, + { pattern: /#upparivar/i, tag: '#upparivar' }, + { pattern: /#event/i, tag: '#event' }, +]; + +export function detectTags(text: string, senderJid: string, adminJids: string[]): string[] { + const tags: string[] = []; + + for (const { pattern, tag } of HASHTAGS) { + if (pattern.test(text)) tags.push(tag); + } + + if (text.trimStart().startsWith('/tower')) tags.push('#tower-command'); + + if (adminJids.includes(senderJid)) tags.push('#admin'); + + return tags; +} + +export function isFlagged(tags: string[]): boolean { + return tags.length > 0; +}