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

main
ts 122 lines 4.24 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as agw2 from "@distilled.cloud/aws/apigatewayv2";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import HttpApiTestFunctionLive, { HttpApiTestFunction } from "./http-handler";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Route + permission propagation after the first deploy takes a few
15 // seconds; cap the exponential backoff so we poll steadily.
16 const edgePropagationRetry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
17 effect.pipe(
18 Effect.retry({
19 schedule: Schedule.max([
20 Schedule.exponential(500).pipe(
21 Schedule.modifyDelay(({ duration }) =>
22 Effect.succeed(
23 Duration.isGreaterThan(duration, Duration.seconds(5))
24 ? Duration.seconds(5)
25 : duration,
26 ),
27 ),
28 ),
29 Schedule.recurs(20),
30 ]),
31 }),
32 );
33
34 test.provider(
35 "HTTP API proxies to Lambda (payload 2.0 end-to-end)",
36 (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39
40 const out = yield* stack.deploy(
41 Effect.gen(function* () {
42 const fn = yield* HttpApiTestFunction.pipe(Effect.provide(HttpApiTestFunctionLive));
43 const { api, stage, url } = yield* AWS.ApiGatewayV2.HttpApi("TestHttpApi", {
44 handler: fn,
45 timeout: "29 seconds",
46 });
47 return {
48 url,
49 apiId: api.apiId,
50 stageName: stage.stageName,
51 };
52 }),
53 );
54
55 expect(out.url).toContain(out.apiId);
56 expect(out.stageName).toBe("$default");
57 const baseUrl = out.url.replace(/\/+$/, "");
58
59 // The `timeout: "29 seconds"` Duration.Input lands on the wire as
60 // whole milliseconds (out-of-band verification via distilled).
61 const integrations = yield* agw2.getIntegrations({ ApiId: out.apiId });
62 expect(integrations.Items?.[0]?.TimeoutInMillis).toBe(29_000);
63
64 // 1. Routing + query strings (retries through edge propagation).
65 const echo = yield* edgePropagationRetry(
66 HttpClient.get(`${baseUrl}/echo?foo=bar&baz=qux`).pipe(
67 Effect.flatMap((response) =>
68 response.status === 200
69 ? response.json
70 : Effect.fail(new Error(`echo returned ${response.status}`)),
71 ),
72 ),
73 );
74 expect(echo).toEqual({
75 method: "GET",
76 path: "/echo",
77 query: { foo: "bar", baz: "qux" },
78 });
79
80 // 2. Path parameters. Route propagation is independent, so the echo
81 // route becoming live does not guarantee this route is visible yet.
82 const item = yield* edgePropagationRetry(
83 HttpClient.get(`${baseUrl}/items/widget-42`).pipe(
84 Effect.flatMap((response) =>
85 response.status === 200
86 ? response.json
87 : Effect.fail(new Error(`GET /items/widget-42 returned ${response.status}`)),
88 ),
89 ),
90 );
91 expect(item).toEqual({ id: "widget-42" });
92
93 // 3. POST body round-trip. This handler is side-effect-free, so retrying
94 // the request while AWS propagates the POST route is safe.
95 const created = yield* edgePropagationRetry(
96 HttpClient.execute(
97 HttpClientRequest.post(`${baseUrl}/items`).pipe(
98 HttpClientRequest.bodyJsonUnsafe({ name: "widget", count: 2 }),
99 ),
100 ).pipe(
101 Effect.flatMap((response) =>
102 response.status === 201
103 ? response.json
104 : Effect.fail(new Error(`POST /items returned ${response.status}`)),
105 ),
106 ),
107 );
108 expect(created).toEqual({
109 received: { name: "widget", count: 2 },
110 });
111
112 // 4. Unknown routes fall through to the handler's 404.
113 const missing = yield* HttpClient.get(`${baseUrl}/nope`);
114 expect(missing.status).toBe(404);
115
116 yield* stack.destroy();
117 }),
118 {
119 tags: ["provider:aws", "provider:aws:apigatewayv2", "provider:aws:lambda", "live"],
120 timeout: 600_000,
121 },
122 );