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

main
ts 274 lines 9.76 KB
Raw
1 import {
2 OrganizationSpendingLimit,
3 organizationSpendingLimitSnapshot,
4 validateOrganizationSpendingLimit,
5 } from "@alchemy.run/neon/OrganizationSpendingLimit";
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, stripUnowned, Unowned } 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
17 const context = {
18 id: "Alert",
19 fqn: "Alert",
20 instanceId: "alert-validation",
21 oldBindings: [],
22 newBindings: [],
23 };
24
25 test(
26 "spending alerts require positive integer cents and an organization identity",
27 Effect.gen(function* () {
28 for (const spendingLimitCents of [0, -1, 1.5, Infinity, NaN, Number.MAX_SAFE_INTEGER + 1]) {
29 expect(
30 Result.isFailure(
31 yield* validateOrganizationSpendingLimit({
32 orgId: "org-dedicated-fixture",
33 spendingLimitCents,
34 }).pipe(Effect.result),
35 ),
36 ).toBe(true);
37 }
38 expect(
39 Result.isFailure(
40 yield* validateOrganizationSpendingLimit({ orgId: "", spendingLimitCents: 100 }).pipe(
41 Effect.result,
42 ),
43 ),
44 ).toBe(true);
45 yield* validateOrganizationSpendingLimit({
46 orgId: "org-dedicated-fixture",
47 spendingLimitCents: 100,
48 });
49 }),
50 { tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"] },
51 );
52
53 test.provider(
54 "spending organization changes fail before replacing the existing policy",
55 () =>
56 Effect.gen(function* () {
57 const provider = yield* OrganizationSpendingLimit.Provider;
58 const olds = { orgId: "org-old", spendingLimitCents: 100 };
59 expect(
60 yield* provider.diff!({
61 ...context,
62 olds,
63 news: { orgId: "org-new", spendingLimitCents: 100 },
64 output: undefined,
65 }).pipe(
66 Effect.as(false),
67 Effect.catchTag("InvalidOrganizationSpendingLimit", () => Effect.succeed(true)),
68 ),
69 ).toBe(true);
70 for (const news of [
71 Output.literal(olds),
72 Effect.succeed(olds),
73 { ...olds, orgId: Output.literal("org-new") },
74 ]) {
75 expect(
76 yield* provider.diff!({ ...context, olds, news, output: undefined }),
77 ).toBeUndefined();
78 }
79 expect(
80 yield* provider.diff!({
81 ...context,
82 olds,
83 news: { ...olds, spendingLimitCents: Output.literal(200) },
84 output: undefined,
85 }),
86 ).toBeUndefined();
87 }).pipe(
88 Effect.provideService(
89 SDK.Credentials,
90 Effect.die("Unexpected Neon request in a no-I/O guard test"),
91 ),
92 ),
93 { tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"] },
94 );
95
96 test.provider(
97 "spending reconciliation rejects a cached identity from another organization before I/O",
98 () =>
99 Effect.gen(function* () {
100 const provider = yield* OrganizationSpendingLimit.Provider;
101 expect(
102 yield* provider
103 .reconcile({
104 ...context,
105 bindings: [],
106 session: { note: () => Effect.void },
107 olds: undefined,
108 news: { orgId: "org-new", spendingLimitCents: 100 },
109 output: {
110 orgId: "org-old",
111 spendingLimitCents: 200,
112 initialSpendingLimitCents: 200,
113 managedSpendingLimitCents: 200,
114 },
115 })
116 .pipe(
117 Effect.as(false),
118 Effect.catchTag("InvalidOrganizationSpendingLimit", () => Effect.succeed(true)),
119 ),
120 ).toBe(true);
121 }).pipe(
122 Effect.provideService(
123 SDK.Credentials,
124 Effect.die("Unexpected Neon request in a no-I/O guard test"),
125 ),
126 ),
127 { tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"] },
128 );
129
130 test(
131 "spending snapshots capture null without adoption and retain the baseline across interrupted writes",
132 Effect.sync(() => {
133 for (const baseline of [null, 400]) {
134 const snapshot = organizationSpendingLimitSnapshot("org-fixture", baseline);
135 expect(Unowned.is(snapshot)).toBe(baseline !== null);
136 expect(snapshot.initialSpendingLimitCents).toBe(baseline);
137 expect(snapshot.managedSpendingLimitCents).toBe(baseline);
138 const refreshed = organizationSpendingLimitSnapshot(
139 "org-fixture",
140 100,
141 stripUnowned(snapshot),
142 );
143 expect(Unowned.is(refreshed)).toBe(false);
144 expect(refreshed.spendingLimitCents).toBe(100);
145 expect(refreshed.initialSpendingLimitCents).toBe(baseline);
146 expect(refreshed.managedSpendingLimitCents).toBe(baseline);
147 const disappeared = organizationSpendingLimitSnapshot("org-fixture", null, refreshed);
148 expect(disappeared.initialSpendingLimitCents).toBe(baseline);
149 }
150 }),
151 { tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"] },
152 );
153
154 test.provider(
155 "spending reconciliation refuses missing baseline snapshots before I/O even with adoption enabled",
156 () =>
157 Effect.gen(function* () {
158 const provider = yield* OrganizationSpendingLimit.Provider;
159 const incomplete = organizationSpendingLimitSnapshot("org-fixture", null);
160 yield* Effect.sync(() => Reflect.deleteProperty(incomplete, "initialSpendingLimitCents"));
161 for (const output of [undefined, incomplete]) {
162 expect(
163 yield* provider
164 .reconcile({
165 ...context,
166 bindings: [],
167 session: {
168 note: () => Effect.void,
169 },
170 olds: undefined,
171 news: { orgId: "org-fixture", spendingLimitCents: 100 },
172 output,
173 })
174 .pipe(
175 adopt(true),
176 Effect.as(false),
177 Effect.catchTag("InvalidOrganizationSpendingLimit", () => Effect.succeed(true)),
178 ),
179 ).toBe(true);
180 }
181 }).pipe(
182 Effect.provideService(
183 SDK.Credentials,
184 Effect.die("Unexpected Neon request in a no-I/O guard test"),
185 ),
186 ),
187 { tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"] },
188 );
189
190 const orgId = process.env.NEON_GOVERNANCE_TEST_ORG_ID;
191 const threshold = Number(process.env.NEON_GOVERNANCE_TEST_SPENDING_LIMIT_CENTS);
192 const enabled =
193 !!orgId &&
194 process.env.NEON_GOVERNANCE_TEST_SPENDING === "1" &&
195 Number.isSafeInteger(threshold) &&
196 threshold > 0 &&
197 threshold < Number.MAX_SAFE_INTEGER - 100;
198
199 test.provider.skipIf(!enabled)(
200 "dedicated spending fixture preserves adopted baseline and removes only managed new alerts",
201 (stack) =>
202 Effect.gen(function* () {
203 yield* stack.destroy();
204 const organization = orgId!;
205 const request = { org_id: organization };
206 const original = (yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents;
207 const application = (value: number, takeOwnership = false) =>
208 OrganizationSpendingLimit("Alert", { orgId: organization, spendingLimitCents: value }).pipe(
209 adopt(takeOwnership),
210 );
211
212 if (original === null) {
213 const planned = yield* stack.plan(application(threshold));
214 expect(planned.resources.Alert.state?.attr).toMatchObject({
215 orgId: organization,
216 spendingLimitCents: null,
217 initialSpendingLimitCents: null,
218 managedSpendingLimitCents: null,
219 });
220 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBeNull();
221 const created = yield* stack.deploy(application(threshold));
222 expect(created.initialSpendingLimitCents).toBeNull();
223 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
224 threshold,
225 );
226 yield* stack.deploy(application(threshold + 1));
227 yield* SDK.setOrganizationSpendingLimit({
228 ...request,
229 spending_limit_cents: threshold + 2,
230 });
231 expect(Result.isFailure(yield* stack.destroy().pipe(Effect.result))).toBe(true);
232 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
233 threshold + 2,
234 );
235 yield* SDK.setOrganizationSpendingLimit({
236 ...request,
237 spending_limit_cents: threshold + 1,
238 });
239 yield* stack.destroy();
240 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBeNull();
241 }
242
243 const baseline = original ?? threshold + 100;
244 if (original === null)
245 yield* SDK.setOrganizationSpendingLimit({ ...request, spending_limit_cents: baseline });
246 expect(Result.isFailure(yield* stack.plan(application(threshold)).pipe(Effect.result))).toBe(
247 true,
248 );
249 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
250 baseline,
251 );
252 const adopted = yield* stack.deploy(application(threshold, true));
253 expect(adopted.initialSpendingLimitCents).toBe(baseline);
254 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
255 threshold,
256 );
257 const updated = yield* stack.deploy(application(threshold + 1));
258 expect(updated.initialSpendingLimitCents).toBe(baseline);
259 yield* stack.destroy();
260 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
261 baseline,
262 );
263 if (original === null) yield* SDK.deleteOrganizationSpendingLimit(request);
264 expect((yield* SDK.getOrganizationSpendingLimit(request)).spending_limit_cents).toBe(
265 original,
266 );
267 yield* stack.destroy();
268 }),
269 {
270 tags: ["provider:neon", "provider:neon:organizationspendinglimit", "live"],
271 timeout: 120_000,
272 exclusive: "neon:governance",
273 },
274 );