|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { AgentRuntime } from "../runtime"; |
| 3 | +import { KnowledgeItem, Memory, UUID } from "../types"; |
| 4 | +import knowledge from "../knowledge"; |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +vi.mock("../embedding", () => ({ |
| 8 | + embed: vi.fn().mockResolvedValue([0.1, 0.2, 0.3]), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../generation", () => ({ |
| 12 | + splitChunks: vi.fn().mockResolvedValue(["chunk1", "chunk2"]), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../uuid", () => ({ |
| 16 | + stringToUuid: vi.fn().mockImplementation((str) => str as UUID), |
| 17 | +})); |
| 18 | + |
| 19 | +describe("Knowledge", () => { |
| 20 | + let mockRuntime: AgentRuntime; |
| 21 | + const mockAgentId = "agent-123" as UUID; |
| 22 | + const mockMemoryId = "memory-123" as UUID; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + mockRuntime = { |
| 26 | + agentId: mockAgentId, |
| 27 | + knowledgeManager: { |
| 28 | + searchMemoriesByEmbedding: vi.fn().mockResolvedValue([ |
| 29 | + { |
| 30 | + content: { |
| 31 | + text: "Test fragment", |
| 32 | + source: mockMemoryId, |
| 33 | + }, |
| 34 | + similarity: 0.9, |
| 35 | + }, |
| 36 | + ]), |
| 37 | + createMemory: vi.fn().mockResolvedValue(undefined), |
| 38 | + }, |
| 39 | + documentsManager: { |
| 40 | + getMemoryById: vi.fn().mockResolvedValue({ |
| 41 | + content: { text: "Test document" }, |
| 42 | + }), |
| 43 | + createMemory: vi.fn().mockResolvedValue(undefined), |
| 44 | + }, |
| 45 | + } as unknown as AgentRuntime; |
| 46 | + }); |
| 47 | + |
| 48 | + describe("get", () => { |
| 49 | + it("should retrieve knowledge based on message content", async () => { |
| 50 | + const message: Memory = { |
| 51 | + agentId: mockAgentId, |
| 52 | + content: { text: "test query" }, |
| 53 | + } as Memory; |
| 54 | + |
| 55 | + const result = await knowledge.get(mockRuntime, message); |
| 56 | + |
| 57 | + expect(result).toEqual(["Test document"]); |
| 58 | + expect(mockRuntime.knowledgeManager.searchMemoriesByEmbedding).toHaveBeenCalled(); |
| 59 | + expect(mockRuntime.documentsManager.getMemoryById).toHaveBeenCalledWith(mockMemoryId); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("set", () => { |
| 64 | + it("should store knowledge item and its fragments", async () => { |
| 65 | + const knowledgeItem: KnowledgeItem = { |
| 66 | + id: "test-id" as UUID, |
| 67 | + content: { text: "test content" }, |
| 68 | + }; |
| 69 | + |
| 70 | + await knowledge.set(mockRuntime, knowledgeItem); |
| 71 | + |
| 72 | + expect(mockRuntime.documentsManager.createMemory).toHaveBeenCalled(); |
| 73 | + expect(mockRuntime.knowledgeManager.createMemory).toHaveBeenCalledTimes(2); // Once for each chunk |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("preprocess", () => { |
| 78 | + it("should clean markdown and code blocks from text", () => { |
| 79 | + const input = ` |
| 80 | +# Header |
| 81 | +\`\`\` |
| 82 | +code block |
| 83 | +\`\`\` |
| 84 | +\`inline code\` |
| 85 | + |
| 86 | +[link text](http://example.com) |
| 87 | +<div>html</div> |
| 88 | +--- |
| 89 | +`; |
| 90 | + const expected = "header image alt link text html"; |
| 91 | + expect(knowledge.preprocess(input)).toBe(expected); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should handle empty input", () => { |
| 95 | + expect(knowledge.preprocess("")).toBe(""); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should convert text to lowercase", () => { |
| 99 | + const plainText = "This is Just Plain TEXT."; |
| 100 | + expect(knowledge.preprocess(plainText)).toBe("this is just plain text"); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments