|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { messageDuplicateChecker } from "./duplicate-scanner"; |
| 3 | +import { User } from "discord.js"; |
| 4 | +import { fromPartial } from "@total-typescript/shoehorn"; |
| 5 | +import { duplicateCache } from "./duplicate-scanner"; |
| 6 | + |
| 7 | +const maxMessagesPerUser = 5; |
| 8 | +const maxCacheSize = 100; |
| 9 | +const maxTrivialCharacters = 10; |
| 10 | +// Mock dependencies |
| 11 | +const mockBot = { |
| 12 | + channels: { |
| 13 | + fetch: vi.fn().mockResolvedValue({ |
| 14 | + type: "GUILD_TEXT", |
| 15 | + send: vi.fn().mockResolvedValue({ |
| 16 | + delete: vi.fn().mockResolvedValue(undefined), |
| 17 | + }), |
| 18 | + }), |
| 19 | + }, |
| 20 | +}; |
| 21 | +const mockMessage = (content: string, authorId: string, isBot = false) => { |
| 22 | + return { |
| 23 | + content, |
| 24 | + author: { id: authorId, bot: isBot } as User, |
| 25 | + delete: vi.fn(), |
| 26 | + channel: { |
| 27 | + send: vi.fn().mockResolvedValue({ |
| 28 | + delete: vi.fn().mockResolvedValue(undefined), |
| 29 | + }), |
| 30 | + }, |
| 31 | + }; |
| 32 | +}; |
| 33 | +describe("Duplicate Scanner Tests", () => { |
| 34 | + beforeEach(() => { |
| 35 | + // Reset the cache before each test |
| 36 | + duplicateCache.clear(); |
| 37 | + }); |
| 38 | + it(`should not store messages less than ${maxTrivialCharacters} characters`, async () => { |
| 39 | + const msg = mockMessage("Help me", "user1"); |
| 40 | + const bot = mockBot; |
| 41 | + messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 42 | + const userMessages = duplicateCache.get("user1"); |
| 43 | + expect(userMessages).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should store messages correctly in the cache", async () => { |
| 47 | + const msg = mockMessage("Hello world", "user1"); |
| 48 | + const bot = mockBot; |
| 49 | + |
| 50 | + messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 51 | + |
| 52 | + const userMessages = duplicateCache.get("user1"); |
| 53 | + expect(userMessages).toBeDefined(); |
| 54 | + expect(userMessages?.has("hello world")).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it(`should enforce max size of ${maxMessagesPerUser} messages per user`, async () => { |
| 58 | + const bot = mockBot; |
| 59 | + for (let i = 1; i <= maxMessagesPerUser; i++) { |
| 60 | + const msg = mockMessage(`Message to delete ${i}`, "user1"); |
| 61 | + await messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 62 | + } |
| 63 | + |
| 64 | + const userMessages = duplicateCache.get("user1"); |
| 65 | + expect(userMessages).toBeDefined(); |
| 66 | + expect(userMessages?.size).toBe(maxMessagesPerUser); |
| 67 | + |
| 68 | + const msg = mockMessage("New Message", "user1"); |
| 69 | + await messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 70 | + |
| 71 | + expect(userMessages?.size).toBe(maxMessagesPerUser); |
| 72 | + expect(userMessages?.has("message 1")).toBe(false); // First message should be removed |
| 73 | + expect(userMessages?.has("new message")).toBe(true); // New message should be added |
| 74 | + }); |
| 75 | + |
| 76 | + it(`should enforce max size of ${maxCacheSize} users in the cache`, async () => { |
| 77 | + const bot = mockBot; |
| 78 | + |
| 79 | + for (let i = 1; i <= maxCacheSize; i++) { |
| 80 | + const msg = mockMessage("Hello world", `user${i}`); |
| 81 | + await messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 82 | + } |
| 83 | + |
| 84 | + expect(duplicateCache.size).toBe(maxCacheSize); |
| 85 | + |
| 86 | + const msg = mockMessage("Hello world", "user101"); |
| 87 | + await messageDuplicateChecker.handleMessage?.(fromPartial({ msg, bot })); |
| 88 | + |
| 89 | + expect(duplicateCache.size).toBe(maxCacheSize); |
| 90 | + expect(duplicateCache.has("user1")).toBe(false); |
| 91 | + expect(duplicateCache.has("user101")).toBe(true); |
| 92 | + }); |
| 93 | +}); |
0 commit comments