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 { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import AppConfigEventsTestFunctionLive, { |
| 11 | AppConfigEventsTestFunction, |
| 12 | } from "./fixtures/events-handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "AppConfigEvents"); |
| 17 | |
| 18 | let baseUrl: string; |
| 19 | |
| 20 | interface EventBody { |
| 21 | event: { |
| 22 | InvocationId?: string; |
| 23 | Type?: string; |
| 24 | DeploymentNumber?: number; |
| 25 | Environment?: { Id?: string }; |
| 26 | } | null; |
| 27 | } |
| 28 | |
| 29 | class DeploymentRequestFailed extends Data.TaggedError("DeploymentRequestFailed")<{ |
| 30 | readonly status: number; |
| 31 | readonly body: string; |
| 32 | }> {} |
| 33 | |
| 34 | class DeploymentEventPending extends Data.TaggedError("DeploymentEventPending")<{ |
| 35 | readonly deploymentNumber: number; |
| 36 | }> {} |
| 37 | |
| 38 | const startDeployment = Effect.suspend(() => |
| 39 | HttpClient.execute( |
| 40 | HttpClientRequest.post(`${baseUrl}/deploy`).pipe( |
| 41 | HttpClientRequest.bodyJsonUnsafe({ version: "1" }), |
| 42 | ), |
| 43 | ).pipe( |
| 44 | Effect.flatMap((response) => |
| 45 | response.status === 200 |
| 46 | ? response.json |
| 47 | : response.text.pipe( |
| 48 | Effect.flatMap((body) => |
| 49 | Effect.fail(new DeploymentRequestFailed({ status: response.status, body })), |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | Effect.map((json) => json as { deploymentNumber?: number; state?: string }), |
| 54 | // Lambda URL cold starts and fresh execution-role permissions can surface |
| 55 | // as transient 5xx responses. Retry those only; genuine 4xx responses are |
| 56 | // actionable test failures. |
| 57 | Effect.retry({ |
| 58 | while: (error) => error._tag === "DeploymentRequestFailed" && error.status >= 500, |
| 59 | schedule: Schedule.exponential("1 second"), |
| 60 | times: 6, |
| 61 | }), |
| 62 | ), |
| 63 | ); |
| 64 | |
| 65 | const awaitCompleteEvent = (deploymentNumber: number) => |
| 66 | HttpClient.get(`${baseUrl}/event?number=${deploymentNumber}&type=OnDeploymentComplete`).pipe( |
| 67 | Effect.flatMap((response) => response.json), |
| 68 | Effect.map((json) => json as unknown as EventBody), |
| 69 | Effect.flatMap((body) => |
| 70 | body.event === null |
| 71 | ? Effect.fail(new DeploymentEventPending({ deploymentNumber })) |
| 72 | : Effect.succeed(body.event), |
| 73 | ), |
| 74 | Effect.retry({ |
| 75 | while: (error) => error._tag === "DeploymentEventPending", |
| 76 | schedule: Schedule.fixed("3 seconds"), |
| 77 | times: 5, |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | describe( |
| 82 | "AppConfig DeploymentEventSource", |
| 83 | { |
| 84 | tags: [ |
| 85 | "provider:aws", |
| 86 | "provider:aws:appconfig", |
| 87 | "provider:aws:lambda", |
| 88 | "provider:aws:s3", |
| 89 | "live", |
| 90 | ], |
| 91 | }, |
| 92 | () => { |
| 93 | beforeAll( |
| 94 | Effect.gen(function* () { |
| 95 | yield* Effect.logInfo("AppConfig event source setup: destroying previous resources"); |
| 96 | yield* sharedStack.destroy(); |
| 97 | |
| 98 | yield* Effect.logInfo( |
| 99 | "AppConfig event source setup: deploying app -> env -> extension -> Lambda", |
| 100 | ); |
| 101 | const { functionUrl } = yield* sharedStack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | return yield* AppConfigEventsTestFunction; |
| 104 | }).pipe(Effect.provide(AppConfigEventsTestFunctionLive)), |
| 105 | ); |
| 106 | |
| 107 | expect(functionUrl).toBeTruthy(); |
| 108 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 109 | |
| 110 | // Readiness probe — fresh function URLs take seconds to serve 200s. |
| 111 | yield* HttpClient.get(`${baseUrl}/ping`).pipe( |
| 112 | Effect.flatMap((response) => |
| 113 | response.status === 200 |
| 114 | ? Effect.succeed(response) |
| 115 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 116 | ), |
| 117 | Effect.retry({ |
| 118 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]), |
| 119 | }), |
| 120 | ); |
| 121 | }), |
| 122 | { timeout: 300_000 }, |
| 123 | ); |
| 124 | afterAll(sharedStack.destroy(), { timeout: 300_000 }); |
| 125 | |
| 126 | test.provider( |
| 127 | "deployment notifications invoke the Lambda through the extension", |
| 128 | () => |
| 129 | Effect.gen(function* () { |
| 130 | // AppConfig notifications are fire-and-forget. The first invocation |
| 131 | // can be lost while the freshly-created AppConfig invoke role is |
| 132 | // propagating; waiting longer for that same notification cannot make |
| 133 | // it reappear. Retry the whole bounded start+observe cycle so each |
| 134 | // attempt emits a fresh action-point notification. |
| 135 | const observed = yield* Effect.gen(function* () { |
| 136 | const started = yield* startDeployment; |
| 137 | const deploymentNumber = started.deploymentNumber; |
| 138 | if (deploymentNumber === undefined) { |
| 139 | return yield* Effect.fail( |
| 140 | new DeploymentRequestFailed({ |
| 141 | status: 200, |
| 142 | body: "StartDeployment response omitted DeploymentNumber", |
| 143 | }), |
| 144 | ); |
| 145 | } |
| 146 | return { deploymentNumber, event: yield* awaitCompleteEvent(deploymentNumber) }; |
| 147 | }).pipe( |
| 148 | Effect.retry({ |
| 149 | while: (error) => error._tag === "DeploymentEventPending", |
| 150 | schedule: Schedule.fixed("5 seconds"), |
| 151 | times: 3, |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | expect(observed.deploymentNumber).toBeGreaterThan(0); |
| 156 | expect(observed.event.InvocationId).toBeTruthy(); |
| 157 | expect(observed.event.DeploymentNumber).toBe(observed.deploymentNumber); |
| 158 | }), |
| 159 | { timeout: 120_000 }, |
| 160 | ); |
| 161 | }, |
| 162 | ); |