The best way to control Claude Code, Codex and more from any computer or phone

main
ts 106 lines 3.8 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 cacheClusterConnectEnvPrefix,
4 replicationGroupConnectEnvPrefix,
5 } from "@alchemy.run/aws/ElastiCache";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as Lambda from "@distilled.cloud/aws/lambda";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as Redacted from "effect/Redacted";
12 import * as Schedule from "effect/Schedule";
13 import ProvisionedCacheDataPlaneLive, {
14 ProvisionedCacheDataPlaneFunction,
15 } from "./Provisioned.DataPlane.handler.ts";
16 import { shareProvisionedNetwork } from "./ProvisionedFixture.ts";
17
18 const { test, beforeAll, afterAll } = Test.make({
19 providers: AWS.providers(),
20 });
21 shareProvisionedNetwork({ beforeAll, afterAll });
22
23 // A type alias so it stays comparable to `response.json`'s JsonObject.
24 type ConnectionProbe = {
25 valkey: {
26 host: string;
27 port: number;
28 readerHost?: string;
29 readerPort?: number;
30 tls: boolean;
31 };
32 memcached: {
33 endpoints: Array<{ address: string; port: number }>;
34 tls: boolean;
35 };
36 };
37
38 test.provider.skipIf(!!process.env.FAST)(
39 "connects from a VPC Lambda to provisioned Valkey and Memcached",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43 const { fn } = yield* stack.deploy(
44 Effect.gen(function* () {
45 const fn = yield* ProvisionedCacheDataPlaneFunction;
46 return { fn };
47 }).pipe(Effect.provide(ProvisionedCacheDataPlaneLive)),
48 );
49 const baseUrl = fn.functionUrl?.replace(/\/+$/, "");
50 expect(baseUrl).toBeDefined();
51 const config = yield* Lambda.getFunctionConfiguration({
52 FunctionName: fn.functionName,
53 });
54 const env = config.Environment?.Variables ?? {};
55 const envValue = (key: string) => {
56 const value = env[key];
57 return value === undefined || typeof value === "string" ? value : Redacted.value(value);
58 };
59 const valkeyPrefix = replicationGroupConnectEnvPrefix("Valkey");
60 expect(envValue(`${valkeyPrefix}_HOST`)).toBeTruthy();
61 expect(envValue(`${valkeyPrefix}_PORT`)).toBe("6379");
62 expect(envValue(`${valkeyPrefix}_TLS`)).toBe("false");
63 const memcachedPrefix = cacheClusterConnectEnvPrefix("Memcached");
64 expect(envValue(`${memcachedPrefix}_TLS`)).toBe("false");
65 expect(JSON.parse(envValue(`${memcachedPrefix}_ENDPOINTS`) ?? "[]")).toHaveLength(1);
66
67 const getJson = (path: string, times: number) =>
68 HttpClient.get(`${baseUrl}${path}`).pipe(
69 Effect.flatMap((response) =>
70 response.status === 200
71 ? response.json
72 : response.text.pipe(
73 Effect.flatMap((body) =>
74 Effect.fail(new Error(`${path} returned ${response.status}: ${body}`)),
75 ),
76 ),
77 ),
78 Effect.retry({
79 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(times)]),
80 }),
81 );
82
83 const connection = (yield* getJson("/connection", 60)) as ConnectionProbe;
84 expect(connection.valkey.host).toBeTruthy();
85 expect(connection.valkey.port).toBe(6379);
86 expect(connection.memcached.endpoints).toHaveLength(1);
87 expect(connection.memcached.endpoints[0]?.port).toBe(11211);
88
89 const valkey = yield* getJson("/valkey?value=hello-valkey", 10);
90 expect((valkey as { value: string }).value).toBe("hello-valkey");
91 const memcached = yield* getJson("/memcached?value=hello-memcached", 10);
92 expect((memcached as { value: string }).value).toBe("hello-memcached");
93
94 yield* stack.destroy();
95 }),
96 {
97 tags: [
98 "provider:aws",
99 "provider:aws:ec2",
100 "provider:aws:elasticache",
101 "provider:aws:lambda",
102 "live",
103 ],
104 timeout: 2_700_000,
105 },
106 );