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

main
ts 149 lines 5.83 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy";
7 import * as Provider from "alchemy/Provider";
8 import { isResourceState, State, type ResourceState } from "alchemy/State";
9 import * as Cause from "effect/Cause";
10 import * as Effect from "effect/Effect";
11 import * as Stream from "effect/Stream";
12
13 const { test } = Test.make({ providers: Cloudflare.providers(), state: Cloudflare.state() });
14
15 test.provider(
16 "list enumerates the deployed infrastructure target",
17 (stack) =>
18 Effect.gen(function* () {
19 const { accountId } = yield* yield* CloudflareEnvironment;
20
21 yield* stack.destroy();
22
23 const target = yield* stack.deploy(
24 Effect.gen(function* () {
25 return yield* Cloudflare.Access.InfrastructureTarget("ListTarget", {
26 hostname: "list-test.bastion.internal",
27 ip: { ipv4: { ipAddr: "10.7.0.42" } },
28 });
29 }),
30 );
31
32 const provider = yield* Provider.findProvider(Cloudflare.Access.InfrastructureTarget);
33 const all = yield* provider.list();
34
35 const found = all.find((t) => t.targetId === target.targetId);
36 expect(found).toBeDefined();
37 expect(found?.accountId).toEqual(accountId);
38 expect(found?.hostname).toEqual(target.hostname);
39 expect(found?.ip.ipv4?.ipAddr).toEqual("10.7.0.42");
40
41 yield* stack.destroy();
42 }),
43 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
44 );
45
46 test.provider(
47 "recovers a half-created target whose creating-state lost Output-valued props (#736)",
48 (stack) =>
49 Effect.gen(function* () {
50 const hostname = "wedged-recovery.bastion.internal";
51
52 yield* stack.destroy();
53
54 // Safety net: a previously crashed run leaves an orphaned cloud
55 // target (its state row is wedged attr-less, so destroy can't reach
56 // it) which would fail the fresh deploy with OwnedBySomeoneElse.
57 // Sweep any target with our deterministic hostname out-of-band.
58 const { accountId } = yield* yield* CloudflareEnvironment;
59 const orphans = yield* zeroTrust.listAccessInfrastructureTargets
60 .items({ accountId, hostname })
61 .pipe(
62 Stream.filter((t) => t.hostname === hostname),
63 Stream.runCollect,
64 );
65 yield* Effect.forEach(Array.from(orphans), (t) =>
66 zeroTrust
67 .deleteAccessInfrastructureTarget({ accountId, targetId: t.id })
68 .pipe(Effect.catchTag("TargetNotFound", () => Effect.void)),
69 );
70
71 const deployTarget = (adoptIt?: boolean) =>
72 stack.deploy(
73 Effect.gen(function* () {
74 const target = Cloudflare.Access.InfrastructureTarget("WedgedTarget", {
75 hostname,
76 ip: { ipv4: { ipAddr: "10.7.0.99" } },
77 });
78 return yield* adoptIt ? target.pipe(adopt(true)) : target;
79 }),
80 );
81
82 const created = yield* deployTarget();
83
84 // Rewrite the target's persisted row into the wedged shape an
85 // interrupted deploy leaves behind: `creating`, no attributes, and
86 // the Output-valued `ip` prop lost in the round-trip. `hostname`
87 // survives so the cold-read identity path runs.
88 const state = yield* yield* State;
89 const stage = stack.stage;
90 const fqns = yield* state.list({ stack: stack.name, stage });
91 const rows = yield* Effect.forEach(fqns, (fqn) =>
92 state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))),
93 );
94 const wedged = rows.find(
95 (r): r is { fqn: string; row: ResourceState } =>
96 isResourceState(r.row) && r.row.resourceType === "Cloudflare.Access.InfrastructureTarget",
97 );
98 if (!wedged) {
99 return yield* Effect.die(
100 new Error("no Cloudflare.Access.InfrastructureTarget state row found after deploy"),
101 );
102 }
103 yield* state.set({
104 stack: stack.name,
105 stage,
106 fqn: wedged.fqn,
107 value: {
108 ...wedged.row,
109 status: "creating",
110 attr: undefined,
111 props: { ...wedged.row.props, ip: undefined },
112 },
113 });
114
115 // Before the #736 fix, `read` crashed here with
116 // `TypeError: undefined is not an object (evaluating 'ip.ipv4')`
117 // (resolvedIp(olds.ip) with olds.ip === undefined). After the fix,
118 // read falls back to the output hint and matches by hostname —
119 // but targets carry no ownership markers, so the match surfaces as
120 // `Unowned`. Since #862, resuming an interrupted create onto an
121 // Unowned resource fails loudly instead of silently taking over.
122 const error = yield* deployTarget().pipe(
123 Effect.as(undefined),
124 Effect.catchCause((cause) => Effect.succeed(findOwnedError(cause))),
125 );
126 expect(error).toBeInstanceOf(OwnedBySomeoneElse);
127
128 // adopt(true) opts in: the engine converges onto the same target —
129 // no duplicate created.
130 const recovered = yield* deployTarget(true);
131 expect(recovered.targetId).toEqual(created.targetId);
132 expect(recovered.hostname).toEqual(created.hostname);
133 expect(recovered.ip.ipv4?.ipAddr).toEqual("10.7.0.99");
134
135 yield* stack.destroy();
136 }),
137 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], timeout: 240_000 },
138 );
139
140 const findOwnedError = (cause: Cause.Cause<unknown>): OwnedBySomeoneElse | undefined =>
141 cause.reasons
142 .map((reason) =>
143 Cause.isFailReason(reason)
144 ? reason.error
145 : Cause.isDieReason(reason)
146 ? reason.defect
147 : undefined,
148 )
149 .find((value): value is OwnedBySomeoneElse => value instanceof OwnedBySomeoneElse);