diff --git a/src/bot/__tests__/filters.test.ts b/src/bot/__tests__/filters.test.ts index 7beb54a..3e079f0 100644 --- a/src/bot/__tests__/filters.test.ts +++ b/src/bot/__tests__/filters.test.ts @@ -1,6 +1,11 @@ import * as filters from "../filter"; import prisma, { MatchingAlgorithms } from "../../../generated/prisma"; +interface FilterTestCase { + input: string, + expected: boolean; +} + const templateFilter: prisma.Filter = { id: 0, guild_id: "", @@ -14,18 +19,69 @@ const templateFilter: prisma.Filter = { }; -describe("Match: ALL", () => { }); +const runFilterTest = (filter: prisma.Filter, testCases: FilterTestCase[]) => { + for (const { input, expected } of testCases) { + test(`Input: ${input}`, () => { + const result = filters.mapAlgorithmToFunction(filter, input); + expect(result).toBe(expected); + }); + } +}; + +describe("Match: ALL", () => { + const filter: prisma.Filter = { + ...templateFilter, + name: "Block All Words - CASE SENSITIVE", + value: String.raw`one TWO threE`, + matching_algorithm: MatchingAlgorithms.ALL, + is_insensitive: false + }; + + const testCases: FilterTestCase[] = [ + { + input: "one two three", + expected: false + }, + { + input: "one threE four five", + expected: false + }, + { + input: "two four one six three", + expected: false + }, + { + input: "one TWO threE four five", + expected: true + }, + { + input: "nine ten seven eight one", + expected: false + }, + { + input: "five four threE TWO one", + expected: true + }, + { + input: "six nine one three eight", + expected: false + } + ]; + + runFilterTest(filter, testCases); +}); describe("Match: ANY", () => { const filter: prisma.Filter = { ...templateFilter, name: "Block American Politics", value: String.raw`trump biden democrat republican gop dnc kamala harris`, + matching_algorithm: MatchingAlgorithms.ANY, is_insensitive: true }; - const testCases: { input: string, expected: boolean }[] = [ -{ + const testCases: FilterTestCase[] = [ + { input: "Republicans float new tax breaks for tips, local taxes in Trump budget package", expected: true // Contains 'republican' and 'trump'. }, @@ -42,11 +98,7 @@ describe("Match: ANY", () => { expected: true // Contains 'biden'. }, { - input: "Biden Announces New Initiative to Tackle Climate Change", - expected: true // Contains 'biden'. - }, - { - input: "Joe Biden gives thoughts on Donald Trump, Vladimir Putin, Ukraine, and Kamala Harris in first interview since leaving office", + input: "Joe Biden gives thoughts on Donald Trump and Kamala Harris in first interview since leaving office", expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'. }, { @@ -63,12 +115,7 @@ describe("Match: ANY", () => { } ]; - for (const { input, expected } of testCases) { - test(`Test input: ${input}`, () => { - const result = filters.any(filter, input); - expect(result).toBe(expected); - }); - } + runFilterTest(filter, testCases); }); describe("Match: LITERAL", () => { }); @@ -78,10 +125,11 @@ describe("Match: REGEX", () => { ...templateFilter, name: "Block American Politics", value: String.raw`\b(trump|biden|democrat|republican|gop|dnc|kamala|harris)\b`, + matching_algorithm: MatchingAlgorithms.REGEX, is_insensitive: true }; - const testCases: { input: string, expected: boolean }[] = [ + const testCases: FilterTestCase[] = [ { input: "Republicans float new tax breaks for tips, local taxes in Trump budget package", expected: true // Contains 'republican' and 'trump'. @@ -99,11 +147,7 @@ describe("Match: REGEX", () => { expected: true // Contains 'biden'. }, { - input: "Biden Announces New Initiative to Tackle Climate Change", - expected: true // Contains 'biden'. - }, - { - input: "Joe Biden gives thoughts on Donald Trump, Vladimir Putin, Ukraine, and Kamala Harris in first interview since leaving office", + input: "Joe Biden gives thoughts on Donald Trump and Kamala Harris in first interview since leaving office", expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'. }, { @@ -120,12 +164,7 @@ describe("Match: REGEX", () => { } ]; - for (const { input, expected } of testCases) { - test(`Test input: ${input}`, () => { - const result = filters.regex(filter, input); - expect(result).toBe(expected); - }); - } + runFilterTest(filter, testCases); }); describe("Match: FUZZY", () => { });