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

main
ts 214 lines 6.4 KB
Raw
1 import * as Stripe from "@alchemy.run/stripe";
2 import { isMissingStripeResource } from "@alchemy.run/stripe/missing";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import {
6 GetTaxRegistration,
7 GetTaxSettings,
8 CreateTaxSettings,
9 } from "@distilled.cloud/stripe/stripe";
10 import * as Provider from "alchemy/Provider";
11 import * as Effect from "effect/Effect";
12 import { MinimumLogLevel } from "effect/References";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: Stripe.providers() });
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 const isMissing = isMissingStripeResource;
20
21 const FAR_FUTURE = 1_893_456_000; // 2030-01-01T00:00:00Z
22
23 const ensureHeadOffice = Effect.gen(function* () {
24 const settings = yield* GetTaxSettings({});
25 if (settings.head_office != null) return;
26 yield* CreateTaxSettings({
27 head_office: {
28 address: {
29 country: "US",
30 line1: "123 Market St",
31 city: "San Francisco",
32 state: "CA",
33 postal_code: "94105",
34 },
35 },
36 });
37 });
38
39 const waitUntilExpired = (id: string) =>
40 GetTaxRegistration({ id }).pipe(
41 Effect.map((registration) =>
42 registration.status === "expired" ? ("expired" as const) : ("active" as const),
43 ),
44 Effect.catchIf(isMissing, () => Effect.succeed("expired" as const)),
45 Effect.repeat({
46 schedule: Schedule.spaced("1 second"),
47 until: (status) => status === "expired",
48 times: 10,
49 }),
50 );
51
52 test.provider(
53 "create, update, and expire a tax registration",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57 yield* ensureHeadOffice;
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* Stripe.TaxRegistration("LifecycleReg", {
62 country: "TJ",
63 countryOptions: { tj: { type: "simplified" } },
64 });
65 }),
66 );
67
68 expect(created.id).toMatch(/^taxreg_/);
69 expect(created.country).toEqual("TJ");
70 expect(created.countryOptions.tj?.type).toEqual("simplified");
71 expect(created.status).toEqual("active");
72 expect(created.expiresAt).toBeUndefined();
73 expect(created.activeFrom).toEqual(expect.any(Number));
74 expect(created.created).toEqual(expect.any(Number));
75 expect(created.livemode).toEqual(false);
76
77 const fetched = yield* GetTaxRegistration({ id: created.id });
78 expect(fetched.id).toEqual(created.id);
79 expect(fetched.country).toEqual("TJ");
80 expect(fetched.country_options.tj?.type).toEqual("simplified");
81 expect(fetched.status).toEqual("active");
82 expect(fetched.expires_at).toBeNull();
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* Stripe.TaxRegistration("LifecycleReg", {
87 country: "TJ",
88 countryOptions: { tj: { type: "simplified" } },
89 expiresAt: FAR_FUTURE,
90 });
91 }),
92 );
93
94 expect(updated.id).toEqual(created.id);
95 expect(updated.country).toEqual("TJ");
96 expect(updated.expiresAt).toEqual(FAR_FUTURE);
97 expect(updated.status).toEqual("active");
98
99 const refetched = yield* GetTaxRegistration({ id: updated.id });
100 expect(refetched.id).toEqual(updated.id);
101 expect(refetched.expires_at).toEqual(FAR_FUTURE);
102 expect(refetched.status).toEqual("active");
103
104 yield* stack.destroy();
105
106 const expired = yield* waitUntilExpired(created.id);
107 expect(expired).toEqual("expired");
108 const deactivated = yield* GetTaxRegistration({ id: created.id });
109 expect(deactivated.status).toEqual("expired");
110 }).pipe(logLevel),
111 {
112 tags: [
113 "provider:stripe",
114 "provider:stripe:taxregistration",
115 "provider:stripe:taxsettings",
116 "live",
117 ],
118 timeout: 120_000,
119 },
120 );
121
122 test.provider(
123 "list enumerates the deployed tax registration",
124 (stack) =>
125 Effect.gen(function* () {
126 yield* stack.destroy();
127 yield* ensureHeadOffice;
128
129 const deployed = yield* stack.deploy(
130 Effect.gen(function* () {
131 return yield* Stripe.TaxRegistration("ListReg", {
132 country: "ZM",
133 countryOptions: { zm: { type: "simplified" } },
134 });
135 }),
136 );
137
138 const provider = yield* Provider.findProvider(Stripe.TaxRegistration);
139 const all = yield* provider.list();
140 const found = all.find((registration) => registration.id === deployed.id);
141 expect(found).toBeDefined();
142 expect(found?.country).toEqual("ZM");
143 expect(found?.countryOptions.zm?.type).toEqual("simplified");
144 expect(found?.status).toEqual("active");
145
146 yield* stack.destroy();
147
148 const expired = yield* waitUntilExpired(deployed.id);
149 expect(expired).toEqual("expired");
150 }).pipe(logLevel),
151 {
152 tags: [
153 "provider:stripe",
154 "provider:stripe:taxregistration",
155 "provider:stripe:taxsettings",
156 "live",
157 ],
158 timeout: 120_000,
159 },
160 );
161
162 test.provider(
163 "replace when country changes",
164 (stack) =>
165 Effect.gen(function* () {
166 yield* stack.destroy();
167 yield* ensureHeadOffice;
168
169 const created = yield* stack.deploy(
170 Effect.gen(function* () {
171 return yield* Stripe.TaxRegistration("ReplaceReg", {
172 country: "SR",
173 countryOptions: { sr: { type: "standard" } },
174 });
175 }),
176 );
177
178 expect(created.country).toEqual("SR");
179
180 const replaced = yield* stack.deploy(
181 Effect.gen(function* () {
182 return yield* Stripe.TaxRegistration("ReplaceReg", {
183 country: "UZ",
184 countryOptions: { uz: { type: "simplified" } },
185 });
186 }),
187 );
188
189 expect(replaced.id).not.toEqual(created.id);
190 expect(replaced.country).toEqual("UZ");
191 expect(replaced.countryOptions.uz?.type).toEqual("simplified");
192
193 const fetched = yield* GetTaxRegistration({ id: replaced.id });
194 expect(fetched.id).toEqual(replaced.id);
195 expect(fetched.country).toEqual("UZ");
196
197 const oldExpired = yield* waitUntilExpired(created.id);
198 expect(oldExpired).toEqual("expired");
199
200 yield* stack.destroy();
201
202 const gone = yield* waitUntilExpired(replaced.id);
203 expect(gone).toEqual("expired");
204 }).pipe(logLevel),
205 {
206 tags: [
207 "provider:stripe",
208 "provider:stripe:taxregistration",
209 "provider:stripe:taxsettings",
210 "live",
211 ],
212 timeout: 120_000,
213 },
214 );