@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 112 lines 4.34 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ScalableTarget, ScheduledAction } from "@alchemy.run/aws/ApplicationAutoScaling";
3 import { Table } from "@alchemy.run/aws/DynamoDB";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as aas from "@distilled.cloud/aws/application-auto-scaling";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 const describeAction = (scheduledActionName: string) =>
14 aas
15 .describeScheduledActions({
16 ServiceNamespace: "dynamodb",
17 ScheduledActionNames: [scheduledActionName],
18 })
19 .pipe(
20 Effect.map((res) =>
21 res.ScheduledActions?.find((a) => a.ScheduledActionName === scheduledActionName),
22 ),
23 );
24
25 const waitUntilActionGone = (scheduledActionName: string) =>
26 describeAction(scheduledActionName).pipe(
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (action) => action === undefined,
30 times: 10,
31 }),
32 );
33
34 // Scheduled-action lifecycle against a provisioned DynamoDB table. The
35 // schedule is a far-future one-time `at(...)` so the action never actually
36 // fires during the test.
37 test.provider(
38 "scheduled action lifecycle (dynamodb)",
39 (stack) =>
40 Effect.gen(function* () {
41 yield* stack.destroy();
42
43 const deploy = (props: { schedule: string; minCapacity: number }) =>
44 stack.deploy(
45 Effect.gen(function* () {
46 const table = yield* Table("AasScheduleTable", {
47 partitionKey: "id",
48 attributes: { id: "S" },
49 billingMode: "PROVISIONED",
50 provisionedThroughput: {
51 ReadCapacityUnits: 1,
52 WriteCapacityUnits: 1,
53 },
54 });
55 const target = yield* ScalableTarget("AasScheduleTarget", {
56 serviceNamespace: "dynamodb",
57 resourceId: Output.interpolate`table/${table.tableName}`,
58 scalableDimension: "dynamodb:table:ReadCapacityUnits",
59 minCapacity: 1,
60 maxCapacity: 10,
61 });
62 const action = yield* ScheduledAction("AasScheduledAction", {
63 serviceNamespace: target.serviceNamespace,
64 resourceId: target.resourceId,
65 scalableDimension: target.scalableDimension,
66 schedule: props.schedule,
67 timezone: "UTC",
68 scalableTargetAction: { MinCapacity: props.minCapacity },
69 });
70 return {
71 resourceId: target.resourceId.as<string>(),
72 scheduledActionName: action.scheduledActionName.as<string>(),
73 scheduledActionArn: action.scheduledActionArn.as<string>(),
74 schedule: action.schedule.as<string>(),
75 };
76 }),
77 );
78
79 const created = yield* deploy({
80 schedule: "at(2030-01-01T00:00:00)",
81 minCapacity: 2,
82 });
83 expect(created.scheduledActionArn).toContain("scheduledAction");
84 expect(created.schedule).toBe("at(2030-01-01T00:00:00)");
85
86 // Out-of-band: the action is attached to the target with the requested
87 // capacity adjustment.
88 const observed = yield* describeAction(created.scheduledActionName);
89 expect(observed?.Schedule).toBe("at(2030-01-01T00:00:00)");
90 expect(observed?.ScalableTargetAction?.MinCapacity).toBe(2);
91 expect(observed?.ResourceId).toBe(created.resourceId);
92
93 // Update in place — `putScheduledAction` upserts by (name, target).
94 const updated = yield* deploy({
95 schedule: "at(2030-06-01T00:00:00)",
96 minCapacity: 3,
97 });
98 expect(updated.scheduledActionName).toEqual(created.scheduledActionName);
99 expect(updated.scheduledActionArn).toEqual(created.scheduledActionArn);
100 const observedAfterUpdate = yield* describeAction(created.scheduledActionName);
101 expect(observedAfterUpdate?.Schedule).toBe("at(2030-06-01T00:00:00)");
102 expect(observedAfterUpdate?.ScalableTargetAction?.MinCapacity).toBe(3);
103
104 yield* stack.destroy();
105 const gone = yield* waitUntilActionGone(created.scheduledActionName);
106 expect(gone).toBeUndefined();
107 }),
108 {
109 tags: ["provider:aws", "provider:aws:applicationautoscaling", "provider:aws:dynamodb", "live"],
110 timeout: 240_000,
111 },
112 );