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

main
ts 190 lines 7.1 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 GrafanaTestFunctionLive, { GrafanaTestFunction } from "./handler";
11 import GrafanaWorkspaceTestFunctionLive, {
12 GrafanaWorkspaceTestFunction,
13 } from "./workspace-handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "GrafanaBindings");
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 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 // The fixture occasionally answers a transient 5xx under load (cold re-init,
29 // IAM propagation on the freshly attached policy that the handler's
30 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx surfaces
31 // immediately.
32 const send = (request: HttpClientRequest.HttpClientRequest) =>
33 HttpClient.execute(request).pipe(
34 Effect.flatMap((response) =>
35 response.status >= 500
36 ? response.text.pipe(
37 Effect.flatMap((body) =>
38 Effect.fail(new TransientUpstream({ status: response.status, body })),
39 ),
40 )
41 : Effect.succeed(response),
42 ),
43 Effect.retry({
44 while: (e) => e._tag === "TransientUpstream",
45 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
46 }),
47 );
48
49 const getJson = (baseUrl: string, path: string) =>
50 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
51
52 const postJson = (baseUrl: string, path: string) =>
53 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
54
55 const awaitReady = (baseUrl: string) =>
56 HttpClient.get(`${baseUrl}/bindings`).pipe(
57 Effect.flatMap((response) =>
58 response.status === 200
59 ? Effect.succeed(response)
60 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
61 ),
62 Effect.retry({ schedule: readinessPolicy }),
63 );
64
65 let baseUrl: string;
66
67 describe.sequential(
68 "Grafana Bindings",
69 { tags: ["provider:aws", "provider:aws:grafana", "provider:aws:lambda", "live"] },
70 () => {
71 beforeAll(
72 Effect.gen(function* () {
73 yield* Effect.logInfo("Grafana test setup: destroying previous resources");
74 yield* sharedStack.destroy();
75
76 yield* Effect.logInfo("Grafana test setup: deploying fixture");
77 const attrs = yield* sharedStack.deploy(
78 Effect.gen(function* () {
79 return yield* GrafanaTestFunction;
80 }).pipe(Effect.provide(GrafanaTestFunctionLive)),
81 );
82
83 expect(attrs.functionUrl).toBeTruthy();
84 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
85 yield* awaitReady(baseUrl);
86 }),
87 { timeout: 240_000 },
88 );
89
90 afterAll(
91 Effect.gen(function* () {
92 yield* sharedStack.destroy();
93 }),
94 { timeout: 120_000 },
95 );
96
97 describe("binding registration", () => {
98 test.provider("the account-level capability initializes", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* getJson(baseUrl, "/bindings")) as { bound: string[] };
101 expect(response.bound).toEqual(["listVersions"]);
102 }),
103 );
104 });
105
106 describe("ListVersions", () => {
107 test.provider("lists the Grafana versions available for new workspaces", (_stack) =>
108 Effect.gen(function* () {
109 const response = (yield* getJson(baseUrl, "/versions")) as { versions: string[] };
110 expect(response.versions.length).toBeGreaterThan(0);
111 for (const version of response.versions) {
112 expect(version).toMatch(/^\d+(\.\d+)*$/);
113 }
114 }),
115 );
116 });
117
118 // GATED: creating the workspace the workspace-scoped bindings attach to is
119 // asynchronous (several minutes to reach ACTIVE). Run with
120 // AWS_TEST_GRAFANA=1 — same gate as the Workspace lifecycle test.
121 describe("workspace-scoped bindings (gated)", () => {
122 test.provider.skipIf(!process.env.AWS_TEST_GRAFANA)(
123 "auth/config reads, permissions, and the service-account + token round-trip",
124 (stack) =>
125 Effect.gen(function* () {
126 yield* stack.destroy();
127
128 const attrs = yield* stack.deploy(
129 Effect.gen(function* () {
130 return yield* GrafanaWorkspaceTestFunction;
131 }).pipe(Effect.provide(GrafanaWorkspaceTestFunctionLive)),
132 );
133 const url = attrs.functionUrl!.replace(/\/+$/, "");
134 yield* awaitReady(url);
135
136 const bindings = (yield* getJson(url, "/bindings")) as { bound: string[] };
137 expect(bindings.bound).toHaveLength(15);
138 expect(bindings.bound).toContain("createToken");
139 expect(bindings.bound).toContain("associateLicense");
140
141 const auth = (yield* getJson(url, "/auth")) as {
142 providers: string[];
143 samlStatus?: string;
144 };
145 expect(auth.providers).toContain("SAML");
146
147 const config = (yield* getJson(url, "/config")) as {
148 hasConfiguration: boolean;
149 grafanaVersion?: string;
150 };
151 expect(config.hasConfiguration).toBe(true);
152
153 const permissions = (yield* getJson(url, "/permissions")) as {
154 count?: number;
155 errorTag?: string;
156 };
157 if (permissions.errorTag) {
158 // SSO-permission listing on a SAML-only workspace may be
159 // rejected with a typed error — still proves binding + IAM.
160 expect(["ValidationException", "AccessDeniedException"]).toContain(
161 permissions.errorTag,
162 );
163 } else {
164 expect(permissions.count).toBeGreaterThanOrEqual(0);
165 }
166
167 const accounts = (yield* getJson(url, "/service-accounts")) as { count: number };
168 expect(accounts.count).toBe(0);
169
170 const roundtrip = (yield* postJson(url, "/service-account-roundtrip")) as {
171 serviceAccountId: string;
172 grafanaRole: string;
173 keyPrefix: string;
174 keyLength: number;
175 tokenCount: number;
176 };
177 expect(roundtrip.grafanaRole).toBe("EDITOR");
178 // Grafana service-account tokens are `glsa_...` secrets; the
179 // binding returns the key Redacted and the fixture unwraps it.
180 expect(roundtrip.keyPrefix).toBe("glsa_");
181 expect(roundtrip.keyLength).toBeGreaterThan(10);
182 expect(roundtrip.tokenCount).toBe(1);
183
184 yield* stack.destroy();
185 }),
186 { timeout: 900_000 },
187 );
188 });
189 },
190 );