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, 04:45 UTC
| 1 | /** |
| 2 | * Live test for `StateMachine.fromProgram`: deploy a typed `Sfn` program |
| 3 | * (Lambda invoke + retry, inline Map, Choice, typed Fail + catchTag) as an |
| 4 | * EXPRESS workflow, execute it synchronously, and assert the execution |
| 5 | * output — alongside a raw-ASL `StateMachine` in the same stack proving the |
| 6 | * low-level `definition` path is unchanged. |
| 7 | */ |
| 8 | import * as AWS from "@alchemy.run/aws"; |
| 9 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 10 | import { StateMachine } from "@alchemy.run/aws/StepFunctions"; |
| 11 | import { expect } from "@alchemy.run/test"; |
| 12 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 13 | import * as sfn from "@distilled.cloud/aws/sfn"; |
| 14 | import * as Data from "effect/Data"; |
| 15 | import * as Duration from "effect/Duration"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Redacted from "effect/Redacted"; |
| 18 | import * as Schedule from "effect/Schedule"; |
| 19 | import { makeOrderProgram, type OrderOutput } from "./fixtures/order-program.ts"; |
| 20 | |
| 21 | const { test } = Test.make({ providers: AWS.providers() }); |
| 22 | |
| 23 | // Keep this a file:// URL — `.pathname` yields `/D:/...` on Windows, which |
| 24 | // resolveMainPath cannot stat; it converts URLs via fileURLToPath itself. |
| 25 | const doublerMain = new URL("./fixtures/doubler.ts", import.meta.url).href; |
| 26 | |
| 27 | const plain = (value: string | Redacted.Redacted<string> | undefined): string | undefined => |
| 28 | value === undefined ? undefined : typeof value === "string" ? value : Redacted.value(value); |
| 29 | |
| 30 | /** |
| 31 | * A sync execution finished in a non-SUCCEEDED state. Fresh execution |
| 32 | * roles propagate eventually — the first executions can fail on IAM — so |
| 33 | * the caller retries this tag on a bounded schedule. |
| 34 | */ |
| 35 | class ExecutionNotSucceeded extends Data.TaggedError("ExecutionNotSucceeded")<{ |
| 36 | readonly status: string; |
| 37 | readonly error: string | undefined; |
| 38 | readonly cause: string | undefined; |
| 39 | }> {} |
| 40 | |
| 41 | test.provider( |
| 42 | "fromProgram deploys and executes a typed program; raw definition path unchanged", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const outputs = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | const doubler = yield* Lambda.Function("SfnDoubler", { |
| 48 | main: doublerMain, |
| 49 | handler: "handler", |
| 50 | isExternal: true, |
| 51 | functionUrl: false, |
| 52 | timeout: Duration.seconds(15), |
| 53 | }); |
| 54 | const machine = yield* StateMachine.fromProgram("OrderProgram", { |
| 55 | type: "EXPRESS", |
| 56 | program: makeOrderProgram(doubler), |
| 57 | }); |
| 58 | // raw ASL path, same stack — stays first-class underneath |
| 59 | const raw = yield* StateMachine("RawWorkflow", { |
| 60 | definition: { |
| 61 | Comment: "raw", |
| 62 | StartAt: "Done", |
| 63 | States: { |
| 64 | Done: { Type: "Pass", Result: { ok: true }, End: true }, |
| 65 | }, |
| 66 | }, |
| 67 | }); |
| 68 | return { machine, raw }; |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(outputs.machine.stateMachineArn).toContain(":stateMachine:"); |
| 73 | expect(outputs.machine.type).toBe("EXPRESS"); |
| 74 | |
| 75 | // the compiled definition round-trips as JSONata ASL |
| 76 | const described = yield* sfn.describeStateMachine({ |
| 77 | stateMachineArn: outputs.machine.stateMachineArn, |
| 78 | }); |
| 79 | const definition = JSON.parse(plain(described.definition)!) as { |
| 80 | QueryLanguage: string; |
| 81 | StartAt: string; |
| 82 | States: Record<string, { Type: string }>; |
| 83 | }; |
| 84 | expect(definition.QueryLanguage).toBe("JSONata"); |
| 85 | const stateTypes = Object.values(definition.States).map((s) => s.Type); |
| 86 | expect(stateTypes).toContain("Task"); |
| 87 | expect(stateTypes).toContain("Map"); |
| 88 | expect(stateTypes).toContain("Choice"); |
| 89 | |
| 90 | // execute synchronously and assert the program's output end-to-end; |
| 91 | // retry non-SUCCEEDED results through IAM propagation on the fresh |
| 92 | // execution role (bounded) |
| 93 | const result = yield* sfn |
| 94 | .startSyncExecution({ |
| 95 | stateMachineArn: outputs.machine.stateMachineArn, |
| 96 | input: JSON.stringify({ value: 6, items: [1, 2, 3] }), |
| 97 | }) |
| 98 | .pipe( |
| 99 | Effect.flatMap((execution) => |
| 100 | execution.status === "SUCCEEDED" |
| 101 | ? Effect.succeed(execution) |
| 102 | : Effect.fail( |
| 103 | new ExecutionNotSucceeded({ |
| 104 | status: execution.status, |
| 105 | error: plain(execution.error), |
| 106 | cause: plain(execution.cause), |
| 107 | }), |
| 108 | ), |
| 109 | ), |
| 110 | Effect.retry({ |
| 111 | while: (e) => e._tag === "ExecutionNotSucceeded", |
| 112 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 113 | }), |
| 114 | ); |
| 115 | const output = JSON.parse(plain(result.output)!) as OrderOutput; |
| 116 | expect(output).toEqual({ |
| 117 | doubled: 12, |
| 118 | items: [2, 4, 6], |
| 119 | size: "big", |
| 120 | recovered: "no stock", |
| 121 | }); |
| 122 | |
| 123 | // Choice's false branch: small input |
| 124 | const small = yield* sfn |
| 125 | .startSyncExecution({ |
| 126 | stateMachineArn: outputs.machine.stateMachineArn, |
| 127 | input: JSON.stringify({ value: 2, items: [] }), |
| 128 | }) |
| 129 | .pipe( |
| 130 | Effect.flatMap((execution) => |
| 131 | execution.status === "SUCCEEDED" |
| 132 | ? Effect.succeed(execution) |
| 133 | : Effect.fail( |
| 134 | new ExecutionNotSucceeded({ |
| 135 | status: execution.status, |
| 136 | error: plain(execution.error), |
| 137 | cause: plain(execution.cause), |
| 138 | }), |
| 139 | ), |
| 140 | ), |
| 141 | Effect.retry({ |
| 142 | while: (e) => e._tag === "ExecutionNotSucceeded", |
| 143 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(5)]), |
| 144 | }), |
| 145 | ); |
| 146 | const smallOutput = JSON.parse(plain(small.output)!) as OrderOutput; |
| 147 | expect(smallOutput.size).toBe("small"); |
| 148 | expect(smallOutput.items).toEqual([]); |
| 149 | |
| 150 | // raw path: definition stored verbatim (modulo formatting) |
| 151 | const rawDescribed = yield* sfn.describeStateMachine({ |
| 152 | stateMachineArn: outputs.raw.stateMachineArn, |
| 153 | }); |
| 154 | expect(JSON.parse(plain(rawDescribed.definition)!).Comment).toBe("raw"); |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | }), |
| 158 | { |
| 159 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:stepfunctions", "live"], |
| 160 | timeout: 180_000, |
| 161 | }, |
| 162 | ); |