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

main
ts 191 lines 6.99 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 GetIssuingPersonalizationDesigns,
7 GetIssuingPersonalizationDesign,
8 GetIssuingPhysicalBundles,
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 Result from "effect/Result";
14
15 const { test } = Test.make({ providers: Stripe.providers() });
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 const ISSUING_ENABLED = process.env.STRIPE_TEST_ISSUING === "1";
20
21 const pickPhysicalBundle = Effect.gen(function* () {
22 const standard = yield* GetIssuingPhysicalBundles({
23 type: "standard",
24 status: "active",
25 limit: 1,
26 });
27 if (standard.data[0] !== undefined) return standard.data[0];
28 const active = yield* GetIssuingPhysicalBundles({
29 status: "active",
30 limit: 1,
31 });
32 if (active.data[0] !== undefined) return active.data[0];
33 const any = yield* GetIssuingPhysicalBundles({ limit: 1 });
34 return any.data[0];
35 });
36
37 test.provider(
38 "issuing personalization designs entitlement probe",
39 (stack) =>
40 Effect.gen(function* () {
41 yield* stack.destroy();
42
43 const result = yield* GetIssuingPersonalizationDesigns({ limit: 1 }).pipe(Effect.result);
44
45 if (Result.isFailure(result)) {
46 // Unentitled — distilled must fail with a typed tag, never
47 // UnknownStripeError. Exact error:
48 // InvalidRequestError: "Your account is not set up to use Issuing.
49 // Please visit https://dashboard.stripe.com/issuing/overview to get
50 // started."
51 expect(result.failure._tag).not.toEqual("UnknownStripeError");
52 expect(["InvalidRequestError", "Forbidden"]).toContain(result.failure._tag);
53 if (result.failure._tag === "InvalidRequestError") {
54 expect(result.failure.message).toContain("not set up to use Issuing");
55 }
56 yield* stack.destroy();
57 return;
58 }
59
60 expect(Array.isArray(result.success.data)).toBe(true);
61 yield* stack.destroy();
62 }).pipe(logLevel),
63 {
64 tags: ["provider:stripe", "provider:stripe:issuingpersonalizationdesign", "live"],
65 timeout: 60_000,
66 },
67 );
68
69 test.provider.skipIf(!ISSUING_ENABLED)(
70 "create, update, and destroy a personalization design",
71 (stack) =>
72 Effect.gen(function* () {
73 yield* stack.destroy();
74
75 const bundle = yield* pickPhysicalBundle;
76 expect(bundle).toBeDefined();
77 const physicalBundle = bundle!.id;
78
79 const created = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* Stripe.IssuingPersonalizationDesign("CardDesign", {
82 physicalBundle,
83 name: "Alchemy Card Design",
84 metadata: { line: "test" },
85 });
86 }),
87 );
88
89 expect(created.id).toMatch(/^ipcd_/);
90 expect(created.name).toEqual("Alchemy Card Design");
91 expect(created.physicalBundle).toEqual(physicalBundle);
92 expect(created.lookupKey).toEqual(expect.any(String));
93 expect(created.isDefault).toEqual(false);
94 expect(created.metadata).toMatchObject({ line: "test" });
95 expect(created.created).toEqual(expect.any(Number));
96 expect(created.livemode).toEqual(false);
97 expect(["active", "inactive", "rejected", "review"]).toContain(created.status);
98
99 const fetched = yield* GetIssuingPersonalizationDesign({
100 personalization_design: created.id,
101 });
102 expect(fetched.id).toEqual(created.id);
103 expect(fetched.name).toEqual("Alchemy Card Design");
104 expect(
105 typeof fetched.physical_bundle === "string"
106 ? fetched.physical_bundle
107 : fetched.physical_bundle.id,
108 ).toEqual(physicalBundle);
109 expect(fetched.metadata?.line).toEqual("test");
110 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
111 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
112 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
113
114 const updated = yield* stack.deploy(
115 Effect.gen(function* () {
116 return yield* Stripe.IssuingPersonalizationDesign("CardDesign", {
117 physicalBundle,
118 name: "Alchemy Card Design Updated",
119 metadata: { line: "test", version: "2" },
120 });
121 }),
122 );
123
124 expect(updated.id).toEqual(created.id);
125 expect(updated.name).toEqual("Alchemy Card Design Updated");
126 expect(updated.physicalBundle).toEqual(physicalBundle);
127 expect(updated.metadata).toEqual({ line: "test", version: "2" });
128
129 const refetched = yield* GetIssuingPersonalizationDesign({
130 personalization_design: updated.id,
131 });
132 expect(refetched.id).toEqual(updated.id);
133 expect(refetched.name).toEqual("Alchemy Card Design Updated");
134 expect(refetched.metadata?.line).toEqual("test");
135 expect(refetched.metadata?.version).toEqual("2");
136 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
137
138 yield* stack.destroy();
139
140 // No delete/archive API — destroy is a no-op and the design remains.
141 const residue = yield* GetIssuingPersonalizationDesign({
142 personalization_design: created.id,
143 }).pipe(Effect.catchIf(isMissingStripeResource, () => Effect.succeed(undefined)));
144 expect(residue).toBeDefined();
145 expect(residue?.id).toEqual(created.id);
146 }).pipe(logLevel),
147 {
148 tags: ["provider:stripe", "provider:stripe:issuingpersonalizationdesign", "live"],
149 timeout: 120_000,
150 },
151 );
152
153 test.provider.skipIf(!ISSUING_ENABLED)(
154 "list enumerates the deployed personalization design",
155 (stack) =>
156 Effect.gen(function* () {
157 yield* stack.destroy();
158
159 const bundle = yield* pickPhysicalBundle;
160 expect(bundle).toBeDefined();
161 const physicalBundle = bundle!.id;
162
163 const deployed = yield* stack.deploy(
164 Effect.gen(function* () {
165 return yield* Stripe.IssuingPersonalizationDesign("ListDesign", {
166 physicalBundle,
167 name: "Alchemy List Card Design",
168 metadata: { kind: "list" },
169 });
170 }),
171 );
172
173 const provider = yield* Provider.findProvider(Stripe.IssuingPersonalizationDesign);
174 const all = yield* provider.list();
175 const found = all.find((design) => design.id === deployed.id);
176 expect(found).toBeDefined();
177 expect(found?.name).toEqual(deployed.name);
178 expect(found?.physicalBundle).toEqual(physicalBundle);
179 expect(found?.metadata).toMatchObject({ kind: "list" });
180
181 yield* stack.destroy();
182
183 // Residue stays listed — destroy cannot untag or delete the object.
184 const after = yield* provider.list();
185 expect(after.find((design) => design.id === deployed.id)).toBeDefined();
186 }).pipe(logLevel),
187 {
188 tags: ["provider:stripe", "provider:stripe:issuingpersonalizationdesign", "live"],
189 timeout: 120_000,
190 },
191 );