@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 146 lines 5.53 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { LogGroup } from "@alchemy.run/aws/Logs/LogGroup";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { driftOf, repairDrift, waitForDrift } from "../drift.ts";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const describeLogGroup = Effect.fn(function* (logGroupName: string) {
13 const described = yield* logs.describeLogGroups({ logGroupNamePrefix: logGroupName, limit: 1 });
14 return (described.logGroups ?? []).find((group) => group.logGroupName === logGroupName);
15 });
16
17 // Canonical `list()` test (AWS account/region-scoped collection): deploy a real
18 // log group, resolve the provider from context via the typed `findProvider`,
19 // call `list()`, and assert the deployed log group appears in the
20 // exhaustively-paginated result.
21 test.provider(
22 "list enumerates the deployed log group",
23 (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 const logGroup = yield* stack.deploy(
28 Effect.gen(function* () {
29 return yield* LogGroup("ListLogGroup", {
30 logGroupName: "alchemy-test-log-group-list",
31 retention: "7 days",
32 });
33 }),
34 );
35
36 const provider = yield* Provider.findProvider(LogGroup);
37 const all = yield* provider.list();
38
39 expect(all.some((g) => g.logGroupName === logGroup.logGroupName)).toBe(true);
40
41 yield* stack.destroy();
42 // Assert the log group is gone after the final destroy.
43 expect(yield* describeLogGroup(logGroup.logGroupName)).toBeUndefined();
44 }),
45 { tags: ["provider:aws", "provider:aws:logs", "live"] },
46 );
47
48 test.provider(
49 "configures log group class and deletion protection",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 return yield* LogGroup("ConfiguredLogGroup", {
57 retention: "7 days",
58 logGroupClass: "INFREQUENT_ACCESS",
59 deletionProtectionEnabled: false,
60 });
61 }),
62 );
63
64 expect(created.logGroupClass).toBe("INFREQUENT_ACCESS");
65 expect(created.deletionProtectionEnabled).toBe(false);
66
67 const observedCreated = yield* describeLogGroup(created.logGroupName);
68 expect(observedCreated?.logGroupClass).toBe("INFREQUENT_ACCESS");
69 expect(observedCreated?.deletionProtectionEnabled).toBe(false);
70 expect(observedCreated?.retentionInDays).toBe(7);
71
72 const updated = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* LogGroup("ConfiguredLogGroup", {
75 retention: "14 days",
76 logGroupClass: "INFREQUENT_ACCESS",
77 deletionProtectionEnabled: true,
78 });
79 }),
80 );
81
82 expect(updated.logGroupClass).toBe("INFREQUENT_ACCESS");
83 expect(updated.deletionProtectionEnabled).toBe(true);
84
85 const observedUpdated = yield* describeLogGroup(updated.logGroupName);
86 expect(observedUpdated?.logGroupClass).toBe("INFREQUENT_ACCESS");
87 expect(observedUpdated?.deletionProtectionEnabled).toBe(true);
88 expect(observedUpdated?.retentionInDays).toBe(14);
89
90 const replaced = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* LogGroup("ConfiguredLogGroup", {
93 retention: "14 days",
94 logGroupClass: "STANDARD",
95 deletionProtectionEnabled: false,
96 });
97 }),
98 );
99
100 expect(replaced.logGroupName).not.toBe(updated.logGroupName);
101 expect(replaced.logGroupClass).toBe("STANDARD");
102 expect(replaced.deletionProtectionEnabled).toBe(false);
103
104 const observedReplaced = yield* describeLogGroup(replaced.logGroupName);
105 expect(observedReplaced?.logGroupClass ?? "STANDARD").toBe("STANDARD");
106 expect(observedReplaced?.deletionProtectionEnabled).toBe(false);
107 expect(observedReplaced?.retentionInDays).toBe(14);
108 expect(yield* describeLogGroup(updated.logGroupName)).toBeUndefined();
109
110 yield* stack.destroy();
111 expect(yield* describeLogGroup(replaced.logGroupName)).toBeUndefined();
112 }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))),
113 { tags: ["provider:aws", "provider:aws:logs", "live"] },
114 );
115
116 // #2688: `read` must observe the live tags, not carry the persisted ones.
117 test.provider(
118 "drift detects and repairs out-of-band log group tags",
119 (stack) =>
120 Effect.gen(function* () {
121 yield* stack.destroy();
122
123 const group = yield* stack.deploy(
124 LogGroup("DriftLogGroup", { retention: "1 day", tags: { team: "declared" } }),
125 );
126 expect((yield* driftOf(stack, "DriftLogGroup"))?.action).toBe("unchanged");
127
128 yield* logs.tagResource({
129 resourceArn: group.logGroupArn,
130 tags: { team: "out-of-band" },
131 });
132
133 const drifted = yield* waitForDrift(stack, "DriftLogGroup", "drifted");
134 expect(drifted?.attr?.tags?.team).toBe("out-of-band");
135
136 const repaired = yield* repairDrift(stack);
137 expect(repaired.resources.DriftLogGroup?.action).toBe("repaired");
138 expect((yield* driftOf(stack, "DriftLogGroup"))?.action).toBe("unchanged");
139 const tags = yield* logs.listTagsForResource({ resourceArn: group.logGroupArn });
140 expect(tags.tags?.team).toBe("declared");
141
142 yield* stack.destroy();
143 expect(yield* describeLogGroup(group.logGroupName)).toBeUndefined();
144 }),
145 { tags: ["provider:aws", "provider:aws:logs", "live"] },
146 );