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

main
ts 151 lines 5.3 KB
Raw
1 import { describe, expect, test } from "@alchemy.run/test";
2 import * as Cause from "effect/Cause";
3 import * as Effect from "effect/Effect";
4 import * as Exit from "effect/Exit";
5 import * as Layer from "effect/Layer";
6 import type * as Scope from "effect/Scope";
7 import { Action } from "@/Action";
8 import * as Alchemist from "@/Alchemist/index.ts";
9 import { Stack } from "@/index.ts";
10 import { inMemoryState } from "@/State/index.ts";
11
12 type Equal<A, B> =
13 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
14
15 type Output = { readonly url: string };
16
17 const stack = Stack(
18 "AlchemistRuntime",
19 { providers: Layer.empty, state: inMemoryState() },
20 Effect.succeed({ url: "https://example.com" } as Output),
21 );
22
23 const run = <A, E>(program: Effect.Effect<A, E, Alchemist.Services | Scope.Scope>) =>
24 Effect.runPromise(program.pipe(Effect.provide(Alchemist.layer()), Effect.scoped));
25
26 describe("Alchemist runtime", { tags: ["unit", "local"] }, () => {
27 test("deploys and destroys a stack value under Alchemist.layer() alone", async () => {
28 // Each operation closes its own scope, releasing the stage's lease.
29 const program = Effect.gen(function* () {
30 const output = yield* Effect.scoped(
31 Effect.flatMap(
32 Alchemist.plan({ stack, operation: "deploy", stage: "test" }),
33 Alchemist.apply,
34 ),
35 );
36 const changes = yield* Effect.scoped(
37 Effect.gen(function* () {
38 const destroy = yield* Alchemist.plan({ stack, operation: "destroy", stage: "test" });
39 yield* Alchemist.apply(destroy);
40 return Alchemist.hasChanges(destroy);
41 }),
42 );
43 return { output, changes };
44 });
45 const runnable: Effect.Effect<unknown, unknown, never> = program.pipe(
46 Effect.provide(Alchemist.layer()),
47 );
48 expect(await Effect.runPromise(runnable)).toEqual({
49 output: { url: "https://example.com" },
50 changes: false,
51 });
52 });
53
54 test("destroy and drift never run the stack's program", async () => {
55 let runs = 0;
56 const counted = Stack(
57 "AlchemistProgramRuns",
58 { providers: Layer.empty, state: inMemoryState() },
59 Effect.sync(() => ++runs),
60 );
61 await run(
62 Effect.gen(function* () {
63 yield* Effect.scoped(
64 Effect.flatMap(
65 Alchemist.plan({ stack: counted, operation: "deploy", stage: "test" }),
66 Alchemist.apply,
67 ),
68 );
69 yield* Effect.scoped(
70 Effect.flatMap(
71 Alchemist.Drift.scan({ stack: counted, stage: "test" }),
72 Alchemist.Drift.execute,
73 ),
74 );
75 yield* Effect.scoped(
76 Effect.flatMap(
77 Alchemist.plan({ stack: counted, operation: "destroy", stage: "test" }),
78 Alchemist.apply,
79 ),
80 );
81 }),
82 );
83 expect(runs).toBe(1);
84 });
85
86 test("apply reports to its own subscriber, and actions count as changes", async () => {
87 const Sync = Action("Sync", (_: { n: number }) => Effect.succeed({ ok: true }));
88 const withAction = Stack(
89 "AlchemistActionOnly",
90 { providers: Layer.empty, state: inMemoryState() },
91 Effect.gen(function* () {
92 yield* Sync({ n: 1 });
93 return {};
94 }),
95 );
96 const seen: Array<string> = [];
97 const changes = await run(
98 Effect.gen(function* () {
99 // Planned under one subscriber, applied under another: the plan's
100 // captured services must not carry the planning loggers along.
101 const plan = yield* Alchemist.plan({
102 stack: withAction,
103 operation: "deploy",
104 stage: "test",
105 }).pipe(Alchemist.onEvent(() => Effect.void));
106 yield* Alchemist.apply(plan).pipe(
107 Alchemist.onEvent((event) => Effect.sync(() => seen.push(event.type))),
108 );
109 return Alchemist.hasChanges(plan);
110 }),
111 );
112 expect(changes).toBe(true);
113 expect(seen).toContain("apply.resource.status");
114 });
115
116 test("infers the deploy output from the stack and selection", () => {
117 const full = Alchemist.plan({ stack, operation: "deploy", stage: "prod" });
118 const included = Alchemist.plan({
119 stack,
120 operation: "deploy",
121 stage: "prod",
122 include: ["Branch"],
123 });
124 const excluded = Alchemist.plan({
125 stack,
126 operation: "deploy",
127 stage: "prod",
128 exclude: ["Other"],
129 });
130 const optional = (options: Alchemist.PlanOptions) => Alchemist.plan({ ...options, stack });
131 const fullApply = Effect.flatMap(full, Alchemist.apply);
132 const includedApply = Effect.flatMap(included, Alchemist.apply);
133 const destroyApply = Effect.flatMap(
134 Alchemist.plan({ stack, operation: "destroy", stage: "prod" }),
135 Alchemist.apply,
136 );
137
138 type PlanOutput<T> =
139 T extends Effect.Effect<Alchemist.StackPlan<infer O>, any, any> ? O : never;
140 const assertions: [
141 Equal<PlanOutput<typeof full>, Output>,
142 Equal<PlanOutput<typeof included>, undefined>,
143 Equal<PlanOutput<typeof excluded>, undefined>,
144 Equal<PlanOutput<ReturnType<typeof optional>>, Output | undefined>,
145 Equal<Effect.Success<typeof fullApply>, Output>,
146 Equal<Effect.Success<typeof includedApply>, undefined>,
147 Equal<Effect.Success<typeof destroyApply>, undefined>,
148 ] = [true, true, true, true, true, true, true];
149 expect(assertions.every(Boolean)).toBe(true);
150 });
151 });