@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 109 lines 4.58 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { HttpNamespace, InstanceRegistration, Service } 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 findInstance = (serviceId: string, instanceId: string) =>
13 sd.getInstance({ ServiceId: serviceId, InstanceId: instanceId }).pipe(
14 Effect.map((r) => r.Instance),
15 Effect.catchTag(["InstanceNotFound", "ServiceNotFound"], () => Effect.succeed(undefined)),
16 );
17
18 class InstanceStillExists extends Data.TaggedError("InstanceStillExists")<{
19 readonly instanceId: string;
20 }> {}
21
22 const assertInstanceDeleted = (serviceId: string, instanceId: string) =>
23 findInstance(serviceId, instanceId).pipe(
24 Effect.flatMap((instance) =>
25 instance === undefined ? Effect.void : Effect.fail(new InstanceStillExists({ instanceId })),
26 ),
27 Effect.retry({
28 while: (e) => e._tag === "InstanceStillExists",
29 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
30 }),
31 );
32
33 test.provider(
34 "register two instances, update attributes, destroy chain (instances -> service -> namespace)",
35 (stack) =>
36 Effect.gen(function* () {
37 yield* stack.destroy();
38
39 const makeStack = (alphaEndpoint: string) =>
40 Effect.gen(function* () {
41 const namespace = yield* HttpNamespace("InstanceTestNamespace", {
42 name: "alchemy-test-cloudmap-instances",
43 });
44 const service = yield* Service("Workers", {
45 namespaceId: namespace.namespaceId,
46 name: "workers",
47 });
48 const alpha = yield* InstanceRegistration("Alpha", {
49 serviceId: service.serviceId,
50 instanceId: "alpha",
51 attributes: { endpoint: alphaEndpoint, zone: "us-west-2a" },
52 });
53 const beta = yield* InstanceRegistration("Beta", {
54 serviceId: service.serviceId,
55 instanceId: "beta",
56 attributes: { endpoint: "http://beta.internal:8080" },
57 });
58 return { namespace, service, alpha, beta };
59 });
60
61 const { namespace, service, alpha, beta } = yield* stack.deploy(
62 makeStack("http://alpha.internal:8080"),
63 );
64
65 expect(alpha.instanceId).toBe("alpha");
66 expect(beta.instanceId).toBe("beta");
67 expect(alpha.serviceId).toBe(service.serviceId);
68
69 // out-of-band verification via distilled
70 const alphaInstance = yield* findInstance(service.serviceId, "alpha");
71 expect(alphaInstance?.Attributes?.endpoint).toBe("http://alpha.internal:8080");
72 expect(alphaInstance?.Attributes?.zone).toBe("us-west-2a");
73 const betaInstance = yield* findInstance(service.serviceId, "beta");
74 expect(betaInstance?.Attributes?.endpoint).toBe("http://beta.internal:8080");
75
76 // both instances are discoverable via the data-plane API
77 const discovered = yield* sd.discoverInstances({
78 NamespaceName: "alchemy-test-cloudmap-instances",
79 ServiceName: "workers",
80 HealthStatus: "ALL",
81 });
82 const discoveredIds = (discovered.Instances ?? []).map((i) => i.InstanceId).sort();
83 expect(discoveredIds).toEqual(["alpha", "beta"]);
84
85 // attribute update re-registers (upsert) with the same identity
86 const updated = yield* stack.deploy(makeStack("http://alpha.internal:9090"));
87 expect(updated.alpha.serviceId).toBe(service.serviceId);
88 expect(updated.alpha.instanceId).toBe("alpha");
89
90 const afterUpdate = yield* findInstance(service.serviceId, "alpha");
91 expect(afterUpdate?.Attributes?.endpoint).toBe("http://alpha.internal:9090");
92
93 // destroy tears down instances -> service -> namespace in order
94 yield* stack.destroy();
95 yield* assertInstanceDeleted(service.serviceId, "alpha");
96 yield* assertInstanceDeleted(service.serviceId, "beta");
97 const serviceGone = yield* sd.getService({ Id: service.serviceId }).pipe(
98 Effect.map(() => false),
99 Effect.catchTag("ServiceNotFound", () => Effect.succeed(true)),
100 );
101 expect(serviceGone).toBe(true);
102 const namespaceGone = yield* sd.getNamespace({ Id: namespace.namespaceId }).pipe(
103 Effect.map(() => false),
104 Effect.catchTag("NamespaceNotFound", () => Effect.succeed(true)),
105 );
106 expect(namespaceGone).toBe(true);
107 }),
108 { tags: ["provider:aws", "provider:aws:cloudmap", "live"], timeout: 240_000 },
109 );