|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import nock from "nock"; |
| 3 | +import { createClient } from "../../src/index.ts"; |
| 4 | + |
| 5 | +describe("Agents Module", () => { |
| 6 | + let base44: ReturnType<typeof createClient>; |
| 7 | + let scope: nock.Scope; |
| 8 | + const appId = "test-app-id"; |
| 9 | + const serverUrl = "https://api.base44.com"; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + base44 = createClient({ serverUrl, appId }); |
| 13 | + scope = nock(serverUrl); |
| 14 | + nock.disableNetConnect(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + nock.cleanAll(); |
| 19 | + nock.enableNetConnect(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("getConversations", () => { |
| 23 | + test("should fetch all conversations", async () => { |
| 24 | + const mockConversations = [ |
| 25 | + { id: "conv-1", agent_name: "support", messages: [] }, |
| 26 | + { id: "conv-2", agent_name: "sales", messages: [] }, |
| 27 | + ]; |
| 28 | + scope.get(`/api/apps/${appId}/agents/conversations`).reply(200, mockConversations); |
| 29 | + |
| 30 | + const result = await base44.agents.getConversations(); |
| 31 | + expect(result).toEqual(mockConversations); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("getConversation", () => { |
| 36 | + test("should fetch a specific conversation", async () => { |
| 37 | + const mockConversation = { id: "conv-1", agent_name: "support", messages: [] }; |
| 38 | + scope.get(`/api/apps/${appId}/agents/conversations/conv-1`).reply(200, mockConversation); |
| 39 | + |
| 40 | + const result = await base44.agents.getConversation("conv-1"); |
| 41 | + expect(result).toEqual(mockConversation); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("createConversation", () => { |
| 46 | + test("should create a conversation", async () => { |
| 47 | + const created = { id: "conv-new", agent_name: "support", messages: [] }; |
| 48 | + scope.post(`/api/apps/${appId}/agents/conversations`).reply(200, created); |
| 49 | + |
| 50 | + const result = await base44.agents.createConversation({ agent_name: "support" }); |
| 51 | + expect(result).toEqual(created); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("addMessage", () => { |
| 56 | + test("should post to v2 endpoint", async () => { |
| 57 | + const conversation = { id: "conv-1", agent_name: "support", messages: [] } as any; |
| 58 | + const response = { id: "msg-1", role: "assistant", content: "Hello!" }; |
| 59 | + scope.post(`/api/apps/${appId}/agents/conversations/v2/conv-1/messages`).reply(200, response); |
| 60 | + |
| 61 | + const result = await base44.agents.addMessage(conversation, { role: "user", content: "Hi" }); |
| 62 | + expect(result).toEqual(response); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("getWhatsAppConnectURL", () => { |
| 67 | + test("should return URL without token when no auth", () => { |
| 68 | + const url = base44.agents.getWhatsAppConnectURL("support"); |
| 69 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/whatsapp`); |
| 70 | + }); |
| 71 | + |
| 72 | + test("should include token when authenticated", () => { |
| 73 | + const authed = createClient({ serverUrl, appId, token: "test-token" }); |
| 74 | + const url = authed.agents.getWhatsAppConnectURL("support"); |
| 75 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/whatsapp?token=test-token`); |
| 76 | + }); |
| 77 | + |
| 78 | + test("should encode agent name", () => { |
| 79 | + const url = base44.agents.getWhatsAppConnectURL("my agent"); |
| 80 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/my%20agent/whatsapp`); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("getTelegramConnectURL", () => { |
| 85 | + test("should return URL without token when no auth", () => { |
| 86 | + const url = base44.agents.getTelegramConnectURL("support"); |
| 87 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/telegram`); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should include token when authenticated", () => { |
| 91 | + const authed = createClient({ serverUrl, appId, token: "test-token" }); |
| 92 | + const url = authed.agents.getTelegramConnectURL("support"); |
| 93 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/telegram?token=test-token`); |
| 94 | + }); |
| 95 | + |
| 96 | + test("should encode agent name", () => { |
| 97 | + const url = base44.agents.getTelegramConnectURL("my agent"); |
| 98 | + expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/my%20agent/telegram`); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments