@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 162 lines 5.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 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 CodeConnectionsTestFunctionLive, {
11 CodeConnectionsTestFunction,
12 fixtureConnectionName,
13 } from "./handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "CodeConnectionsBindings");
18
19 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
20 // over 60s on a fresh deploy.
21 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
22
23 let baseUrl: string;
24
25 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28 }> {}
29
30 // The shared Lambda fixture occasionally answers a transient 5xx under load
31 // (cold re-init, IAM propagation on the freshly attached policy that the
32 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
33 // 4xx/assertion failure surfaces immediately.
34 const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new TransientUpstream({ status: response.status, body })),
41 ),
42 )
43 : Effect.succeed(response),
44 ),
45 Effect.retry({
46 while: (e) => e._tag === "TransientUpstream",
47 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
48 }),
49 );
50
51 describe.sequential(
52 "CodeConnections Bindings",
53 {
54 tags: [
55 "provider:aws",
56 "provider:aws:codeconnections",
57 "provider:aws:connection",
58 "provider:aws:lambda",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* Effect.logInfo("CodeConnections test setup: destroying previous resources");
66 yield* sharedStack.destroy();
67
68 yield* Effect.logInfo("CodeConnections test setup: deploying fixture");
69 const { functionUrl } = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 return yield* CodeConnectionsTestFunction;
72 }).pipe(Effect.provide(CodeConnectionsTestFunctionLive)),
73 );
74
75 expect(functionUrl).toBeTruthy();
76 baseUrl = functionUrl!.replace(/\/+$/, "");
77
78 const readinessUrl = `${baseUrl}/bindings`;
79 yield* Effect.logInfo(`CodeConnections test setup: probing readiness at ${readinessUrl}`);
80 yield* HttpClient.get(readinessUrl).pipe(
81 Effect.flatMap((response) =>
82 response.status === 200
83 ? Effect.succeed(response)
84 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
85 ),
86 Effect.tapError((error) =>
87 Effect.logWarning(
88 `CodeConnections test setup: fixture not ready yet (${String(error)})`,
89 ),
90 ),
91 Effect.retry({ schedule: readinessPolicy }),
92 );
93 }),
94 { timeout: 240_000 },
95 );
96
97 afterAll(sharedStack.destroy(), { timeout: 120_000 });
98
99 describe("binding registration", () => {
100 test.provider("all capabilities initialize in the runtime", (_stack) =>
101 Effect.gen(function* () {
102 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
103 Effect.flatMap((r) => r.json),
104 );
105 expect((response as any).bound).toHaveLength(4);
106 }),
107 );
108 });
109
110 describe("GetConnection", () => {
111 test.provider("reads the fixture connection's live state", (_stack) =>
112 Effect.gen(function* () {
113 const response = yield* send(HttpClientRequest.get(`${baseUrl}/connection`)).pipe(
114 Effect.flatMap((r) => r.json),
115 );
116 expect((response as any).name).toBe(fixtureConnectionName);
117 // The OAuth handshake is a manual console step, so the fixture
118 // connection stays PENDING.
119 expect((response as any).status).toBe("PENDING");
120 expect((response as any).providerType).toBe("GitHub");
121 }),
122 );
123 });
124
125 describe("ListConnections", () => {
126 test.provider("lists the fixture connection among the account's connections", (_stack) =>
127 Effect.gen(function* () {
128 const response = yield* send(HttpClientRequest.get(`${baseUrl}/connections`)).pipe(
129 Effect.flatMap((r) => r.json),
130 );
131 expect((response as any).names).toContain(fixtureConnectionName);
132 }),
133 );
134 });
135
136 describe("ListHosts", () => {
137 test.provider(
138 "enumerates the account's hosts (empty unless a self-managed provider is registered)",
139 (_stack) =>
140 Effect.gen(function* () {
141 const response = yield* send(HttpClientRequest.get(`${baseUrl}/hosts`)).pipe(
142 Effect.flatMap((r) => r.json),
143 );
144 expect(Array.isArray((response as any).names)).toBe(true);
145 }),
146 );
147 });
148
149 describe("ListRepositoryLinks", () => {
150 test.provider(
151 "enumerates the account's repository links (empty unless Git sync is configured)",
152 (_stack) =>
153 Effect.gen(function* () {
154 const response = yield* send(HttpClientRequest.get(`${baseUrl}/repository-links`)).pipe(
155 Effect.flatMap((r) => r.json),
156 );
157 expect(Array.isArray((response as any).repositories)).toBe(true);
158 }),
159 );
160 });
161 },
162 );