|
1 | 1 | import type { IAgentBuilder } from '@mastra/core/agent-builder/ee'; |
2 | 2 | import type { IMastraEditor } from '@mastra/core/editor'; |
3 | | -import { describe, it, expect, vi } from 'vitest'; |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
4 | 4 |
|
5 | 5 | import { |
6 | 6 | GET_EDITOR_BUILDER_AVAILABLE_MODELS_ROUTE, |
@@ -613,18 +613,47 @@ describe('GET /editor/builder/infrastructure route metadata', () => { |
613 | 613 | describe('GET /editor/builder/models/available', () => { |
614 | 614 | const runAvailable = (editor?: Partial<IMastraEditor>) => |
615 | 615 | GET_EDITOR_BUILDER_AVAILABLE_MODELS_ROUTE.handler({ mastra: createMockMastra(editor) } as any) as Promise<{ |
616 | | - providers: Array<{ id: string; models: string[] }>; |
| 616 | + providers: Array<{ id: string; models: string[]; connected: boolean }>; |
617 | 617 | }>; |
618 | 618 |
|
619 | | - it('returns the full provider list when no editor / policy is configured', async () => { |
| 619 | + const originalOpenAIKey = process.env.OPENAI_API_KEY; |
| 620 | + const originalAnthropicKey = process.env.ANTHROPIC_API_KEY; |
| 621 | + |
| 622 | + beforeEach(() => { |
| 623 | + // The endpoint only surfaces providers with a configured API key, so make |
| 624 | + // exactly one provider "connected" and ensure another is not. |
| 625 | + process.env.OPENAI_API_KEY = 'test-key'; |
| 626 | + delete process.env.ANTHROPIC_API_KEY; |
| 627 | + }); |
| 628 | + |
| 629 | + afterEach(() => { |
| 630 | + if (originalOpenAIKey === undefined) delete process.env.OPENAI_API_KEY; |
| 631 | + else process.env.OPENAI_API_KEY = originalOpenAIKey; |
| 632 | + if (originalAnthropicKey === undefined) delete process.env.ANTHROPIC_API_KEY; |
| 633 | + else process.env.ANTHROPIC_API_KEY = originalAnthropicKey; |
| 634 | + }); |
| 635 | + |
| 636 | + it('returns only providers with a configured API key when no policy is configured', async () => { |
620 | 637 | const { providers } = await runAvailable(); |
621 | 638 |
|
622 | 639 | expect(providers.length).toBeGreaterThan(0); |
| 640 | + // Every returned provider is connected (has its API key configured). |
| 641 | + expect(providers.every(p => p.connected)).toBe(true); |
| 642 | + |
623 | 643 | const openai = providers.find(p => p.id === 'openai'); |
624 | 644 | expect(openai).toBeDefined(); |
625 | 645 | expect(openai!.models.length).toBeGreaterThan(0); |
626 | 646 | }); |
627 | 647 |
|
| 648 | + it('omits providers whose API key is not configured', async () => { |
| 649 | + const { providers } = await runAvailable(); |
| 650 | + |
| 651 | + // anthropic has no API key set in this test, so it must be filtered out |
| 652 | + // even though it exists in the provider registry. |
| 653 | + expect(providers.find(p => p.id === 'anthropic')).toBeUndefined(); |
| 654 | + expect(providers.find(p => p.id === 'openai')).toBeDefined(); |
| 655 | + }); |
| 656 | + |
628 | 657 | it('filters models down to the active policy allowlist (provider wildcard)', async () => { |
629 | 658 | const builder: IAgentBuilder = { |
630 | 659 | enabled: true, |
|
0 commit comments