The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { DbInstance } from "@alchemy.run/aws/Timestream"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 6 | import * as influxdb from "@distilled.cloud/aws/timestream-influxdb"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const envList = (value: string | undefined) => |
| 15 | (value ?? "") |
| 16 | .split(",") |
| 17 | .map((s) => s.trim()) |
| 18 | .filter(Boolean); |
| 19 | |
| 20 | const defaultNetwork = Effect.gen(function* () { |
| 21 | const subnetIds = envList(process.env.AWS_TEST_SUBNET_IDS); |
| 22 | const securityGroupIds = envList(process.env.AWS_TEST_SECURITY_GROUP_IDS); |
| 23 | if (subnetIds.length > 0 && securityGroupIds.length > 0) { |
| 24 | return { subnetIds, securityGroupIds }; |
| 25 | } |
| 26 | const vpc = yield* getDefaultVpc; |
| 27 | const subnets = yield* EC2.describeSubnets({ |
| 28 | Filters: [ |
| 29 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 30 | { Name: "default-for-az", Values: ["true"] }, |
| 31 | ], |
| 32 | }); |
| 33 | const groups = yield* EC2.describeSecurityGroups({ |
| 34 | Filters: [ |
| 35 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 36 | { Name: "group-name", Values: ["default"] }, |
| 37 | ], |
| 38 | }); |
| 39 | const defaultSubnetIds = (subnets.Subnets ?? []) |
| 40 | .map((s) => s.SubnetId) |
| 41 | .filter((id): id is string => id !== undefined) |
| 42 | .sort() |
| 43 | .slice(0, 1); |
| 44 | const securityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 45 | if (defaultSubnetIds.length === 0 || securityGroupId === undefined) { |
| 46 | return yield* Effect.die( |
| 47 | new Error("default VPC is missing subnets or its default security group"), |
| 48 | ); |
| 49 | } |
| 50 | return { subnetIds: defaultSubnetIds, securityGroupIds: [securityGroupId] }; |
| 51 | }); |
| 52 | |
| 53 | // timestream-influxdb IS accessible (unlike Timestream LiveAnalytics), but |
| 54 | // provisioning a DB instance takes ~15–20 minutes and is EC2-backed/costly. |
| 55 | // The ungated probe asserts the distilled wiring produces a typed error; the |
| 56 | // full lifecycle is skipped under `--fast`. |
| 57 | describe( |
| 58 | "AWS.Timestream.DbInstance", |
| 59 | { tags: ["provider:aws", "provider:aws:timestream", "live"] }, |
| 60 | () => { |
| 61 | test.provider( |
| 62 | "getDbInstance for a bad identifier yields a typed ValidationException", |
| 63 | (_stack) => |
| 64 | Effect.gen(function* () { |
| 65 | // A malformed identifier is rejected up-front with a typed |
| 66 | // ValidationException (a well-formed but absent id would instead yield |
| 67 | // ResourceNotFoundException); either way the distilled wiring surfaces |
| 68 | // a typed error rather than an untyped catch-all. |
| 69 | const error = yield* influxdb |
| 70 | .getDbInstance({ identifier: "alchemy-timestream-does-not-exist" }) |
| 71 | .pipe(Effect.flip); |
| 72 | expect(["ValidationException", "ResourceNotFoundException"]).toContain(error._tag); |
| 73 | }), |
| 74 | { timeout: 60_000 }, |
| 75 | ); |
| 76 | |
| 77 | test.provider.skipIf(!!process.env.FAST)( |
| 78 | "create, wait, and delete an InfluxDB instance", |
| 79 | (stack) => |
| 80 | Effect.gen(function* () { |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | // Explicit AWS_TEST_SUBNET_IDS / AWS_TEST_SECURITY_GROUP_IDS win; |
| 84 | // otherwise use the default VPC's default-for-AZ subnets and its |
| 85 | // default security group. |
| 86 | const network = yield* defaultNetwork; |
| 87 | |
| 88 | const instance = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* DbInstance("Influx", { |
| 91 | dbInstanceType: "db.influx.medium", |
| 92 | allocatedStorage: 20, |
| 93 | vpcSubnetIds: network.subnetIds, |
| 94 | vpcSecurityGroupIds: network.securityGroupIds, |
| 95 | password: Redacted.make("AlchemySuperSecretPw1"), |
| 96 | tags: { Environment: "test" }, |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(instance.id).toBeDefined(); |
| 102 | expect(instance.arn).toBeDefined(); |
| 103 | expect(instance.status).toBe("AVAILABLE"); |
| 104 | |
| 105 | const described = yield* influxdb.getDbInstance({ identifier: instance.id }); |
| 106 | expect(described.name).toBe(instance.name); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | |
| 110 | yield* Effect.gen(function* () { |
| 111 | const gone = yield* influxdb.getDbInstance({ identifier: instance.id }).pipe( |
| 112 | Effect.map(() => false), |
| 113 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)), |
| 114 | ); |
| 115 | if (!gone) return yield* Effect.fail({ _tag: "StillExists" as const }); |
| 116 | }).pipe( |
| 117 | Effect.retry({ |
| 118 | while: (e: { _tag: string }) => e._tag === "StillExists", |
| 119 | schedule: Schedule.max([Schedule.spaced("20 seconds"), Schedule.recurs(90)]), |
| 120 | }), |
| 121 | ); |
| 122 | }), |
| 123 | { timeout: 2_400_000 }, |
| 124 | ); |
| 125 | }, |
| 126 | ); |