The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { applyEnvRegionOverride, parseAwsSsoLoginOutput } from "@alchemy.run/aws/AuthProvider"; |
| 2 | import { describe, expect, it } from "@alchemy.run/test"; |
| 3 | import { loadConfigProvider } from "alchemy/Util/ConfigProvider"; |
| 4 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 5 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | |
| 11 | const withEnv = (env: Record<string, string>) => |
| 12 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))); |
| 13 | |
| 14 | // Simulates credentials resolved from an SSO profile whose ~/.aws/config |
| 15 | // region differs from the region the user explicitly set in the environment. |
| 16 | const profileCreds = { accountId: "123456789012", region: "us-west-2" }; |
| 17 | |
| 18 | describe("parseAwsSsoLoginOutput", { tags: ["unit", "provider:aws", "local"] }, () => { |
| 19 | it("reads browser authorization JSON", () => { |
| 20 | expect( |
| 21 | parseAwsSsoLoginOutput( |
| 22 | JSON.stringify({ authorizationUrl: "https://oidc.example.com/authorize?state=abc" }), |
| 23 | ), |
| 24 | ).toEqual({ url: "https://oidc.example.com/authorize?state=abc", code: undefined }); |
| 25 | }); |
| 26 | |
| 27 | it("reads the aws login browser prompt", () => { |
| 28 | expect( |
| 29 | parseAwsSsoLoginOutput(`Attempting to open your default browser. If the browser does not open, open the following URL. |
| 30 | |
| 31 | https://us-east-1.signin.aws.amazon.com/v1/authorize?client_id=abc |
| 32 | |
| 33 | `), |
| 34 | ).toEqual({ |
| 35 | url: "https://us-east-1.signin.aws.amazon.com/v1/authorize?client_id=abc", |
| 36 | code: undefined, |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it("reads the text AWS emits despite --output json", () => { |
| 41 | expect( |
| 42 | parseAwsSsoLoginOutput(`Browser will not be automatically opened. |
| 43 | Please visit the following URL: |
| 44 | |
| 45 | https://example.awsapps.com/start/#/device |
| 46 | |
| 47 | Then enter the code: |
| 48 | |
| 49 | CRKF-LVXR`), |
| 50 | ).toEqual({ url: "https://example.awsapps.com/start/#/device", code: "CRKF-LVXR" }); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | describe("applyEnvRegionOverride", { tags: ["unit", "provider:aws", "local"] }, () => { |
| 55 | it.effect("AWS_REGION overrides the profile region", () => |
| 56 | Effect.gen(function* () { |
| 57 | const creds = yield* applyEnvRegionOverride(profileCreds); |
| 58 | expect(creds.region).toBe("us-east-2"); |
| 59 | expect(creds.accountId).toBe("123456789012"); |
| 60 | }).pipe(withEnv({ AWS_REGION: "us-east-2" })), |
| 61 | ); |
| 62 | |
| 63 | // AWS_DEFAULT_REGION is a default, not an override — the profile's region |
| 64 | // is explicit configuration and must win over it. |
| 65 | it.effect("AWS_DEFAULT_REGION does NOT override the profile region", () => |
| 66 | Effect.gen(function* () { |
| 67 | const creds = yield* applyEnvRegionOverride(profileCreds); |
| 68 | expect(creds.region).toBe("us-west-2"); |
| 69 | }).pipe(withEnv({ AWS_DEFAULT_REGION: "eu-west-1" })), |
| 70 | ); |
| 71 | |
| 72 | it.effect("falls back to the profile region when no env is set", () => |
| 73 | Effect.gen(function* () { |
| 74 | const creds = yield* applyEnvRegionOverride(profileCreds); |
| 75 | expect(creds.region).toBe("us-west-2"); |
| 76 | }).pipe(withEnv({})), |
| 77 | ); |
| 78 | |
| 79 | it.effect( |
| 80 | "process environment overrides the default dotenv region", |
| 81 | () => |
| 82 | Effect.scoped( |
| 83 | Effect.gen(function* () { |
| 84 | const fs = yield* FileSystem.FileSystem; |
| 85 | const path = yield* Path.Path; |
| 86 | const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-config-provider-" }); |
| 87 | yield* fs.writeFileString(path.join(tempDir, ".env"), "AWS_REGION=ap-south-1\n"); |
| 88 | |
| 89 | const originalCwd = process.cwd(); |
| 90 | const originalRegion = process.env.AWS_REGION; |
| 91 | yield* Effect.gen(function* () { |
| 92 | yield* Effect.sync(() => { |
| 93 | process.chdir(tempDir); |
| 94 | process.env.AWS_REGION = "us-east-1"; |
| 95 | }); |
| 96 | |
| 97 | const configProvider = yield* loadConfigProvider(Option.none()); |
| 98 | const creds = yield* applyEnvRegionOverride({ |
| 99 | accountId: "654654387918", |
| 100 | region: "us-west-2", |
| 101 | }).pipe(Effect.provide(ConfigProvider.layer(configProvider))); |
| 102 | expect(creds.region).toBe("us-east-1"); |
| 103 | expect(creds.accountId).toBe("654654387918"); |
| 104 | }).pipe( |
| 105 | Effect.ensuring( |
| 106 | Effect.sync(() => { |
| 107 | process.chdir(originalCwd); |
| 108 | if (originalRegion === undefined) { |
| 109 | delete process.env.AWS_REGION; |
| 110 | } else { |
| 111 | process.env.AWS_REGION = originalRegion; |
| 112 | } |
| 113 | }), |
| 114 | ), |
| 115 | ); |
| 116 | }), |
| 117 | ).pipe(Effect.provide(PlatformServices)), |
| 118 | { exclusive: true }, |
| 119 | ); |
| 120 | }); |