@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 159 lines 6.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Bucket } from "@alchemy.run/aws/S3";
3 import { Canary, Group } from "@alchemy.run/aws/Synthetics";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as synthetics from "@distilled.cloud/aws/synthetics";
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 // Trivial heartbeat script — no page load, just a successful step.
14 const HEARTBEAT_SCRIPT = `
15 const synthetics = require("Synthetics");
16 const log = require("SyntheticsLogger");
17
18 exports.handler = async function () {
19 return await synthetics.executeStep("heartbeat", async function () {
20 log.info("heartbeat ok");
21 });
22 };
23 `;
24
25 // The provider's delete waits until the canary is gone, so this should
26 // resolve quickly; the bounded retry covers read-after-delete lag. It also
27 // pins the distilled patch: getCanary's typed ResourceNotFoundException.
28 const assertCanaryGone = (canaryName: string) =>
29 synthetics.getCanary({ Name: canaryName }).pipe(
30 Effect.flatMap(() => Effect.fail(new Error(`canary ${canaryName} still exists`))),
31 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
32 Effect.retry({
33 while: (e) => e instanceof Error,
34 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
35 }),
36 );
37
38 const assertGroupGone = (groupName: string) =>
39 synthetics.getGroup({ GroupIdentifier: groupName }).pipe(
40 Effect.flatMap(() => Effect.fail(new Error(`group ${groupName} still exists`))),
41 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
42 Effect.retry({
43 while: (e) => e instanceof Error,
44 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
45 }),
46 );
47
48 describe.sequential(
49 "AWS.Synthetics.Canary",
50 { tags: ["provider:aws", "provider:aws:s3", "provider:aws:synthetics", "live"] },
51 () => {
52 test.provider(
53 "creates a stopped canary + group, updates the schedule, and deletes",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const deployCanary = (scheduleExpression: string, groupTag: string) =>
59 stack.deploy(
60 Effect.gen(function* () {
61 const bucket = yield* Bucket("CanaryArtifacts", { forceDestroy: true });
62 const canary = yield* Canary("Heartbeat", {
63 script: HEARTBEAT_SCRIPT,
64 artifactS3Location: Output.interpolate`s3://${bucket.bucketName}/heartbeat`,
65 schedule: { expression: scheduleExpression },
66 });
67 const group = yield* Group("HeartbeatGroup", {
68 members: [canary.canaryArn],
69 tags: { alchemyTest: groupTag },
70 });
71 return {
72 canaryName: canary.canaryName,
73 canaryArn: canary.canaryArn,
74 executionRoleArn: canary.executionRoleArn,
75 groupName: group.groupName,
76 groupArn: group.groupArn,
77 };
78 }),
79 );
80
81 const created = yield* deployCanary("rate(5 minutes)", "one");
82
83 // Out-of-band verification via distilled.
84 const observed = yield* synthetics.getCanary({ Name: created.canaryName });
85 // Never started → READY (a stopped-after-running canary is STOPPED).
86 expect(["READY", "STOPPED"]).toContain(observed.Canary?.Status?.State);
87 expect(observed.Canary?.RuntimeVersion).toBe("syn-nodejs-puppeteer-16.1");
88 expect(observed.Canary?.Schedule?.Expression).toBe("rate(5 minutes)");
89 expect(observed.Canary?.ExecutionRoleArn).toBe(created.executionRoleArn);
90 expect(observed.Canary?.Tags?.["alchemy::id"]).toBe("Heartbeat");
91
92 // The group exists, is tagged, and holds the canary as its member.
93 const observedGroup = yield* synthetics.getGroup({ GroupIdentifier: created.groupName });
94 expect(observedGroup.Group?.Name).toBe(created.groupName);
95 expect(observedGroup.Group?.Arn).toBe(created.groupArn);
96 expect(observedGroup.Group?.Tags?.["alchemy::id"]).toBe("HeartbeatGroup");
97 expect(observedGroup.Group?.Tags?.alchemyTest).toBe("one");
98 const members = yield* synthetics.listGroupResources({
99 GroupIdentifier: created.groupName,
100 });
101 expect(members.Resources).toContain(created.canaryArn);
102
103 // Update the schedule + group tags in place (same physical names).
104 const updated = yield* deployCanary("rate(10 minutes)", "two");
105 expect(updated.canaryName).toBe(created.canaryName);
106 expect(updated.groupName).toBe(created.groupName);
107 const observedUpdated = yield* synthetics.getCanary({ Name: created.canaryName });
108 expect(observedUpdated.Canary?.Schedule?.Expression).toBe("rate(10 minutes)");
109 const updatedGroup = yield* synthetics.getGroup({ GroupIdentifier: created.groupName });
110 expect(updatedGroup.Group?.Tags?.alchemyTest).toBe("two");
111
112 yield* stack.destroy();
113 yield* assertCanaryGone(created.canaryName);
114 yield* assertGroupGone(created.groupName);
115 }),
116 { timeout: 420_000 },
117 );
118
119 // A live canary run takes ~1-2 minutes end to end — skipped under --fast.
120 test.provider.skipIf(!!process.env.FAST)(
121 "starts the canary and records a successful run",
122 (stack) =>
123 Effect.gen(function* () {
124 yield* stack.destroy();
125
126 const { canaryName } = yield* stack.deploy(
127 Effect.gen(function* () {
128 const bucket = yield* Bucket("RunArtifacts", { forceDestroy: true });
129 const canary = yield* Canary("HeartbeatRun", {
130 script: HEARTBEAT_SCRIPT,
131 artifactS3Location: Output.interpolate`s3://${bucket.bucketName}/run`,
132 schedule: { expression: "rate(1 minute)" },
133 start: true,
134 });
135 return { canaryName: canary.canaryName };
136 }),
137 );
138
139 const running = yield* synthetics.getCanary({ Name: canaryName });
140 expect(running.Canary?.Status?.State).toBe("RUNNING");
141
142 // Poll for the first completed successful run.
143 const passedRun = yield* synthetics.getCanaryRuns({ Name: canaryName }).pipe(
144 Effect.map((r) => (r.CanaryRuns ?? []).find((run) => run.Status?.State === "PASSED")),
145 Effect.repeat({
146 schedule: Schedule.spaced("10 seconds"),
147 until: (run) => run !== undefined,
148 times: 24,
149 }),
150 );
151 expect(passedRun?.Status?.State).toBe("PASSED");
152
153 yield* stack.destroy();
154 yield* assertCanaryGone(canaryName);
155 }),
156 { timeout: 600_000 },
157 );
158 },
159 );