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