From ab4c76f19710edb64905d4ed3920fd351907218e Mon Sep 17 00:00:00 2001 From: niraja03 Date: Wed, 24 Jun 2026 06:24:06 +0530 Subject: [PATCH] test(ts-sdk): add Together LLM unit tests --- mem0-ts/src/oss/tests/together.test.ts | 124 +++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 mem0-ts/src/oss/tests/together.test.ts diff --git a/mem0-ts/src/oss/tests/together.test.ts b/mem0-ts/src/oss/tests/together.test.ts new file mode 100644 index 000000000..8358e251f --- /dev/null +++ b/mem0-ts/src/oss/tests/together.test.ts @@ -0,0 +1,124 @@ +/// +/** + * Together LLM — unit tests (mocked OpenAI). + */ + +import { TogetherLLM } from "../src/llms/together"; + +const mockCreate = jest.fn(); + +jest.mock("openai", () => { + return jest.fn().mockImplementation(() => ({ + chat: { completions: { create: mockCreate } }, + })); +}); + +describe("TogetherLLM (unit)", () => { + beforeEach(() => mockCreate.mockClear()); + + it("throws when no API key is provided", () => { + const original = process.env.TOGETHER_API_KEY; + delete process.env.TOGETHER_API_KEY; + try { + expect(() => new TogetherLLM({})).toThrow("Together API key is required"); + } finally { + if (original !== undefined) process.env.TOGETHER_API_KEY = original; + } + }); + + it("generateResponse() returns a text response", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [ + { + message: { + content: "Hello, world!", + role: "assistant", + tool_calls: null, + }, + }, + ], + }); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + const result = await llm.generateResponse([ + { role: "user", content: "Hi" }, + ]); + + expect(mockCreate).toHaveBeenCalledTimes(1); + expect(result).toBe("Hello, world!"); + }); + + it("generateResponse() handles tool calls", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [ + { + message: { + content: "", + role: "assistant", + tool_calls: [ + { + function: { + name: "get_weather", + arguments: '{"city": "London"}', + }, + }, + ], + }, + }, + ], + }); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + const result = await llm.generateResponse( + [{ role: "user", content: "What is the weather?" }], + undefined, + [{ type: "function", function: { name: "get_weather" } }], + ); + + expect(result).toEqual({ + content: "", + role: "assistant", + toolCalls: [{ name: "get_weather", arguments: '{"city": "London"}' }], + }); + }); + + it("generateResponse() wraps API errors with a clear message", async () => { + mockCreate.mockRejectedValueOnce(new Error("Connection refused")); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + + await expect( + llm.generateResponse([{ role: "user", content: "Hi" }]), + ).rejects.toThrow("Together LLM failed: Connection refused"); + }); + + it("generateChat() returns LLMResponse shape", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [ + { + message: { content: "I can help with that.", role: "assistant" }, + }, + ], + }); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + const result = await llm.generateChat([ + { role: "user", content: "Help me" }, + ]); + + expect(result).toEqual({ + content: "I can help with that.", + role: "assistant", + }); + }); + + it("generateChat() wraps API errors with a clear message", async () => { + mockCreate.mockRejectedValueOnce(new Error("Timeout")); + + const llm = new TogetherLLM({ apiKey: "test-key" }); + + await expect( + llm.generateChat([{ role: "user", content: "Hi" }]), + ).rejects.toThrow("Together LLM failed: Timeout"); + }); +});