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

main
ts 94 lines 3.48 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { EventBus } from "@alchemy.run/aws/EventBridge/EventBus";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
6 import { Region } from "@distilled.cloud/aws/Region";
7 import * as Provider from "alchemy/Provider";
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 /** Typed wait-until-gone: poll describeEventBus until ResourceNotFoundException. */
14 const assertBusGone = Effect.fn(function* (name: string) {
15 const gone = yield* eventbridge.describeEventBus({ Name: name }).pipe(
16 Effect.map(() => false),
17 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
18 Effect.repeat({
19 schedule: Schedule.spaced("2 seconds"),
20 until: (isGone): boolean => isGone,
21 times: 10,
22 }),
23 );
24 expect(gone).toBe(true);
25 });
26
27 // Canonical `list()` test (AWS account/region-scoped collection): deploy a real
28 // event bus, resolve the provider from context via the typed `findProvider`,
29 // call `list()`, and assert the deployed bus appears in the
30 // exhaustively-paginated result.
31 test.provider(
32 "list enumerates the deployed event bus",
33 (stack) =>
34 Effect.gen(function* () {
35 yield* stack.destroy();
36
37 const bus = yield* stack.deploy(
38 Effect.gen(function* () {
39 return yield* EventBus("ListEventBus", {
40 name: "alchemy-test-eventbus-list",
41 });
42 }),
43 );
44
45 const provider = yield* Provider.findProvider(EventBus);
46 const all = yield* provider.list();
47
48 expect(all.some((b) => b.eventBusName === bus.eventBusName)).toBe(true);
49 // The AWS-managed `default` bus must be excluded.
50 expect(all.some((b) => b.eventBusName === "default")).toBe(false);
51
52 yield* stack.destroy();
53
54 yield* assertBusGone("alchemy-test-eventbus-list");
55 }),
56 {
57 tags: ["provider:aws", "provider:aws:eventbridge", "live"],
58 timeout: 120_000,
59 },
60 );
61
62 const describeBusIn = (name: string, region: string) =>
63 eventbridge.describeEventBus({ Name: name }).pipe(
64 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
65 Effect.provideService(Region, Effect.succeed(region)),
66 );
67
68 test.provider(
69 "region places the bus in another region and changing it replaces the bus",
70 (stack) =>
71 Effect.gen(function* () {
72 yield* stack.destroy();
73 const home = yield* yield* Region;
74 const other = home === "us-east-1" ? "us-east-2" : "us-east-1";
75
76 const remote = yield* stack.deploy(EventBus("RegionalBus", { region: other }));
77 expect(remote.eventBusArn).toContain(`:events:${other}:`);
78 expect((yield* describeBusIn(remote.eventBusName, other))?.Arn).toBe(remote.eventBusArn);
79 expect(yield* describeBusIn(remote.eventBusName, home)).toBeUndefined();
80
81 // Moving the bus back to the stack's region replaces it.
82 const local = yield* stack.deploy(EventBus("RegionalBus", {}));
83 expect(local.eventBusArn).toContain(`:events:${home}:`);
84 expect((yield* describeBusIn(local.eventBusName, home))?.Arn).toBe(local.eventBusArn);
85 expect(yield* describeBusIn(remote.eventBusName, other)).toBeUndefined();
86
87 yield* stack.destroy();
88 expect(yield* describeBusIn(local.eventBusName, home)).toBeUndefined();
89 }),
90 {
91 tags: ["provider:aws", "provider:aws:eventbridge", "live"],
92 timeout: 120_000,
93 },
94 );