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 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { StateMachine } from "@alchemy.run/aws/StepFunctions"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as iam from "@distilled.cloud/aws/iam"; |
| 6 | import * as sfn from "@distilled.cloud/aws/sfn"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { driftOf, repairDrift, waitForDrift } from "../drift.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | const plain = (value: string | Redacted.Redacted<string>): string => |
| 16 | typeof value === "string" ? value : Redacted.value(value); |
| 17 | |
| 18 | class StateMachineStillExists extends Data.TaggedError("StateMachineStillExists")<{ |
| 19 | readonly stateMachineArn: string; |
| 20 | }> {} |
| 21 | |
| 22 | // Deletion is asynchronous and slow — a machine stays visible in DELETING |
| 23 | // for minutes after deleteStateMachine returns. Deletion having been |
| 24 | // initiated (status DELETING) or completed (typed StateMachineDoesNotExist) |
| 25 | // both count as deleted; only an ACTIVE machine means destroy failed. |
| 26 | const assertStateMachineDeleted = (stateMachineArn: string) => |
| 27 | sfn.describeStateMachine({ stateMachineArn }).pipe( |
| 28 | Effect.flatMap((machine) => |
| 29 | machine.status === "DELETING" |
| 30 | ? Effect.void |
| 31 | : Effect.fail(new StateMachineStillExists({ stateMachineArn })), |
| 32 | ), |
| 33 | Effect.catchTag("StateMachineDoesNotExist", () => Effect.void), |
| 34 | Effect.retry({ |
| 35 | while: (e) => e._tag === "StateMachineStillExists", |
| 36 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | class RoleStillExists extends Data.TaggedError("RoleStillExists")<{ |
| 41 | readonly roleName: string; |
| 42 | }> {} |
| 43 | |
| 44 | const assertRoleDeleted = (roleName: string) => |
| 45 | iam.getRole({ RoleName: roleName }).pipe( |
| 46 | Effect.flatMap(() => Effect.fail(new RoleStillExists({ roleName }))), |
| 47 | Effect.catchTag("NoSuchEntityException", () => Effect.void), |
| 48 | Effect.retry({ |
| 49 | while: (e) => e._tag === "RoleStillExists", |
| 50 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | const passDefinition = (comment: string) => ({ |
| 55 | Comment: comment, |
| 56 | StartAt: "Done", |
| 57 | States: { |
| 58 | Done: { Type: "Pass", Result: { ok: true }, End: true }, |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | test.provider( |
| 63 | "create, update definition, update tags, destroy STANDARD state machine", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const machine = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* StateMachine("Workflow", { |
| 69 | definition: passDefinition("v1"), |
| 70 | tags: { Environment: "test" }, |
| 71 | }); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | expect(machine.stateMachineArn).toContain(":stateMachine:"); |
| 76 | expect(machine.type).toBe("STANDARD"); |
| 77 | expect(machine.roleArn).toContain(":role/"); |
| 78 | expect(machine.roleName).toBeDefined(); |
| 79 | |
| 80 | // out-of-band verification via distilled |
| 81 | const created = yield* sfn.describeStateMachine({ |
| 82 | stateMachineArn: machine.stateMachineArn, |
| 83 | }); |
| 84 | expect(created.type).toBe("STANDARD"); |
| 85 | expect(created.roleArn).toBe(machine.roleArn); |
| 86 | expect(JSON.parse(plain(created.definition)).Comment).toBe("v1"); |
| 87 | |
| 88 | const tags = yield* sfn.listTagsForResource({ |
| 89 | resourceArn: machine.stateMachineArn, |
| 90 | }); |
| 91 | const tagRecord = Object.fromEntries((tags.tags ?? []).map((t) => [t.key, t.value])); |
| 92 | expect(tagRecord.Environment).toBe("test"); |
| 93 | expect(tagRecord["alchemy::id"]).toBe("Workflow"); |
| 94 | |
| 95 | // update the definition and add a tag — same physical machine |
| 96 | const updated = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* StateMachine("Workflow", { |
| 99 | definition: passDefinition("v2"), |
| 100 | tags: { Environment: "test", Extra: "1" }, |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | expect(updated.stateMachineArn).toBe(machine.stateMachineArn); |
| 105 | |
| 106 | const afterUpdate = yield* sfn.describeStateMachine({ |
| 107 | stateMachineArn: machine.stateMachineArn, |
| 108 | }); |
| 109 | expect(JSON.parse(plain(afterUpdate.definition)).Comment).toBe("v2"); |
| 110 | const revisionAfterUpdate = afterUpdate.revisionId; |
| 111 | |
| 112 | const afterUpdateTags = yield* sfn.listTagsForResource({ |
| 113 | resourceArn: machine.stateMachineArn, |
| 114 | }); |
| 115 | expect( |
| 116 | Object.fromEntries((afterUpdateTags.tags ?? []).map((t) => [t.key, t.value])).Extra, |
| 117 | ).toBe("1"); |
| 118 | |
| 119 | // no-op deploy converges without creating a new revision |
| 120 | yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* StateMachine("Workflow", { |
| 123 | definition: passDefinition("v2"), |
| 124 | tags: { Environment: "test", Extra: "1" }, |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | const afterNoop = yield* sfn.describeStateMachine({ |
| 129 | stateMachineArn: machine.stateMachineArn, |
| 130 | }); |
| 131 | expect(afterNoop.revisionId).toBe(revisionAfterUpdate); |
| 132 | |
| 133 | // remove a tag |
| 134 | yield* stack.deploy( |
| 135 | Effect.gen(function* () { |
| 136 | return yield* StateMachine("Workflow", { |
| 137 | definition: passDefinition("v2"), |
| 138 | tags: { Environment: "test" }, |
| 139 | }); |
| 140 | }), |
| 141 | ); |
| 142 | const afterTagRemoval = yield* sfn.listTagsForResource({ |
| 143 | resourceArn: machine.stateMachineArn, |
| 144 | }); |
| 145 | const remaining = Object.fromEntries( |
| 146 | (afterTagRemoval.tags ?? []).map((t) => [t.key, t.value]), |
| 147 | ); |
| 148 | expect(remaining.Extra).toBeUndefined(); |
| 149 | expect(remaining.Environment).toBe("test"); |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | yield* assertStateMachineDeleted(machine.stateMachineArn); |
| 153 | yield* assertRoleDeleted(machine.roleName!); |
| 154 | }), |
| 155 | { |
| 156 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:stepfunctions", "live"], |
| 157 | timeout: 120_000, |
| 158 | }, |
| 159 | ); |
| 160 | |
| 161 | test.provider( |
| 162 | "EXPRESS workflow with tracing; type change triggers replacement", |
| 163 | (stack) => |
| 164 | Effect.gen(function* () { |
| 165 | const express = yield* stack.deploy( |
| 166 | Effect.gen(function* () { |
| 167 | return yield* StateMachine("TypedWorkflow", { |
| 168 | type: "EXPRESS", |
| 169 | tracingEnabled: true, |
| 170 | definition: passDefinition("express"), |
| 171 | }); |
| 172 | }), |
| 173 | ); |
| 174 | expect(express.type).toBe("EXPRESS"); |
| 175 | |
| 176 | const observed = yield* sfn.describeStateMachine({ |
| 177 | stateMachineArn: express.stateMachineArn, |
| 178 | }); |
| 179 | expect(observed.type).toBe("EXPRESS"); |
| 180 | expect(observed.tracingConfiguration?.enabled).toBe(true); |
| 181 | |
| 182 | // changing the type replaces the machine (new instance id => new name) |
| 183 | const standard = yield* stack.deploy( |
| 184 | Effect.gen(function* () { |
| 185 | return yield* StateMachine("TypedWorkflow", { |
| 186 | type: "STANDARD", |
| 187 | definition: passDefinition("standard"), |
| 188 | }); |
| 189 | }), |
| 190 | ); |
| 191 | expect(standard.type).toBe("STANDARD"); |
| 192 | expect(standard.stateMachineArn).not.toBe(express.stateMachineArn); |
| 193 | |
| 194 | const observedStandard = yield* sfn.describeStateMachine({ |
| 195 | stateMachineArn: standard.stateMachineArn, |
| 196 | }); |
| 197 | expect(observedStandard.type).toBe("STANDARD"); |
| 198 | yield* assertStateMachineDeleted(express.stateMachineArn); |
| 199 | |
| 200 | yield* stack.destroy(); |
| 201 | yield* assertStateMachineDeleted(standard.stateMachineArn); |
| 202 | }), |
| 203 | { |
| 204 | tags: ["provider:aws", "provider:aws:stepfunctions", "live"], |
| 205 | timeout: 120_000, |
| 206 | }, |
| 207 | ); |
| 208 | |
| 209 | test.provider( |
| 210 | "string definition with substitutions", |
| 211 | (stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const machine = yield* stack.deploy( |
| 214 | Effect.gen(function* () { |
| 215 | return yield* StateMachine("TemplatedWorkflow", { |
| 216 | definition: JSON.stringify({ |
| 217 | Comment: "${comment}", |
| 218 | StartAt: "Done", |
| 219 | States: { Done: { Type: "Pass", End: true } }, |
| 220 | }), |
| 221 | definitionSubstitutions: { comment: "substituted-value" }, |
| 222 | }); |
| 223 | }), |
| 224 | ); |
| 225 | |
| 226 | const observed = yield* sfn.describeStateMachine({ |
| 227 | stateMachineArn: machine.stateMachineArn, |
| 228 | }); |
| 229 | expect(JSON.parse(plain(observed.definition)).Comment).toBe("substituted-value"); |
| 230 | |
| 231 | yield* stack.destroy(); |
| 232 | yield* assertStateMachineDeleted(machine.stateMachineArn); |
| 233 | }), |
| 234 | { |
| 235 | tags: ["provider:aws", "provider:aws:stepfunctions", "live"], |
| 236 | timeout: 120_000, |
| 237 | }, |
| 238 | ); |
| 239 | |
| 240 | // #2688: `read` must observe the definition, tracing, tags and the managed |
| 241 | // execution-role policy, or out-of-band changes read as `unchanged`. |
| 242 | test.provider( |
| 243 | "drift detects and repairs out-of-band definition, tracing and role policy changes", |
| 244 | (stack) => |
| 245 | Effect.gen(function* () { |
| 246 | yield* stack.destroy(); |
| 247 | |
| 248 | const machine = yield* stack.deploy( |
| 249 | StateMachine("DriftWorkflow", { |
| 250 | definition: passDefinition("declared"), |
| 251 | tags: { team: "declared" }, |
| 252 | }), |
| 253 | ); |
| 254 | expect((yield* driftOf(stack, "DriftWorkflow"))?.action).toBe("unchanged"); |
| 255 | |
| 256 | yield* sfn.updateStateMachine({ |
| 257 | stateMachineArn: machine.stateMachineArn, |
| 258 | definition: JSON.stringify(passDefinition("out-of-band")), |
| 259 | tracingConfiguration: { enabled: true }, |
| 260 | }); |
| 261 | const { PolicyNames } = yield* iam.listRolePolicies({ RoleName: machine.roleName! }); |
| 262 | for (const PolicyName of PolicyNames ?? []) { |
| 263 | yield* iam.putRolePolicy({ |
| 264 | RoleName: machine.roleName!, |
| 265 | PolicyName, |
| 266 | PolicyDocument: JSON.stringify({ |
| 267 | Version: "2012-10-17", |
| 268 | Statement: [{ Effect: "Allow", Action: ["sqs:SendMessage"], Resource: ["*"] }], |
| 269 | }), |
| 270 | }); |
| 271 | } |
| 272 | |
| 273 | const drifted = yield* waitForDrift(stack, "DriftWorkflow", "drifted"); |
| 274 | expect(drifted?.attr?.configuration?.tracingEnabled).toBe(true); |
| 275 | expect(drifted?.attr?.configuration?.definition).toContain("out-of-band"); |
| 276 | |
| 277 | const repaired = yield* repairDrift(stack); |
| 278 | expect(repaired.resources.DriftWorkflow?.action).toBe("repaired"); |
| 279 | expect((yield* waitForDrift(stack, "DriftWorkflow", "unchanged"))?.action).toBe("unchanged"); |
| 280 | const restored = yield* sfn.describeStateMachine({ |
| 281 | stateMachineArn: machine.stateMachineArn, |
| 282 | }); |
| 283 | expect(JSON.parse(plain(restored.definition)).Comment).toBe("declared"); |
| 284 | expect(restored.tracingConfiguration?.enabled ?? false).toBe(false); |
| 285 | |
| 286 | yield* stack.destroy(); |
| 287 | yield* assertStateMachineDeleted(machine.stateMachineArn); |
| 288 | yield* assertRoleDeleted(machine.roleName!); |
| 289 | }), |
| 290 | { |
| 291 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:stepfunctions", "live"], |
| 292 | timeout: 180_000, |
| 293 | }, |
| 294 | ); |