1import * as AWS from "@/AWS";
2import { LogGroup } from "@/AWS/Logs/LogGroup.ts";
3import * as Provider from "@/Provider";
4import * as Test from "@/Test/Alchemy";
5import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8
9const { test } = Test.make({ providers: AWS.providers() });
10
11const describeLogGroup = Effect.fn(function* (logGroupName: string) {
12 const described = yield* logs.describeLogGroups({
13 logGroupNamePrefix: logGroupName,
14 limit: 1,
15 });
16 return (described.logGroups ?? []).find(
17 (group) => group.logGroupName === logGroupName,
18 );
19});
20
21// Canonical `list()` test (AWS account/region-scoped collection): deploy a real
22// log group, resolve the provider from context via the typed `findProvider`,
23// call `list()`, and assert the deployed log group appears in the
24// exhaustively-paginated result.
25test.provider(
26 "list enumerates the deployed log group",
27 (stack) =>
28 Effect.gen(function* () {
29 yield* stack.destroy();
30
31 const logGroup = yield* stack.deploy(
32 Effect.gen(function* () {
33 return yield* LogGroup("ListLogGroup", {
34 logGroupName: "alchemy-test-log-group-list",
35 retention: "7 days",
36 });
37 }),
38 );
39
40 const provider = yield* Provider.findProvider(LogGroup);
41 const all = yield* provider.list();
42
43 expect(all.some((g) => g.logGroupName === logGroup.logGroupName)).toBe(
44 true,
45 );
46
47 yield* stack.destroy();
48 // Assert the log group is gone after the final destroy.
49 expect(yield* describeLogGroup(logGroup.logGroupName)).toBeUndefined();
50 }),
51 { tags: ["provider:aws", "provider:aws:logs", "live"] },
52);
53
54test.provider(
55 "configures log group class and deletion protection",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 return yield* LogGroup("ConfiguredLogGroup", {
63 retention: "7 days",
64 logGroupClass: "INFREQUENT_ACCESS",
65 deletionProtectionEnabled: false,
66 });
67 }),
68 );
69
70 expect(created.logGroupClass).toBe("INFREQUENT_ACCESS");
71 expect(created.deletionProtectionEnabled).toBe(false);
72
73 const observedCreated = yield* describeLogGroup(created.logGroupName);
74 expect(observedCreated?.logGroupClass).toBe("INFREQUENT_ACCESS");
75 expect(observedCreated?.deletionProtectionEnabled).toBe(false);
76 expect(observedCreated?.retentionInDays).toBe(7);
77
78 const updated = yield* stack.deploy(
79 Effect.gen(function* () {
80 return yield* LogGroup("ConfiguredLogGroup", {
81 retention: "14 days",
82 logGroupClass: "INFREQUENT_ACCESS",
83 deletionProtectionEnabled: true,
84 });
85 }),
86 );
87
88 expect(updated.logGroupClass).toBe("INFREQUENT_ACCESS");
89 expect(updated.deletionProtectionEnabled).toBe(true);
90
91 const observedUpdated = yield* describeLogGroup(updated.logGroupName);
92 expect(observedUpdated?.logGroupClass).toBe("INFREQUENT_ACCESS");
93 expect(observedUpdated?.deletionProtectionEnabled).toBe(true);
94 expect(observedUpdated?.retentionInDays).toBe(14);
95
96 const replaced = yield* stack.deploy(
97 Effect.gen(function* () {
98 return yield* LogGroup("ConfiguredLogGroup", {
99 retention: "14 days",
100 logGroupClass: "STANDARD",
101 deletionProtectionEnabled: false,
102 });
103 }),
104 );
105
106 expect(replaced.logGroupName).not.toBe(updated.logGroupName);
107 expect(replaced.logGroupClass).toBe("STANDARD");
108 expect(replaced.deletionProtectionEnabled).toBe(false);
109
110 const observedReplaced = yield* describeLogGroup(replaced.logGroupName);
111 expect(observedReplaced?.logGroupClass ?? "STANDARD").toBe("STANDARD");
112 expect(observedReplaced?.deletionProtectionEnabled).toBe(false);
113 expect(observedReplaced?.retentionInDays).toBe(14);
114 expect(yield* describeLogGroup(updated.logGroupName)).toBeUndefined();
115
116 yield* stack.destroy();
117 expect(yield* describeLogGroup(replaced.logGroupName)).toBeUndefined();
118 }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))),
119 { tags: ["provider:aws", "provider:aws:logs", "live"] },
120);