@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 133 lines 4.62 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Group } from "@alchemy.run/aws/XRay";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as xray from "@distilled.cloud/aws/xray";
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 findGroup = (groupName: string) =>
13 xray.getGroup({ GroupName: groupName }).pipe(
14 Effect.map((r) => r.Group),
15 Effect.catchTag("GroupNotFound", () => Effect.succeed(undefined)),
16 );
17
18 class GroupStillExists extends Data.TaggedError("GroupStillExists")<{
19 readonly groupName: string;
20 }> {}
21
22 const assertGroupDeleted = (groupName: string) =>
23 findGroup(groupName).pipe(
24 Effect.flatMap((group) =>
25 group === undefined ? Effect.void : Effect.fail(new GroupStillExists({ groupName })),
26 ),
27 Effect.retry({
28 while: (e) => e._tag === "GroupStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 test.provider(
34 "create, update filter + insights, delete group",
35 (stack) =>
36 Effect.gen(function* () {
37 yield* stack.destroy();
38
39 const group = yield* stack.deploy(
40 Effect.gen(function* () {
41 return yield* Group("TestGroup", {
42 filterExpression: 'service("alchemy-xray-test")',
43 tags: { Environment: "test" },
44 });
45 }),
46 );
47
48 expect(group.groupName).toBeDefined();
49 expect(group.groupArn).toContain(":group/");
50
51 // out-of-band verification via distilled
52 const created = yield* findGroup(group.groupName);
53 expect(created?.FilterExpression).toBe('service("alchemy-xray-test")');
54 expect(created?.InsightsConfiguration?.InsightsEnabled).toBe(false);
55 const tags = yield* xray
56 .listTagsForResource({ ResourceARN: group.groupArn })
57 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
58 expect(tags.Environment).toBe("test");
59 expect(tags["alchemy::id"]).toBe("TestGroup");
60
61 // update the filter expression and enable insights
62 const updated = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* Group("TestGroup", {
65 filterExpression: 'service("alchemy-xray-test") AND responsetime > 2',
66 insightsEnabled: true,
67 tags: { Environment: "test" },
68 });
69 }),
70 );
71 expect(updated.groupName).toBe(group.groupName);
72 expect(updated.groupArn).toBe(group.groupArn);
73
74 const afterUpdate = yield* findGroup(group.groupName);
75 expect(afterUpdate?.FilterExpression).toBe(
76 'service("alchemy-xray-test") AND responsetime > 2',
77 );
78 expect(afterUpdate?.InsightsConfiguration?.InsightsEnabled).toBe(true);
79
80 // disabling insights converges back
81 yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* Group("TestGroup", {
84 filterExpression: 'service("alchemy-xray-test") AND responsetime > 2',
85 tags: { Environment: "test" },
86 });
87 }),
88 );
89 const afterDisable = yield* findGroup(group.groupName);
90 expect(afterDisable?.InsightsConfiguration?.InsightsEnabled).toBe(false);
91
92 yield* stack.destroy();
93 yield* assertGroupDeleted(group.groupName);
94 }),
95 { tags: ["provider:aws", "provider:aws:xray", "live"], timeout: 120_000 },
96 );
97
98 test.provider(
99 "custom group name and replacement on rename",
100 (stack) =>
101 Effect.gen(function* () {
102 yield* stack.destroy();
103
104 const first = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* Group("NamedGroup", {
107 groupName: "alchemy-test-group-a",
108 filterExpression: 'service("alchemy-a")',
109 });
110 }),
111 );
112 expect(first.groupName).toBe("alchemy-test-group-a");
113
114 // renaming triggers a replacement: new physical group, old one gone
115 const second = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* Group("NamedGroup", {
118 groupName: "alchemy-test-group-b",
119 filterExpression: 'service("alchemy-b")',
120 });
121 }),
122 );
123 expect(second.groupName).toBe("alchemy-test-group-b");
124
125 const observed = yield* findGroup("alchemy-test-group-b");
126 expect(observed?.FilterExpression).toBe('service("alchemy-b")');
127 yield* assertGroupDeleted("alchemy-test-group-a");
128
129 yield* stack.destroy();
130 yield* assertGroupDeleted("alchemy-test-group-b");
131 }),
132 { tags: ["provider:aws", "provider:aws:xray", "live"], timeout: 120_000 },
133 );