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

main
ts 130 lines 5.33 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 Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Schedule from "effect/Schedule";
9 import CloudTrailLakeTestFunctionLive, { CloudTrailLakeTestFunction } from "./lake-handler";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
14 readonly status: number;
15 readonly body: string;
16 }> {}
17
18 // Retry transient 5xx from the Lambda fixture (cold re-init, IAM propagation
19 // on the freshly attached cloudtrail policy, the store's settling window
20 // exhausting the handler's bounded retry). Genuine 4xx return immediately.
21 const send = (request: HttpClientRequest.HttpClientRequest) =>
22 HttpClient.execute(request).pipe(
23 Effect.flatMap((response) =>
24 response.status >= 500
25 ? response.text.pipe(
26 Effect.flatMap((body) =>
27 Effect.fail(new TransientUpstream({ status: response.status, body })),
28 ),
29 )
30 : Effect.succeed(response),
31 ),
32 Effect.retry({
33 while: (e) => e._tag === "TransientUpstream",
34 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
35 }),
36 );
37
38 const getJson = (url: string) =>
39 send(HttpClientRequest.get(url)).pipe(Effect.flatMap((r) => r.json));
40
41 class QueryStillRunning extends Data.TaggedError("QueryStillRunning") {}
42
43 // GATED: CloudTrail Lake is closed to new customers (see the typed
44 // CloudTrailLakeOnboardingClosed probe in EventDataStore.test.ts) — deploying
45 // the fixture's EventDataStore only works on an account that was onboarded
46 // to Lake before the closure. Set AWS_TEST_CLOUDTRAIL_LAKE=1 to run it.
47 test.provider.skipIf(!process.env.AWS_TEST_CLOUDTRAIL_LAKE)(
48 "Lake query bindings: start, describe, results, list, cancel, generate",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const { functionUrl } = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* CloudTrailLakeTestFunction;
56 }).pipe(Effect.provide(CloudTrailLakeTestFunctionLive)),
57 );
58 expect(functionUrl).toBeTruthy();
59 const baseUrl = functionUrl!.replace(/\/+$/, "");
60
61 // Readiness: fresh function URLs take a few seconds to serve 200s.
62 yield* HttpClient.get(`${baseUrl}/query/list`).pipe(
63 Effect.flatMap((response) =>
64 response.status === 200
65 ? Effect.succeed(response)
66 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
67 ),
68 Effect.retry({
69 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]),
70 }),
71 );
72
73 // StartQuery — the fixture threads the store's ID into the FROM
74 // clause and rides out the store's settling window.
75 const started = (yield* getJson(`${baseUrl}/query/run`)) as any;
76 expect(started.queryId).toBeTruthy();
77
78 // DescribeQuery — poll to a terminal status (an empty store finishes
79 // fast; FAILED still proves IAM + typed wiring).
80 const terminal = yield* getJson(`${baseUrl}/query/describe?id=${started.queryId}`).pipe(
81 Effect.flatMap((body: any) =>
82 body.status === "QUEUED" || body.status === "RUNNING"
83 ? Effect.fail(new QueryStillRunning())
84 : Effect.succeed(body),
85 ),
86 Effect.retry({
87 while: (e) => e._tag === "QueryStillRunning",
88 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
89 }),
90 );
91 expect(["FINISHED", "FAILED", "CANCELLED", "TIMED_OUT"]).toContain((terminal as any).status);
92
93 // GetQueryResults — an empty store yields zero rows.
94 const results = (yield* getJson(`${baseUrl}/query/results?id=${started.queryId}`)) as any;
95 expect(results.rows).toBeGreaterThanOrEqual(0);
96
97 // ListQueries — the injected EventDataStore scopes the listing.
98 const list = (yield* getJson(`${baseUrl}/query/list`)) as any;
99 expect(list.ids).toContain(started.queryId);
100
101 // CancelQuery — the query already finished, so CloudTrail rejects
102 // with the typed InactiveQueryException (a fresh cancel racing the
103 // query instead returns CANCELLED); both prove the wiring.
104 const cancelled = (yield* send(
105 HttpClientRequest.post(`${baseUrl}/query/cancel?id=${started.queryId}`),
106 ).pipe(Effect.flatMap((r) => r.json))) as any;
107 if (cancelled.errorTag !== undefined) {
108 expect(cancelled.errorTag.length).toBeGreaterThan(0);
109 } else {
110 expect(cancelled.status).toBeTruthy();
111 }
112
113 // GenerateQuery — natural-language SQL generation; success or a
114 // typed rejection both prove the wiring.
115 const generated = (yield* send(HttpClientRequest.post(`${baseUrl}/query/generate`)).pipe(
116 Effect.flatMap((r) => r.json),
117 )) as any;
118 if (generated.errorTag !== undefined) {
119 expect(generated.errorTag.length).toBeGreaterThan(0);
120 } else {
121 expect(generated.statement).toBeTruthy();
122 }
123
124 yield* stack.destroy();
125 }),
126 {
127 tags: ["provider:aws", "provider:aws:cloudtrail", "provider:aws:lambda", "live"],
128 timeout: 600_000,
129 },
130 );