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

main
ts 142 lines 4.77 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import LambdaBindingsTestFunctionLive, { LambdaBindingsTestFunction } from "./bindings-handler.ts";
10
11 const testOptions = { providers: AWS.providers() };
12 const { test, beforeAll, afterAll } = Test.make(testOptions);
13 const sharedStack = Core.scratchStack(testOptions, "LambdaBindings");
14
15 let baseUrl: string;
16 let deployedFunctionName: string;
17
18 // Fresh function URLs take a few seconds to start serving 200s; ride
19 // through propagation + cold start with a bounded retry.
20 const getJson = (path: string, times = 10) =>
21 HttpClient.get(`${baseUrl}${path}`).pipe(
22 Effect.flatMap((response) =>
23 response.status === 200
24 ? response.json
25 : response.text.pipe(
26 Effect.flatMap((body) =>
27 Effect.fail(new Error(`${path} returned ${response.status}: ${body}`)),
28 ),
29 ),
30 ),
31 Effect.retry({
32 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(times)]),
33 }),
34 );
35
36 describe("Lambda Bindings", { tags: ["provider:aws", "provider:aws:lambda", "live"] }, () => {
37 beforeAll(
38 Effect.gen(function* () {
39 yield* sharedStack.destroy();
40 const { functionName, functionUrl } = yield* sharedStack.deploy(
41 Effect.gen(function* () {
42 return yield* LambdaBindingsTestFunction;
43 }).pipe(Effect.provide(LambdaBindingsTestFunctionLive)),
44 );
45 expect(functionUrl).toBeTruthy();
46 baseUrl = functionUrl!.replace(/\/+$/, "");
47 deployedFunctionName = functionName;
48 }),
49 { timeout: 300_000 },
50 );
51
52 afterAll(
53 Effect.gen(function* () {
54 yield* sharedStack.destroy();
55 // Out-of-band proof the destroy removed the host function. The raw
56 // afterAll context has no providers layer (only `test.provider` bodies
57 // do), so the distilled call must be wrapped in `Core.withProviders`
58 // to satisfy Credentials/HttpClient/Region.
59 if (deployedFunctionName) {
60 yield* Core.withProviders(
61 Lambda.getFunction({ FunctionName: deployedFunctionName }).pipe(
62 Effect.flatMap(() =>
63 Effect.fail(new Error(`Function ${deployedFunctionName} still exists`)),
64 ),
65 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
66 Effect.retry({
67 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
68 }),
69 ),
70 testOptions,
71 "LambdaBindings",
72 );
73 }
74 }),
75 { timeout: 180_000 },
76 );
77
78 test.provider(
79 "InvokeFunction invokes the target function through its alias",
80 () =>
81 Effect.gen(function* () {
82 const result = (yield* getJson("/invoke", 30)) as {
83 statusCode: number;
84 executedVersion: string;
85 };
86 expect(result.statusCode).toBe(200);
87 // The target sets `alias: "live"`, so the binding runs the version
88 // the alias serves, never `$LATEST`.
89 expect(result.executedVersion).toMatch(/^[1-9]\d*$/);
90 }),
91 { timeout: 120_000 },
92 );
93
94 test.provider(
95 "InvokeWithResponseStream streams the target's response",
96 () =>
97 Effect.gen(function* () {
98 const result = (yield* getJson("/invoke-stream")) as {
99 statusCode: number;
100 payload: string;
101 complete: boolean;
102 eventShapes: string[][];
103 };
104 expect(result.statusCode).toBe(200);
105 expect(result.complete).toBe(true);
106 expect(result.payload, `event shapes: ${JSON.stringify(result.eventShapes)}`).toContain(
107 "ok",
108 );
109 }),
110 { timeout: 120_000 },
111 );
112
113 test.provider(
114 "GetFunction reads the target's configuration",
115 () =>
116 Effect.gen(function* () {
117 const result = (yield* getJson("/get-function")) as { functionName: string };
118 expect(result.functionName).toBeTruthy();
119 }),
120 { timeout: 120_000 },
121 );
122
123 test.provider(
124 "GetAccountSettings reads account limits",
125 () =>
126 Effect.gen(function* () {
127 const result = (yield* getJson("/account-settings")) as { concurrentExecutions: number };
128 expect(result.concurrentExecutions).toBeGreaterThan(0);
129 }),
130 { timeout: 120_000 },
131 );
132
133 test.provider(
134 "ListFunctions lists functions in the region",
135 () =>
136 Effect.gen(function* () {
137 const result = (yield* getJson("/list-functions")) as { count: number };
138 expect(result.count).toBeGreaterThan(0);
139 }),
140 { timeout: 120_000 },
141 );
142 });