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

main
ts 138 lines 5.32 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { PrivateDnsNamespace, 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 Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { getDefaultVpc } from "../DefaultVpc.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const findService = (serviceId: string) =>
15 sd.getService({ Id: serviceId }).pipe(
16 Effect.map((r) => r.Service),
17 Effect.catchTag("ServiceNotFound", () => Effect.succeed(undefined)),
18 );
19
20 class ServiceStillExists extends Data.TaggedError("ServiceStillExists")<{
21 readonly serviceId: string;
22 }> {}
23
24 const assertServiceDeleted = (serviceId: string) =>
25 findService(serviceId).pipe(
26 Effect.flatMap((service) =>
27 service === undefined ? Effect.void : Effect.fail(new ServiceStillExists({ serviceId })),
28 ),
29 Effect.retry({
30 while: (e) => e._tag === "ServiceStillExists",
31 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
32 }),
33 );
34
35 test.provider(
36 "DNS service lifecycle: create, update TTL + description, replace on record type change",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40 const vpc = yield* getDefaultVpc;
41
42 const makeStack = (
43 records: { type: "A" | "SRV"; ttl: Duration.Input }[],
44 description: string,
45 attributes?: Record<string, string>,
46 ) =>
47 Effect.gen(function* () {
48 const namespace = yield* PrivateDnsNamespace("ServiceTestNamespace", {
49 name: "alchemy-test-cloudmap-service.local",
50 vpc: vpc.vpcId,
51 });
52 // no pinned name: replacement must generate a fresh physical name
53 // (a pinned name would collide during create-before-delete)
54 const service = yield* Service("Backend", {
55 namespaceId: namespace.namespaceId,
56 description,
57 dnsRecords: records,
58 routingPolicy: "MULTIVALUE",
59 attributes,
60 tags: { Environment: "test" },
61 });
62 return { namespace, service };
63 });
64
65 const findServiceAttributes = (serviceId: string) =>
66 sd
67 .getServiceAttributes({ ServiceId: serviceId })
68 .pipe(Effect.map((r) => r.ServiceAttributes?.Attributes ?? {}));
69
70 const { namespace, service } = yield* stack.deploy(
71 makeStack([{ type: "A", ttl: "10 seconds" }], "initial", {
72 tier: "backend",
73 stage: "one",
74 }),
75 );
76
77 expect(service.serviceId).toBeDefined();
78 expect(service.serviceArn).toContain(":service/");
79 // engine-generated physical name (long test-derived prefixes truncate,
80 // so only assert shape)
81 expect(service.serviceName.length).toBeGreaterThan(0);
82 expect(service.serviceName.length).toBeLessThanOrEqual(63);
83 expect(service.namespaceId).toBe(namespace.namespaceId);
84 expect(service.namespaceName).toBe("alchemy-test-cloudmap-service.local");
85
86 // out-of-band verification via distilled
87 const created = yield* findService(service.serviceId);
88 expect(created?.Description).toBe("initial");
89 expect(created?.DnsConfig?.RoutingPolicy).toBe("MULTIVALUE");
90 expect(created?.DnsConfig?.DnsRecords).toEqual([{ Type: "A", TTL: 10 }]);
91 const tags = yield* sd
92 .listTagsForResource({ ResourceARN: service.serviceArn })
93 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
94 expect(tags.Environment).toBe("test");
95 expect(tags["alchemy::id"]).toBe("Backend");
96
97 // custom service attributes were written on create
98 expect(yield* findServiceAttributes(service.serviceId)).toEqual({
99 tier: "backend",
100 stage: "one",
101 });
102
103 // TTL + description + attributes are mutable via updateService /
104 // updateServiceAttributes / deleteServiceAttributes (tier changed,
105 // stage removed, extra added)
106 const updated = yield* stack.deploy(
107 makeStack([{ type: "A", ttl: "30 seconds" }], "updated", {
108 tier: "web",
109 extra: "x",
110 }),
111 );
112 expect(updated.service.serviceId).toBe(service.serviceId);
113
114 const afterUpdate = yield* findService(service.serviceId);
115 expect(afterUpdate?.Description).toBe("updated");
116 expect(afterUpdate?.DnsConfig?.DnsRecords).toEqual([{ Type: "A", TTL: 30 }]);
117 expect(yield* findServiceAttributes(service.serviceId)).toEqual({
118 tier: "web",
119 extra: "x",
120 });
121
122 // changing the record TYPE replaces the service
123 const replaced = yield* stack.deploy(
124 makeStack([{ type: "SRV", ttl: "30 seconds" }], "updated"),
125 );
126 expect(replaced.service.serviceId).not.toBe(service.serviceId);
127 const afterReplace = yield* findService(replaced.service.serviceId);
128 expect(afterReplace?.DnsConfig?.DnsRecords).toEqual([{ Type: "SRV", TTL: 30 }]);
129 yield* assertServiceDeleted(service.serviceId);
130
131 yield* stack.destroy();
132 yield* assertServiceDeleted(replaced.service.serviceId);
133 }),
134 {
135 tags: ["provider:aws", "provider:aws:cloudmap", "provider:aws:ec2", "live"],
136 timeout: 240_000,
137 },
138 );