|
| 1 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 2 | +import { ModelProviderName, IAgentRuntime } from '../types'; |
| 3 | +import { models } from '../models'; |
| 4 | +import { |
| 5 | + generateText, |
| 6 | + generateTrueOrFalse, |
| 7 | + splitChunks, |
| 8 | +} from '../generation'; |
| 9 | + |
| 10 | +// Mock the elizaLogger |
| 11 | +vi.mock('../index.ts', () => ({ |
| 12 | + elizaLogger: { |
| 13 | + log: vi.fn(), |
| 14 | + info: vi.fn(), |
| 15 | + error: vi.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock the generation functions |
| 20 | +vi.mock('../generation', async () => { |
| 21 | + const actual = await vi.importActual('../generation'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + generateText: vi.fn().mockImplementation(async ({ context }) => { |
| 25 | + if (!context) return ''; |
| 26 | + return 'mocked response'; |
| 27 | + }), |
| 28 | + generateTrueOrFalse: vi.fn().mockImplementation(async () => { |
| 29 | + return true; |
| 30 | + }), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +describe('Generation', () => { |
| 35 | + let mockRuntime: IAgentRuntime; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + // Setup mock runtime for tests |
| 39 | + mockRuntime = { |
| 40 | + modelProvider: ModelProviderName.OPENAI, |
| 41 | + token: 'mock-token', |
| 42 | + character: { |
| 43 | + modelEndpointOverride: undefined, |
| 44 | + }, |
| 45 | + getSetting: vi.fn().mockImplementation((key: string) => { |
| 46 | + if (key === 'LLAMACLOUD_MODEL_LARGE') return false; |
| 47 | + if (key === 'LLAMACLOUD_MODEL_SMALL') return false; |
| 48 | + return undefined; |
| 49 | + }), |
| 50 | + } as unknown as IAgentRuntime; |
| 51 | + |
| 52 | + // Clear all mocks before each test |
| 53 | + vi.clearAllMocks(); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('generateText', () => { |
| 57 | + it('should return empty string for empty context', async () => { |
| 58 | + const result = await generateText({ |
| 59 | + runtime: mockRuntime, |
| 60 | + context: '', |
| 61 | + modelClass: 'completion', |
| 62 | + }); |
| 63 | + expect(result).toBe(''); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return mocked response for non-empty context', async () => { |
| 67 | + const result = await generateText({ |
| 68 | + runtime: mockRuntime, |
| 69 | + context: 'test context', |
| 70 | + modelClass: 'completion', |
| 71 | + }); |
| 72 | + expect(result).toBe('mocked response'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should use correct model settings from provider config', () => { |
| 76 | + const modelProvider = mockRuntime.modelProvider; |
| 77 | + const modelSettings = models[modelProvider].settings; |
| 78 | + |
| 79 | + expect(modelSettings).toBeDefined(); |
| 80 | + expect(modelSettings.temperature).toBeDefined(); |
| 81 | + expect(modelSettings.frequency_penalty).toBeDefined(); |
| 82 | + expect(modelSettings.presence_penalty).toBeDefined(); |
| 83 | + expect(modelSettings.maxInputTokens).toBeDefined(); |
| 84 | + expect(modelSettings.maxOutputTokens).toBeDefined(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('generateTrueOrFalse', () => { |
| 89 | + it('should return boolean value', async () => { |
| 90 | + const result = await generateTrueOrFalse({ |
| 91 | + runtime: mockRuntime, |
| 92 | + context: 'test context', |
| 93 | + modelClass: 'completion', |
| 94 | + }); |
| 95 | + expect(typeof result).toBe('boolean'); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('splitChunks', () => { |
| 100 | + it('should split content into chunks of specified size', async () => { |
| 101 | + const content = 'a'.repeat(1000); |
| 102 | + const chunkSize = 100; |
| 103 | + const bleed = 20; |
| 104 | + |
| 105 | + const chunks = await splitChunks(content, chunkSize, bleed); |
| 106 | + |
| 107 | + expect(chunks.length).toBeGreaterThan(0); |
| 108 | + // Check if chunks overlap properly |
| 109 | + for (let i = 1; i < chunks.length; i++) { |
| 110 | + const prevChunkEnd = chunks[i - 1].slice(-bleed); |
| 111 | + const currentChunkStart = chunks[i].slice(0, bleed); |
| 112 | + expect(prevChunkEnd).toBe(currentChunkStart); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + it('should handle empty content', async () => { |
| 117 | + const chunks = await splitChunks('', 100, 20); |
| 118 | + expect(chunks).toEqual([]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle content smaller than chunk size', async () => { |
| 122 | + const content = 'small content'; |
| 123 | + const chunks = await splitChunks(content, 100, 20); |
| 124 | + expect(chunks).toEqual([content]); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments