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
34 changes: 10 additions & 24 deletions src/assets/__tests__/assets.snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,22 @@ describe('Assets Directory Snapshots', () => {
});
});

describe('Static assets', () => {
describe.skipIf(assetFiles.filter(f => f.startsWith('static/')).length === 0)('Static assets', () => {
const staticFiles = assetFiles.filter(f => f.startsWith('static/'));

if (staticFiles.length > 0) {
it.each(staticFiles)('static/%s should match snapshot', file => {
const content = readFileContent(path.join(ASSETS_DIR, file));
expect(content).toMatchSnapshot();
});
} else {
it('static directory is empty or does not exist', () => {
// Static assets may not exist
expect(true).toBe(true);
});
}
it.each(staticFiles)('static/%s should match snapshot', file => {
const content = readFileContent(path.join(ASSETS_DIR, file));
expect(content).toMatchSnapshot();
});
});

describe('TypeScript assets', () => {
describe.skipIf(assetFiles.filter(f => f.startsWith('typescript/')).length === 0)('TypeScript assets', () => {
const tsFiles = assetFiles.filter(f => f.startsWith('typescript/'));

if (tsFiles.length > 0) {
it.each(tsFiles)('typescript/%s should match snapshot', file => {
const content = readFileContent(path.join(ASSETS_DIR, file));
expect(content).toMatchSnapshot();
});
} else {
it('typescript directory is empty or contains only placeholder files', () => {
// TypeScript assets may not exist yet
expect(true).toBe(true);
});
}
it.each(tsFiles)('typescript/%s should match snapshot', file => {
const content = readFileContent(path.join(ASSETS_DIR, file));
expect(content).toMatchSnapshot();
});
});

describe('Root-level assets', () => {
Expand Down
43 changes: 43 additions & 0 deletions src/cli/commands/dev/browser-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from '../../operations/dev/web-ui';
import type { HarnessInfo } from '../../operations/dev/web-ui/constants';
import { listMemoryRecords, retrieveMemoryRecords } from '../../operations/memory';
import { loadDeployedProjectConfig, resolveAgentOrHarness } from '../../operations/resolve-agent';
import { fetchTraceRecords, listTraces } from '../../operations/traces';
import { LayoutProvider } from '../../tui/context';
import { runCliDeploy } from '../deploy/progress';
import { render } from 'ink';
Expand Down Expand Up @@ -246,6 +248,47 @@ export async function runBrowserMode(opts: BrowserModeOptions): Promise<void> {
? (agentNameParam, startTime, endTime) => collector.listTraces(agentNameParam, startTime, endTime)
: undefined,
onGetTrace: collector ? (agentNameParam, traceId) => collector.getTraceSpans(agentNameParam, traceId) : undefined,
onListCloudWatchTraces: async (agentName, harnessName, startTime, endTime) => {
try {
const configIO = new ConfigIO({ baseDir });
const context = await loadDeployedProjectConfig(configIO);
const resolved = await resolveAgentOrHarness(context, { runtime: agentName, harness: harnessName });
if (!resolved.success) return { success: false, error: resolved.error };
return listTraces({
region: resolved.agent.region,
runtimeId: resolved.agent.runtimeId,
agentName: resolved.agent.agentName,
startTime,
endTime,
});
} catch (err) {
return {
success: false,
error: `Failed to list CloudWatch traces: ${err instanceof Error ? err.message : String(err)}`,
};
}
},
onGetCloudWatchTrace: async (agentName, harnessName, traceId, startTime, endTime) => {
try {
const configIO = new ConfigIO({ baseDir });
const context = await loadDeployedProjectConfig(configIO);
const resolved = await resolveAgentOrHarness(context, { runtime: agentName, harness: harnessName });
if (!resolved.success) return { success: false, error: resolved.error };
return fetchTraceRecords({
region: resolved.agent.region,
runtimeId: resolved.agent.runtimeId,
traceId,
startTime,
endTime,
includeSpans: true,
});
} catch (err) {
return {
success: false,
error: `Failed to get CloudWatch trace: ${err instanceof Error ? err.message : String(err)}`,
};
}
},
onListMemoryRecords: async (memoryName, namespace, strategyId) => {
const deployed = await resolveDeployedHandlers(baseDir, onLog);
if (!deployed.onListMemoryRecords) return { success: false, error: 'No deployed AgentCore Memory found' };
Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/import/__tests__/import-gateway-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,13 @@ describe('handleImportGateway', () => {
// ── Name validation ─────────────────────────────────────────────────────

describe('Name validation', () => {
it('rejects invalid name starting with a number', async () => {
mockGetGatewayDetail.mockResolvedValue(makeGatewayDetail({ name: '123gateway' }));
it('rejects invalid name with special characters', async () => {
mockGetGatewayDetail.mockResolvedValue(makeGatewayDetail({ name: 'gateway_with_underscores!' }));

const result = await handleImportGateway({ arn: GATEWAY_ARN });

expect(result.success).toBe(false);
expect(result.error).toContain('Invalid name');
expect(result.error).toContain('must start with a letter');
expect(mockConfigIOInstance.writeProjectSpec).not.toHaveBeenCalled();
});

Expand Down
8 changes: 5 additions & 3 deletions src/cli/commands/import/import-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
GatewayPolicyEngineConfiguration,
OutboundAuth,
} from '../../../schema';
import { GatewayNameSchema } from '../../../schema';
import type { GatewayDetail, GatewayTargetDetail } from '../../aws/agentcore-control';
import {
getGatewayDetail,
Expand All @@ -17,7 +18,7 @@ import {
listAllGateways,
} from '../../aws/agentcore-control';
import { isAccessDeniedError } from '../../errors';
import { ANSI, NAME_REGEX } from './constants';
import { ANSI } from './constants';
import { executeCdkImportPipeline } from './import-pipeline';
import {
failResult,
Expand Down Expand Up @@ -425,10 +426,11 @@ export async function handleImportGateway(options: ImportResourceOptions): Promi
// 4. Validate name
logger.startStep('Validate name');
let localName = options.name ?? gatewayDetail.name;
if (!NAME_REGEX.test(localName)) {
const nameResult = GatewayNameSchema.safeParse(localName);
if (!nameResult.success) {
return failResult(
logger,
`Invalid name "${localName}". Name must start with a letter and contain only letters, numbers, and underscores (max 48 chars).`,
`Invalid name "${localName}". ${nameResult.error.issues[0]?.message ?? 'Invalid gateway name'}`,
'gateway',
localName
);
Expand Down
233 changes: 233 additions & 0 deletions src/cli/operations/dev/web-ui/__tests__/cloudwatch-traces.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { handleGetCloudWatchTrace, handleListCloudWatchTraces } from '../handlers/cloudwatch-traces.js';
import type { RouteContext } from '../handlers/route-context.js';
import type { IncomingMessage, ServerResponse } from 'http';
import { beforeEach, describe, expect, it, vi } from 'vitest';

function mockRes(): ServerResponse & { _status: number; _headers: Record<string, string>; _body: string } {
const res = {
_status: 0,
_headers: {} as Record<string, string>,
_body: '',
writeHead(status: number, headers?: Record<string, string>) {
res._status = status;
if (headers) Object.assign(res._headers, headers);
return res;
},
setHeader(name: string, value: string) {
res._headers[name] = value;
},
end(body?: string) {
if (body) res._body = body;
},
};
return res as unknown as ServerResponse & { _status: number; _headers: Record<string, string>; _body: string };
}

function mockReq(url: string): IncomingMessage {
return { url, headers: { host: 'localhost:8081' } } as unknown as IncomingMessage;
}

function mockCtx(overrides: Partial<RouteContext['options']> = {}): RouteContext {
return {
options: {
mode: 'dev',
agents: [],
harnesses: [],
uiPort: 8081,
...overrides,
},
runningAgents: new Map(),
startingAgents: new Map(),
agentErrors: new Map(),
setCorsHeaders: vi.fn(),
readBody: vi.fn(),
} satisfies RouteContext;
}

describe('handleListCloudWatchTraces', () => {
beforeEach(() => {
vi.restoreAllMocks();
});

it('returns 404 when no handler configured', async () => {
const ctx = mockCtx();
const req = mockReq('/api/cloudwatch-traces?agentName=my-agent');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(404);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('not available');
});

it('returns 400 when neither agentName nor harnessName provided', async () => {
const handler = vi.fn();
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(400);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('agentName');
expect(body.error).toContain('harnessName');
expect(handler).not.toHaveBeenCalled();
});

it('returns 400 when both agentName and harnessName provided', async () => {
const handler = vi.fn();
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces?agentName=a&harnessName=h');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(400);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('agentName');
expect(body.error).toContain('harnessName');
expect(handler).not.toHaveBeenCalled();
});

it('calls handler with agentName and returns traces', async () => {
const traces = [{ traceId: 't1' }, { traceId: 't2' }];
const handler = vi.fn().mockResolvedValue({ success: true, traces });
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces?agentName=my-agent');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(200);
expect(handler).toHaveBeenCalledWith('my-agent', undefined, undefined, undefined);
const body = JSON.parse(res._body);
expect(body.success).toBe(true);
expect(body.traces).toEqual(traces);
});

it('calls handler with harnessName', async () => {
const handler = vi.fn().mockResolvedValue({ success: true, traces: [] });
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces?harnessName=my-harness');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(200);
expect(handler).toHaveBeenCalledWith(undefined, 'my-harness', undefined, undefined);
});

it('returns 500 when handler throws', async () => {
const handler = vi.fn().mockRejectedValue(new Error('boom'));
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces?agentName=my-agent');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(500);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('Failed to list CloudWatch traces');
});

it('returns 400 for invalid startTime', async () => {
const handler = vi.fn();
const ctx = mockCtx({ onListCloudWatchTraces: handler });
const req = mockReq('/api/cloudwatch-traces?agentName=my-agent&startTime=notanumber');
const res = mockRes();

await handleListCloudWatchTraces(ctx, req, res);

expect(res._status).toBe(400);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('startTime');
expect(handler).not.toHaveBeenCalled();
});
});

describe('handleGetCloudWatchTrace', () => {
beforeEach(() => {
vi.restoreAllMocks();
});

it('returns 404 when no handler configured', async () => {
const ctx = mockCtx();
const req = mockReq('/api/cloudwatch-traces/abc123?agentName=my-agent');
const res = mockRes();

await handleGetCloudWatchTrace(ctx, req, res);

expect(res._status).toBe(404);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('not available');
});

it('returns 400 when traceId is missing', async () => {
const handler = vi.fn();
const ctx = mockCtx({ onGetCloudWatchTrace: handler });
const req = mockReq('/api/cloudwatch-traces/?agentName=my-agent');
const res = mockRes();

await handleGetCloudWatchTrace(ctx, req, res);

expect(res._status).toBe(400);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('traceId');
expect(handler).not.toHaveBeenCalled();
});

it('returns 400 when neither agentName nor harnessName provided', async () => {
const handler = vi.fn();
const ctx = mockCtx({ onGetCloudWatchTrace: handler });
const req = mockReq('/api/cloudwatch-traces/abc123');
const res = mockRes();

await handleGetCloudWatchTrace(ctx, req, res);

expect(res._status).toBe(400);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('agentName');
expect(body.error).toContain('harnessName');
expect(handler).not.toHaveBeenCalled();
});

it('returns 500 when handler throws', async () => {
const handler = vi.fn().mockRejectedValue(new Error('boom'));
const ctx = mockCtx({ onGetCloudWatchTrace: handler });
const req = mockReq('/api/cloudwatch-traces/abc123?agentName=my-agent');
const res = mockRes();

await handleGetCloudWatchTrace(ctx, req, res);

expect(res._status).toBe(500);
const body = JSON.parse(res._body);
expect(body.success).toBe(false);
expect(body.error).toContain('Failed to get CloudWatch trace');
});

it('calls handler and returns records', async () => {
const records = [{ record: 'data1' }];
const handler = vi.fn().mockResolvedValue({ success: true, records });
const ctx = mockCtx({ onGetCloudWatchTrace: handler });
const req = mockReq('/api/cloudwatch-traces/abc123?agentName=my-agent');
const res = mockRes();

await handleGetCloudWatchTrace(ctx, req, res);

expect(res._status).toBe(200);
expect(handler).toHaveBeenCalledWith('my-agent', undefined, 'abc123', undefined, undefined);
const body = JSON.parse(res._body);
expect(body.success).toBe(true);
expect(body.records).toEqual(records);
});
});
Loading