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

main
ts 200 lines 6.78 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 { GetCustomerTaxIdsById } from "@distilled.cloud/stripe/stripe";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Stripe.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const isMissing = isMissingStripeResource;
16
17 const waitUntilGone = (customer: string, id: string) =>
18 GetCustomerTaxIdsById({ customer, id }).pipe(
19 Effect.as("found" as const),
20 Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28 test.provider(
29 "create, update, and delete a customer tax id",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const created = yield* stack.deploy(
35 Effect.gen(function* () {
36 const customer = yield* Stripe.Customer("TaxIdLifecycleCustomer", {
37 email: "alchemy.taxid.lifecycle@example.com",
38 name: "Alchemy Tax ID Lifecycle",
39 });
40 const taxId = yield* Stripe.CustomerTaxId("LifecycleVat", {
41 customer: customer.id,
42 type: "eu_vat",
43 value: "DE123456789",
44 });
45 return { customer, taxId };
46 }),
47 );
48
49 expect(created.taxId.id).toMatch(/^txi_/);
50 expect(created.taxId.customer).toEqual(created.customer.id);
51 expect(created.taxId.type).toEqual("eu_vat");
52 expect(created.taxId.value).toEqual("DE123456789");
53 expect(created.taxId.country).toEqual("DE");
54 expect(created.taxId.livemode).toEqual(false);
55 expect(created.taxId.created).toEqual(expect.any(Number));
56
57 const fetched = yield* GetCustomerTaxIdsById({
58 customer: created.taxId.customer,
59 id: created.taxId.id,
60 });
61 expect(fetched.id).toEqual(created.taxId.id);
62 expect(fetched.type).toEqual("eu_vat");
63 expect(fetched.value).toEqual("DE123456789");
64 expect(fetched.livemode).toEqual(false);
65
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 const customer = yield* Stripe.Customer("TaxIdLifecycleCustomer", {
69 email: "alchemy.taxid.lifecycle@example.com",
70 name: "Alchemy Tax ID Lifecycle",
71 });
72 const taxId = yield* Stripe.CustomerTaxId("LifecycleVat", {
73 customer: customer.id,
74 type: "eu_vat",
75 value: "DE123456789",
76 });
77 return { customer, taxId };
78 }),
79 );
80
81 expect(updated.taxId.id).toEqual(created.taxId.id);
82 expect(updated.taxId.customer).toEqual(created.customer.id);
83 expect(updated.taxId.type).toEqual("eu_vat");
84 expect(updated.taxId.value).toEqual("DE123456789");
85
86 yield* stack.destroy();
87
88 const gone = yield* waitUntilGone(created.taxId.customer, created.taxId.id);
89 expect(gone).toEqual("gone");
90 }).pipe(logLevel),
91 {
92 tags: ["provider:stripe", "provider:stripe:customer", "provider:stripe:customertaxid", "live"],
93 timeout: 120_000,
94 },
95 );
96
97 test.provider(
98 "list enumerates the deployed customer tax id",
99 (stack) =>
100 Effect.gen(function* () {
101 yield* stack.destroy();
102
103 const deployed = yield* stack.deploy(
104 Effect.gen(function* () {
105 const customer = yield* Stripe.Customer("TaxIdListCustomer", {
106 email: "alchemy.taxid.list@example.com",
107 name: "Alchemy Tax ID List",
108 });
109 const taxId = yield* Stripe.CustomerTaxId("ListVat", {
110 customer: customer.id,
111 type: "eu_vat",
112 value: "ATU12345678",
113 });
114 return { customer, taxId };
115 }),
116 );
117
118 const provider = yield* Provider.findProvider(Stripe.CustomerTaxId);
119 const all = yield* provider.list();
120 const found = all.find((taxId) => taxId.id === deployed.taxId.id);
121 expect(found).toBeDefined();
122 expect(found?.customer).toEqual(deployed.customer.id);
123 expect(found?.type).toEqual("eu_vat");
124 expect(found?.value).toEqual("ATU12345678");
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(deployed.taxId.customer, deployed.taxId.id);
129 expect(gone).toEqual("gone");
130
131 const after = yield* provider.list();
132 expect(after.find((taxId) => taxId.id === deployed.taxId.id)).toBeUndefined();
133 }).pipe(logLevel),
134 {
135 tags: ["provider:stripe", "provider:stripe:customer", "provider:stripe:customertaxid", "live"],
136 timeout: 120_000,
137 },
138 );
139
140 test.provider(
141 "replace when the tax id value changes",
142 (stack) =>
143 Effect.gen(function* () {
144 yield* stack.destroy();
145
146 const created = yield* stack.deploy(
147 Effect.gen(function* () {
148 const customer = yield* Stripe.Customer("TaxIdReplaceCustomer", {
149 email: "alchemy.taxid.replace@example.com",
150 name: "Alchemy Tax ID Replace",
151 });
152 const taxId = yield* Stripe.CustomerTaxId("ReplaceVat", {
153 customer: customer.id,
154 type: "eu_vat",
155 value: "DE123456789",
156 });
157 return { customer, taxId };
158 }),
159 );
160
161 expect(created.taxId.value).toEqual("DE123456789");
162
163 const replaced = yield* stack.deploy(
164 Effect.gen(function* () {
165 const customer = yield* Stripe.Customer("TaxIdReplaceCustomer", {
166 email: "alchemy.taxid.replace@example.com",
167 name: "Alchemy Tax ID Replace",
168 });
169 const taxId = yield* Stripe.CustomerTaxId("ReplaceVat", {
170 customer: customer.id,
171 type: "eu_vat",
172 value: "DE000000000",
173 });
174 return { customer, taxId };
175 }),
176 );
177
178 expect(replaced.taxId.id).not.toEqual(created.taxId.id);
179 expect(replaced.taxId.customer).toEqual(created.customer.id);
180 expect(replaced.taxId.value).toEqual("DE000000000");
181
182 const newFetched = yield* GetCustomerTaxIdsById({
183 customer: replaced.taxId.customer,
184 id: replaced.taxId.id,
185 });
186 expect(newFetched.value).toEqual("DE000000000");
187
188 const oldGone = yield* waitUntilGone(created.taxId.customer, created.taxId.id);
189 expect(oldGone).toEqual("gone");
190
191 yield* stack.destroy();
192
193 const gone = yield* waitUntilGone(replaced.taxId.customer, replaced.taxId.id);
194 expect(gone).toEqual("gone");
195 }).pipe(logLevel),
196 {
197 tags: ["provider:stripe", "provider:stripe:customer", "provider:stripe:customertaxid", "live"],
198 timeout: 120_000,
199 },
200 );