The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import { describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | |
| 9 | class RefundDeclined extends Domain.Rejection("RefundDeclined", { reason: Schema.String }) {} |
| 10 | class CardFrozen extends Domain.Rejection("CardFrozen") {} |
| 11 | |
| 12 | class Payments extends Domain.ExternalSystem("Payments", { |
| 13 | refund: { |
| 14 | args: { settlementId: Schema.String, amount: Schema.Int }, |
| 15 | success: { refundId: Schema.String }, |
| 16 | error: RefundDeclined, |
| 17 | }, |
| 18 | ping: {}, |
| 19 | }) {} |
| 20 | |
| 21 | /** An adapter whose answers the test picks, to break each side of the schema. */ |
| 22 | const adapter = (answer: (args: { settlementId: string; amount: number }) => unknown) => { |
| 23 | const seen: Array<unknown> = []; |
| 24 | const layer = Payments.toLayer( |
| 25 | Effect.succeed({ |
| 26 | refund: (args) => { |
| 27 | seen.push(args); |
| 28 | return answer(args) as never; |
| 29 | }, |
| 30 | ping: () => Effect.succeed("pong" as unknown as void), |
| 31 | }), |
| 32 | ); |
| 33 | return { seen, layer }; |
| 34 | }; |
| 35 | |
| 36 | const refund = (args: unknown) => |
| 37 | Effect.gen(function* () { |
| 38 | const payments = yield* Payments; |
| 39 | return yield* payments.refund(args as never); |
| 40 | }); |
| 41 | |
| 42 | const defectOf = <A, E>(exit: Exit.Exit<A, E>) => |
| 43 | Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined; |
| 44 | |
| 45 | describe("Domain ExternalSystem", { tags: ["unit", "local"] }, () => { |
| 46 | it.effect("passes valid arguments to the adapter and returns its answer", () => |
| 47 | Effect.gen(function* () { |
| 48 | const { seen, layer } = adapter(({ settlementId }) => |
| 49 | Effect.succeed({ refundId: `re_${settlementId}` }), |
| 50 | ); |
| 51 | const result = yield* refund({ settlementId: "st_1", amount: 500 }).pipe( |
| 52 | Effect.provide(layer), |
| 53 | ); |
| 54 | expect(result).toEqual({ refundId: "re_st_1" }); |
| 55 | expect(seen).toEqual([{ settlementId: "st_1", amount: 500 }]); |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | it.effect("keeps a declared error a typed failure", () => |
| 60 | Effect.gen(function* () { |
| 61 | const { layer } = adapter(() => Effect.fail(new RefundDeclined({ reason: "card-closed" }))); |
| 62 | const error = yield* refund({ settlementId: "st_1", amount: 500 }).pipe( |
| 63 | Effect.flip, |
| 64 | Effect.provide(layer), |
| 65 | ); |
| 66 | expect(error).toEqual(new RefundDeclined({ reason: "card-closed" })); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | it.effect("arguments the schema rejects die before the adapter runs", () => |
| 71 | Effect.gen(function* () { |
| 72 | const { seen, layer } = adapter(() => Effect.succeed({ refundId: "re_1" })); |
| 73 | const exit = yield* refund({ settlementId: "st_1", amount: 1.5 }).pipe( |
| 74 | Effect.exit, |
| 75 | Effect.provide(layer), |
| 76 | ); |
| 77 | const defect = defectOf(exit); |
| 78 | expect(defect).toMatchObject({ |
| 79 | _tag: "ExternalSystemSchemaError", |
| 80 | system: "Payments", |
| 81 | op: "refund", |
| 82 | part: "args", |
| 83 | }); |
| 84 | expect(String((defect as Error).message)).toMatch( |
| 85 | /^Payments\.refund was called with arguments its schema rejects\n.*amount/s, |
| 86 | ); |
| 87 | expect(seen).toEqual([]); |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | it.effect("a value the success schema rejects dies", () => |
| 92 | Effect.gen(function* () { |
| 93 | const { layer } = adapter(() => Effect.succeed({ refundId: 42 })); |
| 94 | const exit = yield* refund({ settlementId: "st_1", amount: 500 }).pipe( |
| 95 | Effect.exit, |
| 96 | Effect.provide(layer), |
| 97 | ); |
| 98 | expect(defectOf(exit)).toMatchObject({ system: "Payments", op: "refund", part: "success" }); |
| 99 | expect(String((defectOf(exit) as Error).message)).toMatch( |
| 100 | /^Payments\.refund returned a value its success schema rejects\n.*refundId/s, |
| 101 | ); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | it.effect("an error the operation does not declare dies", () => |
| 106 | Effect.gen(function* () { |
| 107 | const { layer } = adapter(() => Effect.fail(new CardFrozen())); |
| 108 | const exit = yield* refund({ settlementId: "st_1", amount: 500 }).pipe( |
| 109 | Effect.exit, |
| 110 | Effect.provide(layer), |
| 111 | ); |
| 112 | expect(defectOf(exit)).toMatchObject({ system: "Payments", op: "refund", part: "error" }); |
| 113 | expect(String((defectOf(exit) as Error).message)).toMatch( |
| 114 | /^Payments\.refund failed with an error its error schema rejects\n/, |
| 115 | ); |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | it.effect("keeps every error of a declared list a typed failure", () => |
| 120 | Effect.gen(function* () { |
| 121 | class Cards extends Domain.ExternalSystem("Cards", { |
| 122 | charge: { args: { amount: Schema.Int }, error: [RefundDeclined, CardFrozen] }, |
| 123 | }) {} |
| 124 | const layer = Cards.toLayer( |
| 125 | Effect.succeed({ |
| 126 | charge: ({ amount }) => |
| 127 | amount > 100 |
| 128 | ? Effect.fail(new CardFrozen()) |
| 129 | : Effect.fail(new RefundDeclined({ reason: "low" })), |
| 130 | }), |
| 131 | ); |
| 132 | const charge = (amount: number) => |
| 133 | Effect.gen(function* () { |
| 134 | const cards = yield* Cards; |
| 135 | return yield* cards.charge({ amount }); |
| 136 | }).pipe(Effect.flip, Effect.provide(layer)); |
| 137 | expect(yield* charge(500)).toEqual(new CardFrozen()); |
| 138 | expect(yield* charge(5)).toEqual(new RefundDeclined({ reason: "low" })); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | it.effect("drops fields the success schema does not declare", () => |
| 143 | Effect.gen(function* () { |
| 144 | const { layer } = adapter(() => Effect.succeed({ refundId: "re_1", raw: { vendor: "x" } })); |
| 145 | const result = yield* refund({ settlementId: "st_1", amount: 500 }).pipe( |
| 146 | Effect.provide(layer), |
| 147 | ); |
| 148 | expect(result).toEqual({ refundId: "re_1" }); |
| 149 | }), |
| 150 | ); |
| 151 | |
| 152 | it.effect("leaves the value of an operation without a success schema alone", () => |
| 153 | Effect.gen(function* () { |
| 154 | const { layer } = adapter(() => Effect.succeed({ refundId: "re_1" })); |
| 155 | const result = yield* Effect.gen(function* () { |
| 156 | const payments = yield* Payments; |
| 157 | return yield* payments.ping(); |
| 158 | }).pipe(Effect.provide(layer)); |
| 159 | expect(result).toBe("pong"); |
| 160 | }), |
| 161 | ); |
| 162 | |
| 163 | it.effect("validate: false skips every check", () => |
| 164 | Effect.gen(function* () { |
| 165 | const layer = Payments.toLayer( |
| 166 | Effect.succeed({ |
| 167 | refund: () => Effect.succeed({ refundId: 42 } as never), |
| 168 | ping: () => Effect.void, |
| 169 | }), |
| 170 | { validate: false }, |
| 171 | ); |
| 172 | const result = yield* refund({ settlementId: "st_1", amount: 1.5 }).pipe( |
| 173 | Effect.provide(layer), |
| 174 | ); |
| 175 | expect(result).toEqual({ refundId: 42 }); |
| 176 | }), |
| 177 | ); |
| 178 | |
| 179 | it.effect("refuses an operation named after a static of the class", () => |
| 180 | Effect.gen(function* () { |
| 181 | class Clash extends Domain.ExternalSystem("Clash", { |
| 182 | // @ts-expect-error toLayer is the class's own static |
| 183 | toLayer: {}, |
| 184 | }) {} |
| 185 | const exit = yield* Effect.exit( |
| 186 | Layer.build(Clash.toLayer(Effect.succeed({} as never))).pipe(Effect.scoped), |
| 187 | ); |
| 188 | expect(defectOf(exit)).toMatchObject({ |
| 189 | _tag: "DefinitionError", |
| 190 | reason: { _tag: "ReservedOperation", system: "Clash", operation: "toLayer" }, |
| 191 | }); |
| 192 | }), |
| 193 | ); |
| 194 | }); |