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

main
ts 197 lines 6.63 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as redis from "@distilled.cloud/gcp/redis_v1";
8 import * as Effect from "effect/Effect";
9 import { expectProbe } from "../bindingHost.ts";
10 import RedisBindingsHost, { Acl, Cache, WRITTEN_VALUE, slow } from "./fixtures/bindings-host.ts";
11
12 const testOptions = { providers: GCP.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "RedisBindings");
15
16 let baseUrl: string;
17 let member: string;
18 let aclName: string;
19 let cacheName: string | undefined;
20
21 const scoped = (name: string) =>
22 `resource.name == "${name}" || resource.name.startsWith("${name}/")`;
23
24 /**
25 * `[role, condition expression]` the host holds on the project: Memorystore
26 * has no resource-level IAM, so every grant is a project grant narrowed by
27 * an IAM Condition on the bound resource's name.
28 */
29 const hostProjectGrants = Effect.gen(function* () {
30 const { project } = yield* GcpEnvironment.current;
31 const policy = yield* resourcemanager.getIamPolicyProjects({
32 resource: `projects/${project}`,
33 body: { options: { requestedPolicyVersion: 3 } },
34 });
35 return (policy.bindings ?? [])
36 .filter((binding) => (binding.members ?? []).includes(member))
37 .map((binding) => [binding.role, binding.condition?.expression]);
38 });
39
40 describe(
41 "Redis Bindings",
42 { tags: ["provider:gcp", "provider:gcp:redis", "provider:gcp:run", "live"] },
43 () => {
44 beforeAll(
45 Effect.gen(function* () {
46 yield* sharedStack.destroy();
47 const out = yield* sharedStack.deploy(
48 Effect.gen(function* () {
49 const host = yield* RedisBindingsHost;
50 return {
51 uri: host.uri,
52 serviceAccount: host.serviceAccount,
53 acl: (yield* Acl).name,
54 cache: slow ? (yield* Cache).name : undefined,
55 };
56 }),
57 );
58 baseUrl = out.uri!;
59 member = `serviceAccount:${out.serviceAccount!}`;
60 aclName = out.acl;
61 cacheName = out.cache;
62 }),
63 { timeout: 2_400_000 },
64 );
65
66 afterAll(sharedStack.destroy(), { timeout: 1_800_000 });
67
68 describe("GetAclPolicy", () => {
69 test.provider(
70 "reads the ACL policy, granted viewer scoped to it",
71 (_stack) =>
72 Effect.gen(function* () {
73 const live = yield* expectProbe<redis.AclPolicy>(baseUrl, "getAclPolicy");
74 expect(live.name).toEqual(aclName);
75 expect(live.rules).toEqual([{ username: "reader", rule: "on ~cache:* +get" }]);
76 expect(yield* hostProjectGrants).toContainEqual([
77 "roles/redis.viewer",
78 scoped(aclName),
79 ]);
80 }),
81 {
82 tags: ["provider:gcp", "provider:gcp:redis", "live"],
83 timeout: 600_000,
84 },
85 );
86 });
87
88 // Memorystore instance create + delete runs well past 5 minutes.
89 describe.skipIf(!slow)("GetInstance", () => {
90 test.provider(
91 "reads the instance, granted viewer scoped to it",
92 (_stack) =>
93 Effect.gen(function* () {
94 const live = yield* expectProbe<redis.Instance>(baseUrl, "getInstance");
95 expect(live.name).toEqual(cacheName);
96 expect(live.authEnabled).toEqual(true);
97 expect(yield* hostProjectGrants).toContainEqual([
98 "roles/redis.viewer",
99 scoped(cacheName!),
100 ]);
101 }),
102 {
103 tags: ["provider:gcp", "provider:gcp:redis", "live"],
104 timeout: 600_000,
105 },
106 );
107 });
108
109 describe.skipIf(!slow)("GetAuthString", () => {
110 test.provider(
111 "reads the AUTH string, granted admin scoped to the instance",
112 (_stack) =>
113 Effect.gen(function* () {
114 const auth = yield* expectProbe<redis.InstanceAuthString>(baseUrl, "getAuthString");
115 const direct = yield* redis.getAuthStringProjectsLocationsInstances({
116 name: cacheName!,
117 });
118 expect(auth.authString).toEqual(direct.authString);
119 expect(auth.authString?.length ?? 0).toBeGreaterThan(0);
120 expect(yield* hostProjectGrants).toContainEqual([
121 "roles/redis.admin",
122 scoped(cacheName!),
123 ]);
124 }),
125 {
126 tags: ["provider:gcp", "provider:gcp:redis", "live"],
127 timeout: 600_000,
128 },
129 );
130 });
131
132 describe.skipIf(!slow)("WriteRedis", () => {
133 test.provider(
134 "writes over AUTH, the AUTH lookup granted admin scoped to the instance",
135 (_stack) =>
136 Effect.gen(function* () {
137 const out = yield* expectProbe<{ incremented: number }>(baseUrl, "writeRedis");
138 expect(out.incremented).toBeGreaterThan(0);
139 expect(yield* hostProjectGrants).toContainEqual([
140 "roles/redis.admin",
141 scoped(cacheName!),
142 ]);
143 }),
144 {
145 tags: ["provider:gcp", "provider:gcp:redis", "live"],
146 timeout: 600_000,
147 },
148 );
149 });
150
151 describe.skipIf(!slow)("ReadRedis", () => {
152 test.provider(
153 "reads what WriteRedis wrote, over AUTH",
154 (_stack) =>
155 Effect.gen(function* () {
156 const out = yield* expectProbe<{
157 value: string | null;
158 exists: number;
159 pong: string;
160 }>(baseUrl, "readRedis");
161 expect(out).toEqual({
162 value: WRITTEN_VALUE,
163 exists: 1,
164 pong: "PONG",
165 });
166 }),
167 {
168 tags: ["provider:gcp", "provider:gcp:redis", "live"],
169 timeout: 600_000,
170 },
171 );
172 });
173
174 describe.skipIf(!slow)("ReadWriteRedis", () => {
175 test.provider(
176 "round-trips a key over AUTH",
177 (_stack) =>
178 Effect.gen(function* () {
179 const out = yield* expectProbe<{
180 value: string | null;
181 deleted: number;
182 }>(baseUrl, "readWriteRedis");
183 expect(out).toEqual({ value: "rw", deleted: 1 });
184
185 const grants = yield* hostProjectGrants;
186 // Every grant is condition-scoped; nothing lands on the whole
187 // project.
188 expect(grants.filter(([, expression]) => !expression)).toEqual([]);
189 }),
190 {
191 tags: ["provider:gcp", "provider:gcp:redis", "live"],
192 timeout: 600_000,
193 },
194 );
195 });
196 },
197 );