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 { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as appsync from "@distilled.cloud/aws/appsync"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | const authorizerPath = fileURLToPath(new URL("./authorizer-handler.ts", import.meta.url)); |
| 14 | |
| 15 | class ApiStillExists extends Data.TaggedError("ApiStillExists")<{ |
| 16 | readonly apiId: string; |
| 17 | }> {} |
| 18 | |
| 19 | /** Poll until `getGraphqlApi` returns the typed NotFoundException. */ |
| 20 | const assertApiDeleted = (apiId: string) => |
| 21 | appsync.getGraphqlApi({ apiId }).pipe( |
| 22 | Effect.flatMap(() => Effect.fail(new ApiStillExists({ apiId }))), |
| 23 | Effect.retry({ |
| 24 | while: (e) => e._tag === "ApiStillExists", |
| 25 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 26 | }), |
| 27 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 28 | ); |
| 29 | |
| 30 | const SCHEMA_V1 = ` |
| 31 | type Query { |
| 32 | hello: String |
| 33 | } |
| 34 | schema { query: Query } |
| 35 | `; |
| 36 | |
| 37 | const SCHEMA_V2 = ` |
| 38 | type Query { |
| 39 | hello: String |
| 40 | version: Int |
| 41 | } |
| 42 | schema { query: Query } |
| 43 | `; |
| 44 | |
| 45 | test.provider( |
| 46 | "create, update, delete GraphQL API with schema and api key", |
| 47 | (stack) => |
| 48 | Effect.gen(function* () { |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const deployApi = (schema: string, xray: boolean) => |
| 52 | stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const api = yield* AWS.AppSync.GraphqlApi("LifecycleApi", { |
| 55 | schema, |
| 56 | xrayEnabled: xray, |
| 57 | queryDepthLimit: xray ? 5 : undefined, |
| 58 | environmentVariables: xray ? { STAGE: "updated", EXTRA: "1" } : { STAGE: "initial" }, |
| 59 | }); |
| 60 | const key = yield* AWS.AppSync.ApiKey("Key", { |
| 61 | api, |
| 62 | description: "test key", |
| 63 | }); |
| 64 | return { |
| 65 | apiId: api.apiId, |
| 66 | apiArn: api.apiArn, |
| 67 | name: api.name, |
| 68 | graphqlUrl: api.graphqlUrl, |
| 69 | authenticationType: api.authenticationType, |
| 70 | keyId: key.id, |
| 71 | }; |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | const out = yield* deployApi(SCHEMA_V1, false); |
| 76 | |
| 77 | expect(out.apiId).toBeTruthy(); |
| 78 | expect(out.graphqlUrl).toContain("appsync-api"); |
| 79 | expect(out.authenticationType).toBe("API_KEY"); |
| 80 | expect(Redacted.value(out.keyId)).toMatch(/^da2-/); |
| 81 | |
| 82 | // Out-of-band verification via distilled. |
| 83 | const remote = yield* appsync.getGraphqlApi({ apiId: out.apiId }); |
| 84 | expect(remote.graphqlApi?.name).toBe(out.name); |
| 85 | // Internal Alchemy tags brand the API. |
| 86 | expect(Object.keys(remote.graphqlApi?.tags ?? {}).length).toBeGreaterThan(0); |
| 87 | |
| 88 | // Schema applied and settled. |
| 89 | const schemaStatus = yield* appsync.getSchemaCreationStatus({ |
| 90 | apiId: out.apiId, |
| 91 | }); |
| 92 | expect(["SUCCESS", "ACTIVE"]).toContain(schemaStatus.status); |
| 93 | |
| 94 | // Environment variables applied (ctx.env in resolver code). |
| 95 | const envVars = yield* appsync.getGraphqlApiEnvironmentVariables({ |
| 96 | apiId: out.apiId, |
| 97 | }); |
| 98 | expect(envVars.environmentVariables).toEqual({ STAGE: "initial" }); |
| 99 | |
| 100 | // The API key is registered on the API. |
| 101 | const keyId = Redacted.value(out.keyId); |
| 102 | const keys = yield* appsync.listApiKeys({ apiId: out.apiId }); |
| 103 | expect(keys.apiKeys?.map((k) => k.id)).toContain(keyId); |
| 104 | expect(keys.apiKeys?.find((k) => k.id === keyId)?.description).toBe("test key"); |
| 105 | |
| 106 | // Update in place: schema v2 + xray + query depth limit. |
| 107 | const updated = yield* deployApi(SCHEMA_V2, true); |
| 108 | expect(updated.apiId).toBe(out.apiId); |
| 109 | expect(Redacted.value(updated.keyId)).toBe(keyId); |
| 110 | |
| 111 | const afterUpdate = yield* appsync.getGraphqlApi({ apiId: out.apiId }); |
| 112 | expect(afterUpdate.graphqlApi?.xrayEnabled).toBe(true); |
| 113 | expect(afterUpdate.graphqlApi?.queryDepthLimit).toBe(5); |
| 114 | |
| 115 | // The environment-variable map is replaced wholesale on update. |
| 116 | const envVarsAfter = yield* appsync.getGraphqlApiEnvironmentVariables({ |
| 117 | apiId: out.apiId, |
| 118 | }); |
| 119 | expect(envVarsAfter.environmentVariables).toEqual({ |
| 120 | STAGE: "updated", |
| 121 | EXTRA: "1", |
| 122 | }); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | yield* assertApiDeleted(out.apiId); |
| 126 | }), |
| 127 | { tags: ["provider:aws", "provider:aws:appsync", "live"], timeout: 240_000 }, |
| 128 | ); |
| 129 | |
| 130 | test.provider( |
| 131 | "authentication modes: AWS_LAMBDA primary with API_KEY + Cognito additional", |
| 132 | (stack) => |
| 133 | Effect.gen(function* () { |
| 134 | yield* stack.destroy(); |
| 135 | |
| 136 | const out = yield* stack.deploy( |
| 137 | Effect.gen(function* () { |
| 138 | const authorizer = yield* AWS.Lambda.Function("Authorizer", { |
| 139 | main: authorizerPath, |
| 140 | handler: "handler", |
| 141 | isExternal: true, |
| 142 | functionUrl: false, |
| 143 | }); |
| 144 | const pool = yield* AWS.Cognito.UserPool("AuthPool", {}); |
| 145 | const api = yield* AWS.AppSync.GraphqlApi("AuthApi", { |
| 146 | authenticationType: "AWS_LAMBDA", |
| 147 | lambdaAuthorizerConfig: { |
| 148 | authorizerUri: authorizer.functionArn, |
| 149 | // Duration.Input prop — sent to AWS as whole seconds. |
| 150 | authorizerResultTtl: "10 minutes", |
| 151 | }, |
| 152 | additionalAuthenticationProviders: [ |
| 153 | { authenticationType: "API_KEY" }, |
| 154 | { |
| 155 | authenticationType: "AMAZON_COGNITO_USER_POOLS", |
| 156 | userPoolConfig: { userPoolId: pool.userPoolId }, |
| 157 | }, |
| 158 | ], |
| 159 | schema: SCHEMA_V1, |
| 160 | }); |
| 161 | yield* AWS.Lambda.Permission("AppSyncInvokeAuthorizer", { |
| 162 | functionName: authorizer.functionName, |
| 163 | principal: "appsync.amazonaws.com", |
| 164 | action: "lambda:InvokeFunction", |
| 165 | sourceArn: api.apiArn, |
| 166 | }); |
| 167 | return { |
| 168 | apiId: api.apiId, |
| 169 | authenticationType: api.authenticationType, |
| 170 | authorizerArn: authorizer.functionArn, |
| 171 | }; |
| 172 | }), |
| 173 | ); |
| 174 | |
| 175 | expect(out.authenticationType).toBe("AWS_LAMBDA"); |
| 176 | |
| 177 | const remote = yield* appsync.getGraphqlApi({ apiId: out.apiId }); |
| 178 | expect(remote.graphqlApi?.authenticationType).toBe("AWS_LAMBDA"); |
| 179 | expect(remote.graphqlApi?.lambdaAuthorizerConfig?.authorizerUri).toBe(out.authorizerArn); |
| 180 | expect(remote.graphqlApi?.lambdaAuthorizerConfig?.authorizerResultTtlInSeconds).toBe(600); |
| 181 | expect( |
| 182 | remote.graphqlApi?.additionalAuthenticationProviders |
| 183 | ?.map((p) => p.authenticationType) |
| 184 | .sort(), |
| 185 | ).toEqual(["AMAZON_COGNITO_USER_POOLS", "API_KEY"].sort()); |
| 186 | |
| 187 | yield* stack.destroy(); |
| 188 | yield* assertApiDeleted(out.apiId); |
| 189 | }), |
| 190 | { |
| 191 | tags: [ |
| 192 | "provider:aws", |
| 193 | "provider:aws:appsync", |
| 194 | "provider:aws:cognito", |
| 195 | "provider:aws:lambda", |
| 196 | "live", |
| 197 | ], |
| 198 | timeout: 240_000, |
| 199 | }, |
| 200 | ); |
| 201 | |
| 202 | // ApiCache instances bill hourly and take ~10-20 minutes to provision — |
| 203 | // props are fully implemented but the live path is gated. |
| 204 | test.provider.skipIf(!process.env.AWS_TEST_APPSYNC_CACHE)( |
| 205 | "api cache lifecycle (hourly billing — gated)", |
| 206 | (stack) => |
| 207 | Effect.gen(function* () { |
| 208 | yield* stack.destroy(); |
| 209 | |
| 210 | const out = yield* stack.deploy( |
| 211 | Effect.gen(function* () { |
| 212 | const api = yield* AWS.AppSync.GraphqlApi("CachedApi", { |
| 213 | schema: SCHEMA_V1, |
| 214 | cache: { |
| 215 | type: "SMALL", |
| 216 | behavior: "FULL_REQUEST_CACHING", |
| 217 | ttl: "60 seconds", |
| 218 | }, |
| 219 | }); |
| 220 | return { apiId: api.apiId }; |
| 221 | }), |
| 222 | ); |
| 223 | |
| 224 | // Cache exists (status may still be CREATING — we don't wait). |
| 225 | const cache = yield* appsync.getApiCache({ apiId: out.apiId }); |
| 226 | expect(cache.apiCache?.apiCachingBehavior).toBe("FULL_REQUEST_CACHING"); |
| 227 | expect(cache.apiCache?.ttl).toBe(60); |
| 228 | |
| 229 | yield* stack.destroy(); |
| 230 | yield* assertApiDeleted(out.apiId); |
| 231 | }), |
| 232 | { tags: ["provider:aws", "provider:aws:appsync", "live"], timeout: 240_000 }, |
| 233 | ); |