The best way to control Claude Code, Codex and more from any computer or phone

main
ts 86 lines 2.62 KB
Raw
1 import { LOCAL_ACCOUNT_ID, localAccountId } from "@alchemy.run/cloudflare/LocalAccount";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { ProfileStore, type ProviderConfig } from "alchemy/Auth/Profile";
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(ConfigProvider.fromUnknown({ CLOUDFLARE_ACCOUNT_ID: ENV_ACCOUNT })),
64 ),
65 ),
66 ).toBe(ENV_ACCOUNT);
67 }),
68 { tags: ["unit", "local"] },
69 );
70
71 test(
72 "an unconfigured profile retains the credentialless fallback",
73 Effect.gen(function* () {
74 const profiles = yield* ProfileStore;
75 expect(
76 yield* localAccountId.pipe(
77 Effect.provideService(ProfileStore, {
78 ...profiles,
79 getProfile: () => Effect.succeed(undefined),
80 }),
81 Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({}))),
82 ),
83 ).toBe(LOCAL_ACCOUNT_ID);
84 }),
85 { tags: ["unit", "local"] },
86 );