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 { |
| 3 | CacheCluster, |
| 4 | cacheClusterConnectEnvPrefix, |
| 5 | ConnectCacheCluster, |
| 6 | ConnectCacheClusterHttp, |
| 7 | ConnectReplicationGroup, |
| 8 | ConnectReplicationGroupHttp, |
| 9 | ReplicationGroup, |
| 10 | replicationGroupConnectEnvPrefix, |
| 11 | validateReplicationGroupProps, |
| 12 | } from "@alchemy.run/aws/ElastiCache"; |
| 13 | import { sameStringSet } from "@alchemy.run/aws/ElastiCache/internal"; |
| 14 | import { expect } from "@alchemy.run/test"; |
| 15 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 16 | import * as ElastiCache from "@distilled.cloud/aws/elasticache"; |
| 17 | import * as Output from "alchemy/Output"; |
| 18 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as Schedule from "effect/Schedule"; |
| 21 | import { |
| 22 | assertCacheClusterGone, |
| 23 | assertReplicationGroupGone, |
| 24 | getProvisionedNetwork, |
| 25 | shareProvisionedNetwork, |
| 26 | } from "./ProvisionedFixture.ts"; |
| 27 | |
| 28 | const { test, beforeAll, afterAll } = Test.make({ |
| 29 | providers: AWS.providers(), |
| 30 | }); |
| 31 | shareProvisionedNetwork({ beforeAll, afterAll }); |
| 32 | test( |
| 33 | "connection names and unordered subnet IDs are stable", |
| 34 | Effect.sync(() => { |
| 35 | expect(replicationGroupConnectEnvPrefix("SessionCache")).toBe("ELASTICACHE_SESSIONCACHE"); |
| 36 | expect(cacheClusterConnectEnvPrefix("SessionCache")).toBe("ELASTICACHE_SESSIONCACHE"); |
| 37 | expect(sameStringSet(["subnet-a", "subnet-b"], ["subnet-b", "subnet-a"])).toBe(true); |
| 38 | expect(sameStringSet(["subnet-a"], ["subnet-b"])).toBe(false); |
| 39 | }), |
| 40 | { |
| 41 | tags: ["provider:aws", "provider:aws:elasticache", "live"], |
| 42 | timeout: 2_700_000, |
| 43 | }, |
| 44 | ); |
| 45 | |
| 46 | test( |
| 47 | "invalid replication-group configuration fails before an AWS create request", |
| 48 | Effect.sync(() => { |
| 49 | const invalid = [ |
| 50 | { automaticFailoverEnabled: true, replicasPerNodeGroup: 0 }, |
| 51 | { |
| 52 | multiAzEnabled: true, |
| 53 | automaticFailoverEnabled: false, |
| 54 | replicasPerNodeGroup: 1, |
| 55 | }, |
| 56 | { replicasPerNodeGroup: 6 }, |
| 57 | { numNodeGroups: 0 }, |
| 58 | { port: 0 }, |
| 59 | { |
| 60 | transitEncryptionEnabled: false, |
| 61 | transitEncryptionMode: "preferred" as const, |
| 62 | }, |
| 63 | ]; |
| 64 | for (const props of invalid) { |
| 65 | const error = validateReplicationGroupProps({ |
| 66 | description: "invalid cache", |
| 67 | engine: "valkey", |
| 68 | ...props, |
| 69 | }); |
| 70 | expect(error?._tag).toBe("InvalidReplicationGroupConfiguration"); |
| 71 | } |
| 72 | }), |
| 73 | { |
| 74 | tags: ["provider:aws", "provider:aws:elasticache", "live"], |
| 75 | timeout: 2_700_000, |
| 76 | }, |
| 77 | ); |
| 78 | |
| 79 | const withEnv = <A, E = never, R = never>( |
| 80 | values: Record<string, string>, |
| 81 | effect: Effect.Effect<A, E, R>, |
| 82 | ) => { |
| 83 | const previous = Object.fromEntries(Object.keys(values).map((key) => [key, process.env[key]])); |
| 84 | return Effect.gen(function* () { |
| 85 | yield* Effect.sync(() => Object.assign(process.env, values)); |
| 86 | return yield* effect; |
| 87 | }).pipe( |
| 88 | Effect.ensuring( |
| 89 | Effect.sync(() => { |
| 90 | for (const [key, value] of Object.entries(previous)) { |
| 91 | if (value === undefined) delete process.env[key]; |
| 92 | else process.env[key] = value; |
| 93 | } |
| 94 | }), |
| 95 | ), |
| 96 | ); |
| 97 | }; |
| 98 | |
| 99 | test( |
| 100 | "provisioned cache bindings read the endpoints published to a Function", |
| 101 | Effect.gen(function* () { |
| 102 | const replicationGroup = { |
| 103 | LogicalId: "ProvisionedBinding", |
| 104 | configurationEndpointAddress: Output.asOutput(undefined), |
| 105 | configurationEndpointPort: Output.asOutput(undefined), |
| 106 | primaryEndpointAddress: Output.asOutput("primary.example"), |
| 107 | primaryEndpointPort: Output.asOutput(6379), |
| 108 | readerEndpointAddress: Output.asOutput("reader.example"), |
| 109 | readerEndpointPort: Output.asOutput(6379), |
| 110 | transitEncryptionEnabled: Output.asOutput(true), |
| 111 | } as unknown as ReplicationGroup; |
| 112 | const memcached = { |
| 113 | LogicalId: "MemcachedBinding", |
| 114 | endpoints: Output.asOutput([{ address: "node.example", port: 11211 }]), |
| 115 | transitEncryptionEnabled: Output.asOutput(false), |
| 116 | } as unknown as CacheCluster; |
| 117 | const replicationPrefix = replicationGroupConnectEnvPrefix(replicationGroup.LogicalId); |
| 118 | const memcachedPrefix = cacheClusterConnectEnvPrefix(memcached.LogicalId); |
| 119 | const replicationConnect = yield* ConnectReplicationGroup(replicationGroup).pipe( |
| 120 | Effect.provide(ConnectReplicationGroupHttp), |
| 121 | ); |
| 122 | const memcachedConnect = yield* ConnectCacheCluster(memcached).pipe( |
| 123 | Effect.provide(ConnectCacheClusterHttp), |
| 124 | ); |
| 125 | const [replication, cluster] = yield* withEnv( |
| 126 | { |
| 127 | [`${replicationPrefix}_HOST`]: "primary.example", |
| 128 | [`${replicationPrefix}_PORT`]: "6379", |
| 129 | [`${replicationPrefix}_TLS`]: "true", |
| 130 | [`${replicationPrefix}_READER_HOST`]: "reader.example", |
| 131 | [`${replicationPrefix}_READER_PORT`]: "6379", |
| 132 | [`${memcachedPrefix}_ENDPOINTS`]: JSON.stringify([ |
| 133 | { address: "node.example", port: 11211 }, |
| 134 | ]), |
| 135 | [`${memcachedPrefix}_TLS`]: "false", |
| 136 | }, |
| 137 | Effect.all([replicationConnect, memcachedConnect]).pipe( |
| 138 | Effect.provide(RuntimeContext.phantom), |
| 139 | ), |
| 140 | ); |
| 141 | |
| 142 | expect(replication).toEqual({ |
| 143 | host: "primary.example", |
| 144 | port: 6379, |
| 145 | readerHost: "reader.example", |
| 146 | readerPort: 6379, |
| 147 | tls: true, |
| 148 | }); |
| 149 | expect(cluster).toEqual({ |
| 150 | endpoints: [{ address: "node.example", port: 11211 }], |
| 151 | tls: false, |
| 152 | }); |
| 153 | }), |
| 154 | { |
| 155 | tags: ["provider:aws", "provider:aws:elasticache", "live"], |
| 156 | timeout: 2_700_000, |
| 157 | }, |
| 158 | ); |
| 159 | |
| 160 | const assertSubnetGroupGone = (name: string) => |
| 161 | ElastiCache.describeCacheSubnetGroups({ CacheSubnetGroupName: name }).pipe( |
| 162 | Effect.flatMap(() => Effect.fail(new Error(`subnet group '${name}' still exists`))), |
| 163 | Effect.catchTag("CacheSubnetGroupNotFoundFault", () => Effect.void), |
| 164 | Effect.retry({ |
| 165 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 166 | }), |
| 167 | ); |
| 168 | |
| 169 | const finalSnapshotName = "alchemy-elasticache-final-snapshot"; |
| 170 | |
| 171 | const deleteFinalSnapshot = () => |
| 172 | ElastiCache.deleteSnapshot({ SnapshotName: finalSnapshotName }).pipe( |
| 173 | Effect.catchTag("SnapshotNotFoundFault", () => Effect.void), |
| 174 | Effect.retry({ |
| 175 | while: (error) => error._tag === "InvalidSnapshotStateFault", |
| 176 | schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(12)]), |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | const assertFinalSnapshotAvailable = () => |
| 181 | ElastiCache.describeSnapshots({ SnapshotName: finalSnapshotName }).pipe( |
| 182 | Effect.flatMap((result) => |
| 183 | result.Snapshots?.[0]?.SnapshotStatus === "available" |
| 184 | ? Effect.void |
| 185 | : Effect.fail(new Error(`snapshot '${finalSnapshotName}' is not ready`)), |
| 186 | ), |
| 187 | Effect.retry({ |
| 188 | schedule: Schedule.max([Schedule.fixed("30 seconds"), Schedule.recurs(30)]), |
| 189 | }), |
| 190 | ); |
| 191 | |
| 192 | test.provider( |
| 193 | "create, update, verify, and destroy an ElastiCache subnet group", |
| 194 | (stack) => |
| 195 | Effect.gen(function* () { |
| 196 | yield* stack.destroy(); |
| 197 | const net = yield* getProvisionedNetwork; |
| 198 | const deploy = (description: string) => |
| 199 | stack.deploy( |
| 200 | Effect.gen(function* () { |
| 201 | const group = yield* AWS.ElastiCache.SubnetGroup("Subnets", { |
| 202 | description, |
| 203 | subnetIds: net.subnetIds, |
| 204 | tags: { fixture: "elasticache-provisioned" }, |
| 205 | }); |
| 206 | return { group, subnetIds: net.subnetIds }; |
| 207 | }), |
| 208 | ); |
| 209 | |
| 210 | const { group, subnetIds } = yield* deploy("alchemy cache subnets"); |
| 211 | expect(group.subnetGroupArn).toContain(":subnetgroup:"); |
| 212 | expect(new Set(group.subnetIds)).toEqual(new Set(subnetIds)); |
| 213 | |
| 214 | const described = yield* ElastiCache.describeCacheSubnetGroups({ |
| 215 | CacheSubnetGroupName: group.subnetGroupName, |
| 216 | }); |
| 217 | expect(described.CacheSubnetGroups?.[0]?.CacheSubnetGroupDescription).toBe( |
| 218 | "alchemy cache subnets", |
| 219 | ); |
| 220 | |
| 221 | const { group: updated } = yield* deploy("alchemy cache subnets v2"); |
| 222 | expect(updated.subnetGroupName).toBe(group.subnetGroupName); |
| 223 | expect(updated.description).toBe("alchemy cache subnets v2"); |
| 224 | |
| 225 | yield* stack.destroy(); |
| 226 | yield* assertSubnetGroupGone(group.subnetGroupName); |
| 227 | }), |
| 228 | { |
| 229 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 230 | timeout: 180_000, |
| 231 | }, |
| 232 | ); |
| 233 | |
| 234 | test.provider.skipIf(!!process.env.FAST)( |
| 235 | "create, update, verify, and destroy a provisioned Valkey replication group", |
| 236 | (stack) => |
| 237 | Effect.gen(function* () { |
| 238 | yield* stack.destroy(); |
| 239 | const net = yield* getProvisionedNetwork; |
| 240 | const deploy = (description: string) => |
| 241 | stack.deploy( |
| 242 | Effect.gen(function* () { |
| 243 | const cache = yield* ReplicationGroup("Cache", { |
| 244 | description, |
| 245 | engine: "valkey", |
| 246 | nodeType: "cache.t4g.micro", |
| 247 | subnetGroupName: net.subnetGroupName, |
| 248 | securityGroupIds: [net.securityGroupId], |
| 249 | replicasPerNodeGroup: 0, |
| 250 | transitEncryptionEnabled: true, |
| 251 | tags: { fixture: "elasticache-provisioned" }, |
| 252 | }); |
| 253 | return { cache }; |
| 254 | }), |
| 255 | ); |
| 256 | const { cache } = yield* deploy("alchemy provisioned cache"); |
| 257 | expect(cache.status).toBe("available"); |
| 258 | expect(cache.replicationGroupArn).toContain(":replicationgroup:"); |
| 259 | expect(cache.primaryEndpointAddress).toBeDefined(); |
| 260 | const observed = yield* ElastiCache.describeReplicationGroups({ |
| 261 | ReplicationGroupId: cache.replicationGroupId, |
| 262 | }); |
| 263 | expect(observed.ReplicationGroups?.[0]?.Status).toBe("available"); |
| 264 | |
| 265 | const { cache: updated } = yield* deploy("alchemy provisioned cache v2"); |
| 266 | expect(updated.replicationGroupId).toBe(cache.replicationGroupId); |
| 267 | expect(updated.status).toBe("available"); |
| 268 | |
| 269 | yield* stack.destroy(); |
| 270 | yield* assertReplicationGroupGone(cache.replicationGroupId); |
| 271 | }), |
| 272 | { |
| 273 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 274 | timeout: 2_700_000, |
| 275 | }, |
| 276 | ); |
| 277 | |
| 278 | test.provider.skipIf(!!process.env.FAST)( |
| 279 | "create, scale, verify, and destroy a provisioned Memcached cluster", |
| 280 | (stack) => |
| 281 | Effect.gen(function* () { |
| 282 | yield* stack.destroy(); |
| 283 | const net = yield* getProvisionedNetwork; |
| 284 | const deploy = (numCacheNodes: number) => |
| 285 | stack.deploy( |
| 286 | Effect.gen(function* () { |
| 287 | const cache = yield* CacheCluster("Cache", { |
| 288 | subnetGroupName: net.subnetGroupName, |
| 289 | securityGroupIds: [net.securityGroupId], |
| 290 | numCacheNodes, |
| 291 | tags: { fixture: "elasticache-memcached" }, |
| 292 | }); |
| 293 | return { cache }; |
| 294 | }), |
| 295 | ); |
| 296 | |
| 297 | const { cache } = yield* deploy(1); |
| 298 | expect(cache.status).toBe("available"); |
| 299 | expect(cache.endpoints).toHaveLength(1); |
| 300 | |
| 301 | const { cache: scaled } = yield* deploy(2); |
| 302 | expect(scaled.cacheClusterId).toBe(cache.cacheClusterId); |
| 303 | expect(scaled.endpoints).toHaveLength(2); |
| 304 | |
| 305 | const observed = yield* ElastiCache.describeCacheClusters({ |
| 306 | CacheClusterId: scaled.cacheClusterId, |
| 307 | ShowCacheNodeInfo: true, |
| 308 | }); |
| 309 | expect(observed.CacheClusters?.[0]?.NumCacheNodes).toBe(2); |
| 310 | |
| 311 | yield* stack.destroy(); |
| 312 | yield* assertCacheClusterGone(cache.cacheClusterId); |
| 313 | }), |
| 314 | { |
| 315 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 316 | timeout: 2_700_000, |
| 317 | }, |
| 318 | ); |
| 319 | |
| 320 | test.provider.skipIf(!!process.env.FAST)( |
| 321 | "replace a replication group when its port changes", |
| 322 | (stack) => |
| 323 | Effect.gen(function* () { |
| 324 | yield* stack.destroy(); |
| 325 | const net = yield* getProvisionedNetwork; |
| 326 | const deploy = (port: number) => |
| 327 | stack.deploy( |
| 328 | Effect.gen(function* () { |
| 329 | const cache = yield* ReplicationGroup("Cache", { |
| 330 | description: "alchemy replacement cache", |
| 331 | engine: "valkey", |
| 332 | nodeType: "cache.t4g.micro", |
| 333 | subnetGroupName: net.subnetGroupName, |
| 334 | securityGroupIds: [net.securityGroupId], |
| 335 | replicasPerNodeGroup: 0, |
| 336 | transitEncryptionEnabled: true, |
| 337 | port, |
| 338 | }); |
| 339 | return { cache }; |
| 340 | }), |
| 341 | ); |
| 342 | |
| 343 | const { cache } = yield* deploy(6379); |
| 344 | const { cache: replaced } = yield* deploy(6380); |
| 345 | expect(replaced.replicationGroupId).not.toBe(cache.replicationGroupId); |
| 346 | expect(replaced.primaryEndpointPort).toBe(6380); |
| 347 | yield* assertReplicationGroupGone(cache.replicationGroupId); |
| 348 | |
| 349 | yield* stack.destroy(); |
| 350 | yield* assertReplicationGroupGone(replaced.replicationGroupId); |
| 351 | }), |
| 352 | { |
| 353 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 354 | timeout: 2_700_000, |
| 355 | }, |
| 356 | ); |
| 357 | |
| 358 | test.provider.skipIf(!!process.env.FAST)( |
| 359 | "add and remove a Valkey replica", |
| 360 | (stack) => |
| 361 | Effect.gen(function* () { |
| 362 | yield* stack.destroy(); |
| 363 | const net = yield* getProvisionedNetwork; |
| 364 | const deploy = (replicasPerNodeGroup: number) => |
| 365 | stack.deploy( |
| 366 | Effect.gen(function* () { |
| 367 | const cache = yield* ReplicationGroup("Cache", { |
| 368 | description: "alchemy scaling cache", |
| 369 | engine: "valkey", |
| 370 | nodeType: "cache.t4g.micro", |
| 371 | subnetGroupName: net.subnetGroupName, |
| 372 | securityGroupIds: [net.securityGroupId], |
| 373 | replicasPerNodeGroup, |
| 374 | automaticFailoverEnabled: replicasPerNodeGroup > 0, |
| 375 | transitEncryptionEnabled: true, |
| 376 | }); |
| 377 | return { cache }; |
| 378 | }), |
| 379 | ); |
| 380 | |
| 381 | const { cache } = yield* deploy(0); |
| 382 | const { cache: scaled } = yield* deploy(1); |
| 383 | expect(scaled.replicationGroupId).toBe(cache.replicationGroupId); |
| 384 | |
| 385 | const withReplica = yield* ElastiCache.describeReplicationGroups({ |
| 386 | ReplicationGroupId: scaled.replicationGroupId, |
| 387 | }); |
| 388 | expect(withReplica.ReplicationGroups?.[0]?.NodeGroups?.[0]?.NodeGroupMembers).toHaveLength(2); |
| 389 | |
| 390 | const { cache: reduced } = yield* deploy(0); |
| 391 | const withoutReplica = yield* ElastiCache.describeReplicationGroups({ |
| 392 | ReplicationGroupId: reduced.replicationGroupId, |
| 393 | }); |
| 394 | expect(withoutReplica.ReplicationGroups?.[0]?.NodeGroups?.[0]?.NodeGroupMembers).toHaveLength( |
| 395 | 1, |
| 396 | ); |
| 397 | |
| 398 | yield* stack.destroy(); |
| 399 | yield* assertReplicationGroupGone(reduced.replicationGroupId); |
| 400 | }), |
| 401 | { |
| 402 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 403 | timeout: 2_700_000, |
| 404 | }, |
| 405 | ); |
| 406 | |
| 407 | test.provider.skipIf(!!process.env.FAST)( |
| 408 | "create and clean up the requested final snapshot", |
| 409 | (stack) => |
| 410 | Effect.gen(function* () { |
| 411 | yield* stack.destroy(); |
| 412 | yield* deleteFinalSnapshot(); |
| 413 | const net = yield* getProvisionedNetwork; |
| 414 | const { cache } = yield* stack.deploy( |
| 415 | Effect.gen(function* () { |
| 416 | const cache = yield* ReplicationGroup("Cache", { |
| 417 | description: "alchemy final snapshot cache", |
| 418 | engine: "valkey", |
| 419 | nodeType: "cache.t4g.micro", |
| 420 | subnetGroupName: net.subnetGroupName, |
| 421 | securityGroupIds: [net.securityGroupId], |
| 422 | replicasPerNodeGroup: 0, |
| 423 | transitEncryptionEnabled: true, |
| 424 | finalSnapshotName, |
| 425 | }); |
| 426 | return { cache }; |
| 427 | }), |
| 428 | ); |
| 429 | |
| 430 | yield* stack.destroy(); |
| 431 | yield* assertReplicationGroupGone(cache.replicationGroupId); |
| 432 | yield* assertFinalSnapshotAvailable(); |
| 433 | yield* deleteFinalSnapshot(); |
| 434 | }).pipe(Effect.ensuring(deleteFinalSnapshot().pipe(Effect.ignore))), |
| 435 | { |
| 436 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elasticache", "live"], |
| 437 | timeout: 2_700_000, |
| 438 | }, |
| 439 | ); |