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

main
ts 184 lines 6.54 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM";
3 import { Schedule } from "@alchemy.run/aws/Scheduler/Schedule";
4 import { ScheduleGroup } from "@alchemy.run/aws/Scheduler/ScheduleGroup";
5 import { Queue } from "@alchemy.run/aws/SQS/Queue";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as scheduler from "@distilled.cloud/aws/scheduler";
9 import * as Provider from "alchemy/Provider";
10 import * as Effect from "effect/Effect";
11 import { driftActions, repairDrift, waitForDrift } from "../drift.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Canonical `list()` test (AWS account/region-scoped collection across all
16 // schedule groups): deploy a real schedule (with the IAM role + SQS target it
17 // requires), resolve the provider from context via the typed `findProvider`,
18 // call `list()`, and assert the deployed schedule appears in the
19 // exhaustively-paginated result.
20 test.provider(
21 "list enumerates the deployed schedule",
22 (stack) =>
23 Effect.gen(function* () {
24 yield* stack.destroy();
25
26 // The Schedule provider internally retries the "execution role must allow
27 // EventBridge Scheduler to assume the role" ValidationException until the
28 // freshly-created IAM role propagates, so the deploy needs no retry here.
29 const deployed = yield* stack.deploy(
30 Effect.gen(function* () {
31 const queue = yield* Queue("ListScheduleQueue", {
32 queueName: "alchemy-test-schedule-list-queue",
33 });
34
35 const role = yield* Role("ListScheduleRole", {
36 roleName: "alchemy-test-schedule-list-role",
37 assumeRolePolicyDocument: {
38 Version: "2012-10-17",
39 Statement: [
40 {
41 Effect: "Allow",
42 Principal: { Service: "scheduler.amazonaws.com" },
43 Action: ["sts:AssumeRole"],
44 },
45 ],
46 },
47 inlinePolicies: {
48 ScheduleTarget: {
49 Version: "2012-10-17",
50 Statement: [
51 { Effect: "Allow", Action: ["sqs:SendMessage"], Resource: [queue.queueArn] },
52 ],
53 },
54 },
55 });
56
57 return yield* Schedule("ListSchedule", {
58 name: "alchemy-test-schedule-list",
59 scheduleExpression: "rate(1 hour)",
60 flexibleTimeWindow: { Mode: "OFF" },
61 target: { Arn: queue.queueArn, RoleArn: role.roleArn },
62 });
63 }),
64 );
65
66 const provider = yield* Provider.findProvider(Schedule);
67 const all = yield* provider.list();
68
69 expect(all.some((s) => s.scheduleName === deployed.scheduleName)).toBe(true);
70 expect(
71 all.some(
72 (s) => s.scheduleName === deployed.scheduleName && s.scheduleArn === deployed.scheduleArn,
73 ),
74 ).toBe(true);
75
76 yield* stack.destroy();
77 }),
78 {
79 tags: [
80 "provider:aws",
81 "provider:aws:iam",
82 "provider:aws:scheduler",
83 "provider:aws:sqs",
84 "live",
85 ],
86 },
87 );
88
89 // #2688: `read` must observe the schedule definition and the group's tags,
90 // or an out-of-band change reads as `unchanged`.
91 test.provider(
92 "drift detects and repairs out-of-band schedule and group changes",
93 (stack) =>
94 Effect.gen(function* () {
95 yield* stack.destroy();
96
97 const { schedule, group } = yield* stack.deploy(
98 Effect.gen(function* () {
99 const queue = yield* Queue("DriftScheduleQueue", {});
100 const role = yield* Role("DriftScheduleRole", {
101 assumeRolePolicyDocument: {
102 Version: "2012-10-17",
103 Statement: [
104 {
105 Effect: "Allow",
106 Principal: { Service: "scheduler.amazonaws.com" },
107 Action: ["sts:AssumeRole"],
108 },
109 ],
110 },
111 inlinePolicies: {
112 ScheduleTarget: {
113 Version: "2012-10-17",
114 Statement: [
115 { Effect: "Allow", Action: ["sqs:SendMessage"], Resource: [queue.queueArn] },
116 ],
117 },
118 },
119 });
120 const group = yield* ScheduleGroup("DriftScheduleGroup", {
121 tags: { team: "declared" },
122 });
123 const schedule = yield* Schedule("DriftSchedule", {
124 groupName: group.scheduleGroupName,
125 scheduleExpression: "rate(1 hour)",
126 scheduleExpressionTimezone: "UTC",
127 flexibleTimeWindow: { Mode: "OFF" },
128 target: { Arn: queue.queueArn, RoleArn: role.roleArn },
129 });
130 return { schedule, group };
131 }),
132 );
133 const actions = yield* driftActions(stack);
134 expect(Object.values(actions).every((action) => action === "unchanged")).toBe(true);
135
136 const observed = yield* scheduler.getSchedule({
137 Name: schedule.scheduleName,
138 GroupName: schedule.groupName,
139 });
140 yield* scheduler.updateSchedule({
141 Name: schedule.scheduleName,
142 GroupName: schedule.groupName,
143 ScheduleExpression: "rate(7 days)",
144 FlexibleTimeWindow: { Mode: "FLEXIBLE", MaximumWindowInMinutes: 15 },
145 Target: observed.Target!,
146 });
147 yield* scheduler.tagResource({
148 ResourceArn: group.scheduleGroupArn,
149 Tags: [{ Key: "team", Value: "out-of-band" }],
150 });
151
152 const drifted = yield* waitForDrift(stack, "DriftSchedule", "drifted");
153 expect(drifted?.attr?.configuration).toMatchObject({
154 scheduleExpression: "rate(7 days)",
155 flexibleTimeWindow: { Mode: "FLEXIBLE" },
156 });
157 const groupDrifted = yield* waitForDrift(stack, "DriftScheduleGroup", "drifted");
158 expect(groupDrifted?.attr?.tags?.team).toBe("out-of-band");
159
160 const repaired = yield* repairDrift(stack);
161 expect(repaired.resources.DriftSchedule?.action).toBe("repaired");
162 expect(repaired.resources.DriftScheduleGroup?.action).toBe("repaired");
163 const after = yield* driftActions(stack);
164 expect(Object.values(after).every((action) => action === "unchanged")).toBe(true);
165 const restored = yield* scheduler.getSchedule({
166 Name: schedule.scheduleName,
167 GroupName: schedule.groupName,
168 });
169 expect(restored.ScheduleExpression).toBe("rate(1 hour)");
170 expect(restored.FlexibleTimeWindow?.Mode).toBe("OFF");
171
172 yield* stack.destroy();
173 }),
174 {
175 tags: [
176 "provider:aws",
177 "provider:aws:iam",
178 "provider:aws:scheduler",
179 "provider:aws:sqs",
180 "live",
181 ],
182 timeout: 240_000,
183 },
184 );