@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 147 lines 5.17 KB
Raw
1 import { expect } from "bun:test";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as Stripe from "@alchemy.run/stripe";
4 import * as Test from "@alchemy.run/test/Bun";
5 import {
6 DeleteAccount,
7 GetAccountByAccount,
8 GetWebhookEndpoints,
9 UpdateAccount,
10 } from "@distilled.cloud/stripe/stripe";
11 import * as Alchemy from "alchemy";
12 import * as Effect from "effect/Effect";
13 import * as FetchHttpClient from "effect/http/FetchHttpClient";
14 import * as HttpClient from "effect/http/HttpClient";
15 import * as HttpClientRequest from "effect/http/HttpClientRequest";
16 import * as Layer from "effect/Layer";
17 import * as Schedule from "effect/Schedule";
18 import Stack from "../alchemy.run.ts";
19
20 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
21 providers: Layer.mergeAll(Cloudflare.providers(), Stripe.providers()),
22 state: Alchemy.localState(),
23 });
24
25 const { getWhenReady } = Test;
26
27 // Out-of-band verification resolves the same stored Stripe key the deploy
28 // uses.
29 const StripeHttp = Layer.mergeAll(
30 Stripe.StripeAuth,
31 Stripe.fromAuthProvider(),
32 FetchHttpClient.layer,
33 );
34
35 interface MerchantView {
36 id: string;
37 email: string;
38 onboarded: boolean;
39 chargesEnabled: boolean;
40 payoutsEnabled: boolean;
41 updatedAt: number;
42 }
43
44 const stack = beforeAll(deploy(Stack), { timeout: 180_000 });
45
46 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
47 timeout: 120_000,
48 });
49
50 const readMerchant = (base: string, id: string) =>
51 Effect.gen(function* () {
52 const res = yield* HttpClient.execute(HttpClientRequest.get(`${base}/merchants/${id}`));
53 expect(res.status).toBe(200);
54 return (yield* res.json) as unknown as MerchantView;
55 });
56
57 test(
58 "deploys a worker URL and provisions the account.updated webhook",
59 Effect.gen(function* () {
60 const { url } = yield* stack;
61 expect(url).toBeString();
62 const delivery = `${url.replace(/\/+$/, "")}/webhooks/stripe`;
63 const endpoints = yield* GetWebhookEndpoints({ limit: 100 }).pipe(Effect.provide(StripeHttp));
64 const endpoint = endpoints.data.find((e) => e.url.replace(/\/+$/, "") === delivery);
65 expect(endpoint).toBeDefined();
66 expect(endpoint?.enabled_events).toContain("account.updated");
67 }),
68 );
69
70 // Connect must be enabled on the test account. Skipped otherwise.
71 test.skipIf(process.env.STRIPE_TEST_CONNECT !== "1")(
72 "onboards a merchant, resumes onboarding, and records account.updated",
73 Effect.gen(function* () {
74 const { url } = yield* stack;
75 const base = url.replace(/\/+$/, "");
76 yield* getWhenReady(base);
77
78 // 1. Sign up.
79 const signup = yield* HttpClient.execute(
80 HttpClientRequest.post(`${base}/merchants`).pipe(
81 HttpClientRequest.bodyJsonUnsafe({ email: "merchant@example.com" }),
82 ),
83 );
84 expect(signup.status).toBe(201);
85 const { merchantId, onboardingUrl } = (yield* signup.json) as {
86 merchantId: string;
87 onboardingUrl: string;
88 };
89 expect(merchantId).toMatch(/^acct_/);
90 expect(onboardingUrl).toMatch(/^https:\/\/connect\.stripe\.com\//);
91
92 const account = yield* GetAccountByAccount({ account: merchantId }).pipe(
93 Effect.provide(StripeHttp),
94 );
95 expect(account.type).toEqual("express");
96 expect(account.details_submitted).toBe(false);
97
98 // The row exists and the merchant is not live yet.
99 const fresh = yield* readMerchant(base, merchantId);
100 expect(fresh.email).toEqual("merchant@example.com");
101 expect(fresh.onboarded).toBe(false);
102 expect(fresh.chargesEnabled).toBe(false);
103
104 // 2. Links are single-use; the platform can always mint another.
105 const resume = yield* HttpClient.execute(
106 HttpClientRequest.post(`${base}/merchants/${merchantId}/onboarding`),
107 );
108 expect(resume.status).toBe(200);
109 const resumed = (yield* resume.json) as { onboardingUrl: string };
110 expect(resumed.onboardingUrl).toMatch(/^https:\/\/connect\.stripe\.com\//);
111 expect(resumed.onboardingUrl).not.toEqual(onboardingUrl);
112
113 // Unknown merchants get a 404, not a Stripe error.
114 const unknown = yield* HttpClient.execute(
115 HttpClientRequest.post(`${base}/merchants/acct_doesnotexist/onboarding`),
116 );
117 expect(unknown.status).toBe(404);
118
119 // 3. Any change to the account fires account.updated; the handler
120 // copies the capability flags into D1 and bumps updated_at. Nudging
121 // metadata is enough to trigger it without completing the full
122 // onboarding form. Wait a beat so the bump is distinguishable from
123 // the row's creation second.
124 yield* Effect.sleep("2 seconds");
125 yield* UpdateAccount({
126 account: merchantId,
127 metadata: { touched: String(Date.now()) },
128 }).pipe(Effect.provide(StripeHttp));
129
130 const observed = yield* readMerchant(base, merchantId).pipe(
131 Effect.repeat({
132 schedule: Schedule.spaced("5 seconds"),
133 until: (m) => m.updatedAt > fresh.updatedAt,
134 times: 24,
135 }),
136 );
137 expect(observed.id).toEqual(merchantId);
138 expect(observed.onboarded).toBe(account.details_submitted ?? false);
139 expect(observed.chargesEnabled).toBe(account.charges_enabled ?? false);
140
141 yield* DeleteAccount({ account: merchantId }).pipe(
142 Effect.catch(() => Effect.void),
143 Effect.provide(StripeHttp),
144 );
145 }),
146 { timeout: 300_000 },
147 );