| 1 | import { ProfileStore, type ProviderConfig } from "@/Auth/Profile.ts"; |
| 2 | import { LOCAL_ACCOUNT_ID, localAccountId } from "@/Cloudflare/LocalAccount.ts"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: Layer.empty }); |
| 10 | const PROFILE_ACCOUNT = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 11 | const ENV_ACCOUNT = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 12 | |
| 13 | for (const [label, config] of Object.entries({ |
| 14 | token: { |
| 15 | method: "stored", |
| 16 | credentialType: "apiToken", |
| 17 | apiToken: "unused", |
| 18 | accountId: PROFILE_ACCOUNT, |
| 19 | }, |
| 20 | expiredOAuth: { |
| 21 | method: "oauth", |
| 22 | access: "expired", |
| 23 | refresh: "unused", |
| 24 | expires: 0, |
| 25 | scopes: [], |
| 26 | accountId: PROFILE_ACCOUNT, |
| 27 | }, |
| 28 | } satisfies Record<string, ProviderConfig>)) { |
| 29 | test( |
| 30 | `local account uses the selected ${label} profile without authenticating`, |
| 31 | Effect.gen(function* () { |
| 32 | const profiles = yield* ProfileStore; |
| 33 | const account = yield* localAccountId.pipe( |
| 34 | Effect.provideService(ProfileStore, { |
| 35 | ...profiles, |
| 36 | current: Effect.succeed({ |
| 37 | name: "selected", |
| 38 | source: "configuration" as const, |
| 39 | }), |
| 40 | getProfile: (name) => { |
| 41 | expect(name).toBe("selected"); |
| 42 | return Effect.succeed({ |
| 43 | id: name, |
| 44 | providers: { Cloudflare: config }, |
| 45 | }); |
| 46 | }, |
| 47 | }), |
| 48 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({}))), |
| 49 | ); |
| 50 | expect(account).toBe(PROFILE_ACCOUNT); |
| 51 | }), |
| 52 | { tags: ["unit", "local"] }, |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | test( |
| 57 | "environment account takes precedence without credentials or a profile read", |
| 58 | Effect.gen(function* () { |
| 59 | // No auth or profile services supplied: neither is needed for an env ID. |
| 60 | expect( |
| 61 | yield* localAccountId.pipe( |
| 62 | Effect.provide( |
| 63 | ConfigProvider.layer( |
| 64 | ConfigProvider.fromUnknown({ CLOUDFLARE_ACCOUNT_ID: ENV_ACCOUNT }), |
| 65 | ), |
| 66 | ), |
| 67 | ), |
| 68 | ).toBe(ENV_ACCOUNT); |
| 69 | }), |
| 70 | { tags: ["unit", "local"] }, |
| 71 | ); |
| 72 | |
| 73 | test( |
| 74 | "an unconfigured profile retains the credentialless fallback", |
| 75 | Effect.gen(function* () { |
| 76 | const profiles = yield* ProfileStore; |
| 77 | expect( |
| 78 | yield* localAccountId.pipe( |
| 79 | Effect.provideService(ProfileStore, { |
| 80 | ...profiles, |
| 81 | getProfile: () => Effect.succeed(undefined), |
| 82 | }), |
| 83 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({}))), |
| 84 | ), |
| 85 | ).toBe(LOCAL_ACCOUNT_ID); |
| 86 | }), |
| 87 | { tags: ["unit", "local"] }, |
| 88 | ); |