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

main
ts 171 lines 6.25 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import { describe, expect, test } from "@alchemy.run/test";
3 import * as TestCore from "@alchemy.run/test/Core";
4 import * as Config from "effect/Config";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Layer from "effect/Layer";
8 import * as Option from "effect/Option";
9 import * as Result from "effect/Result";
10 import path from "pathe";
11 import { authRegistryLayer, collectAuthProviders } from "@/Alchemist/AuthRegistry.ts";
12 import { DEFAULT_ENTRYPOINT, importStack, StackModuleLoader } from "@/Alchemist/Entrypoint.ts";
13 import * as Alchemist from "@/Alchemist/index.ts";
14 import { buildProviders } from "@/Alchemist/Operations.ts";
15 import { Secrets, Stack, inMemoryState } from "@/index.ts";
16 import * as Interaction from "@/Interaction.ts";
17 import { evalStack } from "../../src/EvalStack";
18 import { TestLayers } from "../test.resources";
19
20 const fixtureAbsolutePath = fileURLToPath(
21 import.meta.resolve("./fixtures/import-stack-fixture.ts"),
22 );
23 const fixtureRelativePath = path.relative(process.cwd(), fixtureAbsolutePath);
24
25 const runFixture = (path: string) =>
26 TestCore.run(
27 importStack(path).pipe(
28 Effect.flatMap((stackEffect) =>
29 evalStack(stackEffect, (stack) => Effect.succeed(stack.output), {
30 stage: "test",
31 }),
32 ),
33 ),
34 {
35 providers: TestLayers(),
36 },
37 );
38
39 describe("importStack", { tags: ["unit", "local"] }, () => {
40 test("--env-file is rejected when the stack declares its own secrets", () =>
41 TestCore.run(
42 Effect.gen(function* () {
43 const fs = yield* FileSystem.FileSystem;
44 const dir = yield* fs.makeTempDirectoryScoped();
45 const envFile = path.join(dir, "cli.env");
46 yield* fs.writeFileString(envFile, "ALCHEMY_DOTENV_SESSION_VALUE=cli");
47 const stack = Stack(
48 "session-secrets-declared",
49 {
50 state: inMemoryState(),
51 providers: Layer.empty,
52 secrets: [Secrets.DotEnv({ path: envFile })],
53 },
54 Config.String("ALCHEMY_DOTENV_SESSION_VALUE"),
55 );
56 const result = yield* importStack(fixtureAbsolutePath).pipe(
57 Effect.flatMap((stack) =>
58 Alchemist.plan({ stack, operation: "deploy", stage: "prod", envFile }),
59 ),
60 Effect.result,
61 Effect.provideService(StackModuleLoader, {
62 import: async () => ({ default: stack }),
63 }),
64 );
65 expect(Result.isFailure(result)).toBe(true);
66 if (Result.isFailure(result)) {
67 expect(String(result.failure)).toContain(
68 "--env-file cannot be combined with a stack that declares `secrets`",
69 );
70 }
71 }),
72 { providers: TestLayers() },
73 ));
74
75 test("--env-file and --profile steer the default secrets for stack and provider-only builds", () =>
76 TestCore.run(
77 Effect.gen(function* () {
78 const fs = yield* FileSystem.FileSystem;
79 const dir = yield* fs.makeTempDirectoryScoped();
80 const envFile = path.join(dir, "cli.env");
81 yield* fs.writeFileString(
82 envFile,
83 "ALCHEMY_DOTENV_SESSION_VALUE=cli\nALCHEMY_PROFILE=file-profile",
84 );
85 const observations: string[][] = [];
86 const stack = Stack(
87 "session-secrets-default",
88 {
89 state: inMemoryState(),
90 providers: Layer.effectDiscard(
91 Effect.gen(function* () {
92 observations.push([
93 yield* Config.String("ALCHEMY_DOTENV_SESSION_VALUE"),
94 yield* Config.String("ALCHEMY_PROFILE"),
95 ]);
96 }).pipe(Effect.orDie),
97 ),
98 },
99 Config.String("ALCHEMY_DOTENV_SESSION_VALUE"),
100 );
101 yield* Effect.gen(function* () {
102 const plan = yield* importStack(fixtureAbsolutePath).pipe(
103 Effect.flatMap((stack) =>
104 Alchemist.plan({
105 stack,
106 operation: "deploy",
107 stage: "prod",
108 envFile,
109 profile: "cli-profile",
110 }),
111 ),
112 );
113 // The flag's file replaces `.env` in the default list...
114 expect(plan.native.output).toBe("cli");
115 const providers = yield* importStack(fixtureAbsolutePath).pipe(
116 Effect.flatMap((stack) => buildProviders(stack, { envFile, profile: "cli-profile" })),
117 );
118 expect(
119 yield* Config.String("ALCHEMY_DOTENV_SESSION_VALUE").pipe(Effect.provide(providers)),
120 ).toBe("cli");
121 // ...and --profile beats the ALCHEMY_PROFILE the file sets.
122 expect(observations).toEqual([
123 ["cli", "cli-profile"],
124 ["cli", "cli-profile"],
125 ]);
126 }).pipe(
127 Effect.provideService(StackModuleLoader, {
128 import: async () => ({ default: stack }),
129 }),
130 );
131 }),
132 { providers: TestLayers() },
133 ));
134
135 test("loads stack entrypoint via relative path", () =>
136 expect(runFixture(fixtureRelativePath)).resolves.toBe("import-stack-fixture"));
137
138 test("loads stack entrypoint via absolute path", () =>
139 expect(runFixture(fixtureAbsolutePath)).resolves.toBe("import-stack-fixture"));
140
141 test("memoizes an auth registry within its layer", async () => {
142 const options = {
143 main: DEFAULT_ENTRYPOINT,
144 envFile: Option.none<string>(),
145 profile: "default",
146 };
147 const [first, second] = await TestCore.run(
148 Effect.all([collectAuthProviders(options), collectAuthProviders(options)]).pipe(
149 Effect.provide(authRegistryLayer),
150 Effect.provide(Interaction.layerNonInteractive()),
151 ),
152 { providers: TestLayers() },
153 );
154
155 expect(second).toBe(first);
156 });
157
158 test("reports a missing stack entrypoint as a user-facing error", async () => {
159 const result = await TestCore.run(
160 importStack(path.join(import.meta.dirname, "missing-alchemy.run.ts")).pipe(Effect.result),
161 { providers: TestLayers() },
162 );
163
164 expect(Result.isFailure(result)).toBe(true);
165 if (Result.isFailure(result)) {
166 expect(result.failure._tag).toBe("StackEntrypointError");
167 expect(result.failure.message).toContain("does not exist");
168 expect(result.failure.message).toContain("--config <path>");
169 }
170 });
171 });