|
| 1 | +import type { ApiServices } from './loadMethodRegistry' |
| 2 | +import { getLoadMethod, loadMethodRegistry } from './loadMethodRegistry' |
| 3 | + |
| 4 | +const mockApis: ApiServices = { |
| 5 | + chatModelsApi: { |
| 6 | + getChatModels: jest.fn(), |
| 7 | + getModelsByProvider: jest.fn() |
| 8 | + }, |
| 9 | + toolsApi: { |
| 10 | + getAllTools: jest.fn() |
| 11 | + }, |
| 12 | + credentialsApi: { |
| 13 | + getAllCredentials: jest.fn(), |
| 14 | + getCredentialsByName: jest.fn() |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + jest.clearAllMocks() |
| 20 | +}) |
| 21 | + |
| 22 | +describe('loadMethodRegistry', () => { |
| 23 | + describe('listChatModels', () => { |
| 24 | + it('should call chatModelsApi.getChatModels()', async () => { |
| 25 | + const mockModels = [{ name: 'gpt-4', label: 'GPT-4' }] |
| 26 | + ;(mockApis.chatModelsApi.getChatModels as jest.Mock).mockResolvedValue(mockModels) |
| 27 | + |
| 28 | + const result = await loadMethodRegistry['listChatModels'](mockApis) |
| 29 | + expect(mockApis.chatModelsApi.getChatModels).toHaveBeenCalled() |
| 30 | + expect(result).toEqual(mockModels) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + describe('listTools', () => { |
| 35 | + it('should call toolsApi.getAllTools()', async () => { |
| 36 | + const mockTools = [{ id: '1', name: 'Calculator' }] |
| 37 | + ;(mockApis.toolsApi.getAllTools as jest.Mock).mockResolvedValue(mockTools) |
| 38 | + |
| 39 | + const result = await loadMethodRegistry['listTools'](mockApis) |
| 40 | + expect(mockApis.toolsApi.getAllTools).toHaveBeenCalled() |
| 41 | + expect(result).toEqual(mockTools) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('listCredentials', () => { |
| 46 | + it('should call credentialsApi.getCredentialsByName() with params.name', async () => { |
| 47 | + const mockCredentials = [{ id: '1', name: 'My Key', credentialName: 'openAIApi' }] |
| 48 | + ;(mockApis.credentialsApi.getCredentialsByName as jest.Mock).mockResolvedValue(mockCredentials) |
| 49 | + |
| 50 | + const result = await loadMethodRegistry['listCredentials'](mockApis, { name: 'openAIApi' }) |
| 51 | + expect(mockApis.credentialsApi.getCredentialsByName).toHaveBeenCalledWith('openAIApi') |
| 52 | + expect(result).toEqual(mockCredentials) |
| 53 | + }) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('getLoadMethod', () => { |
| 58 | + it('should return the registry function for a known key', () => { |
| 59 | + const fn = getLoadMethod('listChatModels') |
| 60 | + expect(fn).toBeDefined() |
| 61 | + expect(typeof fn).toBe('function') |
| 62 | + }) |
| 63 | + |
| 64 | + it('should return the registry function for listTools', () => { |
| 65 | + const fn = getLoadMethod('listTools') |
| 66 | + expect(fn).toBeDefined() |
| 67 | + expect(typeof fn).toBe('function') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should return the registry function for listCredentials', () => { |
| 71 | + const fn = getLoadMethod('listCredentials') |
| 72 | + expect(fn).toBeDefined() |
| 73 | + expect(typeof fn).toBe('function') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should return undefined for an unknown key', () => { |
| 77 | + const fn = getLoadMethod('unknownMethod') |
| 78 | + expect(fn).toBeUndefined() |
| 79 | + }) |
| 80 | +}) |
0 commit comments