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

main
ts 146 lines 5.29 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 Data from "effect/Data";
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 OsisTestFunctionLive, { OsisTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "OSISBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
29 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
30 const send = (request: HttpClientRequest.HttpClientRequest) =>
31 HttpClient.execute(request).pipe(
32 Effect.flatMap((response) =>
33 response.status >= 500
34 ? response.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new TransientUpstream({ status: response.status, body })),
37 ),
38 )
39 : Effect.succeed(response),
40 ),
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
44 }),
45 );
46
47 const getJson = (path: string) =>
48 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
49
50 describe.sequential(
51 "OSIS Bindings",
52 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:osis", "live"] },
53 () => {
54 beforeAll(
55 Effect.gen(function* () {
56 yield* Effect.logInfo("OSIS test setup: destroying previous resources");
57 yield* sharedStack.destroy();
58
59 yield* Effect.logInfo("OSIS test setup: deploying fixture");
60 const attrs = yield* sharedStack.deploy(
61 Effect.gen(function* () {
62 return yield* OsisTestFunction;
63 }).pipe(Effect.provide(OsisTestFunctionLive)),
64 );
65
66 expect(attrs.functionUrl).toBeTruthy();
67 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
68
69 const readinessUrl = `${baseUrl}/bindings`;
70 yield* Effect.logInfo(`OSIS test setup: probing readiness at ${readinessUrl}`);
71 yield* HttpClient.get(readinessUrl).pipe(
72 Effect.flatMap((response) =>
73 response.status === 200
74 ? Effect.succeed(response)
75 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
76 ),
77 Effect.tapError((error) =>
78 Effect.logWarning(`OSIS test setup: fixture not ready yet (${String(error)})`),
79 ),
80 Effect.retry({ schedule: readinessPolicy }),
81 );
82 }),
83 { timeout: 240_000 },
84 );
85
86 afterAll(sharedStack.destroy(), { timeout: 120_000 });
87
88 describe("binding registration", () => {
89 test.provider("all four capabilities initialize in the runtime", (_stack) =>
90 Effect.gen(function* () {
91 const response = (yield* getJson("/bindings")) as { bound: string[] };
92 expect(response.bound).toHaveLength(4);
93 }),
94 );
95 });
96
97 describe("ValidatePipeline", () => {
98 test.provider("accepts a valid configuration body", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* getJson("/validate-good")) as {
101 isValid: boolean;
102 errors: string[];
103 };
104 expect(response.isValid).toBe(true);
105 expect(response.errors).toHaveLength(0);
106 }),
107 );
108
109 test.provider("rejects an unknown sink plugin", (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* getJson("/validate-bad")) as {
112 isValid: boolean;
113 errors: string[];
114 };
115 expect(response.isValid).toBe(false);
116 expect(response.errors.length).toBeGreaterThan(0);
117 }),
118 );
119 });
120
121 describe("ListPipelineBlueprints + GetPipelineBlueprint", () => {
122 test.provider("lists the blueprint catalog and fetches one template", (_stack) =>
123 Effect.gen(function* () {
124 const { names } = (yield* getJson("/blueprints")) as { names: string[] };
125 expect(names.length).toBeGreaterThan(0);
126
127 const response = (yield* getJson(`/blueprint?name=${encodeURIComponent(names[0]!)}`)) as {
128 name: string;
129 hasBody: boolean;
130 };
131 expect(response.name).toBe(names[0]);
132 expect(response.hasBody).toBe(true);
133 }),
134 );
135 });
136
137 describe("ListPipelineEndpointConnections", () => {
138 test.provider("enumerates the account's endpoint connections", (_stack) =>
139 Effect.gen(function* () {
140 const response = (yield* getJson("/endpoint-connections")) as { count: number };
141 expect(response.count).toBeGreaterThanOrEqual(0);
142 }),
143 );
144 });
145 },
146 );