The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Resource as CloudControlResource } from "@alchemy.run/aws/CloudControl"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as CloudControl from "@distilled.cloud/aws/cloudcontrol"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | class ResourceStillExists extends Data.TaggedError("ResourceStillExists") {} |
| 17 | |
| 18 | const readValue = ( |
| 19 | properties: string | Redacted.Redacted<string> | undefined, |
| 20 | ): string | undefined => { |
| 21 | const raw = |
| 22 | properties === undefined || typeof properties === "string" |
| 23 | ? properties |
| 24 | : Redacted.value(properties); |
| 25 | if (raw === undefined) return undefined; |
| 26 | return (JSON.parse(raw) as { Value?: string }).Value; |
| 27 | }; |
| 28 | |
| 29 | const assertDeleted = Effect.fn(function* (name: string, typeName = "AWS::SSM::Parameter") { |
| 30 | yield* CloudControl.getResource({ |
| 31 | TypeName: typeName, |
| 32 | Identifier: name, |
| 33 | }).pipe( |
| 34 | Effect.flatMap(() => Effect.fail(new ResourceStillExists())), |
| 35 | Effect.retry({ |
| 36 | while: (e) => e._tag === "ResourceStillExists", |
| 37 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 38 | }), |
| 39 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 40 | ); |
| 41 | }); |
| 42 | |
| 43 | // Cloud Control takes the physical name verbatim, so scope it to the test |
| 44 | // stage (`test_$USER` by default) to keep concurrent developers apart. |
| 45 | const paramNameFor = (stage: string) => `/alchemy-test/${stage}/cloudcontrol/greeting`; |
| 46 | |
| 47 | const resourceDef = (paramName: string, value: string) => |
| 48 | Effect.gen(function* () { |
| 49 | const param = yield* CloudControlResource("CcParam", { |
| 50 | typeName: "AWS::SSM::Parameter", |
| 51 | desiredState: { Name: paramName, Type: "String", Value: value }, |
| 52 | }); |
| 53 | return { param }; |
| 54 | }); |
| 55 | |
| 56 | test.provider( |
| 57 | "create, update (JSON patch), delete an SSM parameter via Cloud Control", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | yield* stack.destroy(); |
| 61 | const paramName = paramNameFor(stack.stage); |
| 62 | |
| 63 | // Create. |
| 64 | const { param: created } = yield* stack.deploy(resourceDef(paramName, "hello")); |
| 65 | expect(created.identifier).toBe(paramName); |
| 66 | expect(created.typeName).toBe("AWS::SSM::Parameter"); |
| 67 | expect(created.properties.Value).toBe("hello"); |
| 68 | |
| 69 | // Verify out-of-band. |
| 70 | const described = yield* CloudControl.getResource({ |
| 71 | TypeName: "AWS::SSM::Parameter", |
| 72 | Identifier: paramName, |
| 73 | }); |
| 74 | expect(readValue(described.ResourceDescription?.Properties)).toBe("hello"); |
| 75 | |
| 76 | // Update the value — a JSON Patch is computed over just the Value key. |
| 77 | const { param: updated } = yield* stack.deploy(resourceDef(paramName, "world")); |
| 78 | expect(updated.identifier).toBe(paramName); |
| 79 | expect(updated.properties.Value).toBe("world"); |
| 80 | |
| 81 | const reDescribed = yield* CloudControl.getResource({ |
| 82 | TypeName: "AWS::SSM::Parameter", |
| 83 | Identifier: paramName, |
| 84 | }); |
| 85 | expect(readValue(reDescribed.ResourceDescription?.Properties)).toBe("world"); |
| 86 | |
| 87 | // Delete + wait gone. |
| 88 | yield* stack.destroy(); |
| 89 | yield* assertDeleted(paramName); |
| 90 | }).pipe(logLevel), |
| 91 | { |
| 92 | tags: ["provider:aws", "provider:aws:cloudcontrol", "live"], |
| 93 | timeout: 240_000, |
| 94 | }, |
| 95 | ); |
| 96 | |
| 97 | // An SSM Command document per test stage. SSM echoes JSON `Content` back as a |
| 98 | // formatted string and never returns the write-only `UpdateMethod`, so an |
| 99 | // update that keeps the content must not compute a content patch. |
| 100 | const documentNameFor = (stage: string) => `alchemy-test-${stage}-cloudcontrol-document`; |
| 101 | |
| 102 | const documentContent = (message: string) => ({ |
| 103 | schemaVersion: "2.2", |
| 104 | description: "Alchemy Cloud Control document test", |
| 105 | mainSteps: [ |
| 106 | { |
| 107 | action: "aws:runShellScript", |
| 108 | name: "echo", |
| 109 | inputs: { runCommand: [`echo ${message}`] }, |
| 110 | }, |
| 111 | ], |
| 112 | }); |
| 113 | |
| 114 | const documentDef = (documentName: string, message: string, owner = "alchemy") => |
| 115 | Effect.gen(function* () { |
| 116 | const document = yield* CloudControlResource("CcDocument", { |
| 117 | typeName: "AWS::SSM::Document", |
| 118 | desiredState: { |
| 119 | Name: documentName, |
| 120 | DocumentType: "Command", |
| 121 | DocumentFormat: "JSON", |
| 122 | UpdateMethod: "NewVersion", |
| 123 | Content: documentContent(message), |
| 124 | Tags: [{ Key: "owner", Value: owner }], |
| 125 | }, |
| 126 | }); |
| 127 | return { document }; |
| 128 | }); |
| 129 | |
| 130 | const readContent = (content: unknown) => |
| 131 | typeof content === "string" ? JSON.parse(content) : content; |
| 132 | |
| 133 | test.provider( |
| 134 | "updating a JSON SSM document without changing its content", |
| 135 | (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | yield* stack.destroy(); |
| 138 | const documentName = documentNameFor(stack.stage); |
| 139 | |
| 140 | const { document: created } = yield* stack.deploy(documentDef(documentName, "hello")); |
| 141 | expect(created.identifier).toBe(documentName); |
| 142 | expect(readContent(created.properties.Content)).toEqual(documentContent("hello")); |
| 143 | |
| 144 | // Tags-only change: reconcile runs with unchanged content. A perpetual |
| 145 | // `Content`/`UpdateMethod` patch would send an UpdateDocument with |
| 146 | // identical content, which SSM rejects. |
| 147 | const { document: unchanged } = yield* stack.deploy( |
| 148 | documentDef(documentName, "hello", "platform"), |
| 149 | ); |
| 150 | expect(unchanged.identifier).toBe(documentName); |
| 151 | expect(readContent(unchanged.properties.Content)).toEqual(documentContent("hello")); |
| 152 | |
| 153 | // A real content change still patches, as a new document version. |
| 154 | const { document: updated } = yield* stack.deploy( |
| 155 | documentDef(documentName, "world", "platform"), |
| 156 | ); |
| 157 | expect(updated.identifier).toBe(documentName); |
| 158 | expect(readContent(updated.properties.Content)).toEqual(documentContent("world")); |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | yield* assertDeleted(documentName, "AWS::SSM::Document"); |
| 162 | }).pipe(logLevel), |
| 163 | { |
| 164 | tags: ["provider:aws", "provider:aws:cloudcontrol", "live"], |
| 165 | timeout: 240_000, |
| 166 | }, |
| 167 | ); |