@@ -20,14 +20,6 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
2020const hasAws = hasAwsCredentials ( ) ;
2121const baseCanRun = prereqs . npm && prereqs . git && prereqs . uv && hasAws ;
2222
23- /**
24- * Run the globally installed `agentcore` CLI.
25- * E2E tests use the packaged binary to validate the published artifact.
26- */
27- async function runAgentCore ( args : string [ ] , cwd : string ) : Promise < RunResult > {
28- return spawnAndCollect ( 'agentcore' , args , cwd ) ;
29- }
30-
3123interface E2EConfig {
3224 framework : string ;
3325 modelProvider : string ;
@@ -76,52 +68,19 @@ export function createE2ESuite(cfg: E2EConfig) {
7668 createArgs . push ( '--api-key' , apiKey ) ;
7769 }
7870
79- const result = await runAgentCore ( createArgs , testDir ) ;
71+ const result = await runAgentCoreCLI ( createArgs , testDir ) ;
8072
8173 expect ( result . exitCode , `Create failed: ${ result . stderr } ` ) . toBe ( 0 ) ;
8274 const json = parseJsonOutput ( result . stdout ) as { projectPath : string } ;
8375 projectPath = json . projectPath ;
8476
85- // TODO: Replace with `agentcore add target` once the CLI command is re-introduced
86- const account =
87- process . env . AWS_ACCOUNT_ID ??
88- execSync ( 'aws sts get-caller-identity --query Account --output text' ) . toString ( ) . trim ( ) ;
89- const region = process . env . AWS_REGION ?? 'us-east-1' ;
90- const awsTargetsPath = join ( projectPath , 'agentcore' , 'aws-targets.json' ) ;
91- await writeFile ( awsTargetsPath , JSON . stringify ( [ { name : 'default' , account, region } ] ) ) ;
92-
93- // Override @aws /agentcore-cdk with a local tarball if provided (for cross-package testing)
94- if ( process . env . CDK_TARBALL ) {
95- execSync ( `npm install -f ${ process . env . CDK_TARBALL } ` , {
96- cwd : join ( projectPath , 'agentcore' , 'cdk' ) ,
97- stdio : 'pipe' ,
98- } ) ;
99- }
77+ await writeAwsTargets ( projectPath ) ;
78+ installCdkTarball ( projectPath ) ;
10079 } , 300000 ) ;
10180
10281 afterAll ( async ( ) => {
10382 if ( projectPath && hasAws ) {
104- await runAgentCore ( [ 'remove' , 'all' , '--json' ] , projectPath ) ;
105- const result = await runAgentCore ( [ 'deploy' , '--yes' , '--json' ] , projectPath ) ;
106-
107- if ( result . exitCode !== 0 ) {
108- console . log ( 'Teardown stdout:' , result . stdout ) ;
109- console . log ( 'Teardown stderr:' , result . stderr ) ;
110- }
111-
112- // Delete the API key credential provider from the account.
113- // These are created as a pre-deploy step outside CDK and are not
114- // cleaned up by stack teardown, so we must delete them explicitly.
115- if ( cfg . modelProvider !== 'Bedrock' && agentName ) {
116- const providerName = `${ agentName } ${ cfg . modelProvider } ` ;
117- const region = process . env . AWS_REGION ?? 'us-east-1' ;
118- try {
119- const client = new BedrockAgentCoreControlClient ( { region } ) ;
120- await client . send ( new DeleteApiKeyCredentialProviderCommand ( { name : providerName } ) ) ;
121- } catch {
122- // Best-effort cleanup — don't fail the test if deletion fails
123- }
124- }
83+ await teardownE2EProject ( projectPath , agentName , cfg . modelProvider ) ;
12584 }
12685 if ( testDir ) await rm ( testDir , { recursive : true , force : true , maxRetries : 3 , retryDelay : 1000 } ) ;
12786 } , 600000 ) ;
@@ -138,7 +97,7 @@ export function createE2ESuite(cfg: E2EConfig) {
13897
13998 await retry (
14099 async ( ) => {
141- const result = await runAgentCore ( [ 'deploy' , '--yes' , '--json' ] , projectPath ) ;
100+ const result = await runAgentCoreCLI ( [ 'deploy' , '--yes' , '--json' ] , projectPath ) ;
142101
143102 if ( result . exitCode !== 0 ) {
144103 console . log ( 'Deploy stdout:' , result . stdout ) ;
@@ -165,7 +124,7 @@ export function createE2ESuite(cfg: E2EConfig) {
165124 // Retry invoke to handle cold-start / runtime initialization delays
166125 await retry (
167126 async ( ) => {
168- const result = await runAgentCore (
127+ const result = await runAgentCoreCLI (
169128 [ 'invoke' , '--prompt' , 'Say hello' , '--agent' , agentName , '--json' ] ,
170129 projectPath
171130 ) ;
@@ -304,3 +263,49 @@ export function createE2ESuite(cfg: E2EConfig) {
304263 ) ;
305264 } ) ;
306265}
266+
267+ export { hasAws , baseCanRun } ;
268+
269+ export function runAgentCoreCLI ( args : string [ ] , cwd : string ) : Promise < RunResult > {
270+ return spawnAndCollect ( 'agentcore' , args , cwd ) ;
271+ }
272+
273+ // TODO: Replace with `agentcore add target` once the CLI command is re-introduced
274+ export async function writeAwsTargets ( projectPath : string ) : Promise < void > {
275+ const account =
276+ process . env . AWS_ACCOUNT_ID ??
277+ execSync ( 'aws sts get-caller-identity --query Account --output text' ) . toString ( ) . trim ( ) ;
278+ const region = process . env . AWS_REGION ?? 'us-east-1' ;
279+ await writeFile (
280+ join ( projectPath , 'agentcore' , 'aws-targets.json' ) ,
281+ JSON . stringify ( [ { name : 'default' , account, region } ] )
282+ ) ;
283+ }
284+
285+ export function installCdkTarball ( projectPath : string ) : void {
286+ if ( process . env . CDK_TARBALL ) {
287+ execSync ( `npm install -f ${ process . env . CDK_TARBALL } ` , {
288+ cwd : join ( projectPath , 'agentcore' , 'cdk' ) ,
289+ stdio : 'pipe' ,
290+ } ) ;
291+ }
292+ }
293+
294+ export async function teardownE2EProject ( projectPath : string , agentName : string , modelProvider : string ) : Promise < void > {
295+ await spawnAndCollect ( 'agentcore' , [ 'remove' , 'all' , '--json' ] , projectPath ) ;
296+ const result = await spawnAndCollect ( 'agentcore' , [ 'deploy' , '--yes' , '--json' ] , projectPath ) ;
297+ if ( result . exitCode !== 0 ) {
298+ console . log ( 'Teardown stdout:' , result . stdout ) ;
299+ console . log ( 'Teardown stderr:' , result . stderr ) ;
300+ }
301+ if ( modelProvider !== 'Bedrock' && agentName ) {
302+ const providerName = `${ agentName } ${ modelProvider } ` ;
303+ const region = process . env . AWS_REGION ?? 'us-east-1' ;
304+ try {
305+ const client = new BedrockAgentCoreControlClient ( { region } ) ;
306+ await client . send ( new DeleteApiKeyCredentialProviderCommand ( { name : providerName } ) ) ;
307+ } catch {
308+ // Best-effort cleanup
309+ }
310+ }
311+ }
0 commit comments