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

main
ts 123 lines 4.54 KB
Raw
1 import { expect, it } from "@alchemy.run/test";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Cause from "effect/Cause";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
6 import * as Schema from "effect/Schema";
7 import {
8 AuthProvider,
9 AuthProvidersLive,
10 describeEnvironment,
11 getAuthProvider,
12 } from "@/Auth/AuthProvider.ts";
13 import { getEnvRedactedRequired } from "@/Auth/Env.ts";
14 import { Interaction, makeNonInteractive } from "@/Interaction.ts";
15
16 const implementation = {
17 configSchema: Schema.Struct({ method: Schema.Literal("custom") }),
18 configure: () => Effect.succeed({ method: "custom" as const }),
19 login: () => Effect.void,
20 logout: () => Effect.void,
21 details: () => Effect.succeed({ lines: [] }),
22 read: () => Effect.void,
23 readEnvironment: getEnvRedactedRequired("CUSTOM_PROVIDER_TOKEN").pipe(Effect.asVoid),
24 environment: [
25 { name: "CUSTOM_PROVIDER_TOKEN", required: true, secret: true },
26 {
27 name: "CUSTOM_PROVIDER_REGION",
28 required: false,
29 alternatives: ["CUSTOM_PROVIDER_DEFAULT_REGION"],
30 },
31 ],
32 };
33
34 it.effect(
35 "auth providers expose their declared environment contract",
36 () =>
37 Effect.gen(function* () {
38 yield* AuthProvider<{ method: "custom" }, void>()("CustomProvider", implementation);
39 const provider = yield* getAuthProvider("CustomProvider");
40
41 expect(provider.environment).toEqual(implementation.environment);
42 expect(describeEnvironment(provider.environment)).toBe(
43 "CUSTOM_PROVIDER_TOKEN, [CUSTOM_PROVIDER_REGION | CUSTOM_PROVIDER_DEFAULT_REGION]",
44 );
45 }).pipe(Effect.provide(AuthProvidersLive), Effect.provide(NodeServices.layer)),
46 { tags: ["unit", "local"] },
47 );
48
49 it.effect(
50 "providers without environment credentials declare nothing",
51 () =>
52 Effect.gen(function* () {
53 const { readEnvironment: _, environment: __, ...profileOnly } = implementation;
54 yield* AuthProvider<{ method: "custom" }, void>()("ProfileOnlyProvider", profileOnly);
55 const provider = yield* getAuthProvider("ProfileOnlyProvider");
56
57 expect(provider.readEnvironment).toBeUndefined();
58 expect(provider.environment).toEqual([]);
59 }).pipe(Effect.provide(AuthProvidersLive), Effect.provide(NodeServices.layer)),
60 { tags: ["unit", "local"] },
61 );
62
63 it.effect(
64 "registration dies when readEnvironment lacks an environment declaration",
65 () =>
66 Effect.gen(function* () {
67 const { environment: _, ...undeclared } = implementation;
68 const exit = yield* AuthProvider<{ method: "custom" }, void>()(
69 "UndeclaredProvider",
70 undeclared,
71 ).pipe(Effect.exit);
72
73 expect(Exit.isFailure(exit)).toBe(true);
74 if (Exit.isFailure(exit)) {
75 expect(String(Cause.squash(exit.cause))).toContain("declare its `environment` variables");
76 }
77 }).pipe(Effect.provide(AuthProvidersLive), Effect.provide(NodeServices.layer)),
78 { tags: ["unit", "local"] },
79 );
80
81 it.effect(
82 "call-time Interaction wins over the one ambient at registration",
83 () =>
84 Effect.gen(function* () {
85 const answered: string[] = [];
86 const quiet = makeNonInteractive({ stdout: { write: () => true } as never });
87 const scripted = (name: string): Interaction["Service"] => ({
88 ...quiet,
89 prompt: {
90 ...quiet.prompt,
91 text: () =>
92 Effect.sync(() => {
93 answered.push(name);
94 return name;
95 }),
96 password: () => Effect.succeed(name),
97 confirm: () => Effect.succeed(true),
98 },
99 task: (_options, effect) => effect,
100 });
101
102 // Registered while "registration" is the ambient Interaction: the
103 // factory's context snapshot must NOT capture it — configure's declared
104 // requirement is resolved by whoever calls it.
105 yield* AuthProvider<{ method: "custom" }, void>()("OverrideProvider", {
106 ...implementation,
107 configure: () =>
108 Effect.gen(function* () {
109 const interaction = yield* Interaction;
110 yield* interaction.prompt.text({ message: "token" }).pipe(Effect.orDie);
111 return { method: "custom" as const };
112 }),
113 }).pipe(Effect.provideService(Interaction, scripted("registration")));
114
115 const provider = yield* getAuthProvider("OverrideProvider");
116 yield* provider
117 .configure("default")
118 .pipe(Effect.provideService(Interaction, scripted("call-time")));
119
120 expect(answered).toEqual(["call-time"]);
121 }).pipe(Effect.provide(AuthProvidersLive), Effect.provide(NodeServices.layer)),
122 { tags: ["unit", "local"] },
123 );