test(bot): write test for 'all' filter

This commit is contained in:
Corban-Lee Jones 2025-05-15 12:14:29 +01:00
parent 540de53cd0
commit 3f58496fa0

View File

@ -1,6 +1,11 @@
import * as filters from "../filter"; import * as filters from "../filter";
import prisma, { MatchingAlgorithms } from "../../../generated/prisma"; import prisma, { MatchingAlgorithms } from "../../../generated/prisma";
interface FilterTestCase {
input: string,
expected: boolean;
}
const templateFilter: prisma.Filter = { const templateFilter: prisma.Filter = {
id: 0, id: 0,
guild_id: "", guild_id: "",
@ -14,17 +19,68 @@ 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", () => { describe("Match: ANY", () => {
const filter: prisma.Filter = { const filter: prisma.Filter = {
...templateFilter, ...templateFilter,
name: "Block American Politics", name: "Block American Politics",
value: String.raw`trump biden democrat republican gop dnc kamala harris`, value: String.raw`trump biden democrat republican gop dnc kamala harris`,
matching_algorithm: MatchingAlgorithms.ANY,
is_insensitive: true 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", input: "Republicans float new tax breaks for tips, local taxes in Trump budget package",
expected: true // Contains 'republican' and 'trump'. expected: true // Contains 'republican' and 'trump'.
@ -42,11 +98,7 @@ describe("Match: ANY", () => {
expected: true // Contains 'biden'. expected: true // Contains 'biden'.
}, },
{ {
input: "Biden Announces New Initiative to Tackle Climate Change", input: "Joe Biden gives thoughts on Donald Trump and Kamala Harris in first interview since leaving office",
expected: true // Contains 'biden'.
},
{
input: "Joe Biden gives thoughts on Donald Trump, Vladimir Putin, Ukraine, and Kamala Harris in first interview since leaving office",
expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'. expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'.
}, },
{ {
@ -63,12 +115,7 @@ describe("Match: ANY", () => {
} }
]; ];
for (const { input, expected } of testCases) { runFilterTest(filter, testCases);
test(`Test input: ${input}`, () => {
const result = filters.any(filter, input);
expect(result).toBe(expected);
});
}
}); });
describe("Match: LITERAL", () => { }); describe("Match: LITERAL", () => { });
@ -78,10 +125,11 @@ describe("Match: REGEX", () => {
...templateFilter, ...templateFilter,
name: "Block American Politics", name: "Block American Politics",
value: String.raw`\b(trump|biden|democrat|republican|gop|dnc|kamala|harris)\b`, value: String.raw`\b(trump|biden|democrat|republican|gop|dnc|kamala|harris)\b`,
matching_algorithm: MatchingAlgorithms.REGEX,
is_insensitive: true 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", input: "Republicans float new tax breaks for tips, local taxes in Trump budget package",
expected: true // Contains 'republican' and 'trump'. expected: true // Contains 'republican' and 'trump'.
@ -99,11 +147,7 @@ describe("Match: REGEX", () => {
expected: true // Contains 'biden'. expected: true // Contains 'biden'.
}, },
{ {
input: "Biden Announces New Initiative to Tackle Climate Change", input: "Joe Biden gives thoughts on Donald Trump and Kamala Harris in first interview since leaving office",
expected: true // Contains 'biden'.
},
{
input: "Joe Biden gives thoughts on Donald Trump, Vladimir Putin, Ukraine, and Kamala Harris in first interview since leaving office",
expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'. expected: true // Contains 'biden', 'trump', 'kamala' and 'harris'.
}, },
{ {
@ -120,12 +164,7 @@ describe("Match: REGEX", () => {
} }
]; ];
for (const { input, expected } of testCases) { runFilterTest(filter, testCases);
test(`Test input: ${input}`, () => {
const result = filters.regex(filter, input);
expect(result).toBe(expected);
});
}
}); });
describe("Match: FUZZY", () => { }); describe("Match: FUZZY", () => { });