1import * as AWS from "@/AWS";
2import { Account } from "@/AWS/Organizations";
3import * as Provider from "@/Provider";
4import * as Test from "@/Test/Alchemy";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7
8const { test } = Test.make({ providers: AWS.providers() });
9
10// Organizations APIs require the caller to be the MANAGEMENT account of an AWS
11// Organization, and creating member accounts is slow + irreversible. So this is
12// a read-only `list()` test: resolve the typed provider, call `list()`, and
13// assert it returns a well-typed `Attributes[]` (the same shape `read`
14// produces).
15//
16// `list()` catches `AWSOrganizationsNotInUseException` and returns `[]` when the
17// testing account is not an organization management account, so this passes on a
18// standalone account too. When run against a management account
19// (`AWS_TEST_ORG_MANAGEMENT=1`) it additionally asserts at least one account
20// (the management account itself) appears.
21test.provider(
22 "list enumerates organization accounts",
23 () =>
24 Effect.gen(function* () {
25 const provider = yield* Provider.findProvider(Account);
26 const all = yield* provider.list();
27
28 expect(Array.isArray(all)).toBe(true);
29 for (const account of all) {
30 expect(typeof account.accountId).toBe("string");
31 expect(typeof account.accountArn).toBe("string");
32 expect(account.tags).toBeDefined();
33 if (account.name != null) {
34 expect(typeof account.name).toBe("string");
35 }
36 if (account.email != null) {
37 expect(typeof account.email).toBe("string");
38 }
39 }
40
41 if (process.env.AWS_TEST_ORG_MANAGEMENT) {
42 expect(all.length).toBeGreaterThan(0);
43 const management = all.find(
44 (account) =>
45 typeof account.name === "string" &&
46 account.name.length > 0 &&
47 typeof account.email === "string" &&
48 account.email.length > 0,
49 );
50 expect(management).toBeDefined();
51 expect(typeof management?.name).toBe("string");
52 expect(management!.name!.length).toBeGreaterThan(0);
53 expect(typeof management?.email).toBe("string");
54 expect(management!.email!.length).toBeGreaterThan(0);
55 }
56 }),
57 { tags: ["provider:aws", "provider:aws:organizations", "live"] },
58);