|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * Tests for API key validation with local providers (Ollama, vLLM). |
| 5 | + * Verifies that the serializer skips API key validation for providers |
| 6 | + * that don't require one, while still enforcing it for cloud providers. |
| 7 | + */ |
| 8 | +import { loggerMock } from '@sim/testing/mocks' |
| 9 | +import { describe, expect, it, vi } from 'vitest' |
| 10 | +import { Serializer } from '@/serializer/index' |
| 11 | + |
| 12 | +const agentBlockConfig = { |
| 13 | + name: 'Agent', |
| 14 | + category: 'blocks', |
| 15 | + bgColor: '#2196F3', |
| 16 | + tools: { |
| 17 | + access: ['openai_chat'], |
| 18 | + config: { |
| 19 | + tool: () => { |
| 20 | + throw new Error('Invalid model selected') |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + subBlocks: [ |
| 25 | + { id: 'model', type: 'combobox', title: 'Model', required: true }, |
| 26 | + { id: 'apiKey', type: 'short-input', title: 'API Key', required: true }, |
| 27 | + { id: 'messages', type: 'messages-input', title: 'Messages' }, |
| 28 | + ], |
| 29 | + inputs: { |
| 30 | + model: { type: 'string' }, |
| 31 | + apiKey: { type: 'string' }, |
| 32 | + messages: { type: 'json' }, |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +vi.mock('@/blocks', () => ({ |
| 37 | + getBlock: (type: string) => { |
| 38 | + if (type === 'agent') return agentBlockConfig |
| 39 | + return null |
| 40 | + }, |
| 41 | + getAllBlocks: () => [agentBlockConfig], |
| 42 | +})) |
| 43 | + |
| 44 | +vi.mock('@/tools/utils', () => ({ |
| 45 | + getTool: () => null, |
| 46 | +})) |
| 47 | + |
| 48 | +vi.mock('@sim/logger', () => loggerMock) |
| 49 | + |
| 50 | +vi.mock('@/providers/utils', () => ({ |
| 51 | + providerRequiresApiKey: (model: string) => { |
| 52 | + if (model.startsWith('vllm/')) return false |
| 53 | + if (model.startsWith('gpt') || model.startsWith('o1') || model.startsWith('o3')) return true |
| 54 | + if (model.includes('claude')) return true |
| 55 | + if (model.includes('gemini')) return true |
| 56 | + if (model.startsWith('grok')) return true |
| 57 | + // Unknown models default to ollama — no API key required |
| 58 | + return false |
| 59 | + }, |
| 60 | +})) |
| 61 | + |
| 62 | +function createAgentBlock(model: string, apiKey: string | null = null): any { |
| 63 | + return { |
| 64 | + id: 'agent-1', |
| 65 | + type: 'agent', |
| 66 | + name: 'Agent', |
| 67 | + position: { x: 0, y: 0 }, |
| 68 | + subBlocks: { |
| 69 | + model: { value: model }, |
| 70 | + apiKey: { value: apiKey }, |
| 71 | + messages: { value: [] }, |
| 72 | + }, |
| 73 | + outputs: {}, |
| 74 | + enabled: true, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +describe('API key validation for local providers', () => { |
| 79 | + it.concurrent('should not require API key for Ollama models', () => { |
| 80 | + const serializer = new Serializer() |
| 81 | + const block = createAgentBlock('llama3.2:1b') |
| 82 | + |
| 83 | + expect(() => { |
| 84 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 85 | + }).not.toThrow() |
| 86 | + }) |
| 87 | + |
| 88 | + it.concurrent('should not require API key for other Ollama model names', () => { |
| 89 | + const serializer = new Serializer() |
| 90 | + const block = createAgentBlock('mistral:7b') |
| 91 | + |
| 92 | + expect(() => { |
| 93 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 94 | + }).not.toThrow() |
| 95 | + }) |
| 96 | + |
| 97 | + it.concurrent('should not require API key for vLLM models', () => { |
| 98 | + const serializer = new Serializer() |
| 99 | + const block = createAgentBlock('vllm/mistral-7b') |
| 100 | + |
| 101 | + expect(() => { |
| 102 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 103 | + }).not.toThrow() |
| 104 | + }) |
| 105 | + |
| 106 | + it.concurrent('should require API key for OpenAI models', () => { |
| 107 | + const serializer = new Serializer() |
| 108 | + const block = createAgentBlock('gpt-4o') |
| 109 | + |
| 110 | + expect(() => { |
| 111 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 112 | + }).toThrow('Agent is missing required fields: API Key') |
| 113 | + }) |
| 114 | + |
| 115 | + it.concurrent('should require API key for Anthropic models', () => { |
| 116 | + const serializer = new Serializer() |
| 117 | + const block = createAgentBlock('claude-sonnet-4-5') |
| 118 | + |
| 119 | + expect(() => { |
| 120 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 121 | + }).toThrow('Agent is missing required fields: API Key') |
| 122 | + }) |
| 123 | + |
| 124 | + it.concurrent('should require API key for Google models', () => { |
| 125 | + const serializer = new Serializer() |
| 126 | + const block = createAgentBlock('gemini-2.0-flash') |
| 127 | + |
| 128 | + expect(() => { |
| 129 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 130 | + }).toThrow('Agent is missing required fields: API Key') |
| 131 | + }) |
| 132 | + |
| 133 | + it.concurrent('should pass validation when API key is provided for cloud provider', () => { |
| 134 | + const serializer = new Serializer() |
| 135 | + const block = createAgentBlock('gpt-4o', 'sk-test-key') |
| 136 | + |
| 137 | + expect(() => { |
| 138 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 139 | + }).not.toThrow() |
| 140 | + }) |
| 141 | + |
| 142 | + it.concurrent('should pass validation for Ollama even with empty string API key', () => { |
| 143 | + const serializer = new Serializer() |
| 144 | + const block = createAgentBlock('llama3.2:1b', '') |
| 145 | + |
| 146 | + expect(() => { |
| 147 | + serializer.serializeWorkflow({ 'agent-1': block }, [], {}, undefined, true) |
| 148 | + }).not.toThrow() |
| 149 | + }) |
| 150 | +}) |
0 commit comments