@@ -13,6 +13,7 @@ import { ResourceType } from '../../../src/models/resource-types.js';
1313import { ApimServiceContext , ResourceDescriptor } from '../../../src/models/types.js' ;
1414import { PublishConfig } from '../../../src/models/config.js' ;
1515import { LogLevel } from '../../../src/lib/logger.js' ;
16+ import { buildEnvMapping } from '../../../src/services/env-mapper.js' ;
1617
1718function createMockClient ( apimResources : Map < ResourceType , Record < string , unknown > [ ] > = new Map ( ) ) {
1819 return {
@@ -277,6 +278,63 @@ describe('delete-unmatched-service', () => {
277278 expect ( result . filter ( ( d ) => d . type === ResourceType . GatewayApi ) ) . toHaveLength ( 0 ) ;
278279 } ) ;
279280
281+ it ( 'should list custom gateway APIs using the deployed parent name' , async ( ) => {
282+ const localDescriptors : ResourceDescriptor [ ] = [
283+ { type : ResourceType . GatewayApi , nameParts : [ 'custom-gateway' ] } ,
284+ ] ;
285+ const listedParents : Array < ResourceDescriptor | undefined > = [ ] ;
286+ const client = createMockClient ( ) ;
287+ client . listResources = async function * ( _ctx : ApimServiceContext , type : ResourceType , parent ?: ResourceDescriptor ) {
288+ if ( type === ResourceType . GatewayApi ) {
289+ listedParents . push ( parent ) ;
290+ if ( parent ?. nameParts [ 0 ] === 'dev-custom-gateway' ) {
291+ yield { name : 'dev-api-stale' } ;
292+ }
293+ }
294+ } ;
295+ const store = createMockStore ( localDescriptors ) ;
296+ store . readAssociation = vi . fn ( ) . mockResolvedValue ( [ ] ) ;
297+ const envMapping = buildEnvMapping ( {
298+ namePrefix : 'dev-' ,
299+ appliesTo : [ ResourceType . Gateway , ResourceType . Api ] ,
300+ } ) ;
301+
302+ const result = await computeDeleteActions ( client , store , testContext , { ...testConfig , envMapping } ) ;
303+
304+ expect ( listedParents ) . toContainEqual ( {
305+ type : ResourceType . Gateway ,
306+ nameParts : [ 'dev-custom-gateway' ] ,
307+ } ) ;
308+ expect ( result ) . toContainEqual ( {
309+ type : ResourceType . GatewayApi ,
310+ nameParts : [ 'dev-custom-gateway' , 'dev-api-stale' ] ,
311+ } ) ;
312+ } ) ;
313+
314+ it ( 'should delete all in-scope managed gateway APIs for an empty desired set without touching other environments' , async ( ) => {
315+ const apimResources = new Map < ResourceType , Record < string , unknown > [ ] > ( [
316+ [ ResourceType . GatewayApi , [
317+ { name : 'dev-api-one' } ,
318+ { name : 'dev-api-two' } ,
319+ { name : 'prod-api' } ,
320+ ] ] ,
321+ ] ) ;
322+ const localDescriptors : ResourceDescriptor [ ] = [
323+ { type : ResourceType . GatewayApi , nameParts : [ 'managed' ] } ,
324+ ] ;
325+ const client = createMockClient ( apimResources ) ;
326+ const store = createMockStore ( localDescriptors ) ;
327+ store . readAssociation = vi . fn ( ) . mockResolvedValue ( [ ] ) ;
328+ const envMapping = buildEnvMapping ( { namePrefix : 'dev-' } ) ;
329+
330+ const result = await computeDeleteActions ( client , store , testContext , { ...testConfig , envMapping } ) ;
331+
332+ expect ( result . filter ( ( descriptor ) => descriptor . type === ResourceType . GatewayApi ) ) . toEqual ( [
333+ { type : ResourceType . GatewayApi , nameParts : [ 'managed' , 'dev-api-one' ] } ,
334+ { type : ResourceType . GatewayApi , nameParts : [ 'managed' , 'dev-api-two' ] } ,
335+ ] ) ;
336+ } ) ;
337+
280338 it ( 'should not touch any gateway assignments when artifacts track no gateways' , async ( ) => {
281339 const apimResources = new Map < ResourceType , Record < string , unknown > [ ] > ( [
282340 [ ResourceType . GatewayApi , [ { name : 'api-a' } , { name : 'api-b' } ] ] ,
0 commit comments