|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// Minimal helper copied/adapted from tests/index.test.ts to capture the |
| 4 | +// onMonitoredMessage handler exported to the Discord bot initializer. |
| 5 | +async function loadMonitoredMessageHandler(setupAdditionalMocks?: () => Promise<void>) { |
| 6 | + let capturedOptions: { onMonitoredMessage?: (message: any) => Promise<void> } | null = null; |
| 7 | + |
| 8 | + await vi.doMock("../src/discord/client.js", async () => { |
| 9 | + class MockDiscordBot { |
| 10 | + constructor(options: any) { |
| 11 | + capturedOptions = options; |
| 12 | + } |
| 13 | + |
| 14 | + async start(): Promise<void> { |
| 15 | + return; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + return { DiscordBot: MockDiscordBot }; |
| 20 | + }); |
| 21 | + |
| 22 | + const processOnSpy = vi.spyOn(process, "on").mockImplementation(() => process as any); |
| 23 | + |
| 24 | + try { |
| 25 | + if (setupAdditionalMocks) { |
| 26 | + await setupAdditionalMocks(); |
| 27 | + } |
| 28 | + |
| 29 | + // Import the module under test after mocking to allow our mocks to be |
| 30 | + // picked up by the module loader. |
| 31 | + await import("../src/index.js"); |
| 32 | + } finally { |
| 33 | + processOnSpy.mockRestore(); |
| 34 | + } |
| 35 | + |
| 36 | + if (!capturedOptions) throw new Error("Failed to capture onMonitoredMessage handler"); |
| 37 | + |
| 38 | + const handler = (capturedOptions as any).onMonitoredMessage; |
| 39 | + if (typeof handler !== "function") throw new Error("Failed to capture onMonitoredMessage handler"); |
| 40 | + |
| 41 | + return handler as (message: any) => Promise<void>; |
| 42 | +} |
| 43 | + |
| 44 | +function createFakeMessage(content: string, overrides: { channelId?: string; messageId?: string; authorId?: string } = {}) { |
| 45 | + const replies: string[] = []; |
| 46 | + const threadMessages: string[] = []; |
| 47 | + |
| 48 | + const thread = { |
| 49 | + id: "thread-1", |
| 50 | + send: vi.fn(async (text: string) => { |
| 51 | + threadMessages.push(String(text)); |
| 52 | + }), |
| 53 | + setArchived: vi.fn(async (_archived: boolean) => undefined), |
| 54 | + }; |
| 55 | + |
| 56 | + const message: any = { |
| 57 | + content, |
| 58 | + author: { id: overrides.authorId ?? "author-1" }, |
| 59 | + id: overrides.messageId ?? "message-1", |
| 60 | + channelId: overrides.channelId ?? "channel-1", |
| 61 | + client: { user: { id: "bot-user-1" } }, |
| 62 | + react: vi.fn(async (_emoji: string) => undefined), |
| 63 | + reply: vi.fn(async (text: string) => { |
| 64 | + replies.push(String(text)); |
| 65 | + }), |
| 66 | + startThread: vi.fn(async (_opts: { name: string; autoArchiveDuration: number }) => thread), |
| 67 | + reactions: { cache: new Map<string, { users: { remove: (id: string) => Promise<void> } }>() }, |
| 68 | + }; |
| 69 | + |
| 70 | + return { message, replies, thread, threadMessages }; |
| 71 | +} |
| 72 | + |
| 73 | +describe("ob add message handler failure modes", () => { |
| 74 | + beforeEach(() => { |
| 75 | + vi.clearAllMocks(); |
| 76 | + vi.resetModules(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("replies with helpful message when referenced message cannot be fetched", async () => { |
| 80 | + const onMonitoredMessage = await loadMonitoredMessageHandler(async () => { |
| 81 | + await vi.doMock("../src/bot/cli-runner.js", () => { |
| 82 | + class MockCliRunnerError extends Error {} |
| 83 | + return { |
| 84 | + runAddCommand: vi.fn(), |
| 85 | + runQueueCommand: vi.fn(), |
| 86 | + runSummaryCommand: vi.fn(), |
| 87 | + isCliAvailable: vi.fn(async () => true), |
| 88 | + CliRunnerError: MockCliRunnerError, |
| 89 | + }; |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + const { message, replies } = createFakeMessage("ob add"); |
| 94 | + |
| 95 | + // Simulate a reply reference but fetching the referenced message fails |
| 96 | + message.reference = { messageId: "ref-1" }; |
| 97 | + message.channel = { messages: { fetch: vi.fn().mockRejectedValue(new Error("nope")) } }; |
| 98 | + |
| 99 | + await onMonitoredMessage(message); |
| 100 | + |
| 101 | + expect(replies.length).toBeGreaterThan(0); |
| 102 | + expect(replies[0]).toContain("couldn't fetch the message"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("rejects oversized inline payloads with an explanatory message", async () => { |
| 106 | + const onMonitoredMessage = await loadMonitoredMessageHandler(async () => { |
| 107 | + await vi.doMock("../src/bot/cli-runner.js", () => { |
| 108 | + class MockCliRunnerError extends Error {} |
| 109 | + return { |
| 110 | + runAddCommand: vi.fn(), |
| 111 | + runQueueCommand: vi.fn(), |
| 112 | + runSummaryCommand: vi.fn(), |
| 113 | + isCliAvailable: vi.fn(async () => true), |
| 114 | + CliRunnerError: MockCliRunnerError, |
| 115 | + }; |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + // Create a payload larger than the default 64KiB limit |
| 120 | + const largeText = "ob add " + "x".repeat(70 * 1024); |
| 121 | + const { message, replies } = createFakeMessage(largeText); |
| 122 | + |
| 123 | + await onMonitoredMessage(message); |
| 124 | + |
| 125 | + expect(replies.length).toBeGreaterThan(0); |
| 126 | + expect(replies[0]).toContain("Text too large to ingest directly"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("reports temp-file write failures to the user", async () => { |
| 130 | + const onMonitoredMessage = await loadMonitoredMessageHandler(async () => { |
| 131 | + await vi.doMock("../src/bot/cli-runner.js", () => { |
| 132 | + class MockCliRunnerError extends Error {} |
| 133 | + return { |
| 134 | + runAddCommand: vi.fn(), |
| 135 | + runQueueCommand: vi.fn(), |
| 136 | + runSummaryCommand: vi.fn(), |
| 137 | + isCliAvailable: vi.fn(async () => true), |
| 138 | + CliRunnerError: MockCliRunnerError, |
| 139 | + }; |
| 140 | + }); |
| 141 | + |
| 142 | + // Ensure we return a deterministic temp file path |
| 143 | + await vi.doMock("../src/discord/utils.js", () => ({ |
| 144 | + makeTempFileName: (prefix = "briefing", ext = "md") => "/tmp/fake-ob-add.txt", |
| 145 | + buildCliErrorReport: () => "", |
| 146 | + })); |
| 147 | + |
| 148 | + // Mock fs/promises to simulate write failure |
| 149 | + await vi.doMock("fs/promises", () => ({ |
| 150 | + writeFile: vi.fn().mockRejectedValue(new Error("no space")), |
| 151 | + unlink: vi.fn().mockResolvedValue(undefined), |
| 152 | + })); |
| 153 | + }); |
| 154 | + |
| 155 | + const { message, replies } = createFakeMessage("ob add small payload"); |
| 156 | + |
| 157 | + await onMonitoredMessage(message); |
| 158 | + |
| 159 | + expect(replies.length).toBeGreaterThan(0); |
| 160 | + expect(replies[0]).toContain("Failed to prepare temporary file for ingestion"); |
| 161 | + }); |
| 162 | +}); |
0 commit comments