t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/EC2/Subnet.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

150 lines
4.5 KiB
TypeScript

import * as AWS from "@/AWS";
import { Subnet, Vpc } from "@/AWS/EC2";
import * as Provider from "@/Provider";
import * as Test from "./VpcTest.ts";
import * as EC2 from "@distilled.cloud/aws/ec2";
import { expect } from "alchemy-test";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
const { test } = Test.make({ providers: AWS.providers() });
const logLevel = Effect.provideService(
MinimumLogLevel,
process.env.DEBUG ? "Debug" : "Info",
);
test.provider(
"create, update, delete subnet",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const { vpc, subnet } = yield* stack.deploy(
Effect.gen(function* () {
const vpc = yield* Vpc("TestVpc", {
cidrBlock: "10.0.0.0/16",
});
const subnet = yield* Subnet("TestSubnet", {
vpcId: vpc.vpcId,
cidrBlock: "10.0.1.0/24",
});
return { vpc, subnet };
}),
);
const actualSubnet = yield* EC2.describeSubnets({
SubnetIds: [subnet.subnetId],
});
expect(actualSubnet.Subnets?.[0]?.SubnetId).toEqual(subnet.subnetId);
expect(actualSubnet.Subnets?.[0]?.CidrBlock).toEqual("10.0.1.0/24");
expect(actualSubnet.Subnets?.[0]?.VpcId).toEqual(vpc.vpcId);
expect(actualSubnet.Subnets?.[0]?.State).toEqual("available");
expect(actualSubnet.Subnets?.[0]?.MapPublicIpOnLaunch).toEqual(false);
// Update subnet attributes
const { subnet: updatedSubnet } = yield* stack.deploy(
Effect.gen(function* () {
const vpc = yield* Vpc("TestVpc", {
cidrBlock: "10.0.0.0/16",
});
const subnet = yield* Subnet("TestSubnet", {
vpcId: vpc.vpcId,
cidrBlock: "10.0.1.0/24",
mapPublicIpOnLaunch: true,
});
return { vpc, subnet };
}),
);
yield* expectSubnetAttribute({
SubnetId: updatedSubnet.subnetId,
Attribute: "mapPublicIpOnLaunch",
Value: true,
});
// Delete subnet and VPC
yield* stack.destroy();
yield* assertSubnetDeleted(subnet.subnetId);
}).pipe(logLevel),
{ tags: ["provider:aws", "provider:aws:ec2", "live"] },
);
test.provider(
"list enumerates the deployed subnet",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const { subnet } = yield* stack.deploy(
Effect.gen(function* () {
const vpc = yield* Vpc("ListVpc", {
cidrBlock: "10.0.0.0/16",
});
const subnet = yield* Subnet("ListSubnet", {
vpcId: vpc.vpcId,
cidrBlock: "10.0.1.0/24",
});
return { vpc, subnet };
}),
);
const provider = yield* Provider.findProvider(Subnet);
const all = yield* provider.list();
expect(all.some((s) => s.subnetId === subnet.subnetId)).toBe(true);
yield* stack.destroy();
yield* assertSubnetDeleted(subnet.subnetId);
}).pipe(logLevel),
{ tags: ["provider:aws", "provider:aws:ec2", "live"] },
);
const expectSubnetAttribute = Effect.fn(function* (props: {
SubnetId: string;
Attribute: "mapPublicIpOnLaunch" | "assignIpv6AddressOnCreation";
Value: boolean;
}) {
yield* EC2.describeSubnets({
SubnetIds: [props.SubnetId],
}).pipe(
Effect.tap(Effect.logDebug),
Effect.flatMap((result) => {
const subnet = result.Subnets?.[0];
const actualValue =
props.Attribute === "mapPublicIpOnLaunch"
? subnet?.MapPublicIpOnLaunch
: subnet?.AssignIpv6AddressOnCreation;
return actualValue === props.Value
? Effect.succeed(result)
: Effect.fail(new SubnetAttributeStale());
}),
Effect.retry({
while: (e) => e instanceof SubnetAttributeStale,
schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]),
}),
);
});
const assertSubnetDeleted = Effect.fn(function* (subnetId: string) {
yield* EC2.describeSubnets({
SubnetIds: [subnetId],
}).pipe(
Effect.flatMap(() => Effect.fail(new SubnetStillExists())),
Effect.retry({
while: (e) => e instanceof SubnetStillExists,
schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]),
}),
Effect.catchTag("InvalidSubnetID.NotFound", () => Effect.void),
);
});
class SubnetStillExists extends Data.TaggedError("SubnetStillExists") {}
class SubnetAttributeStale extends Data.TaggedError("SubnetAttributeStale") {}