|
| 1 | +import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | + |
| 3 | +import { Readable } from 'stream'; |
| 4 | + |
| 5 | +import { FileStorageDriverFactory } from 'src/engine/core-modules/file-storage/file-storage-driver.factory'; |
| 6 | +import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service'; |
| 7 | +import { DefaultAiCatalogService } from 'src/engine/metadata-modules/ai/ai-models/services/default-ai-catalog.service'; |
| 8 | + |
| 9 | +const mockReadFile = jest.fn(); |
| 10 | + |
| 11 | +describe('DefaultAiCatalogService', () => { |
| 12 | + let service: DefaultAiCatalogService; |
| 13 | + let mockConfigService: jest.Mocked<TwentyConfigService>; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + |
| 18 | + mockConfigService = { |
| 19 | + get: jest.fn().mockReturnValue(undefined), |
| 20 | + } as any; |
| 21 | + |
| 22 | + const mockDriverFactory = { |
| 23 | + getCurrentDriver: jest.fn().mockReturnValue({ readFile: mockReadFile }), |
| 24 | + }; |
| 25 | + |
| 26 | + const module: TestingModule = await Test.createTestingModule({ |
| 27 | + providers: [ |
| 28 | + DefaultAiCatalogService, |
| 29 | + { provide: TwentyConfigService, useValue: mockConfigService }, |
| 30 | + { provide: FileStorageDriverFactory, useValue: mockDriverFactory }, |
| 31 | + ], |
| 32 | + }).compile(); |
| 33 | + |
| 34 | + service = module.get(DefaultAiCatalogService); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('onModuleInit', () => { |
| 38 | + it('should use built-in catalog when AI_CATALOG_STORAGE_PATH is not set', async () => { |
| 39 | + await service.onModuleInit(); |
| 40 | + |
| 41 | + const providers = service.getDefaultAiCatalog(); |
| 42 | + |
| 43 | + expect(providers).toBeDefined(); |
| 44 | + expect(Object.keys(providers).length).toBeGreaterThan(0); |
| 45 | + expect(mockReadFile).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should load catalog from storage when AI_CATALOG_STORAGE_PATH is set', async () => { |
| 49 | + const catalog = JSON.stringify({ |
| 50 | + customProvider: { |
| 51 | + npm: '@ai-sdk/openai', |
| 52 | + models: [ |
| 53 | + { |
| 54 | + name: 'custom-model', |
| 55 | + label: 'Custom Model', |
| 56 | + inputCostPerMillionTokens: 1, |
| 57 | + outputCostPerMillionTokens: 2, |
| 58 | + contextWindowTokens: 4096, |
| 59 | + maxOutputTokens: 1024, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + mockConfigService.get.mockImplementation((key: string) => { |
| 66 | + if (key === 'AI_CATALOG_STORAGE_PATH') return 'config/ai-catalog.json'; |
| 67 | + |
| 68 | + return undefined; |
| 69 | + }); |
| 70 | + |
| 71 | + mockReadFile.mockResolvedValue(Readable.from([Buffer.from(catalog)])); |
| 72 | + |
| 73 | + await service.onModuleInit(); |
| 74 | + |
| 75 | + const providers = service.getDefaultAiCatalog(); |
| 76 | + |
| 77 | + expect(Object.keys(providers)).toEqual(['customProvider']); |
| 78 | + expect(providers['customProvider'].name).toBe('customProvider'); |
| 79 | + expect(providers['customProvider'].models?.[0].source).toBe('catalog'); |
| 80 | + expect(mockReadFile).toHaveBeenCalledWith({ |
| 81 | + filePath: 'config/ai-catalog.json', |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should reset catalog to empty object when storage read fails', async () => { |
| 86 | + mockConfigService.get.mockImplementation((key: string) => { |
| 87 | + if (key === 'AI_CATALOG_STORAGE_PATH') return 'config/ai-catalog.json'; |
| 88 | + |
| 89 | + return undefined; |
| 90 | + }); |
| 91 | + |
| 92 | + mockReadFile.mockRejectedValue(new Error('Network error')); |
| 93 | + |
| 94 | + await service.onModuleInit(); |
| 95 | + |
| 96 | + expect(service.getDefaultAiCatalog()).toEqual({}); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should reset catalog to empty object when storage returns invalid JSON', async () => { |
| 100 | + mockConfigService.get.mockImplementation((key: string) => { |
| 101 | + if (key === 'AI_CATALOG_STORAGE_PATH') return 'config/ai-catalog.json'; |
| 102 | + |
| 103 | + return undefined; |
| 104 | + }); |
| 105 | + |
| 106 | + mockReadFile.mockResolvedValue( |
| 107 | + Readable.from([Buffer.from('not valid json')]), |
| 108 | + ); |
| 109 | + |
| 110 | + await service.onModuleInit(); |
| 111 | + |
| 112 | + expect(service.getDefaultAiCatalog()).toEqual({}); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should reset catalog to empty object when payload fails Zod validation', async () => { |
| 116 | + const invalidCatalog = JSON.stringify({ |
| 117 | + badProvider: { models: 'not-an-array' }, |
| 118 | + }); |
| 119 | + |
| 120 | + mockConfigService.get.mockImplementation((key: string) => { |
| 121 | + if (key === 'AI_CATALOG_STORAGE_PATH') return 'config/ai-catalog.json'; |
| 122 | + |
| 123 | + return undefined; |
| 124 | + }); |
| 125 | + |
| 126 | + mockReadFile.mockResolvedValue( |
| 127 | + Readable.from([Buffer.from(invalidCatalog)]), |
| 128 | + ); |
| 129 | + |
| 130 | + await service.onModuleInit(); |
| 131 | + |
| 132 | + expect(service.getDefaultAiCatalog()).toEqual({}); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments