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 { 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 eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 6 | import * as transfer from "@distilled.cloud/aws/transfer"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import TransferTestFunctionLive, { TransferTestFunction } from "./handler"; |
| 13 | import TransferWorkflowTestFunctionLive, { TransferWorkflowTestFunction } from "./workflow-handler"; |
| 14 | |
| 15 | const testOptions = { providers: AWS.providers() }; |
| 16 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 17 | |
| 18 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 19 | // over 60s on a fresh deploy. |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 21 | |
| 22 | const probeReady = (readinessUrl: string) => |
| 23 | HttpClient.get(readinessUrl).pipe( |
| 24 | Effect.flatMap((response) => |
| 25 | response.status === 200 |
| 26 | ? Effect.succeed(response) |
| 27 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 28 | ), |
| 29 | Effect.retry({ schedule: readinessPolicy }), |
| 30 | ); |
| 31 | |
| 32 | // Ungated typed-error probes: prove the distilled error unions carry the |
| 33 | // tags the bindings and providers depend on, at near-zero cost. |
| 34 | describe("typed-error probes", { tags: ["provider:aws", "provider:aws:transfer", "live"] }, () => { |
| 35 | test.provider("startServer on a nonexistent server fails with ResourceNotFoundException", () => |
| 36 | Effect.gen(function* () { |
| 37 | const error = yield* Effect.flip(transfer.startServer({ ServerId: "s-00000000000000000" })); |
| 38 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | test.provider( |
| 43 | "importSshPublicKey on a nonexistent server fails with ResourceNotFoundException", |
| 44 | () => |
| 45 | Effect.gen(function* () { |
| 46 | const error = yield* Effect.flip( |
| 47 | transfer.importSshPublicKey({ |
| 48 | ServerId: "s-00000000000000000", |
| 49 | UserName: "nobody", |
| 50 | SshPublicKeyBody: "ssh-ed25519 AAAA", |
| 51 | }), |
| 52 | ); |
| 53 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | test.provider( |
| 58 | "testIdentityProvider on a nonexistent server fails with ResourceNotFoundException", |
| 59 | () => |
| 60 | Effect.gen(function* () { |
| 61 | const error = yield* Effect.flip( |
| 62 | transfer.testIdentityProvider({ |
| 63 | ServerId: "s-00000000000000000", |
| 64 | UserName: "nobody", |
| 65 | UserPassword: Redacted.make("not-a-real-password"), |
| 66 | }), |
| 67 | ); |
| 68 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | test.provider("sendWorkflowStepState on a nonexistent workflow fails with a typed tag", () => |
| 73 | Effect.gen(function* () { |
| 74 | const error = yield* Effect.flip( |
| 75 | transfer.sendWorkflowStepState({ |
| 76 | WorkflowId: "w-1234567890abcdef0", |
| 77 | ExecutionId: "00000000-0000-0000-0000-000000000000", |
| 78 | Token: "MA==", |
| 79 | Status: "SUCCESS", |
| 80 | }), |
| 81 | ); |
| 82 | // Transfer rejects the nonexistent workflow with the typed |
| 83 | // ValidationException (from the shared CommonErrors union). |
| 84 | expect([ |
| 85 | "ResourceNotFoundException", |
| 86 | "InvalidRequestException", |
| 87 | "ValidationException", |
| 88 | ]).toContain(error._tag); |
| 89 | }), |
| 90 | ); |
| 91 | }); |
| 92 | |
| 93 | // Ungated Lambda fixture: exercises the account-level binding + the |
| 94 | // EventBridge event source without provisioning a Transfer server. |
| 95 | describe.sequential( |
| 96 | "Transfer workflow binding", |
| 97 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:transfer", "live"] }, |
| 98 | () => { |
| 99 | const workflowStack = Core.scratchStack(testOptions, "TransferWorkflowBindings"); |
| 100 | |
| 101 | let baseUrl: string; |
| 102 | let functionArn: string; |
| 103 | |
| 104 | beforeAll( |
| 105 | Effect.gen(function* () { |
| 106 | yield* workflowStack.destroy(); |
| 107 | |
| 108 | const attrs = yield* workflowStack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | return yield* TransferWorkflowTestFunction; |
| 111 | }).pipe(Effect.provide(TransferWorkflowTestFunctionLive)), |
| 112 | ); |
| 113 | |
| 114 | expect(attrs.functionUrl).toBeTruthy(); |
| 115 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 116 | functionArn = attrs.functionArn; |
| 117 | |
| 118 | yield* probeReady(`${baseUrl}/bindings`); |
| 119 | }), |
| 120 | { timeout: 300_000 }, |
| 121 | ); |
| 122 | |
| 123 | afterAll(workflowStack.destroy(), { timeout: 180_000 }); |
| 124 | |
| 125 | test.provider("the capability initializes in the runtime", (_stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const response = yield* HttpClient.get(`${baseUrl}/bindings`).pipe( |
| 128 | Effect.flatMap((r) => r.json), |
| 129 | ); |
| 130 | expect((response as { bound: string[] }).bound).toContain("sendWorkflowStepState"); |
| 131 | }), |
| 132 | ); |
| 133 | |
| 134 | test.provider("SendWorkflowStepState round-trips and rejects with a typed tag", (_stack) => |
| 135 | Effect.gen(function* () { |
| 136 | const response = (yield* HttpClient.execute( |
| 137 | HttpClientRequest.post(`${baseUrl}/workflow-step`), |
| 138 | ).pipe(Effect.flatMap((r) => r.json))) as { ok: boolean; tag?: string }; |
| 139 | // The workflow does not exist — the call must surface the typed |
| 140 | // rejection (not an untyped catch-all, not an IAM denial). |
| 141 | expect(response.ok).toBe(false); |
| 142 | expect([ |
| 143 | "ResourceNotFoundException", |
| 144 | "InvalidRequestException", |
| 145 | "ValidationException", |
| 146 | ]).toContain(response.tag); |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | test.provider( |
| 151 | "consumeFileTransferEvents created an EventBridge rule targeting the function", |
| 152 | (_stack) => |
| 153 | Effect.gen(function* () { |
| 154 | // Out-of-band via distilled: the fixture's consumeFileTransferEvents |
| 155 | // must have materialized as a rule on the default bus with the |
| 156 | // Lambda as target. |
| 157 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 158 | TargetArn: functionArn, |
| 159 | }); |
| 160 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 161 | }), |
| 162 | { tags: ["provider:aws:eventbridge"] }, |
| 163 | ); |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | // A running Transfer server is billed hourly and takes minutes to reach |
| 168 | // ONLINE, so the server/user-scoped binding lifecycle is skipped under |
| 169 | // `--fast` (same gate as Server.test.ts) and always destroys what it |
| 170 | // created. |
| 171 | describe.runIf(!process.env.FAST)( |
| 172 | "Transfer server bindings (slow)", |
| 173 | { |
| 174 | tags: [ |
| 175 | "provider:aws", |
| 176 | "provider:aws:iam", |
| 177 | "provider:aws:lambda", |
| 178 | "provider:aws:transfer", |
| 179 | "live", |
| 180 | ], |
| 181 | }, |
| 182 | () => { |
| 183 | const serverStack = Core.scratchStack(testOptions, "TransferBindings"); |
| 184 | |
| 185 | let baseUrl: string; |
| 186 | |
| 187 | const getJson = (path: string) => |
| 188 | HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 189 | |
| 190 | const send = (method: "POST" | "DELETE", path: string) => |
| 191 | HttpClient.execute( |
| 192 | method === "POST" |
| 193 | ? HttpClientRequest.post(`${baseUrl}${path}`) |
| 194 | : HttpClientRequest.delete(`${baseUrl}${path}`), |
| 195 | ).pipe(Effect.flatMap((r) => r.json)); |
| 196 | |
| 197 | beforeAll( |
| 198 | Effect.gen(function* () { |
| 199 | yield* serverStack.destroy(); |
| 200 | |
| 201 | const attrs = yield* serverStack.deploy( |
| 202 | Effect.gen(function* () { |
| 203 | return yield* TransferTestFunction; |
| 204 | }).pipe(Effect.provide(TransferTestFunctionLive)), |
| 205 | ); |
| 206 | |
| 207 | expect(attrs.functionUrl).toBeTruthy(); |
| 208 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 209 | |
| 210 | yield* probeReady(`${baseUrl}/bindings`); |
| 211 | }), |
| 212 | { timeout: 210_000 }, |
| 213 | ); |
| 214 | |
| 215 | afterAll(serverStack.destroy(), { timeout: 90_000 }); |
| 216 | |
| 217 | test.provider("all eight capabilities initialize in the runtime", (_stack) => |
| 218 | Effect.gen(function* () { |
| 219 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 220 | expect(response.bound).toHaveLength(8); |
| 221 | expect(response.bound).toContain("describeServer"); |
| 222 | expect(response.bound).toContain("importSshPublicKey"); |
| 223 | }), |
| 224 | ); |
| 225 | |
| 226 | test.provider("reads the bound server and lists its users (injected ServerId)", (_stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const server = (yield* getJson("/server")) as { serverId: string; state: string }; |
| 229 | expect(server.serverId).toMatch(/^s-/); |
| 230 | |
| 231 | const users = (yield* getJson("/users")) as { userNames: string[] }; |
| 232 | expect(users.userNames).toContain("alice"); |
| 233 | }), |
| 234 | ); |
| 235 | |
| 236 | test.provider( |
| 237 | "imports, observes, and deletes an SSH key on the bound user", |
| 238 | (_stack) => |
| 239 | Effect.gen(function* () { |
| 240 | const imported = (yield* send("POST", "/key")) as { |
| 241 | ok: boolean; |
| 242 | keyId?: string; |
| 243 | tag?: string; |
| 244 | }; |
| 245 | expect(imported.ok).toBe(true); |
| 246 | const keyId = imported.keyId!; |
| 247 | |
| 248 | const user = (yield* getJson("/user")) as { userName: string; keyIds: string[] }; |
| 249 | expect(user.userName).toBe("alice"); |
| 250 | expect(user.keyIds).toContain(keyId); |
| 251 | |
| 252 | const deleted = (yield* send("DELETE", `/key?id=${encodeURIComponent(keyId)}`)) as { |
| 253 | ok: boolean; |
| 254 | }; |
| 255 | expect(deleted.ok).toBe(true); |
| 256 | }), |
| 257 | { timeout: 120_000 }, |
| 258 | ); |
| 259 | |
| 260 | test.provider( |
| 261 | "TestIdentityProvider on a SERVICE_MANAGED server rejects with the typed tag", |
| 262 | (_stack) => |
| 263 | Effect.gen(function* () { |
| 264 | const tested = (yield* send("POST", "/test-idp")) as { ok: boolean; tag?: string }; |
| 265 | expect(tested.ok).toBe(false); |
| 266 | expect(tested.tag).toBe("InvalidRequestException"); |
| 267 | }), |
| 268 | ); |
| 269 | |
| 270 | // A stop/offline/start transition adds several minutes beyond the already |
| 271 | // slow server provisioning lifecycle. Keep it separately opt-in so the |
| 272 | // standard slow lane remains below the hard 240-second wall. |
| 273 | test.provider.skipIf(!process.env.AWS_TEST_TRANSFER_TRANSITIONS)( |
| 274 | "stops and restarts the bound server (extra slow)", |
| 275 | (_stack) => |
| 276 | Effect.gen(function* () { |
| 277 | const stopped = (yield* send("POST", "/stop")) as { ok: boolean; tag?: string }; |
| 278 | expect(stopped.ok).toBe(true); |
| 279 | |
| 280 | // The server parks in STOPPING for 1-3 minutes before OFFLINE. |
| 281 | const offline = (yield* getJson("/server").pipe( |
| 282 | Effect.repeat({ |
| 283 | schedule: Schedule.spaced("10 seconds"), |
| 284 | until: (r): boolean => (r as { state: string }).state === "OFFLINE", |
| 285 | times: 24, |
| 286 | }), |
| 287 | )) as { state: string }; |
| 288 | expect(offline.state).toBe("OFFLINE"); |
| 289 | |
| 290 | const started = (yield* send("POST", "/start")) as { ok: boolean; tag?: string }; |
| 291 | expect(started.ok).toBe(true); |
| 292 | |
| 293 | // Wait for ONLINE again so the shared fixture's teardown (and any |
| 294 | // later test) sees a settled server rather than STARTING. |
| 295 | const online = (yield* getJson("/server").pipe( |
| 296 | Effect.repeat({ |
| 297 | schedule: Schedule.spaced("10 seconds"), |
| 298 | until: (r): boolean => (r as { state: string }).state === "ONLINE", |
| 299 | times: 24, |
| 300 | }), |
| 301 | )) as { state: string }; |
| 302 | expect(online.state).toBe("ONLINE"); |
| 303 | }), |
| 304 | // stop (STOPPING -> OFFLINE) + start (STARTING -> ONLINE), each up to |
| 305 | // ~4 min of platform transition time. |
| 306 | { timeout: 540_000 }, |
| 307 | ); |
| 308 | }, |
| 309 | ); |