|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import type { GetTraceQuery, ListTracesQuery, LogSource } from "../../../core/observability/index"; |
| 6 | +import type { ProjectBackend, ResolveDeployedResourcesBackendInput } from "../../../core/project"; |
| 7 | +import { ProjectSpecSchema } from "../../../projectSchemas/project"; |
| 8 | +import { |
| 9 | + createSilentLogger, |
| 10 | + TestCoreClient, |
| 11 | + TestGlobalConfigAccessor, |
| 12 | + testIO, |
| 13 | +} from "../../../testing"; |
| 14 | +import { createRootHandler } from "../../index"; |
| 15 | + |
| 16 | +const originalCwd = process.cwd(); |
| 17 | +const temporaryDirectories: string[] = []; |
| 18 | +const DEFAULT_TARGET = { |
| 19 | + name: "default", |
| 20 | + account: "111122223333", |
| 21 | + region: "eu-west-1", |
| 22 | +} as const; |
| 23 | +const PRODUCTION_TARGET = { |
| 24 | + name: "production", |
| 25 | + account: "111122223333", |
| 26 | + region: "ap-southeast-2", |
| 27 | +} as const; |
| 28 | +const TARGET_CREDENTIAL_PROVIDER = async () => ({ |
| 29 | + accessKeyId: "target-access-key", |
| 30 | + secretAccessKey: "target-secret-key", |
| 31 | +}); |
| 32 | +const RUNTIMES = [ |
| 33 | + { |
| 34 | + name: "checkout", |
| 35 | + build: "CodeZip", |
| 36 | + entrypoint: "main.py", |
| 37 | + codeLocation: "app/checkout", |
| 38 | + runtimeVersion: "PYTHON_3_14", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "inventory", |
| 42 | + build: "CodeZip", |
| 43 | + entrypoint: "main.py", |
| 44 | + codeLocation: "app/inventory", |
| 45 | + runtimeVersion: "PYTHON_3_14", |
| 46 | + }, |
| 47 | +] as const; |
| 48 | + |
| 49 | +afterEach(async () => { |
| 50 | + process.chdir(originalCwd); |
| 51 | + await Promise.all( |
| 52 | + temporaryDirectories |
| 53 | + .splice(0) |
| 54 | + .map((directory) => rm(directory, { recursive: true, force: true })), |
| 55 | + ); |
| 56 | +}); |
| 57 | + |
| 58 | +async function inProject( |
| 59 | + runtimes: readonly unknown[], |
| 60 | + targets = [DEFAULT_TARGET, PRODUCTION_TARGET], |
| 61 | +) { |
| 62 | + const root = await mkdtemp(join(tmpdir(), "agentcore-project-traces-")); |
| 63 | + temporaryDirectories.push(root); |
| 64 | + await mkdir(join(root, "agentcore"), { recursive: true }); |
| 65 | + const spec = ProjectSpecSchema.parse({ |
| 66 | + name: "orders", |
| 67 | + version: 1, |
| 68 | + runtimes, |
| 69 | + }); |
| 70 | + await writeFile(join(root, "agentcore", "agentcore.json"), JSON.stringify(spec)); |
| 71 | + await writeFile(join(root, "agentcore", "aws-targets.json"), JSON.stringify(targets)); |
| 72 | + process.chdir(root); |
| 73 | + return root; |
| 74 | +} |
| 75 | + |
| 76 | +function backend() { |
| 77 | + const calls: ResolveDeployedResourcesBackendInput[] = []; |
| 78 | + const value: ProjectBackend = { |
| 79 | + async *build() {}, |
| 80 | + async *deploy() { |
| 81 | + yield* []; |
| 82 | + return { outputs: {} }; |
| 83 | + }, |
| 84 | + async resolveDeployedResources(project, input) { |
| 85 | + calls.push(input); |
| 86 | + return project.spec.runtimes.map(({ name }) => ({ |
| 87 | + resourceType: "runtime" as const, |
| 88 | + name, |
| 89 | + id: `${name}-AbCdEf1234`, |
| 90 | + target: input.target, |
| 91 | + credentialProvider: TARGET_CREDENTIAL_PROVIDER, |
| 92 | + })); |
| 93 | + }, |
| 94 | + async resolveProjectResources() { |
| 95 | + throw new Error("project traces resolve deployed resources, not project resources"); |
| 96 | + }, |
| 97 | + }; |
| 98 | + return { calls, value }; |
| 99 | +} |
| 100 | + |
| 101 | +function command(projectBackend: ProjectBackend) { |
| 102 | + const core = new TestCoreClient({ backends: { CDK: projectBackend } }); |
| 103 | + const io = testIO(); |
| 104 | + const root = createRootHandler(core, { |
| 105 | + io: io.io, |
| 106 | + logger: createSilentLogger(), |
| 107 | + globalConfigAccessor: new TestGlobalConfigAccessor(), |
| 108 | + }); |
| 109 | + return { |
| 110 | + core, |
| 111 | + io, |
| 112 | + run: (args: string[]) => |
| 113 | + root.route([ |
| 114 | + "bun", |
| 115 | + "agentcore", |
| 116 | + "project", |
| 117 | + "traces", |
| 118 | + "runtime", |
| 119 | + ...args, |
| 120 | + "--region", |
| 121 | + "us-east-1", |
| 122 | + ]), |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +describe("project traces runtime", () => { |
| 127 | + test("resolves the only logical Runtime and lists traces in the target region", async () => { |
| 128 | + await inProject([RUNTIMES[0]]); |
| 129 | + const resolved = backend(); |
| 130 | + const subject = command(resolved.value); |
| 131 | + subject.core.observability.traceSummaries = [ |
| 132 | + { |
| 133 | + traceId: "abc123", |
| 134 | + timestamp: "1709391000000", |
| 135 | + sessionId: "session-1", |
| 136 | + }, |
| 137 | + ]; |
| 138 | + |
| 139 | + await subject.run(["list", "--since", "1h"]); |
| 140 | + |
| 141 | + expect(resolved.calls).toEqual([{ target: DEFAULT_TARGET }]); |
| 142 | + const call = subject.core.observability.calls[0]!; |
| 143 | + expect(call.method).toBe("listTraces"); |
| 144 | + expect(call.args[0] as LogSource).toEqual({ |
| 145 | + logGroupName: "/aws/bedrock-agentcore/runtimes/checkout-AbCdEf1234-DEFAULT", |
| 146 | + }); |
| 147 | + expect(call.args[1] as ListTracesQuery).toMatchObject({ limit: 20 }); |
| 148 | + expect(call.args[2]).toEqual({ |
| 149 | + region: DEFAULT_TARGET.region, |
| 150 | + endpointUrl: undefined, |
| 151 | + credentials: TARGET_CREDENTIAL_PROVIDER, |
| 152 | + }); |
| 153 | + expect(subject.io.stdout()).toContain("abc123"); |
| 154 | + }); |
| 155 | + |
| 156 | + test("selects a named Runtime, deployment target, and endpoint qualifier", async () => { |
| 157 | + await inProject(RUNTIMES); |
| 158 | + const resolved = backend(); |
| 159 | + const subject = command(resolved.value); |
| 160 | + |
| 161 | + await subject.run([ |
| 162 | + "list", |
| 163 | + "--name", |
| 164 | + "inventory", |
| 165 | + "--target", |
| 166 | + "production", |
| 167 | + "--qualifier", |
| 168 | + "BLUE", |
| 169 | + "--since", |
| 170 | + "1h", |
| 171 | + "--limit", |
| 172 | + "5", |
| 173 | + ]); |
| 174 | + |
| 175 | + expect(resolved.calls).toEqual([{ target: PRODUCTION_TARGET }]); |
| 176 | + const call = subject.core.observability.calls[0]!; |
| 177 | + expect(call.args[0]).toEqual({ |
| 178 | + logGroupName: "/aws/bedrock-agentcore/runtimes/inventory-AbCdEf1234-BLUE", |
| 179 | + }); |
| 180 | + expect(call.args[1] as ListTracesQuery).toMatchObject({ limit: 5 }); |
| 181 | + expect(call.args[2]).toEqual({ |
| 182 | + region: PRODUCTION_TARGET.region, |
| 183 | + endpointUrl: undefined, |
| 184 | + credentials: TARGET_CREDENTIAL_PROVIDER, |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + test("downloads a trace from the resolved Runtime", async () => { |
| 189 | + await inProject([RUNTIMES[0]]); |
| 190 | + const resolved = backend(); |
| 191 | + const subject = command(resolved.value); |
| 192 | + subject.core.observability.traceRecords = [ |
| 193 | + { "@timestamp": "2026-09-10 12:00:00.000", "@message": { body: "hello" } }, |
| 194 | + ]; |
| 195 | + |
| 196 | + await subject.run(["get", "abc123def456", "--output", "traces/trace.json"]); |
| 197 | + |
| 198 | + const call = subject.core.observability.calls[0]!; |
| 199 | + expect(call.method).toBe("getTrace"); |
| 200 | + expect(call.args[0]).toEqual({ |
| 201 | + logGroupName: "/aws/bedrock-agentcore/runtimes/checkout-AbCdEf1234-DEFAULT", |
| 202 | + }); |
| 203 | + expect(call.args[1] as GetTraceQuery).toMatchObject({ traceId: "abc123def456" }); |
| 204 | + expect(call.args[2]).toEqual({ |
| 205 | + region: DEFAULT_TARGET.region, |
| 206 | + endpointUrl: undefined, |
| 207 | + credentials: TARGET_CREDENTIAL_PROVIDER, |
| 208 | + }); |
| 209 | + |
| 210 | + const output = join(process.cwd(), "traces", "trace.json"); |
| 211 | + expect(subject.io.stdout()).toBe(output); |
| 212 | + expect(JSON.parse(await readFile(output, "utf8"))).toEqual( |
| 213 | + subject.core.observability.traceRecords, |
| 214 | + ); |
| 215 | + }); |
| 216 | +}); |
0 commit comments