@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, 06:00 UTC

main
ts 86 lines 3.21 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import type * as Test from "@alchemy.run/test/Alchemy";
3 import { getProject } from "@distilled.cloud/prisma/management";
4 import { isActionState, State } from "alchemy/State/State";
5 import * as Cause from "effect/Cause";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 /** Poll `read` (true = gone) until it reports gone, then assert it. */
10 export const expectGone = <E, R>(read: Effect.Effect<boolean, E, R>) =>
11 read.pipe(
12 Effect.repeat({
13 schedule: Schedule.spaced("1 second"),
14 times: 8,
15 until: (gone) => gone,
16 }),
17 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
18 );
19
20 export const expectProjectGone = (id: string) =>
21 expectGone(
22 getProject({ id }).pipe(
23 Effect.as(false),
24 Effect.catchTag("NotFound", () => Effect.succeed(true)),
25 ),
26 );
27
28 /** Drop a resource's state row, as if the state store lost it. The cloud resource stays. */
29 export const forgetState = (stack: Test.ScratchStack, fqn: string) =>
30 Effect.gen(function* () {
31 const state = yield* yield* State;
32 yield* state.delete({ stack: stack.name, stage: stack.stage, fqn });
33 }).pipe(Effect.provide(stack.state));
34
35 /** Overwrite fields of a created resource's persisted attributes. */
36 export const patchStateAttr = (
37 stack: Test.ScratchStack,
38 fqn: string,
39 patch: Record<string, unknown>,
40 ) =>
41 Effect.gen(function* () {
42 const state = yield* yield* State;
43 const address = { stack: stack.name, stage: stack.stage, fqn };
44 const stored = yield* state.get(address);
45 if (!stored || isActionState(stored) || stored.status !== "created") {
46 return yield* Effect.die(new Error(`Expected a created state row for '${fqn}'`));
47 }
48 yield* state.set({
49 ...address,
50 value: { ...stored, attr: { ...(stored.attr as object), ...patch } },
51 });
52 }).pipe(Effect.provide(stack.state));
53
54 /** Run an effect that must fail; return its failure and defect values and the printed cause. */
55 export const failureOf = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
56 effect.pipe(
57 Effect.flatMap(() => Effect.die(new Error("Expected the deploy to fail"))),
58 Effect.catchCause((cause) =>
59 Effect.succeed({
60 errors: cause.reasons.flatMap((reason) =>
61 Cause.isFailReason(reason)
62 ? [reason.error]
63 : Cause.isDieReason(reason)
64 ? [reason.defect]
65 : [],
66 ),
67 text: Cause.pretty(cause),
68 }),
69 ),
70 );
71
72 /**
73 * Turn a created row into an interrupted create: status `creating`, no
74 * attributes, same instance ID. The next deploy must recover the resource.
75 */
76 export const markCreating = (stack: Test.ScratchStack, fqn: string) =>
77 Effect.gen(function* () {
78 const state = yield* yield* State;
79 const address = { stack: stack.name, stage: stack.stage, fqn };
80 const stored = yield* state.get(address);
81 if (!stored || isActionState(stored) || stored.status !== "created") {
82 return yield* Effect.die(new Error(`Expected a created state row for '${fqn}'`));
83 }
84 const { attr: _attr, ...creating } = stored;
85 yield* state.set({ ...address, value: { ...creating, status: "creating" } as never });
86 }).pipe(Effect.provide(stack.state));