@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 124 lines 4.71 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 OamTestFunctionLive, { OamTestFunction } from "./handler";
11 import { makeOamTestLease } from "./TestLease.ts";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "OAMBindings");
16 const serviceLease = makeOamTestLease();
17
18 beforeAll(serviceLease.acquire, { timeout: 3_600_000 });
19 afterAll(serviceLease.release);
20
21 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
22 // over 60s on a fresh deploy.
23 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
24
25 let baseUrl: string;
26
27 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
28 readonly status: number;
29 readonly body: string;
30 }> {}
31
32 // The Lambda fixture occasionally answers a transient 5xx under load (cold
33 // re-init, IAM propagation on the freshly attached policy that the handler's
34 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
35 // failure surfaces immediately.
36 const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(new TransientUpstream({ status: response.status, body })),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
50 }),
51 );
52
53 const getJson = (path: string) =>
54 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
55
56 // AWS enforces ONE OAM sink per account per region, so this file must never
57 // overlap with Sink.test.ts (which deploys its own sink). The single-fork
58 // suite run executes files sequentially and both files destroy their sink on
59 // the way in and out.
60 describe.sequential(
61 "OAM Bindings",
62 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:oam", "live"] },
63 () => {
64 beforeAll(
65 Effect.gen(function* () {
66 yield* Effect.logInfo("OAM test setup: destroying previous resources");
67 yield* sharedStack.destroy();
68
69 yield* Effect.logInfo("OAM test setup: deploying fixture");
70 const attrs = yield* sharedStack.deploy(
71 Effect.gen(function* () {
72 return yield* OamTestFunction;
73 }).pipe(Effect.provide(OamTestFunctionLive)),
74 );
75
76 expect(attrs.functionUrl).toBeTruthy();
77 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
78
79 const readinessUrl = `${baseUrl}/bindings`;
80 yield* Effect.logInfo(`OAM test setup: probing readiness at ${readinessUrl}`);
81 yield* HttpClient.get(readinessUrl).pipe(
82 Effect.flatMap((response) =>
83 response.status === 200
84 ? Effect.succeed(response)
85 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
86 ),
87 Effect.tapError((error) =>
88 Effect.logWarning(`OAM test setup: fixture not ready yet (${String(error)})`),
89 ),
90 Effect.retry({ schedule: readinessPolicy }),
91 );
92 }),
93 { timeout: 240_000 },
94 );
95
96 afterAll(sharedStack.destroy(), { timeout: 120_000 });
97
98 describe("binding registration", () => {
99 test.provider("the capability initializes in the runtime", (_stack) =>
100 Effect.gen(function* () {
101 const response = (yield* getJson("/bindings")) as { bound: string[] };
102 expect(response.bound).toEqual(["listAttachedLinks"]);
103 }),
104 );
105 });
106
107 describe("ListAttachedLinks", () => {
108 test.provider("lists the bound sink's attached links (injected sink ARN)", (_stack) =>
109 Effect.gen(function* () {
110 // Same-account links are rejected by OAM, so the fixture's sink has
111 // no attached links — an empty Items list proves both the
112 // oam:ListAttachedLinks grant on the sink ARN and the
113 // SinkIdentifier injection.
114 const response = (yield* getJson("/attached-links")) as {
115 count: number;
116 linkArns: string[];
117 };
118 expect(response.count).toBe(0);
119 expect(response.linkArns).toEqual([]);
120 }),
121 );
122 });
123 },
124 );