Skip to content

Commit 539f1b2

Browse files
committed
test: add validation and routing tests for scaffold flow
Test source/host defaults, Lambda-only enforcement, --host rejection for existing-endpoint, and handleAddGatewayTarget routing based on source type.
1 parent cdfff00 commit 539f1b2

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

src/cli/commands/add/__tests__/actions.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { buildGatewayTargetConfig } from '../actions.js';
22
import type { ValidatedAddGatewayTargetOptions } from '../actions.js';
3-
import { describe, expect, it } from 'vitest';
3+
import { afterEach, describe, expect, it, vi } from 'vitest';
4+
5+
const mockCreateToolFromWizard = vi.fn().mockResolvedValue({ toolName: 'test', projectPath: '/tmp' });
6+
const mockCreateExternalGatewayTarget = vi.fn().mockResolvedValue({ toolName: 'test', projectPath: '' });
7+
8+
vi.mock('../../../operations/mcp/create-mcp', () => ({
9+
createToolFromWizard: (...args: unknown[]) => mockCreateToolFromWizard(...args),
10+
createExternalGatewayTarget: (...args: unknown[]) => mockCreateExternalGatewayTarget(...args),
11+
createGatewayFromWizard: vi.fn(),
12+
}));
413

514
describe('buildGatewayTargetConfig', () => {
615
it('maps name, gateway, language correctly', () => {
@@ -66,3 +75,55 @@ describe('buildGatewayTargetConfig', () => {
6675
expect(config.outboundAuth).toBeUndefined();
6776
});
6877
});
78+
79+
// Dynamic import to pick up mocks
80+
const { handleAddGatewayTarget } = await import('../actions.js');
81+
82+
describe('handleAddGatewayTarget', () => {
83+
afterEach(() => vi.clearAllMocks());
84+
85+
it('routes existing-endpoint to createExternalGatewayTarget', async () => {
86+
const options: ValidatedAddGatewayTargetOptions = {
87+
name: 'test-tool',
88+
language: 'Other',
89+
host: 'Lambda',
90+
source: 'existing-endpoint',
91+
endpoint: 'https://example.com/mcp',
92+
gateway: 'my-gw',
93+
};
94+
95+
await handleAddGatewayTarget(options);
96+
97+
expect(mockCreateExternalGatewayTarget).toHaveBeenCalledOnce();
98+
expect(mockCreateToolFromWizard).not.toHaveBeenCalled();
99+
});
100+
101+
it('routes create-new to createToolFromWizard', async () => {
102+
const options: ValidatedAddGatewayTargetOptions = {
103+
name: 'test-tool',
104+
language: 'Python',
105+
host: 'Lambda',
106+
source: 'create-new',
107+
gateway: 'my-gw',
108+
};
109+
110+
await handleAddGatewayTarget(options);
111+
112+
expect(mockCreateToolFromWizard).toHaveBeenCalledOnce();
113+
expect(mockCreateExternalGatewayTarget).not.toHaveBeenCalled();
114+
});
115+
116+
it('routes to createToolFromWizard when source not specified', async () => {
117+
const options: ValidatedAddGatewayTargetOptions = {
118+
name: 'test-tool',
119+
language: 'Python',
120+
host: 'Lambda',
121+
gateway: 'my-gw',
122+
};
123+
124+
await handleAddGatewayTarget(options);
125+
126+
expect(mockCreateToolFromWizard).toHaveBeenCalledOnce();
127+
expect(mockCreateExternalGatewayTarget).not.toHaveBeenCalled();
128+
});
129+
});

src/cli/commands/add/__tests__/validate.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,47 @@ describe('validate', () => {
369369
const result = await validateAddGatewayTargetOptions(options);
370370
expect(result.valid).toBe(true);
371371
});
372+
373+
// Source and host defaults
374+
it('defaults source to create-new when not specified', async () => {
375+
const options: AddGatewayTargetOptions = { name: 'test-tool', language: 'Python' };
376+
const result = await validateAddGatewayTargetOptions(options);
377+
expect(result.valid).toBe(true);
378+
expect(options.source).toBe('create-new');
379+
});
380+
381+
it('defaults host to Lambda when not specified', async () => {
382+
const options: AddGatewayTargetOptions = { name: 'test-tool', language: 'Python' };
383+
const result = await validateAddGatewayTargetOptions(options);
384+
expect(result.valid).toBe(true);
385+
expect(options.host).toBe('Lambda');
386+
});
387+
388+
// Lambda-only enforcement
389+
it('rejects AgentCoreRuntime host for create-new', async () => {
390+
const options: AddGatewayTargetOptions = {
391+
name: 'test-tool',
392+
language: 'Python',
393+
source: 'create-new',
394+
host: 'AgentCoreRuntime',
395+
};
396+
const result = await validateAddGatewayTargetOptions(options);
397+
expect(result.valid).toBe(false);
398+
expect(result.error).toBe('Only Lambda is supported as compute host for scaffolded targets');
399+
});
400+
401+
// Host rejected for existing-endpoint
402+
it('rejects --host with existing-endpoint', async () => {
403+
const options: AddGatewayTargetOptions = {
404+
name: 'test-tool',
405+
source: 'existing-endpoint',
406+
endpoint: 'https://example.com/mcp',
407+
host: 'Lambda',
408+
};
409+
const result = await validateAddGatewayTargetOptions(options);
410+
expect(result.valid).toBe(false);
411+
expect(result.error).toBe('--host is not applicable for existing endpoint targets');
412+
});
372413
});
373414

374415
describe('validateAddMemoryOptions', () => {

0 commit comments

Comments
 (0)