|
1 | 1 | import { afterEach, describe, expect, spyOn, test } from "bun:test"; |
2 | 2 | import * as fs from "node:fs"; |
3 | | -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
4 | 4 | import { dirname, join, resolve } from "node:path"; |
5 | 5 | import { tmpdir } from "node:os"; |
6 | 6 | import type { Stack } from "@aws-sdk/client-cloudformation"; |
@@ -36,6 +36,7 @@ const OLD_PAYMENT_ARN = |
36 | 36 | "arn:aws:bedrock-agentcore:us-east-1:111122223333:" + |
37 | 37 | `token-vault/default/paymentcredentialprovider/${OLD_PAYMENT_PROVIDER}`; |
38 | 38 | const json = new FsReadWriteJson({ logger: createSilentLogger() }); |
| 39 | +const VENDED_CDK_PACKAGE_JSON = join(import.meta.dir, "../../../assets/cdk/package.json"); |
39 | 40 |
|
40 | 41 | /** A template holding only what CDK adds itself, as an empty project synthesizes. */ |
41 | 42 | const METADATA_ONLY = { CDKMetadata: { Type: "AWS::CDK::Metadata" } }; |
@@ -574,6 +575,46 @@ describe("CdkBackend.build", () => { |
574 | 575 | ]); |
575 | 576 | }); |
576 | 577 |
|
| 578 | + test("syncs the project's CDK dependencies to the vended pins before synthesizing", async () => { |
| 579 | + const input = await project(); |
| 580 | + const vended = JSON.parse(await readFile(VENDED_CDK_PACKAGE_JSON, "utf8")); |
| 581 | + const manifestPath = join(cdkDirectory(input), "package.json"); |
| 582 | + await writeFile( |
| 583 | + manifestPath, |
| 584 | + JSON.stringify({ |
| 585 | + ...vended, |
| 586 | + dependencies: { ...vended.dependencies, "@aws/agentcore-cdk": "1.0.3", zod: "4.1.0" }, |
| 587 | + }), |
| 588 | + ); |
| 589 | + const subject = harness(); |
| 590 | + |
| 591 | + const events = await collect(subject.backend.build(input)); |
| 592 | + |
| 593 | + const updated = JSON.parse(await readFile(manifestPath, "utf8")); |
| 594 | + expect(updated.dependencies).toEqual({ ...vended.dependencies, zod: "4.1.0" }); |
| 595 | + expect(events[0]).toEqual({ |
| 596 | + type: "step", |
| 597 | + message: `updating CDK dependencies: @aws/agentcore-cdk 1.0.3 -> ${vended.dependencies["@aws/agentcore-cdk"]}`, |
| 598 | + }); |
| 599 | + expect(subject.commands).toEqual([ |
| 600 | + { command: ["npm", "install", "--no-audit", "--no-fund"], cwd: cdkDirectory(input) }, |
| 601 | + { command: synthCommand(input), cwd: cdkDirectory(input) }, |
| 602 | + ]); |
| 603 | + }); |
| 604 | + |
| 605 | + test("leaves the CDK dependencies alone when dependency management is disabled", async () => { |
| 606 | + const input = await project(); |
| 607 | + const manifest = JSON.stringify({ dependencies: { "@aws/agentcore-cdk": "1.0.3" } }); |
| 608 | + const manifestPath = join(cdkDirectory(input), "package.json"); |
| 609 | + await writeFile(manifestPath, manifest); |
| 610 | + const subject = harness(); |
| 611 | + |
| 612 | + await collect(subject.backend.build(input, { disableDependencyManagement: true })); |
| 613 | + |
| 614 | + expect(await readFile(manifestPath, "utf8")).toBe(manifest); |
| 615 | + expect(subject.commands).toEqual([{ command: synthCommand(input), cwd: cdkDirectory(input) }]); |
| 616 | + }); |
| 617 | + |
577 | 618 | test("fails actionably when CDK dependencies are missing", async () => { |
578 | 619 | const input = await project(false); |
579 | 620 | const subject = harness(); |
@@ -1441,6 +1482,30 @@ describe("CdkBackend.deploy", () => { |
1441 | 1482 | expect(state.targets.default?.stackArn).toBeUndefined(); |
1442 | 1483 | }); |
1443 | 1484 |
|
| 1485 | + test("installs a missing node_modules before provisioning credentials", async () => { |
| 1486 | + const input = await project(false); |
| 1487 | + await writeFile( |
| 1488 | + join(cdkDirectory(input), "package.json"), |
| 1489 | + await readFile(VENDED_CDK_PACKAGE_JSON, "utf8"), |
| 1490 | + ); |
| 1491 | + await writeAssembly(input, [TARGET.name]); |
| 1492 | + let commandsBeforeProvisioning: string[] = []; |
| 1493 | + // eslint-disable-next-line require-yield -- a spy that only records when provisioning starts |
| 1494 | + const provisionCredentials: CredentialProvisioner = async function* () { |
| 1495 | + commandsBeforeProvisioning = subject.commands.map(({ command }) => command.join(" ")); |
| 1496 | + return {}; |
| 1497 | + }; |
| 1498 | + const subject = harness({ provisionCredentials }); |
| 1499 | + |
| 1500 | + const deployed = await collectDeploy(subject.backend.deploy(input, deployInput())); |
| 1501 | + |
| 1502 | + expect(deployed.events).toContainEqual({ |
| 1503 | + type: "step", |
| 1504 | + message: "installing CDK dependencies (node_modules missing)", |
| 1505 | + }); |
| 1506 | + expect(commandsBeforeProvisioning).toEqual(["npm install --no-audit --no-fund"]); |
| 1507 | + }); |
| 1508 | + |
1444 | 1509 | test("checks local CDK prerequisites before provisioning credentials", async () => { |
1445 | 1510 | const input = await project(false); // no agentcore/cdk/node_modules |
1446 | 1511 | let provisioned = false; |
@@ -1613,6 +1678,30 @@ describe("CdkBackend.deploy", () => { |
1613 | 1678 | expect(subject.transactionSearchRegions).toEqual([]); |
1614 | 1679 | }); |
1615 | 1680 |
|
| 1681 | + test("does not block a teardown on a CDK pin newer than this CLI", async () => { |
| 1682 | + const input = await project(); |
| 1683 | + await writeAssembly(input, [TARGET.name], { resources: METADATA_ONLY }); |
| 1684 | + const vended = JSON.parse(await readFile(VENDED_CDK_PACKAGE_JSON, "utf8")); |
| 1685 | + await writeFile( |
| 1686 | + join(cdkDirectory(input), "package.json"), |
| 1687 | + JSON.stringify({ |
| 1688 | + ...vended, |
| 1689 | + dependencies: { ...vended.dependencies, "@aws/agentcore-cdk": "999.0.0" }, |
| 1690 | + }), |
| 1691 | + ); |
| 1692 | + const subject = harness(); |
| 1693 | + |
| 1694 | + const deployed = await collectDeploy( |
| 1695 | + subject.backend.deploy(input, deployInput({ confirmTeardown: async () => true })), |
| 1696 | + ); |
| 1697 | + |
| 1698 | + expect(deployed.result).toEqual({ outputs: {}, tornDown: true }); |
| 1699 | + expect(deployed.events).toContainEqual({ |
| 1700 | + type: "warning", |
| 1701 | + message: expect.stringContaining("@aws/agentcore-cdk 999.0.0"), |
| 1702 | + }); |
| 1703 | + }); |
| 1704 | + |
1616 | 1705 | test("removes the stack when the teardown is confirmed", async () => { |
1617 | 1706 | const input = await project(); |
1618 | 1707 | await writeAssembly(input, [TARGET.name], { resources: METADATA_ONLY }); |
|
0 commit comments