@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 91 lines 3.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as organizations from "@distilled.cloud/aws/organizations";
4 import * as Provider from "alchemy/Provider";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
7 import * as OrganizationLease from "./OrganizationLease.ts";
8
9 const { test } = OrganizationLease.make({ providers: AWS.providers() }, "shared");
10
11 // Tree-structured enumeration: `list()` walks the org tree (listRoots ->
12 // recursive listOrganizationalUnitsForParent) and hydrates each OU into the
13 // exact `read` Attributes shape. This runs read-only — it neither creates nor
14 // deletes any organizational unit. When the account isn't an organization
15 // management account, the typed `AWSOrganizationsNotInUseException` /
16 // `AccessDeniedException` degrade to `[]`.
17 test.provider(
18 "list enumerates the organizational units",
19 (stack) =>
20 Effect.gen(function* () {
21 const provider = yield* Provider.findProvider(AWS.Organizations.OrganizationalUnit);
22 const all = yield* provider.list();
23
24 // 0 when the account isn't a management account (or has no OUs); otherwise
25 // every discovered OU. Never negative.
26 expect(all.length).toBeGreaterThanOrEqual(0);
27
28 // Each entry carries the well-typed Attributes shape that `read` produces.
29 for (const ou of all) {
30 expect(typeof ou.ouId).toBe("string");
31 expect(ou.ouId.length).toBeGreaterThan(0);
32 expect(typeof ou.ouArn).toBe("string");
33 expect(ou.ouArn.startsWith("arn:aws:organizations::")).toBe(true);
34 expect(typeof ou.name).toBe("string");
35 expect(ou.name.length).toBeGreaterThan(0);
36 expect(typeof ou.tags).toBe("object");
37 }
38
39 yield* stack.destroy();
40 }),
41 { tags: ["provider:aws", "provider:aws:organizations", "live"] },
42 );
43
44 test.provider(
45 "restores ownership tags removed from AWS",
46 (stack) =>
47 Effect.gen(function* () {
48 yield* stack.destroy();
49
50 const roots = yield* organizations.listRoots({});
51 const parentId = roots.Roots?.[0]?.Id;
52 if (!parentId) {
53 return yield* Effect.fail(new Error("An AWS Organizations root is required"));
54 }
55
56 const makeOU = (phase: string) =>
57 AWS.Organizations.OrganizationalUnit("TagDriftOU", { parentId, tags: { phase } });
58 const readTags = (resourceId: string) =>
59 organizations.listTagsForResource.items({ ResourceId: resourceId }).pipe(
60 Stream.runCollect,
61 Effect.map((tags) => Object.fromEntries(tags.map((tag) => [tag.Key!, tag.Value!]))),
62 );
63
64 yield* Effect.gen(function* () {
65 const created = yield* stack.deploy(makeOU("created"));
66 const originalTags = yield* readTags(created.ouId);
67 const ownershipKeys = ["alchemy::stack", "alchemy::stage", "alchemy::id"];
68 for (const key of ownershipKeys) {
69 expect(originalTags[key]).toBeDefined();
70 }
71
72 yield* organizations.untagResource({ ResourceId: created.ouId, TagKeys: ownershipKeys });
73 expect(yield* readTags(created.ouId)).toEqual({ phase: "created" });
74
75 // Changing a user tag forces reconciliation rather than a no-op deploy.
76 const repaired = yield* stack.deploy(makeOU("repaired"));
77 expect(repaired.ouId).toEqual(created.ouId);
78 const expected = { ...originalTags, phase: "repaired" };
79 expect(yield* readTags(created.ouId)).toEqual(expected);
80 expect(repaired.tags).toEqual(expected);
81
82 // Force a second reconciliation and verify ownership tags stay intact.
83 const converged = yield* stack.deploy(makeOU("converged"));
84 const finalTags = { ...originalTags, phase: "converged" };
85 expect(converged.ouId).toEqual(created.ouId);
86 expect(yield* readTags(created.ouId)).toEqual(finalTags);
87 expect(converged.tags).toEqual(finalTags);
88 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie)));
89 }),
90 { tags: ["provider:aws", "provider:aws:organizations", "live"], timeout: 120_000 },
91 );