|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import register from "./index.js"; |
| 3 | + |
| 4 | +describe("thread-ownership plugin", () => { |
| 5 | + const hooks: Record<string, Function> = {}; |
| 6 | + const api = { |
| 7 | + pluginConfig: {}, |
| 8 | + config: { |
| 9 | + agents: { |
| 10 | + list: [{ id: "test-agent", default: true, identity: { name: "TestBot" } }], |
| 11 | + }, |
| 12 | + }, |
| 13 | + id: "thread-ownership", |
| 14 | + name: "Thread Ownership", |
| 15 | + logger: { info: vi.fn(), warn: vi.fn(), debug: vi.fn() }, |
| 16 | + on: vi.fn((hookName: string, handler: Function) => { |
| 17 | + hooks[hookName] = handler; |
| 18 | + }), |
| 19 | + }; |
| 20 | + |
| 21 | + let originalFetch: typeof globalThis.fetch; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + for (const key of Object.keys(hooks)) delete hooks[key]; |
| 26 | + |
| 27 | + process.env.SLACK_FORWARDER_URL = "http://localhost:8750"; |
| 28 | + process.env.SLACK_BOT_USER_ID = "U999"; |
| 29 | + |
| 30 | + originalFetch = globalThis.fetch; |
| 31 | + globalThis.fetch = vi.fn(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + globalThis.fetch = originalFetch; |
| 36 | + delete process.env.SLACK_FORWARDER_URL; |
| 37 | + delete process.env.SLACK_BOT_USER_ID; |
| 38 | + vi.restoreAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("registers message_received and message_sending hooks", () => { |
| 42 | + register(api as any); |
| 43 | + |
| 44 | + expect(api.on).toHaveBeenCalledTimes(2); |
| 45 | + expect(api.on).toHaveBeenCalledWith("message_received", expect.any(Function)); |
| 46 | + expect(api.on).toHaveBeenCalledWith("message_sending", expect.any(Function)); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("message_sending", () => { |
| 50 | + beforeEach(() => { |
| 51 | + register(api as any); |
| 52 | + }); |
| 53 | + |
| 54 | + it("allows non-slack channels", async () => { |
| 55 | + const result = await hooks.message_sending( |
| 56 | + { content: "hello", metadata: { threadTs: "1234.5678", channelId: "C123" }, to: "C123" }, |
| 57 | + { channelId: "discord", conversationId: "C123" }, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result).toBeUndefined(); |
| 61 | + expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("allows top-level messages (no threadTs)", async () => { |
| 65 | + const result = await hooks.message_sending( |
| 66 | + { content: "hello", metadata: {}, to: "C123" }, |
| 67 | + { channelId: "slack", conversationId: "C123" }, |
| 68 | + ); |
| 69 | + |
| 70 | + expect(result).toBeUndefined(); |
| 71 | + expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("claims ownership successfully", async () => { |
| 75 | + vi.mocked(globalThis.fetch).mockResolvedValue( |
| 76 | + new Response(JSON.stringify({ owner: "test-agent" }), { status: 200 }), |
| 77 | + ); |
| 78 | + |
| 79 | + const result = await hooks.message_sending( |
| 80 | + { content: "hello", metadata: { threadTs: "1234.5678", channelId: "C123" }, to: "C123" }, |
| 81 | + { channelId: "slack", conversationId: "C123" }, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(result).toBeUndefined(); |
| 85 | + expect(globalThis.fetch).toHaveBeenCalledWith( |
| 86 | + "http://localhost:8750/api/v1/ownership/C123/1234.5678", |
| 87 | + expect.objectContaining({ |
| 88 | + method: "POST", |
| 89 | + body: JSON.stringify({ agent_id: "test-agent" }), |
| 90 | + }), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("cancels when thread owned by another agent", async () => { |
| 95 | + vi.mocked(globalThis.fetch).mockResolvedValue( |
| 96 | + new Response(JSON.stringify({ owner: "other-agent" }), { status: 409 }), |
| 97 | + ); |
| 98 | + |
| 99 | + const result = await hooks.message_sending( |
| 100 | + { content: "hello", metadata: { threadTs: "1234.5678", channelId: "C123" }, to: "C123" }, |
| 101 | + { channelId: "slack", conversationId: "C123" }, |
| 102 | + ); |
| 103 | + |
| 104 | + expect(result).toEqual({ cancel: true }); |
| 105 | + expect(api.logger.info).toHaveBeenCalledWith(expect.stringContaining("cancelled send")); |
| 106 | + }); |
| 107 | + |
| 108 | + it("fails open on network error", async () => { |
| 109 | + vi.mocked(globalThis.fetch).mockRejectedValue(new Error("ECONNREFUSED")); |
| 110 | + |
| 111 | + const result = await hooks.message_sending( |
| 112 | + { content: "hello", metadata: { threadTs: "1234.5678", channelId: "C123" }, to: "C123" }, |
| 113 | + { channelId: "slack", conversationId: "C123" }, |
| 114 | + ); |
| 115 | + |
| 116 | + expect(result).toBeUndefined(); |
| 117 | + expect(api.logger.warn).toHaveBeenCalledWith( |
| 118 | + expect.stringContaining("ownership check failed"), |
| 119 | + ); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("message_received @-mention tracking", () => { |
| 124 | + beforeEach(() => { |
| 125 | + register(api as any); |
| 126 | + }); |
| 127 | + |
| 128 | + it("tracks @-mentions and skips ownership check for mentioned threads", async () => { |
| 129 | + // Simulate receiving a message that @-mentions the agent. |
| 130 | + await hooks.message_received( |
| 131 | + { content: "Hey @TestBot help me", metadata: { threadTs: "9999.0001", channelId: "C456" } }, |
| 132 | + { channelId: "slack", conversationId: "C456" }, |
| 133 | + ); |
| 134 | + |
| 135 | + // Now send in the same thread -- should skip the ownership HTTP call. |
| 136 | + const result = await hooks.message_sending( |
| 137 | + { content: "Sure!", metadata: { threadTs: "9999.0001", channelId: "C456" }, to: "C456" }, |
| 138 | + { channelId: "slack", conversationId: "C456" }, |
| 139 | + ); |
| 140 | + |
| 141 | + expect(result).toBeUndefined(); |
| 142 | + expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + it("ignores @-mentions on non-slack channels", async () => { |
| 146 | + // Use a unique thread key so module-level state from other tests doesn't interfere. |
| 147 | + await hooks.message_received( |
| 148 | + { content: "Hey @TestBot", metadata: { threadTs: "7777.0001", channelId: "C999" } }, |
| 149 | + { channelId: "discord", conversationId: "C999" }, |
| 150 | + ); |
| 151 | + |
| 152 | + // The mention should not have been tracked, so sending should still call fetch. |
| 153 | + vi.mocked(globalThis.fetch).mockResolvedValue( |
| 154 | + new Response(JSON.stringify({ owner: "test-agent" }), { status: 200 }), |
| 155 | + ); |
| 156 | + |
| 157 | + await hooks.message_sending( |
| 158 | + { content: "Sure!", metadata: { threadTs: "7777.0001", channelId: "C999" }, to: "C999" }, |
| 159 | + { channelId: "slack", conversationId: "C999" }, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(globalThis.fetch).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it("tracks bot user ID mentions via <@U999> syntax", async () => { |
| 166 | + await hooks.message_received( |
| 167 | + { content: "Hey <@U999> help", metadata: { threadTs: "8888.0001", channelId: "C789" } }, |
| 168 | + { channelId: "slack", conversationId: "C789" }, |
| 169 | + ); |
| 170 | + |
| 171 | + const result = await hooks.message_sending( |
| 172 | + { content: "On it!", metadata: { threadTs: "8888.0001", channelId: "C789" }, to: "C789" }, |
| 173 | + { channelId: "slack", conversationId: "C789" }, |
| 174 | + ); |
| 175 | + |
| 176 | + expect(result).toBeUndefined(); |
| 177 | + expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments