diff --git a/extensions/thread-ownership/index.test.ts b/extensions/thread-ownership/index.test.ts index 1db1c51ebe8..41233a742b1 100644 --- a/extensions/thread-ownership/index.test.ts +++ b/extensions/thread-ownership/index.test.ts @@ -28,6 +28,17 @@ describe("thread-ownership plugin", () => { }), }; + function expectOwnershipFetchCall(index: number, url: string, agentId: string) { + const call = vi.mocked(globalThis.fetch).mock.calls[index]; + if (!call) { + throw new Error(`expected ownership fetch call ${index}`); + } + expect(call[0]).toBe(url); + const init = call[1]; + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify({ agent_id: agentId })); + } + beforeEach(() => { vi.clearAllMocks(); for (const key of Object.keys(hooks)) { @@ -109,12 +120,10 @@ describe("thread-ownership plugin", () => { const result = await sendSlackThreadMessage(); expect(result).toBeUndefined(); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C123/1234.5678", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "test-agent" }), - }), + "test-agent", ); }); @@ -133,12 +142,10 @@ describe("thread-ownership plugin", () => { ); expect(result).toBeUndefined(); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C123/1234.5678", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "test-agent" }), - }), + "test-agent", ); }); @@ -157,12 +164,10 @@ describe("thread-ownership plugin", () => { ); expect(result).toBeUndefined(); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C123/1234.5678", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "test-agent" }), - }), + "test-agent", ); }); @@ -183,12 +188,10 @@ describe("thread-ownership plugin", () => { ); expect(result).toBeUndefined(); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C123/1234.5678", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "test-agent" }), - }), + "test-agent", ); }); @@ -238,12 +241,10 @@ describe("thread-ownership plugin", () => { ); expect(result).toBeUndefined(); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C123/1234.5678", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "test-agent" }), - }), + "test-agent", ); }); @@ -255,7 +256,9 @@ describe("thread-ownership plugin", () => { const result = await sendSlackThreadMessage(); expect(result).toEqual({ cancel: true }); - expect(api.logger.info).toHaveBeenCalledWith(expect.stringContaining("cancelled send")); + const infoMessage = vi.mocked(api.logger.info).mock.calls[0]?.[0]; + expect(typeof infoMessage).toBe("string"); + expect(infoMessage).toContain("cancelled send"); }); it("fails open on network error", async () => { @@ -264,9 +267,9 @@ describe("thread-ownership plugin", () => { const result = await sendSlackThreadMessage(); expect(result).toBeUndefined(); - expect(api.logger.warn).toHaveBeenCalledWith( - expect.stringContaining("ownership check failed"), - ); + const warningMessage = vi.mocked(api.logger.warn).mock.calls[0]?.[0]; + expect(typeof warningMessage).toBe("string"); + expect(warningMessage).toContain("ownership check failed"); }); }); @@ -416,12 +419,10 @@ describe("thread-ownership plugin", () => { { channelId: "slack", conversationId: "C789" }, ); - expect(globalThis.fetch).toHaveBeenCalledWith( + expectOwnershipFetchCall( + 0, "http://localhost:8750/api/v1/ownership/C789/8888.0005", - expect.objectContaining({ - method: "POST", - body: JSON.stringify({ agent_id: "live-agent" }), - }), + "live-agent", ); });