UNPKG

@restnfeel/agentc-starter-kit

Version:

한국어 기업용 CMS 모듈 - Task Master AI와 함께 빠르게 웹사이트를 구현할 수 있는 재사용 가능한 컴포넌트 시스템

186 lines (152 loc) 5.36 kB
/** * @fileoverview Unit tests for ChatbotAPI class */ import { ChatbotAPI } from "../core/api/ChatbotAPI"; import type { ChatbotAPIConfig } from "../core/api/ChatbotAPI"; // Mock dependencies jest.mock("../core/services/llmService"); jest.mock("../core/services/vectorStore"); jest.mock("../core/services/storageService"); describe("ChatbotAPI", () => { let api: ChatbotAPI; let mockConfig: ChatbotAPIConfig; beforeEach(() => { mockConfig = { llm: { provider: "openai", model: "gpt-3.5-turbo", apiKey: "test-key", maxTokens: 1000, temperature: 0.7, }, vectorStore: { provider: "memory", dimensions: 1536, }, storage: { provider: "local", }, }; api = new ChatbotAPI(); }); afterEach(() => { jest.clearAllMocks(); }); describe("initialization", () => { it("should create instance successfully", () => { expect(api).toBeInstanceOf(ChatbotAPI); }); it("should initialize with valid config", async () => { const result = await api.initialize(mockConfig); expect(result.success).toBe(true); }); it("should handle initialization errors", async () => { const invalidConfig = { ...mockConfig, llm: undefined }; const result = await api.initialize(invalidConfig); expect(result.success).toBe(false); expect(result.error).toBeDefined(); }); it("should not re-initialize if already initialized", async () => { await api.initialize(mockConfig); const result = await api.initialize(mockConfig); expect(result.success).toBe(true); expect(result.data?.message).toContain("already"); }); }); describe("document management", () => { beforeEach(async () => { await api.initialize(mockConfig); }); it("should upload document successfully", async () => { const mockFile = new File(["test content"], "test.txt", { type: "text/plain", }); const result = await api.uploadDocument(mockFile); expect(result.success).toBe(true); expect(result.data).toBeDefined(); }); it("should handle upload errors", async () => { const invalidFile = null as any; const result = await api.uploadDocument(invalidFile); expect(result.success).toBe(false); expect(result.error).toBeDefined(); }); it("should delete document successfully", async () => { const result = await api.deleteDocument("test-doc-id"); expect(result.success).toBe(true); }); it("should list documents", async () => { const result = await api.getDocuments(); expect(result.success).toBe(true); expect(Array.isArray(result.data)).toBe(true); }); }); describe("conversation management", () => { beforeEach(async () => { await api.initialize(mockConfig); }); it("should start conversation successfully", async () => { const result = await api.startConversation("Test Conversation"); expect(result.success).toBe(true); expect(result.data?.title).toBe("Test Conversation"); }); it("should send message successfully", async () => { const conversation = await api.startConversation(); const result = await api.sendMessage("Hello", conversation.data!.id); expect(result.success).toBe(true); expect(result.data?.content).toBe("Hello"); }); it("should handle send message errors", async () => { const result = await api.sendMessage("Hello", "invalid-conversation-id"); expect(result.success).toBe(false); expect(result.error).toBeDefined(); }); }); describe("search functionality", () => { beforeEach(async () => { await api.initialize(mockConfig); }); it("should search similar documents", async () => { const result = await api.searchSimilarDocuments("test query"); expect(result.success).toBe(true); expect(Array.isArray(result.data)).toBe(true); }); it("should handle search errors", async () => { const result = await api.searchSimilarDocuments(""); expect(result.success).toBe(false); expect(result.error).toBeDefined(); }); }); describe("configuration management", () => { it("should get current configuration", () => { const config = api.getConfiguration(); expect(config).toBeDefined(); }); it("should update configuration", async () => { await api.initialize(mockConfig); const newConfig = { ...mockConfig, llm: { ...mockConfig.llm!, temperature: 0.5, }, }; const result = await api.updateConfiguration(newConfig); expect(result.success).toBe(true); }); }); describe("error handling", () => { it("should handle method calls before initialization", async () => { const uninitializedApi = new ChatbotAPI(); const result = await uninitializedApi.sendMessage("test", "test-id"); expect(result.success).toBe(false); expect(result.error).toContain("not initialized"); }); it("should handle invalid parameters", async () => { await api.initialize(mockConfig); const result = await api.sendMessage("", ""); expect(result.success).toBe(false); expect(result.error).toBeDefined(); }); }); });