Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions integ-tests/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { createTelemetryHelper, runCLI } from '../src/test-utils/index.js';
import { randomUUID } from 'node:crypto';
import { mkdir, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';

describe('import command', () => {
let testDir: string;
let projectDir: string;
const telemetry = createTelemetryHelper();

beforeAll(async () => {
testDir = join(tmpdir(), `agentcore-import-telemetry-${randomUUID()}`);
await mkdir(testDir, { recursive: true });

const projectName = 'ImportTelemetryProj';
const result = await runCLI(['create', '--name', projectName, '--no-agent'], testDir);
if (result.exitCode !== 0) {
throw new Error(`Failed to create project: ${result.stdout} ${result.stderr}`);
}
projectDir = join(testDir, projectName);
});

afterEach(() => {
telemetry.clearEntries();
});

afterAll(async () => {
telemetry.destroy();
await rm(testDir, { recursive: true, force: true });
});

describe('import --source', () => {
it('emits failure telemetry for nonexistent source file', async () => {
const result = await runCLI(['import', '--source', '/nonexistent/file.yaml'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import',
exit_reason: 'failure',
});
});
});

describe('import runtime', () => {
it('emits failure telemetry for invalid ARN', async () => {
const result = await runCLI(['import', 'runtime', '--arn', 'invalid-arn'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import.runtime',
exit_reason: 'failure',
});
});
});

describe('import memory', () => {
it('emits failure telemetry for invalid ARN', async () => {
const result = await runCLI(['import', 'memory', '--arn', 'invalid-arn'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import.memory',
exit_reason: 'failure',
});
});
});

describe('import evaluator', () => {
it('emits failure telemetry for invalid ARN', async () => {
const result = await runCLI(['import', 'evaluator', '--arn', 'invalid-arn'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import.evaluator',
exit_reason: 'failure',
});
});
});

describe('import gateway', () => {
it('emits failure telemetry for invalid ARN', async () => {
const result = await runCLI(['import', 'gateway', '--arn', 'invalid-arn'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import.gateway',
exit_reason: 'failure',
});
});
});
Comment thread
Hweinstock marked this conversation as resolved.

describe('import online-eval', () => {
it('emits failure telemetry for invalid ARN', async () => {
const result = await runCLI(['import', 'online-eval', '--arn', 'invalid-arn'], projectDir, {
env: telemetry.env,
});
expect(result.exitCode).toBe(1);
telemetry.assertMetricEmitted({
command: 'import.online-eval',
exit_reason: 'failure',
});
});
});
});
7 changes: 7 additions & 0 deletions src/cli/commands/import/__tests__/import-gateway-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ vi.mock('../../../../lib', () => ({
this.name = 'NoProjectError';
}
},
ValidationError: class ValidationError extends Error {
constructor(msg: string) {
super(msg);
this.name = 'ValidationError';
}
},
toError: (err: unknown) => (err instanceof Error ? err : new Error(String(err))),
findConfigRoot: (...args: unknown[]) => mockFindConfigRoot(...args),
}));

Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/import/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export async function handleImport(options: ImportOptions): Promise<ImportResult
logger.finalize(false);
return {
success: false,
error: new Error(error),
error: new ValidationError(error),
logPath: logger.getRelativeLogPath(),
};
}
Expand All @@ -224,7 +224,7 @@ export async function handleImport(options: ImportOptions): Promise<ImportResult
logger.finalize(false);
return {
success: false,
error: new Error(error),
error: new ValidationError(error),
logPath: logger.getRelativeLogPath(),
};
}
Expand All @@ -238,7 +238,7 @@ export async function handleImport(options: ImportOptions): Promise<ImportResult
logger.finalize(false);
return {
success: false,
error: new Error(error),
error: new ValidationError(error),
logPath: logger.getRelativeLogPath(),
};
}
Expand Down
54 changes: 29 additions & 25 deletions src/cli/commands/import/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ValidationError } from '../../../lib';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { handleImport } from './actions';
import { ANSI } from './constants';
import { registerImportEvaluator } from './import-evaluator';
Expand Down Expand Up @@ -71,34 +73,36 @@ export const registerImport = (program: Command) => {
return;
}

// Validate source file exists
if (!fs.existsSync(cliOptions.source)) {
console.error(`\x1b[31m[error]${reset} Source file not found: ${cliOptions.source}`);
process.exit(1);
}

const warnings: string[] = [];

const result = await handleImport({
source: cliOptions.source,
target: cliOptions.target,
yes: cliOptions.yes,
onProgress: (message: string) => {
// Collect warnings for end-of-output display
if (message.includes('Warning') || message.includes('\x1b[33m')) {
warnings.push(message);
return;
}

// Skipped items shown dimmed
if (message.startsWith('Skipping')) {
console.log(`${dim}[skip]${reset} ${message}`);
return;
}
const result = await withCommandRunTelemetry('import', {}, async () => {
if (!fs.existsSync(cliOptions.source!)) {
return { success: false as const, error: new ValidationError(`Source file not found: ${cliOptions.source}`) };
}

// Normal progress steps shown as [done]
console.log(`${green}[done]${reset} ${message}`);
},
const importResult = await handleImport({
source: cliOptions.source!,
target: cliOptions.target,
yes: cliOptions.yes,
onProgress: (message: string) => {
// Collect warnings for end-of-output display
if (message.includes('Warning') || message.includes('\x1b[33m')) {
warnings.push(message);
return;
}

// Skipped items shown dimmed
if (message.startsWith('Skipping')) {
console.log(`${dim}[skip]${reset} ${message}`);
return;
}

// Normal progress steps shown as [done]
console.log(`${green}[done]${reset} ${message}`);
},
});

return importResult;
});

if (result.success) {
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/import/import-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
listAllEvaluators,
listAllOnlineEvaluationConfigs,
} from '../../aws/agentcore-control';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { ANSI } from './constants';
import { failResult, parseAndValidateArn } from './import-utils';
import { executeResourceImport } from './resource-import';
Expand Down Expand Up @@ -143,7 +144,7 @@ export function registerImportEvaluator(importCmd: Command): void {
.option('--name <name>', 'Local name for the imported evaluator')
.option('-y, --yes', 'Auto-confirm prompts')
.action(async (cliOptions: ImportResourceOptions) => {
const result = await handleImportEvaluator(cliOptions);
const result = await withCommandRunTelemetry('import.evaluator', {}, () => handleImportEvaluator(cliOptions));

if (result.success) {
console.log('');
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/import/import-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
listAllGateways,
} from '../../aws/agentcore-control';
import { isAccessDeniedError } from '../../errors';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { ANSI } from './constants';
import { executeCdkImportPipeline } from './import-pipeline';
import {
Expand Down Expand Up @@ -667,7 +668,7 @@ export function registerImportGateway(importCmd: Command): void {
.description('Import an existing AgentCore Gateway (with targets) from your AWS account')
.option('--arn <gatewayArn>', 'Gateway ARN to import')
.action(async (cliOptions: ImportResourceOptions) => {
const result = await handleImportGateway(cliOptions);
const result = await withCommandRunTelemetry('import.gateway', {}, () => handleImportGateway(cliOptions));

if (result.success) {
console.log('');
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/import/import-memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Memory } from '../../../schema';
import type { MemoryDetail, MemorySummary } from '../../aws/agentcore-control';
import { getMemoryDetail, listAllMemories } from '../../aws/agentcore-control';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { ANSI } from './constants';
import { parseAndValidateArn } from './import-utils';
import { executeResourceImport } from './resource-import';
Expand Down Expand Up @@ -114,7 +115,7 @@ export function registerImportMemory(importCmd: Command): void {
.option('--name <name>', 'Local name for the imported memory')
.option('-y, --yes', 'Auto-confirm prompts')
.action(async (cliOptions: ImportResourceOptions) => {
const result = await handleImportMemory(cliOptions);
const result = await withCommandRunTelemetry('import.memory', {}, () => handleImportMemory(cliOptions));

if (result.success) {
console.log('');
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/import/import-online-eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
listAllOnlineEvaluationConfigs,
} from '../../aws/agentcore-control';
import { arnPrefix } from '../../aws/partition';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { ANSI } from './constants';
import { failResult, findResourceInDeployedState, parseAndValidateArn } from './import-utils';
import { executeResourceImport } from './resource-import';
Expand Down Expand Up @@ -200,7 +201,7 @@ export function registerImportOnlineEval(importCmd: Command): void {
.option('--name <name>', 'Local name for the imported online eval config')
.option('-y, --yes', 'Auto-confirm prompts')
.action(async (cliOptions: ImportResourceOptions) => {
const result = await handleImportOnlineEval(cliOptions);
const result = await withCommandRunTelemetry('import.online-eval', {}, () => handleImportOnlineEval(cliOptions));

if (result.success) {
console.log('');
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/import/import-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AgentEnvSpec } from '../../../schema';
import type { AgentRuntimeDetail, AgentRuntimeSummary } from '../../aws/agentcore-control';
import { getAgentRuntimeDetail, listAllAgentRuntimes } from '../../aws/agentcore-control';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { ANSI } from './constants';
import { copyAgentSource, failResult, parseAndValidateArn } from './import-utils';
import { executeResourceImport } from './resource-import';
Expand Down Expand Up @@ -211,7 +212,7 @@ export function registerImportRuntime(importCmd: Command): void {
.option('--name <name>', 'Local name for the imported runtime')
.option('-y, --yes', 'Auto-confirm prompts')
.action(async (cliOptions: RuntimeImportOptions) => {
const result = await handleImportRuntime(cliOptions);
const result = await withCommandRunTelemetry('import.runtime', {}, () => handleImportRuntime(cliOptions));

if (result.success) {
console.log('');
Expand Down
Loading
Loading