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

main
ts 121 lines 4.33 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { HttpNamespace } from "@alchemy.run/aws/CloudMap";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as sd from "@distilled.cloud/aws/servicediscovery";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const findNamespace = (namespaceId: string) =>
13 sd.getNamespace({ Id: namespaceId }).pipe(
14 Effect.map((r) => r.Namespace),
15 Effect.catchTag("NamespaceNotFound", () => Effect.succeed(undefined)),
16 );
17
18 class NamespaceStillExists extends Data.TaggedError("NamespaceStillExists")<{
19 readonly namespaceId: string;
20 }> {}
21
22 const assertNamespaceDeleted = (namespaceId: string) =>
23 findNamespace(namespaceId).pipe(
24 Effect.flatMap((namespace) =>
25 namespace === undefined
26 ? Effect.void
27 : Effect.fail(new NamespaceStillExists({ namespaceId })),
28 ),
29 Effect.retry({
30 while: (e) => e._tag === "NamespaceStillExists",
31 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
32 }),
33 );
34
35 test.provider(
36 "create, update description, delete HTTP namespace",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const namespace = yield* stack.deploy(
42 Effect.gen(function* () {
43 return yield* HttpNamespace("TestHttpNamespace", {
44 description: "initial description",
45 tags: { Environment: "test" },
46 });
47 }),
48 );
49
50 expect(namespace.namespaceId).toBeDefined();
51 expect(namespace.namespaceArn).toContain(":namespace/");
52 expect(namespace.httpName).toBeDefined();
53
54 // out-of-band verification via distilled
55 const created = yield* findNamespace(namespace.namespaceId);
56 expect(created?.Type).toBe("HTTP");
57 expect(created?.Description).toBe("initial description");
58 const tags = yield* sd
59 .listTagsForResource({ ResourceARN: namespace.namespaceArn })
60 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
61 expect(tags.Environment).toBe("test");
62 expect(tags["alchemy::id"]).toBe("TestHttpNamespace");
63
64 // update the description (async namespace update operation)
65 const updated = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* HttpNamespace("TestHttpNamespace", {
68 description: "updated description",
69 tags: { Environment: "test", Extra: "yes" },
70 });
71 }),
72 );
73 expect(updated.namespaceId).toBe(namespace.namespaceId);
74 expect(updated.namespaceArn).toBe(namespace.namespaceArn);
75
76 const afterUpdate = yield* findNamespace(namespace.namespaceId);
77 expect(afterUpdate?.Description).toBe("updated description");
78 const tagsAfter = yield* sd
79 .listTagsForResource({ ResourceARN: namespace.namespaceArn })
80 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
81 expect(tagsAfter.Extra).toBe("yes");
82
83 yield* stack.destroy();
84 yield* assertNamespaceDeleted(namespace.namespaceId);
85 }),
86 { tags: ["provider:aws", "provider:aws:cloudmap", "live"], timeout: 240_000 },
87 );
88
89 test.provider(
90 "explicit name and replacement on rename",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94
95 const first = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* HttpNamespace("NamedHttpNamespace", {
98 name: "alchemy-test-cloudmap-http-a",
99 });
100 }),
101 );
102 expect(first.namespaceName).toBe("alchemy-test-cloudmap-http-a");
103
104 // renaming triggers a replacement: new physical namespace, old gone
105 const second = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* HttpNamespace("NamedHttpNamespace", {
108 name: "alchemy-test-cloudmap-http-b",
109 });
110 }),
111 );
112 expect(second.namespaceName).toBe("alchemy-test-cloudmap-http-b");
113 expect(second.namespaceId).not.toBe(first.namespaceId);
114
115 yield* assertNamespaceDeleted(first.namespaceId);
116
117 yield* stack.destroy();
118 yield* assertNamespaceDeleted(second.namespaceId);
119 }),
120 { tags: ["provider:aws", "provider:aws:cloudmap", "live"], timeout: 240_000 },
121 );