@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 189 lines 6.76 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import * as KV from "@alchemy.run/cloudflare/KV";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as kv from "@distilled.cloud/cloudflare/kv";
7 import * as Provider from "alchemy/Provider";
8 import { State } from "alchemy/State";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import { MinimumLogLevel } from "effect/References";
12 import * as Schedule from "effect/Schedule";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 test.provider(
19 "create and delete namespace with default props",
20 (stack) =>
21 Effect.gen(function* () {
22 const { accountId } = yield* yield* CloudflareEnvironment;
23
24 yield* stack.destroy();
25
26 const namespace = yield* stack.deploy(
27 Effect.gen(function* () {
28 return yield* KV.Namespace("DefaultNamespace");
29 }),
30 );
31
32 expect(namespace.title).toBeDefined();
33 expect(namespace.namespaceId).toBeDefined();
34
35 const actualNamespace = yield* kv.getNamespace({
36 accountId,
37 namespaceId: namespace.namespaceId,
38 });
39 expect(actualNamespace.id).toEqual(namespace.namespaceId);
40
41 yield* stack.destroy();
42
43 yield* waitForNamespaceToBeDeleted(namespace.namespaceId, accountId);
44 }).pipe(logLevel),
45 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"] },
46 );
47
48 test.provider(
49 "create, update, delete namespace",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53
54 yield* stack.destroy();
55
56 const namespace = yield* stack.deploy(
57 Effect.gen(function* () {
58 return yield* KV.Namespace("TestNamespace");
59 }),
60 );
61
62 const actualNamespace = yield* kv.getNamespace({
63 accountId,
64 namespaceId: namespace.namespaceId,
65 });
66 expect(actualNamespace.id).toEqual(namespace.namespaceId);
67 expect(actualNamespace.title).toEqual(namespace.title);
68
69 const updatedNamespace = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* KV.Namespace("TestNamespace", { title: namespace.title + "-updated" });
72 }),
73 );
74
75 const actualUpdatedNamespace = yield* kv.getNamespace({
76 accountId,
77 namespaceId: updatedNamespace.namespaceId,
78 });
79 expect(actualUpdatedNamespace.title).toEqual(namespace.title + "-updated");
80 expect(actualUpdatedNamespace.id).toEqual(updatedNamespace.namespaceId);
81
82 yield* stack.destroy();
83
84 yield* waitForNamespaceToBeDeleted(namespace.namespaceId, accountId);
85 }).pipe(logLevel),
86 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"] },
87 );
88
89 // Canonical `list()` test (account-scoped collection): deploy a real
90 // namespace, resolve the provider from context via `findProviderByType`,
91 // call `list()`, and assert the deployed namespace appears in the
92 // exhaustively-paginated result.
93 test.provider(
94 "list enumerates the deployed namespace",
95 (stack) =>
96 Effect.gen(function* () {
97 yield* stack.destroy();
98
99 const namespace = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* KV.Namespace("ListNamespace");
102 }),
103 );
104
105 const provider = yield* Provider.findProvider(KV.Namespace);
106 const all = yield* provider.list();
107
108 expect(all.some((ns) => ns.namespaceId === namespace.namespaceId)).toBe(true);
109
110 yield* stack.destroy();
111 }).pipe(logLevel),
112 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"] },
113 );
114
115 // Engine-level adoption: KV namespaces have no ownership signal (Cloudflare
116 // doesn't expose tags on KV), so a name match in `read` is treated as silent
117 // adoption. The test wipes local state mid-run while leaving the namespace
118 // on Cloudflare — this simulates a fresh state store seeing an existing
119 // resource with the same physical name.
120 test.provider(
121 "existing namespace (matching title) is silently adopted without --adopt",
122 (stack) =>
123 Effect.gen(function* () {
124 const { accountId } = yield* yield* CloudflareEnvironment;
125
126 yield* stack.destroy();
127
128 // Phase 1: deploy normally so a real KV namespace exists on
129 // Cloudflare. No explicit `title` — the engine generates a
130 // random-suffixed physical name (collision-free across concurrent
131 // runs); the deploy output hands back the real title, which pins the
132 // namespace's identity for the adoption phase below.
133 const initial = yield* stack.deploy(
134 Effect.gen(function* () {
135 return yield* KV.Namespace("AdoptableNamespace");
136 }),
137 );
138 const title = initial.title;
139 const initialId = initial.namespaceId;
140 expect(initialId).toBeDefined();
141
142 // Phase 2: wipe local state — the namespace stays on Cloudflare.
143 yield* Effect.gen(function* () {
144 const state = yield* yield* State;
145 yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "AdoptableNamespace" });
146 }).pipe(Effect.provide(stack.state));
147
148 // Phase 3: redeploy without `adopt(true)`. The engine calls
149 // `provider.read`, which lists namespaces, matches by title, and
150 // returns plain attrs — silent adoption.
151 const adopted = yield* stack.deploy(
152 Effect.gen(function* () {
153 return yield* KV.Namespace("AdoptableNamespace", { title });
154 }),
155 );
156
157 // Same physical namespace — adoption, not re-creation.
158 expect(adopted.namespaceId).toEqual(initialId);
159 expect(adopted.title).toEqual(title);
160
161 const persisted = yield* Effect.gen(function* () {
162 const state = yield* yield* State;
163 return yield* state.get({
164 stack: stack.name,
165 stage: stack.stage,
166 fqn: "AdoptableNamespace",
167 });
168 }).pipe(Effect.provide(stack.state));
169
170 expect((persisted as any)?.attr).toMatchObject({ namespaceId: initialId, title });
171
172 yield* stack.destroy();
173 yield* waitForNamespaceToBeDeleted(initialId, accountId);
174 }).pipe(logLevel),
175 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"] },
176 );
177
178 const waitForNamespaceToBeDeleted = Effect.fn(function* (namespaceId: string, accountId: string) {
179 yield* kv.getNamespace({ accountId, namespaceId }).pipe(
180 Effect.flatMap(() => Effect.fail(new NamespaceStillExists())),
181 Effect.retry({
182 while: (e): e is NamespaceStillExists => e instanceof NamespaceStillExists,
183 schedule: Schedule.exponential(100),
184 }),
185 Effect.catchTag("NamespaceNotFound", () => Effect.void),
186 );
187 });
188
189 class NamespaceStillExists extends Data.TaggedError("NamespaceStillExists") {}