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, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { normalizePolicyDocument, type PolicyDocument } from "@alchemy.run/aws/IAM/Policy"; |
| 4 | import { Registry } from "@alchemy.run/aws/Schemas"; |
| 5 | import { describe, expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as schemas from "@distilled.cloud/aws/schemas"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import type { ScopedPlanStatusSession } from "alchemy/Report"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Result from "effect/Result"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | const stubSession = { note: () => Effect.void } as unknown as ScopedPlanStatusSession; |
| 16 | |
| 17 | const assertRegistryGone = (registryName: string) => |
| 18 | schemas.describeRegistry({ RegistryName: registryName }).pipe( |
| 19 | Effect.flatMap(() => Effect.fail(new Error(`registry ${registryName} still exists`))), |
| 20 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 21 | Effect.retry({ |
| 22 | while: (e) => e instanceof Error, |
| 23 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | describe("AWS.Schemas.Registry", { tags: ["provider:aws", "provider:aws:schemas", "live"] }, () => { |
| 28 | test.provider( |
| 29 | "creates, updates, replaces on rename, and deletes a registry", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | // CREATE |
| 35 | const created = yield* stack.deploy( |
| 36 | Effect.gen(function* () { |
| 37 | const registry = yield* Registry("TestRegistry", { |
| 38 | description: "alchemy schemas test registry", |
| 39 | tags: { purpose: "alchemy-test" }, |
| 40 | }); |
| 41 | return { registryName: registry.registryName, registryArn: registry.registryArn }; |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // Verify out-of-band via distilled. |
| 46 | const observed = yield* schemas.describeRegistry({ RegistryName: created.registryName }); |
| 47 | expect(observed.RegistryArn).toEqual(created.registryArn); |
| 48 | expect(observed.Description).toEqual("alchemy schemas test registry"); |
| 49 | expect(observed.Tags?.purpose).toEqual("alchemy-test"); |
| 50 | expect(observed.Tags?.["alchemy::id"]).toEqual("TestRegistry"); |
| 51 | |
| 52 | // list() enumerates the deployed registry (LOCAL scope only). |
| 53 | const provider = yield* Provider.findProvider(Registry); |
| 54 | const all = yield* provider.list(); |
| 55 | expect(all.some((r) => r.registryName === created.registryName)).toBe(true); |
| 56 | expect(all.some((r) => r.registryName === "aws.events")).toBe(false); |
| 57 | |
| 58 | // POLICY — attach a PolicyDocument-valued resource policy in place. |
| 59 | const { accountId } = yield* AWSEnvironment.current; |
| 60 | const policy: PolicyDocument = { |
| 61 | Version: "2012-10-17", |
| 62 | Statement: [ |
| 63 | { |
| 64 | Sid: "AllowOwnAccountRead", |
| 65 | Effect: "Allow", |
| 66 | Principal: { AWS: `arn:aws:iam::${accountId}:root` }, |
| 67 | Action: ["schemas:DescribeRegistry", "schemas:SearchSchemas"], |
| 68 | Resource: created.registryArn, |
| 69 | }, |
| 70 | ], |
| 71 | }; |
| 72 | const deployWithPolicy = stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | const registry = yield* Registry("TestRegistry", { |
| 75 | description: "alchemy schemas test registry", |
| 76 | tags: { purpose: "alchemy-test" }, |
| 77 | policy, |
| 78 | }); |
| 79 | return { registryName: registry.registryName }; |
| 80 | }), |
| 81 | ); |
| 82 | const withPolicy = yield* deployWithPolicy; |
| 83 | expect(withPolicy.registryName).toEqual(created.registryName); |
| 84 | |
| 85 | const observedPolicy = yield* schemas.getResourcePolicy({ |
| 86 | RegistryName: created.registryName, |
| 87 | }); |
| 88 | expect(normalizePolicyDocument(observedPolicy.Policy!)).toEqual( |
| 89 | normalizePolicyDocument(policy), |
| 90 | ); |
| 91 | |
| 92 | // NO-OP RE-DEPLOY — the same PolicyDocument deploys clean and skips |
| 93 | // putResourcePolicy entirely (the revision id is unchanged). |
| 94 | yield* deployWithPolicy; |
| 95 | const afterRedeploy = yield* schemas.getResourcePolicy({ |
| 96 | RegistryName: created.registryName, |
| 97 | }); |
| 98 | expect(afterRedeploy.RevisionId).toEqual(observedPolicy.RevisionId); |
| 99 | expect(normalizePolicyDocument(afterRedeploy.Policy!)).toEqual( |
| 100 | normalizePolicyDocument(policy), |
| 101 | ); |
| 102 | |
| 103 | // UPDATE — description changes in place, user tag is removed. |
| 104 | const updated = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | const registry = yield* Registry("TestRegistry", { |
| 107 | description: "updated description", |
| 108 | }); |
| 109 | return { registryName: registry.registryName, registryArn: registry.registryArn }; |
| 110 | }), |
| 111 | ); |
| 112 | expect(updated.registryName).toEqual(created.registryName); |
| 113 | |
| 114 | const afterUpdate = yield* schemas.describeRegistry({ RegistryName: created.registryName }); |
| 115 | expect(afterUpdate.Description).toEqual("updated description"); |
| 116 | expect(afterUpdate.Tags?.purpose).toBeUndefined(); |
| 117 | expect(afterUpdate.Tags?.["alchemy::id"]).toEqual("TestRegistry"); |
| 118 | |
| 119 | // Omitting `policy` removed the resource policy. |
| 120 | const policyGone = yield* schemas |
| 121 | .getResourcePolicy({ RegistryName: created.registryName }) |
| 122 | .pipe( |
| 123 | Effect.map(() => false), |
| 124 | Effect.catchTag("NotFoundException", () => Effect.succeed(true)), |
| 125 | ); |
| 126 | expect(policyGone).toBe(true); |
| 127 | |
| 128 | // REPLACE — an explicit name change replaces the registry. |
| 129 | const renamed = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | const registry = yield* Registry("TestRegistry", { |
| 132 | registryName: "alchemy-test-schemas-registry-renamed", |
| 133 | }); |
| 134 | return { registryName: registry.registryName, registryArn: registry.registryArn }; |
| 135 | }), |
| 136 | ); |
| 137 | expect(renamed.registryName).toEqual("alchemy-test-schemas-registry-renamed"); |
| 138 | yield* assertRegistryGone(created.registryName); |
| 139 | const afterRename = yield* schemas.describeRegistry({ RegistryName: renamed.registryName }); |
| 140 | expect(afterRename.RegistryArn).toEqual(renamed.registryArn); |
| 141 | |
| 142 | // DELETE |
| 143 | yield* stack.destroy(); |
| 144 | yield* assertRegistryGone(renamed.registryName); |
| 145 | }), |
| 146 | { tags: ["provider:aws:iam"], timeout: 120_000 }, |
| 147 | ); |
| 148 | |
| 149 | test.provider( |
| 150 | "ordinary registry delete protects an untracked schema; force purges it", |
| 151 | (stack) => |
| 152 | Effect.gen(function* () { |
| 153 | yield* stack.destroy(); |
| 154 | |
| 155 | const registry = yield* stack.deploy( |
| 156 | Effect.gen(function* () { |
| 157 | return yield* Registry("CascadeDelete", {}); |
| 158 | }), |
| 159 | ); |
| 160 | // Simulate a schema created before state persistence. Nuke can list the |
| 161 | // registry but the child provider has no account-wide enumeration. |
| 162 | yield* schemas.createSchema({ |
| 163 | RegistryName: registry.registryName, |
| 164 | SchemaName: "UntrackedChild", |
| 165 | Type: "OpenApi3", |
| 166 | Content: JSON.stringify({ |
| 167 | openapi: "3.0.0", |
| 168 | info: { title: "UntrackedChild", version: "1.0.0" }, |
| 169 | paths: {}, |
| 170 | }), |
| 171 | }); |
| 172 | |
| 173 | const provider = yield* Provider.findProvider(Registry); |
| 174 | const deleteInput = { |
| 175 | id: "CascadeDelete", |
| 176 | fqn: "CascadeDelete", |
| 177 | instanceId: "force-delete-test", |
| 178 | olds: {}, |
| 179 | output: registry, |
| 180 | session: stubSession, |
| 181 | bindings: [], |
| 182 | }; |
| 183 | const protectedDelete = yield* Effect.result(provider.delete(deleteInput)); |
| 184 | expect(Result.isFailure(protectedDelete)).toBe(true); |
| 185 | yield* schemas.describeSchema({ |
| 186 | RegistryName: registry.registryName, |
| 187 | SchemaName: "UntrackedChild", |
| 188 | }); |
| 189 | |
| 190 | yield* provider.delete({ ...deleteInput, force: true }); |
| 191 | yield* assertRegistryGone(registry.registryName); |
| 192 | yield* stack.destroy(); |
| 193 | }), |
| 194 | { timeout: 120_000 }, |
| 195 | ); |
| 196 | }); |