The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Stripe from "@alchemy.run/stripe"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { GetTaxSettings } from "@distilled.cloud/stripe/stripe"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const getLive = GetTaxSettings({}); |
| 15 | |
| 16 | const waitForTaxBehavior = (expected: string | null | undefined) => |
| 17 | getLive.pipe( |
| 18 | Effect.flatMap((live) => |
| 19 | (live.defaults.tax_behavior ?? null) === (expected ?? null) |
| 20 | ? Effect.succeed(live) |
| 21 | : Effect.fail({ _tag: "TaxSettingsNotApplied" as const }), |
| 22 | ), |
| 23 | Effect.retry({ |
| 24 | while: (e) => e._tag === "TaxSettingsNotApplied", |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | const otherBehavior = (current: string | null | undefined): "exclusive" | "inclusive" => |
| 31 | current === "exclusive" ? "inclusive" : "exclusive"; |
| 32 | |
| 33 | // Account-level singleton: run serially so tests do not clobber each |
| 34 | // other's captured baseline under the concurrent suite. |
| 35 | describe.sequential( |
| 36 | "TaxSettings", |
| 37 | { tags: ["provider:stripe", "provider:stripe:taxsettings", "live"] }, |
| 38 | () => { |
| 39 | test.provider( |
| 40 | "updates tax behavior in place and restores the baseline on destroy", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const baseline = yield* getLive; |
| 46 | const baselineBehavior = baseline.defaults.tax_behavior ?? undefined; |
| 47 | const target = otherBehavior(baselineBehavior); |
| 48 | const target2 = otherBehavior(target); |
| 49 | |
| 50 | const created = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | return yield* Stripe.TaxSettings("AccountTax", { defaults: { taxBehavior: target } }); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | expect(created.object).toEqual("tax.settings"); |
| 57 | expect(created.taxBehavior).toEqual(target); |
| 58 | expect(created.initialSettings.taxBehavior).toEqual(baselineBehavior); |
| 59 | expect(created.livemode).toEqual(false); |
| 60 | |
| 61 | const live1 = yield* waitForTaxBehavior(target); |
| 62 | expect(live1.defaults.tax_behavior).toEqual(target); |
| 63 | |
| 64 | const updated = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* Stripe.TaxSettings("AccountTax", { |
| 67 | defaults: { taxBehavior: target2 }, |
| 68 | }); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(updated.object).toEqual("tax.settings"); |
| 73 | expect(updated.taxBehavior).toEqual(target2); |
| 74 | expect(updated.initialSettings.taxBehavior).toEqual(baselineBehavior); |
| 75 | |
| 76 | const live2 = yield* waitForTaxBehavior(target2); |
| 77 | expect(live2.defaults.tax_behavior).toEqual(target2); |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | |
| 81 | // Stripe will not unset a field that was originally null. Restore |
| 82 | // only applies when the captured baseline already had a value. |
| 83 | if (baselineBehavior !== undefined) { |
| 84 | const restored = yield* waitForTaxBehavior(baselineBehavior); |
| 85 | expect(restored.defaults.tax_behavior).toEqual(baselineBehavior); |
| 86 | } else { |
| 87 | const after = yield* getLive; |
| 88 | expect(after.defaults.tax_behavior).toEqual(target2); |
| 89 | } |
| 90 | }).pipe(logLevel), |
| 91 | { timeout: 120_000 }, |
| 92 | ); |
| 93 | |
| 94 | test.provider( |
| 95 | "no-op deploy when desired settings already match the live account", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const baseline = yield* getLive; |
| 101 | const taxBehavior = |
| 102 | (baseline.defaults.tax_behavior as Stripe.TaxSettingsTaxBehavior | null | undefined) ?? |
| 103 | undefined; |
| 104 | |
| 105 | const setting = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* Stripe.TaxSettings("NoopTax", { |
| 108 | ...(taxBehavior !== undefined ? { defaults: { taxBehavior } } : {}), |
| 109 | }); |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | expect(setting.taxBehavior).toEqual(taxBehavior); |
| 114 | expect(setting.initialSettings.taxBehavior).toEqual(taxBehavior); |
| 115 | expect(setting.taxCode).toEqual(baseline.defaults.tax_code ?? undefined); |
| 116 | |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | const after = yield* getLive; |
| 120 | expect(after.defaults.tax_behavior).toEqual(baseline.defaults.tax_behavior); |
| 121 | expect(after.defaults.tax_code).toEqual(baseline.defaults.tax_code); |
| 122 | }).pipe(logLevel), |
| 123 | { timeout: 120_000 }, |
| 124 | ); |
| 125 | |
| 126 | test.provider( |
| 127 | "list returns the account tax settings singleton", |
| 128 | (stack) => |
| 129 | Effect.gen(function* () { |
| 130 | yield* stack.destroy(); |
| 131 | |
| 132 | const provider = yield* Provider.findProvider(Stripe.TaxSettings); |
| 133 | const all = yield* provider.list(); |
| 134 | |
| 135 | expect(all.length).toEqual(1); |
| 136 | expect(all[0]?.object).toEqual("tax.settings"); |
| 137 | expect(all[0]?.initialSettings.taxBehavior).toEqual(all[0]?.taxBehavior); |
| 138 | expect(all[0]?.initialSettings.taxCode).toEqual(all[0]?.taxCode); |
| 139 | |
| 140 | yield* stack.destroy(); |
| 141 | }).pipe(logLevel), |
| 142 | { timeout: 120_000 }, |
| 143 | ); |
| 144 | }, |
| 145 | ); |