1import * as AWS from "@/AWS";
2import { EventBus } from "@/AWS/EventBridge/EventBus.ts";
3import { Permission } from "@/AWS/EventBridge/Permission.ts";
4import * as Provider from "@/Provider";
5import * as Test from "@/Test/Alchemy";
6import * as eventbridge from "@distilled.cloud/aws/eventbridge";
7import { expect } from "alchemy-test";
8import * as Effect from "effect/Effect";
9import * as Schedule from "effect/Schedule";
10
11const { test } = Test.make({ providers: AWS.providers() });
12
13/** Typed wait-until-gone: poll describeEventBus until ResourceNotFoundException. */
14const 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. An EventBridge Permission is a statement in an event
28// bus resource policy (there is no list-permissions API). `list()` enumerates
29// every bus, parses each bus's Policy JSON, and emits one Attributes per Sid.
30// Deploy a bus + a permission, resolve the provider via the typed
31// `findProvider`, call `list()`, and assert the deployed statement appears.
32test.provider(
33 "list enumerates the deployed permission",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const permission = yield* stack.deploy(
39 Effect.gen(function* () {
40 const bus = yield* EventBus("ListPermissionBus", {
41 name: "alchemy-test-permission-list",
42 });
43 return yield* Permission("ListPermission", {
44 eventBusName: bus.eventBusName,
45 principal: "123456789012",
46 });
47 }),
48 );
49
50 const provider = yield* Provider.findProvider(Permission);
51 const all = yield* provider.list();
52
53 expect(
54 all.some(
55 (p) =>
56 p.statementId === permission.statementId &&
57 p.eventBusName === permission.eventBusName,
58 ),
59 ).toBe(true);
60
61 yield* stack.destroy();
62
63 yield* assertBusGone("alchemy-test-permission-list");
64 }),
65 {
66 tags: ["provider:aws", "provider:aws:eventbridge", "live"],
67 timeout: 120_000,
68 },
69);