@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 485 lines 20.9 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import type { TestApi as BunTestApi } from "@alchemy.run/test/Bun";
4 import * as TestCore from "@alchemy.run/test/Core";
5 import type { TestApi as VitestTestApi } from "@alchemy.run/test/Vitest";
6 import * as Cause from "effect/Cause";
7 import type { ConfigError } from "effect/Config";
8 import * as Config from "effect/Config";
9 import * as Effect from "effect/Effect";
10 import * as EffectExit from "effect/Exit";
11 import * as Layer from "effect/Layer";
12 import * as Deploy from "@/Deploy.ts";
13 import * as Destroy from "@/Destroy.ts";
14 import * as Alchemy from "@/index.ts";
15 import * as Output from "@/Output.ts";
16 import * as Plan from "@/Plan.ts";
17 import { Stage } from "@/Stage.ts";
18 import * as State from "@/State/index.ts";
19 import { TestLayers, TestResource } from "./test.resources.ts";
20
21 // These tests are compile-time assertions: they verify that the
22 // `Alchemy.Stack` effect permits a `ConfigError` in its body without
23 // forcing the user to `Effect.orDie`. See
24 // https://github.com/alchemy-run/alchemy/issues/479
25 describe("Alchemy.Stack error channel", { tags: ["unit", "local"] }, () => {
26 it("allows ConfigError in the stack body", () => {
27 // A stack body that reads from `effect/Config` fails with `ConfigError`.
28 // Before #479 this required `Effect.orDie`; now it type-checks directly.
29 const stack = Alchemy.Stack(
30 "ConfigErrorStack",
31 {
32 providers: Layer.empty as any,
33 state: Layer.empty as any,
34 },
35 Effect.gen(function* () {
36 const value = yield* Config.String("SOME_CONFIG");
37 return { value };
38 }),
39 );
40
41 // The resulting effect surfaces `ConfigError` in its error channel rather
42 // than `never` — the whole point of the change.
43 type ErrorOf<T> = T extends Effect.Effect<any, infer E, any> ? E : never;
44 const _assertError: ErrorOf<typeof stack> extends ConfigError ? true : false = true;
45 expect(_assertError).toBe(true);
46 });
47
48 it("still accepts an infallible stack body", () => {
49 const stack = Alchemy.Stack(
50 "InfallibleStack",
51 {
52 providers: Layer.empty as any,
53 state: Layer.empty as any,
54 },
55 Effect.succeed({ value: "ok" }),
56 );
57
58 const _stackName: string = stack.stackName;
59 expect(_stackName).toBe("InfallibleStack");
60 expect(typeof stack).toBe("object");
61 });
62 });
63
64 describe("Alchemy.Stack runtime metadata", { tags: ["unit", "local"] }, () => {
65 it("exposes stackName, providers, and state on a configured stack", () => {
66 const providers = Layer.empty;
67 const state = State.inMemoryState();
68 const stack = Alchemy.Stack("MetaStack", { providers, state }, Effect.succeed({ value: "ok" }));
69
70 expect(stack.stackName).toBe("MetaStack");
71 expect(stack.providers).toBe(providers);
72 expect(stack.state).toBe(state);
73 });
74
75 it("exposes stackName on a class reference", () => {
76 class NamedStack extends Alchemy.Stack<NamedStack, { value: string }>()("NamedStack") {}
77
78 expect(NamedStack.stackName).toBe("NamedStack");
79 });
80
81 it("exposes configured metadata identities on a class reference's make result", () => {
82 class NamedStack extends Alchemy.Stack<NamedStack, { value: string }>()("MadeStack") {}
83 const providers = Layer.empty;
84 const state = State.inMemoryState();
85 const stack = NamedStack.make({ providers, state }, Effect.succeed({ value: "ok" }));
86
87 expect(stack.stackName).toBe("MadeStack");
88 expect(stack.providers).toBe(providers);
89 expect(stack.state).toBe(state);
90 });
91
92 it("exposes only name metadata on the inline class reference form", () => {
93 class InlineStack extends Alchemy.Stack<InlineStack>()(
94 "InlineStack",
95 { providers: Layer.empty, state: State.inMemoryState() },
96 Effect.succeed({ value: "ok" }),
97 ) {}
98
99 expect(InlineStack.stackName).toBe("InlineStack");
100 expect(InlineStack).toHaveProperty("providers", undefined);
101 expect(InlineStack).toHaveProperty("state", undefined);
102 });
103 });
104
105 describe("Alchemy.Stack stage references", { tags: ["unit", "local"] }, () => {
106 const state = State.inMemoryState(
107 {},
108 {
109 StagedStack: { prod: { value: "prod" }, "pr-42": { value: "pr-42" } },
110 InlineStagedStack: { dev: { value: "dev" } },
111 },
112 );
113 const resolve = <A>(ref: Effect.Effect<A>) =>
114 ref.pipe(
115 Effect.flatMap((expr) => Output.evaluate(expr, {})),
116 Effect.provide(state),
117 );
118
119 it.effect("stage[name] resolves a declared stage's output", () =>
120 Effect.gen(function* () {
121 class StagedStack extends Alchemy.Stack<
122 StagedStack,
123 { value: string },
124 "prod" | `pr-${number}`
125 >()("StagedStack") {}
126
127 expect(yield* resolve(StagedStack.stage.prod)).toEqual({ value: "prod" });
128 expect(yield* resolve(StagedStack.stage["pr-42"])).toEqual({ value: "pr-42" });
129 }),
130 );
131
132 it.effect("stage[name] resolves on the inline class reference form", () =>
133 Effect.gen(function* () {
134 class InlineStagedStack extends Alchemy.Stack<InlineStagedStack>()(
135 "InlineStagedStack",
136 { providers: Layer.empty, state: State.inMemoryState() },
137 Effect.succeed({ value: "dev" }),
138 ) {}
139
140 expect(yield* resolve(InlineStagedStack.stage.dev)).toEqual({ value: "dev" });
141 }),
142 );
143 });
144
145 describe("Test.make configured stack", { tags: ["unit", "local"] }, () => {
146 const store = State.InMemoryService();
147 const configured = Alchemy.Stack(
148 "TestMetadataStack",
149 { providers: Layer.empty, state: Layer.succeed(State.State, store) },
150 Effect.map(Stage, (stage) => ({ stage })),
151 );
152 const defaults = Test.make(configured);
153 const explicit = Test.make({ ...configured, stage: "metadata-explicit" });
154
155 defaults.test(
156 "uses a default stage string instead of the stack reference proxy",
157 Effect.gen(function* () {
158 expect(yield* State.State).toBe(store);
159 const output = yield* defaults.deploy(configured);
160 expect(typeof output.stage).toBe("string");
161 expect(output.stage).toBe(Test.defaultStage());
162 const state = yield* store;
163 expect(
164 yield* state.getOutput({
165 stack: configured.stackName,
166 stage: output.stage,
167 }),
168 ).toEqual(output);
169 }).pipe(Effect.ensuring(defaults.destroy(configured).pipe(Effect.orDie))),
170 );
171
172 explicit.test(
173 "honors file-level and per-call stage overrides",
174 Effect.gen(function* () {
175 const output = yield* explicit.deploy(configured);
176 expect(output.stage).toBe("metadata-explicit");
177 const override = yield* explicit.deploy(configured, {
178 stage: "metadata-call",
179 });
180 expect(override.stage).toBe("metadata-call");
181 }).pipe(
182 Effect.ensuring(explicit.destroy(configured, { stage: "metadata-call" }).pipe(Effect.orDie)),
183 Effect.ensuring(explicit.destroy(configured).pipe(Effect.orDie)),
184 ),
185 );
186 });
187
188 describe("filtered deployment API", { tags: ["unit", "local"] }, () => {
189 const store = State.InMemoryService();
190 const state = Layer.succeed(State.State, store);
191 const providers = TestLayers();
192 const api = Test.make({
193 providers,
194 state,
195 stage: "filtered-api",
196 sidecar: false,
197 });
198 const program = Alchemy.Stack(
199 "FilteredApi",
200 { providers, state },
201 Effect.gen(function* () {
202 const branch = yield* TestResource("Branch", { string: "database" });
203 const worker = yield* TestResource("Worker", { string: branch.string });
204 return { branch: branch.string, worker: worker.string };
205 }),
206 );
207
208 it("preserves legacy annotated options and exclude output inference across adapters", () => {
209 type Equal<A, B> =
210 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
211 type Output = { branch: string; worker: string };
212 type DeclaredOutput = Effect.Success<typeof program>["output"];
213 const options: TestCore.DeployCallOptions = {
214 stage: "override",
215 force: true,
216 };
217 const full = api.deploy(program, options);
218 const bun: BunTestApi["deploy"] = api.deploy;
219 const vitest: VitestTestApi["deploy"] = api.deploy;
220 const excluded = api.deploy(program, { exclude: ["Worker"] });
221 const bunExcluded = bun(program, { exclude: ["Worker"] });
222 const vitestExcluded = vitest(program, { exclude: ["Worker"] });
223 const optional = (options?: TestCore.FilteredDeployCallOptions) => api.deploy(program, options);
224 const absent = api.deploy(program, {
225 include: undefined,
226 exclude: undefined,
227 });
228 const legacy: Deploy.DeployOptions<DeclaredOutput> = {
229 stack: program,
230 stage: "override",
231 force: true,
232 };
233 const direct = Deploy.deploy(legacy);
234 const piped = Effect.succeed(legacy).pipe(Effect.flatMap(Deploy.deploy));
235 const filter = { ...legacy, exclude: ["Worker"] };
236 const filtered = Effect.succeed(filter).pipe(Effect.flatMap(Deploy.deploy));
237 const optionalDirect = (exclude?: ReadonlyArray<string>) =>
238 Deploy.deploy({ ...legacy, exclude });
239 const scratch = TestCore.scratchStack({ providers }, "ExcludedInference");
240 const declaration = Effect.succeed({ value: "value" });
241 const scratchExcluded = scratch.deploy(declaration, { exclude: ["Other"] });
242 const scratchPlan = scratch.plan(declaration, { exclude: ["Other"] });
243 const assertions: [
244 Equal<Effect.Success<typeof full>, Output>,
245 Equal<Effect.Success<typeof excluded>, undefined>,
246 Equal<Effect.Success<typeof bunExcluded>, undefined>,
247 Equal<Effect.Success<typeof vitestExcluded>, undefined>,
248 Equal<Effect.Success<ReturnType<typeof optional>>, Output | undefined>,
249 Equal<Effect.Success<typeof absent>, Output>,
250 Equal<Effect.Success<typeof direct>, Output>,
251 Equal<Effect.Success<typeof piped>, Output>,
252 Equal<Effect.Success<typeof filtered>, undefined>,
253 Equal<Effect.Success<ReturnType<typeof optionalDirect>>, Output | undefined>,
254 Equal<Effect.Success<typeof scratchExcluded>, undefined>,
255 Equal<Effect.Success<typeof scratchPlan>["output"], undefined>,
256 ] = [true, true, true, true, true, true, true, true, true, true, true, true];
257 expect(assertions.every(Boolean)).toBe(true);
258 });
259
260 it("full deployment and adapter options cannot erase known or optional filters", () => {
261 type Assignable<A, B> = [A] extends [B] ? true : false;
262 type DeclaredOutput = Effect.Success<typeof program>["output"];
263 type Full = Deploy.DeployOptions<DeclaredOutput>;
264 type Base = Omit<Full, "include" | "exclude">;
265 type CoreFull = TestCore.DeployCallOptions;
266 type CoreBase = Omit<CoreFull, "include" | "exclude">;
267 const assignable: [
268 Assignable<Base & { include: ReadonlyArray<string> }, Full>,
269 Assignable<Base & { exclude: ReadonlyArray<string> }, Full>,
270 Assignable<Base & { include?: ReadonlyArray<string> }, Full>,
271 Assignable<Base & { exclude?: ReadonlyArray<string> }, Full>,
272 Assignable<Deploy.FilteredDeployOptions<DeclaredOutput>, Full>,
273 Assignable<CoreBase & { include: ReadonlyArray<string> }, CoreFull>,
274 Assignable<CoreBase & { exclude: ReadonlyArray<string> }, CoreFull>,
275 Assignable<CoreBase & { include?: ReadonlyArray<string> }, CoreFull>,
276 Assignable<CoreBase & { exclude?: ReadonlyArray<string> }, CoreFull>,
277 Assignable<TestCore.FilteredDeployCallOptions, CoreFull>,
278 ] = [false, false, false, false, false, false, false, false, false, false];
279 expect(assignable.some(Boolean)).toBe(false);
280 });
281
282 for (const field of ["include", "exclude"]) {
283 api.test(
284 `destroy rejects injected ${field} before declaration or state effects`,
285 Effect.gen(function* () {
286 const options: { include?: never; exclude?: never } = {};
287 yield* Effect.sync(() => Reflect.set(options, field, ["Branch"]));
288 const blocked = Alchemy.Stack(
289 "BlockedDestroy",
290 { providers, state },
291 Effect.die("declaration ran"),
292 );
293 const scratch = TestCore.scratchStack({ providers }, "BlockedScratch");
294 const effects = [
295 Plan.destroy({ name: "Blocked", stage: "test", ...options }).pipe(Effect.asVoid),
296 Destroy.destroy({ stack: blocked, stage: "test", ...options }),
297 api.destroy(blocked, options),
298 scratch.destroy(options),
299 ];
300 for (const effect of effects) {
301 const exit = yield* effect.pipe(Effect.exit);
302 expect(EffectExit.isFailure(exit)).toBe(true);
303 if (EffectExit.isFailure(exit))
304 expect(Cause.pretty(exit.cause)).toContain("Filtered destroy is not supported");
305 }
306 }),
307 );
308 }
309
310 it("preserves exact direct and piped output inference", () => {
311 type Equal<A, B> =
312 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
313 type Output = { branch: string; worker: string };
314 const full = api.deploy(program);
315 const piped = program.pipe(api.deploy);
316 const forced = api.deploy(program, { force: true });
317 const stageOnly = api.deploy(program, { stage: "override" });
318 const coreStageOnly = TestCore.deploy({ providers }, program, {
319 stage: "override",
320 });
321 const selected = api.deploy(program, { include: ["Branch"] });
322 const optional = (options: TestCore.FilteredDeployCallOptions) => api.deploy(program, options);
323 const core = TestCore.deploy({ providers }, program);
324 const scratch = TestCore.scratchStack({ providers }, "OutputInference");
325 const declaration = Effect.map(TestResource("Branch", { string: "database" }), (branch) => ({
326 branch: branch.string,
327 }));
328 const scratchFull = scratch.deploy(declaration);
329 const scratchPiped = declaration.pipe(scratch.deploy);
330 const scratchForced = scratch.deploy(declaration, { force: true });
331 const scratchSelected = scratch.deploy(declaration, {
332 include: ["Branch"],
333 });
334 const scratchOptional = (options: TestCore.FilteredDeployCallOptions) =>
335 scratch.deploy(declaration, options);
336 const plan = declaration.pipe(scratch.plan);
337 const selectedPlan = scratch.plan(declaration, { include: ["Branch"] });
338 const assertions: [
339 Equal<Effect.Success<typeof full>, Output>,
340 Equal<Effect.Success<typeof piped>, Output>,
341 Equal<Effect.Success<typeof forced>, Output>,
342 Equal<Effect.Success<typeof stageOnly>, Output>,
343 Equal<Effect.Success<typeof coreStageOnly>, Output>,
344 Equal<Effect.Success<typeof selected>, undefined>,
345 Equal<Effect.Success<ReturnType<typeof optional>>, Output | undefined>,
346 Equal<Effect.Success<typeof core>, Output>,
347 Equal<Effect.Success<typeof scratchFull>, { branch: string }>,
348 Equal<Effect.Success<typeof scratchPiped>, { branch: string }>,
349 Equal<Effect.Success<typeof scratchForced>, { branch: string }>,
350 Equal<Effect.Success<typeof scratchSelected>, undefined>,
351 Equal<Effect.Success<ReturnType<typeof scratchOptional>>, { branch: string } | undefined>,
352 Equal<Effect.Success<typeof plan>["output"], Effect.Success<typeof declaration>>,
353 Equal<Effect.Success<typeof selectedPlan>["output"], undefined>,
354 ] = [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true];
355 expect(assertions.every(Boolean)).toBe(true);
356 });
357
358 it("preserves exact direct and higher-order Deploy output types", () => {
359 type Equal<A, B> =
360 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
361 type DeclaredOutput = Effect.Success<typeof program>["output"];
362 type Output = { branch: string; worker: string };
363 const options: Deploy.DeployOptions<DeclaredOutput> = {
364 stack: program,
365 stage: "override",
366 };
367 const direct = Deploy.deploy({ stack: program, stage: "override" });
368 const piped = Effect.succeed(options).pipe(Effect.flatMap(Deploy.deploy));
369 const explicit = Deploy.deploy<DeclaredOutput>({
370 stack: program,
371 stage: "override",
372 });
373 const forced = Deploy.deploy({
374 stack: program,
375 stage: "override",
376 force: true,
377 });
378 const filteredOptions = {
379 stack: program,
380 stage: "override",
381 include: ["Branch"],
382 };
383 const filtered = Deploy.deploy(filteredOptions);
384 const filteredPiped = Effect.succeed(filteredOptions).pipe(Effect.flatMap(Deploy.deploy));
385 const optional = (options: Deploy.FilteredDeployOptions<DeclaredOutput>) =>
386 Deploy.deploy(options);
387 const optionalPiped = (options: Deploy.FilteredDeployOptions<DeclaredOutput>) =>
388 Effect.succeed(options).pipe(Effect.flatMap(Deploy.deploy));
389 const assertions: [
390 Equal<Effect.Success<typeof direct>, Output>,
391 Equal<Effect.Success<typeof piped>, Output>,
392 Equal<Effect.Success<typeof explicit>, Output>,
393 Equal<Effect.Success<typeof forced>, Output>,
394 Equal<Effect.Success<typeof filtered>, undefined>,
395 Equal<Effect.Success<typeof filteredPiped>, undefined>,
396 Equal<Effect.Success<ReturnType<typeof optional>>, Output | undefined>,
397 Equal<Effect.Success<ReturnType<typeof optionalPiped>>, Output | undefined>,
398 ] = [true, true, true, true, true, true, true, true];
399 expect(assertions.every(Boolean)).toBe(true);
400 });
401
402 it("preserves explicit output types with stage options across test adapters", () => {
403 type Equal<A, B> =
404 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
405 type DeclaredOutput = Effect.Success<typeof program>["output"];
406 type Output = { branch: string; worker: string };
407 const bunDeploy: BunTestApi["deploy"] = api.deploy;
408 const vitestDeploy: VitestTestApi["deploy"] = api.deploy;
409 const adapter = api.deploy<DeclaredOutput>(program, { stage: "override" });
410 const core = TestCore.deploy<DeclaredOutput>({ providers }, program, {
411 stage: "override",
412 });
413 const bun = bunDeploy<DeclaredOutput>(program, { stage: "override" });
414 const vitest = vitestDeploy<DeclaredOutput>(program, { stage: "override" });
415 const full = api.deploy<DeclaredOutput>(program);
416 const filtered = api.deploy<DeclaredOutput, [options: { include: ReadonlyArray<string> }]>(
417 program,
418 { include: ["Branch"] },
419 );
420 const optional = (options: TestCore.FilteredDeployCallOptions) =>
421 api.deploy<DeclaredOutput, [options?: TestCore.FilteredDeployCallOptions]>(program, options);
422 const bunPiped = program.pipe(bunDeploy);
423 const vitestPiped = program.pipe(vitestDeploy);
424 const bunFiltered = bunDeploy(program, { include: ["Branch"] });
425 const vitestFiltered = vitestDeploy(program, { include: ["Branch"] });
426 const assertions: [
427 Equal<Effect.Success<typeof adapter>, Output>,
428 Equal<Effect.Success<typeof core>, Output>,
429 Equal<Effect.Success<typeof bun>, Output>,
430 Equal<Effect.Success<typeof vitest>, Output>,
431 Equal<Effect.Success<typeof full>, Output>,
432 Equal<Effect.Success<typeof filtered>, undefined>,
433 Equal<Effect.Success<ReturnType<typeof optional>>, Output | undefined>,
434 Equal<Effect.Success<typeof bunPiped>, Output>,
435 Equal<Effect.Success<typeof vitestPiped>, Output>,
436 Equal<Effect.Success<typeof bunFiltered>, undefined>,
437 Equal<Effect.Success<typeof vitestFiltered>, undefined>,
438 ] = [true, true, true, true, true, true, true, true, true, true, true];
439 expect(assertions.every(Boolean)).toBe(true);
440 });
441
442 api.test(
443 "selected then full deployment reuses the same persisted resource",
444 Effect.gen(function* () {
445 yield* api.destroy(program);
446 const selected: void = yield* api.deploy(program, {
447 include: ["Branch"],
448 });
449 expect(selected).toBeUndefined();
450 const state = yield* store;
451 const key = { stack: program.stackName, stage: "filtered-api" };
452 const branch = yield* state.get({ ...key, fqn: "Branch" });
453 expect(branch).toBeDefined();
454 if (!branch || State.isActionState(branch)) {
455 return yield* Effect.die("Expected a persisted Branch resource");
456 }
457 expect(yield* state.get({ ...key, fqn: "Worker" })).toBeUndefined();
458 expect(yield* state.getOutput(key)).toBeUndefined();
459 const full: { branch: string; worker: string } = yield* api.deploy(program);
460 expect(full).toEqual({ branch: "database", worker: "database" });
461 const piped: { branch: string; worker: string } = yield* program.pipe(api.deploy);
462 expect(piped).toEqual(full);
463 expect(yield* state.get({ ...key, fqn: "Branch" })).toMatchObject({
464 instanceId: branch.instanceId,
465 });
466 expect(yield* state.getOutput(key)).toEqual(full);
467 const direct: void = yield* Deploy.deploy({
468 stack: program,
469 stage: key.stage,
470 include: ["Branch"],
471 });
472 expect(direct).toBeUndefined();
473 expect(yield* state.getOutput(key)).toEqual(full);
474 const directFull: { branch: string; worker: string } = yield* Deploy.deploy({
475 stack: program,
476 stage: key.stage,
477 });
478 expect(directFull).toEqual(full);
479 const invalid = yield* api.deploy(program, { include: [] }).pipe(Effect.exit);
480 expect(EffectExit.isFailure(invalid)).toBe(true);
481 expect(yield* state.getOutput(key)).toEqual(full);
482 yield* api.destroy(program);
483 }),
484 );
485 });