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

main
ts 235 lines 8.62 KB
Raw
1 import { Project } from "@alchemy.run/neon/Project";
2 import {
3 ProjectVPCEndpoint,
4 validateProjectVPCEndpoint,
5 } from "@alchemy.run/neon/ProjectVPCEndpoint";
6 import { providers } from "@alchemy.run/neon/Providers";
7 import { expect } from "@alchemy.run/test";
8 import * as Test from "@alchemy.run/test/Alchemy";
9 import * as SDK from "@distilled.cloud/neon";
10 import { adopt } from "alchemy/AdoptPolicy";
11 import * as Output from "alchemy/Output";
12 import * as Effect from "effect/Effect";
13 import * as Result from "effect/Result";
14
15 const { test } = Test.make({ providers: providers() });
16 const endpoint = {
17 orgId: "org-fixture",
18 regionId: "aws-us-east-2",
19 vpcEndpointId: "vpce-0123456789abcdef0",
20 };
21 const props = { project: { projectId: "fixture-project" }, endpoint, label: "Fixture" };
22 const context = {
23 id: "Restriction",
24 fqn: "Restriction",
25 instanceId: "restriction-validation",
26 oldBindings: [],
27 newBindings: [],
28 };
29
30 test(
31 "project VPC restriction requires a project and valid organization endpoint scope",
32 Effect.gen(function* () {
33 yield* validateProjectVPCEndpoint(props);
34 expect(
35 Result.isFailure(
36 yield* validateProjectVPCEndpoint({ ...props, project: { projectId: "" } }).pipe(
37 Effect.result,
38 ),
39 ),
40 ).toBe(true);
41 expect(
42 Result.isFailure(
43 yield* validateProjectVPCEndpoint({
44 ...props,
45 endpoint: { ...endpoint, regionId: "azure-eastus2" },
46 }).pipe(Effect.result),
47 ),
48 ).toBe(true);
49 }),
50 { tags: ["provider:neon", "provider:neon:projectvpcendpoint", "live"] },
51 );
52
53 test.provider(
54 "project restriction replaces all changed or unresolved scopes with old-grant cleanup first",
55 () =>
56 Effect.gen(function* () {
57 const provider = yield* ProjectVPCEndpoint.Provider;
58 for (const patch of [
59 { project: { projectId: "other-project" } },
60 { project: { projectId: Output.literal("other-project") } },
61 { endpoint: { ...endpoint, orgId: "org-other" } },
62 { endpoint: { ...endpoint, regionId: "aws-us-west-2" } },
63 { endpoint: { ...endpoint, vpcEndpointId: "vpce-abcdef01234567890" } },
64 { endpoint: { ...endpoint, regionId: Output.literal("aws-us-west-2") } },
65 ]) {
66 expect(
67 yield* provider.diff!({
68 ...context,
69 olds: props,
70 news: { ...props, ...patch },
71 output: undefined,
72 }),
73 ).toEqual({ action: "replace", deleteFirst: true });
74 }
75 for (const news of [Output.literal(props), Effect.succeed(props)]) {
76 expect(yield* provider.diff!({ ...context, olds: props, news, output: undefined })).toEqual(
77 { action: "replace", deleteFirst: true },
78 );
79 }
80 expect(
81 yield* provider.diff!({
82 ...context,
83 olds: props,
84 news: { ...props, label: Output.literal("Updated") },
85 output: undefined,
86 }),
87 ).toBeUndefined();
88 }).pipe(
89 Effect.provideService(
90 SDK.Credentials,
91 Effect.die("Unexpected Neon request in a no-I/O guard test"),
92 ),
93 ),
94 { tags: ["provider:neon", "provider:neon:projectvpcendpoint", "live"] },
95 );
96
97 test.provider(
98 "project restriction rejects a cached grant from another project before I/O",
99 () =>
100 Effect.gen(function* () {
101 const provider = yield* ProjectVPCEndpoint.Provider;
102 expect(
103 yield* provider
104 .reconcile({
105 ...context,
106 bindings: [],
107 session: { note: () => Effect.void },
108 olds: undefined,
109 news: { ...props, project: { projectId: "other-project" } },
110 output: {
111 ...endpoint,
112 projectId: props.project.projectId,
113 label: props.label,
114 initialLabel: props.label,
115 managedLabel: props.label,
116 },
117 })
118 .pipe(
119 Effect.as(false),
120 Effect.catchTag("InvalidProjectVPCEndpoint", () => Effect.succeed(true)),
121 ),
122 ).toBe(true);
123 }).pipe(
124 Effect.provideService(
125 SDK.Credentials,
126 Effect.die("Unexpected Neon request in a no-I/O guard test"),
127 ),
128 ),
129 { tags: ["provider:neon", "provider:neon:projectvpcendpoint", "live"] },
130 );
131
132 const orgId = process.env.NEON_GOVERNANCE_TEST_ORG_ID;
133 const regionId = process.env.NEON_GOVERNANCE_TEST_VPC_ENDPOINT_REGION;
134 const vpcEndpointId = process.env.NEON_GOVERNANCE_TEST_VPC_ENDPOINT_ID;
135 const region = (
136 [
137 "aws-us-east-1",
138 "aws-us-east-2",
139 "aws-us-west-2",
140 "aws-eu-central-1",
141 "aws-eu-west-2",
142 "aws-ap-southeast-1",
143 "aws-ap-southeast-2",
144 "aws-sa-east-1",
145 ] as const
146 ).find((value) => value === regionId);
147 const enabled =
148 !!orgId &&
149 !!region &&
150 !!vpcEndpointId &&
151 process.env.NEON_GOVERNANCE_TEST_NETWORK === "1" &&
152 process.env.NEON_GOVERNANCE_TEST_VPC_UNREGISTER !== "1";
153
154 test.provider.skipIf(!enabled)(
155 "dedicated network fixture cleans replaced project grants and preserves adopted restriction labels",
156 (stack) =>
157 Effect.gen(function* () {
158 yield* stack.destroy();
159 const scope = { orgId: orgId!, regionId: region!, vpcEndpointId: vpcEndpointId! };
160 const organizationRequest = {
161 org_id: scope.orgId,
162 region_id: scope.regionId,
163 vpc_endpoint_id: scope.vpcEndpointId,
164 };
165 const organizationBaseline =
166 yield* SDK.getOrganizationVPCEndpointDetails(organizationRequest);
167 const base = Effect.gen(function* () {
168 const a = yield* Project("PrivateProjectA", { orgId: scope.orgId, region: region! });
169 const b = yield* Project("PrivateProjectB", { orgId: scope.orgId, region: region! });
170 return { a, b };
171 });
172 const application = (second: boolean, label: string, takeOwnership = false) =>
173 Effect.gen(function* () {
174 const projects = yield* base;
175 const restriction = yield* ProjectVPCEndpoint("Restriction", {
176 project: second ? projects.b : projects.a,
177 endpoint: scope,
178 label,
179 }).pipe(adopt(takeOwnership));
180 return { ...projects, restriction };
181 });
182 const first = yield* stack.deploy(application(false, "Alchemy project fixture"));
183 const aRequest = { project_id: first.a.projectId, vpc_endpoint_id: scope.vpcEndpointId };
184 const bRequest = { project_id: first.b.projectId, vpc_endpoint_id: scope.vpcEndpointId };
185 expect(first.restriction.initialLabel).toBeNull();
186 expect((yield* SDK.listProjectVPCEndpoints(aRequest)).endpoints).toContainEqual({
187 vpc_endpoint_id: scope.vpcEndpointId,
188 label: "Alchemy project fixture",
189 });
190 yield* stack.deploy(application(false, "Alchemy project updated"));
191 expect((yield* SDK.listProjectVPCEndpoints(aRequest)).endpoints).toContainEqual({
192 vpc_endpoint_id: scope.vpcEndpointId,
193 label: "Alchemy project updated",
194 });
195 yield* stack.deploy(application(true, "Alchemy replacement fixture"));
196 expect((yield* SDK.listProjectVPCEndpoints(aRequest)).endpoints).toEqual([]);
197 expect((yield* SDK.listProjectVPCEndpoints(bRequest)).endpoints).toContainEqual({
198 vpc_endpoint_id: scope.vpcEndpointId,
199 label: "Alchemy replacement fixture",
200 });
201 yield* stack.deploy(base);
202 expect((yield* SDK.listProjectVPCEndpoints(bRequest)).endpoints).toEqual([]);
203
204 yield* SDK.assignProjectVPCEndpoint({ ...aRequest, label: "Original fixture restriction" });
205 const baseline = (yield* SDK.listProjectVPCEndpoints(aRequest)).endpoints;
206 expect(
207 Result.isFailure(
208 yield* stack.plan(application(false, "Adopted fixture")).pipe(Effect.result),
209 ),
210 ).toBe(true);
211 const adopted = yield* stack.deploy(application(false, "Adopted fixture", true));
212 expect(adopted.restriction.initialLabel).toBe("Original fixture restriction");
213 yield* stack.deploy(application(false, "Updated adopted fixture"));
214 yield* stack.deploy(base);
215 expect((yield* SDK.listProjectVPCEndpoints(aRequest)).endpoints).toEqual(baseline);
216 expect((yield* SDK.getOrganizationVPCEndpointDetails(organizationRequest)).label).toBe(
217 organizationBaseline.label,
218 );
219 yield* stack.destroy();
220 for (const projectId of [first.a.projectId, first.b.projectId]) {
221 expect(
222 yield* SDK.getProject({ project_id: projectId }).pipe(
223 Effect.as(false),
224 Effect.catchTag("NotFound", () => Effect.succeed(true)),
225 ),
226 ).toBe(true);
227 }
228 yield* stack.destroy();
229 }),
230 {
231 tags: ["provider:neon", "provider:neon:project", "provider:neon:projectvpcendpoint", "live"],
232 timeout: 120_000,
233 exclusive: "neon:governance",
234 },
235 );