@@ -12,16 +12,21 @@ import {
1212 validateAddIdentityOptions ,
1313 validateAddMemoryOptions ,
1414} from '../validate.js' ;
15- import { afterEach , describe , expect , it , vi } from 'vitest' ;
15+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
1616
1717const mockReadProjectSpec = vi . fn ( ) ;
18+ const mockGetExistingGateways = vi . fn ( ) ;
1819
1920vi . mock ( '../../../../lib/index.js' , ( ) => ( {
2021 ConfigIO : class {
2122 readProjectSpec = mockReadProjectSpec ;
2223 } ,
2324} ) ) ;
2425
26+ vi . mock ( '../../../operations/mcp/create-mcp.js' , ( ) => ( {
27+ getExistingGateways : ( ...args : unknown [ ] ) => mockGetExistingGateways ( ...args ) ,
28+ } ) ) ;
29+
2530// Helper: valid base options for each type
2631const validAgentOptionsByo : AddAgentOptions = {
2732 name : 'TestAgent' ,
@@ -238,19 +243,48 @@ describe('validate', () => {
238243 } ) ;
239244
240245 describe ( 'validateAddGatewayTargetOptions' , ( ) => {
246+ beforeEach ( ( ) => {
247+ // By default, mock that the gateway from validGatewayTargetOptions exists
248+ mockGetExistingGateways . mockResolvedValue ( [ 'my-gateway' ] ) ;
249+ } ) ;
250+
241251 // AC15: Required fields validated
242- it ( 'returns error for missing required fields' , async ( ) => {
243- const requiredFields : { field : keyof AddGatewayTargetOptions ; error : string } [ ] = [
244- { field : 'name' , error : '--name is required' } ,
245- { field : 'language' , error : '--language is required' } ,
246- ] ;
252+ it ( 'returns error for missing name' , async ( ) => {
253+ const opts = { ...validGatewayTargetOptions , name : undefined } ;
254+ const result = await validateAddGatewayTargetOptions ( opts ) ;
255+ expect ( result . valid ) . toBe ( false ) ;
256+ expect ( result . error ) . toBe ( '--name is required' ) ;
257+ } ) ;
247258
248- for ( const { field, error } of requiredFields ) {
249- const opts = { ...validGatewayTargetOptions , [ field ] : undefined } ;
250- const result = await validateAddGatewayTargetOptions ( opts ) ;
251- expect ( result . valid , `Should fail for missing ${ String ( field ) } ` ) . toBe ( false ) ;
252- expect ( result . error ) . toBe ( error ) ;
253- }
259+ it ( 'returns error for missing language (non-existing-endpoint)' , async ( ) => {
260+ const opts = { ...validGatewayTargetOptions , language : undefined } ;
261+ const result = await validateAddGatewayTargetOptions ( opts ) ;
262+ expect ( result . valid ) . toBe ( false ) ;
263+ expect ( result . error ) . toBe ( '--language is required' ) ;
264+ } ) ;
265+
266+ // Gateway is required
267+ it ( 'returns error when --gateway is missing' , async ( ) => {
268+ const opts = { ...validGatewayTargetOptions , gateway : undefined } ;
269+ const result = await validateAddGatewayTargetOptions ( opts ) ;
270+ expect ( result . valid ) . toBe ( false ) ;
271+ expect ( result . error ) . toContain ( '--gateway is required' ) ;
272+ } ) ;
273+
274+ it ( 'returns error when no gateways exist' , async ( ) => {
275+ mockGetExistingGateways . mockResolvedValue ( [ ] ) ;
276+ const result = await validateAddGatewayTargetOptions ( validGatewayTargetOptions ) ;
277+ expect ( result . valid ) . toBe ( false ) ;
278+ expect ( result . error ) . toContain ( 'No gateways found' ) ;
279+ expect ( result . error ) . toContain ( 'agentcore add gateway' ) ;
280+ } ) ;
281+
282+ it ( 'returns error when specified gateway does not exist' , async ( ) => {
283+ mockGetExistingGateways . mockResolvedValue ( [ 'other-gateway' ] ) ;
284+ const result = await validateAddGatewayTargetOptions ( validGatewayTargetOptions ) ;
285+ expect ( result . valid ) . toBe ( false ) ;
286+ expect ( result . error ) . toContain ( 'Gateway "my-gateway" not found' ) ;
287+ expect ( result . error ) . toContain ( 'other-gateway' ) ;
254288 } ) ;
255289
256290 // AC16: Invalid values rejected
@@ -274,6 +308,7 @@ describe('validate', () => {
274308 name : 'test-tool' ,
275309 source : 'existing-endpoint' ,
276310 endpoint : 'https://example.com/mcp' ,
311+ gateway : 'my-gateway' ,
277312 } ;
278313 const result = await validateAddGatewayTargetOptions ( options ) ;
279314 expect ( result . valid ) . toBe ( true ) ;
@@ -285,6 +320,7 @@ describe('validate', () => {
285320 name : 'test-tool' ,
286321 source : 'existing-endpoint' ,
287322 endpoint : 'http://localhost:3000/mcp' ,
323+ gateway : 'my-gateway' ,
288324 } ;
289325 const result = await validateAddGatewayTargetOptions ( options ) ;
290326 expect ( result . valid ) . toBe ( true ) ;
@@ -294,6 +330,7 @@ describe('validate', () => {
294330 const options : AddGatewayTargetOptions = {
295331 name : 'test-tool' ,
296332 source : 'existing-endpoint' ,
333+ gateway : 'my-gateway' ,
297334 } ;
298335 const result = await validateAddGatewayTargetOptions ( options ) ;
299336 expect ( result . valid ) . toBe ( false ) ;
@@ -305,6 +342,7 @@ describe('validate', () => {
305342 name : 'test-tool' ,
306343 source : 'existing-endpoint' ,
307344 endpoint : 'ftp://example.com/mcp' ,
345+ gateway : 'my-gateway' ,
308346 } ;
309347 const result = await validateAddGatewayTargetOptions ( options ) ;
310348 expect ( result . valid ) . toBe ( false ) ;
@@ -316,6 +354,7 @@ describe('validate', () => {
316354 name : 'test-tool' ,
317355 source : 'existing-endpoint' ,
318356 endpoint : 'not-a-url' ,
357+ gateway : 'my-gateway' ,
319358 } ;
320359 const result = await validateAddGatewayTargetOptions ( options ) ;
321360 expect ( result . valid ) . toBe ( false ) ;
@@ -331,6 +370,7 @@ describe('validate', () => {
331370 const options : AddGatewayTargetOptions = {
332371 name : 'test-tool' ,
333372 language : 'Python' ,
373+ gateway : 'my-gateway' ,
334374 outboundAuthType : 'API_KEY' ,
335375 credentialName : 'missing-cred' ,
336376 } ;
@@ -347,6 +387,7 @@ describe('validate', () => {
347387 const options : AddGatewayTargetOptions = {
348388 name : 'test-tool' ,
349389 language : 'Python' ,
390+ gateway : 'my-gateway' ,
350391 outboundAuthType : 'API_KEY' ,
351392 credentialName : 'any-cred' ,
352393 } ;
@@ -363,6 +404,7 @@ describe('validate', () => {
363404 const options : AddGatewayTargetOptions = {
364405 name : 'test-tool' ,
365406 language : 'Python' ,
407+ gateway : 'my-gateway' ,
366408 outboundAuthType : 'API_KEY' ,
367409 credentialName : 'valid-cred' ,
368410 } ;
@@ -429,6 +471,7 @@ describe('validate', () => {
429471 source : 'existing-endpoint' ,
430472 endpoint : 'https://example.com/mcp' ,
431473 host : 'Lambda' ,
474+ gateway : 'my-gateway' ,
432475 } ;
433476 const result = await validateAddGatewayTargetOptions ( options ) ;
434477 expect ( result . valid ) . toBe ( false ) ;
0 commit comments