@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 141 lines 5.44 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Permission } from "@alchemy.run/aws/RAM";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ram from "@distilled.cloud/aws/ram";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 class PermissionStillLive extends Data.TaggedError("PermissionStillLive") {}
17
18 const readPermission = Effect.fn(function* (arn: string) {
19 const detail = yield* ram.getPermission({ permissionArn: arn }).pipe(
20 Effect.map((r) => r.permission),
21 Effect.catchTag("UnknownResourceException", () => Effect.succeed(undefined)),
22 );
23 return detail && detail.status !== "DELETING" && detail.status !== "DELETED" ? detail : undefined;
24 });
25
26 const assertDeleted = Effect.fn(function* (arn: string) {
27 yield* readPermission(arn).pipe(
28 Effect.flatMap((p) => (p === undefined ? Effect.void : Effect.fail(new PermissionStillLive()))),
29 Effect.retry({
30 while: (e) => e instanceof PermissionStillLive,
31 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(15)]),
32 }),
33 );
34 });
35
36 const permissionStack = (props: { actions: string[]; tags?: Record<string, string> }) =>
37 Effect.gen(function* () {
38 const permission = yield* Permission("TestPermission", {
39 resourceType: "appsync:Apis",
40 policyTemplate: { actions: props.actions },
41 tags: props.tags,
42 });
43 return { permission };
44 });
45
46 test.provider(
47 "create, publish new policy version, update tags, delete",
48 (stack) =>
49 Effect.gen(function* () {
50 yield* stack.destroy();
51
52 // 1. Create a least-privilege AppSync API permission (appsync:Apis is
53 // one of the resource types supporting customer managed permissions).
54 const { permission } = yield* stack.deploy(
55 permissionStack({
56 actions: ["appsync:SourceGraphQL"],
57 tags: { team: "platform" },
58 }),
59 );
60
61 expect(permission.permissionArn).toMatch(/^arn:aws:ram:/);
62 expect(permission.resourceType).toBe("appsync:Apis");
63 const arn = permission.permissionArn;
64 const initialVersion = permission.version;
65
66 // 2. Out-of-band: the permission is ATTACHABLE with our policy + tags.
67 const live = yield* readPermission(arn);
68 expect(live?.status).toBe("ATTACHABLE");
69 const policy = JSON.parse(live?.permission ?? "{}") as {
70 Action?: string | string[];
71 };
72 expect(policy.Action).toContain("appsync:SourceGraphQL");
73 expect(live?.tags?.some((t) => t.key === "team" && t.value === "platform")).toBe(true);
74 expect(live?.tags?.some((t) => t.key === "alchemy::id")).toBe(true);
75
76 // 3. Update in place: grow the policy (new default version), add a
77 // tag. ARN is stable; the default version advances.
78 const { permission: updated } = yield* stack.deploy(
79 permissionStack({
80 actions: ["appsync:SourceGraphQL", "appsync:GraphQL"],
81 tags: { team: "platform", env: "prod" },
82 }),
83 );
84 expect(updated.permissionArn).toEqual(arn);
85 expect(updated.version).not.toEqual(initialVersion);
86
87 // 4. Out-of-band: the default version carries the grown policy and
88 // the new tag; the superseded version was cleaned up.
89 const live2 = yield* readPermission(arn);
90 const policy2 = JSON.parse(live2?.permission ?? "{}") as {
91 Action?: string | string[];
92 };
93 expect(policy2.Action).toContain("appsync:GraphQL");
94 expect(live2?.tags?.some((t) => t.key === "env" && t.value === "prod")).toBe(true);
95
96 // listPermissionVersions keeps DELETED versions in the listing — only
97 // the new default remains live.
98 const versions = yield* ram
99 .listPermissionVersions({ permissionArn: arn })
100 .pipe(Effect.map((r) => r.permissions ?? []));
101 const liveVersions = versions.filter(
102 (v) => v.status !== "DELETED" && v.status !== "DELETING",
103 );
104 expect(liveVersions).toHaveLength(1);
105 expect(liveVersions[0]?.version).toEqual(updated.version);
106
107 // 5. Re-deploy with identical props: a no-op — no new version.
108 const { permission: same } = yield* stack.deploy(
109 permissionStack({
110 actions: ["appsync:SourceGraphQL", "appsync:GraphQL"],
111 tags: { team: "platform", env: "prod" },
112 }),
113 );
114 expect(same.version).toEqual(updated.version);
115
116 // 6. Delete and confirm gone.
117 yield* stack.destroy();
118 yield* assertDeleted(arn);
119 }).pipe(logLevel),
120 { tags: ["provider:aws", "provider:aws:ram", "live"], timeout: 240_000 },
121 );
122
123 test.provider(
124 "list enumerates the deployed permission",
125 (stack) =>
126 Effect.gen(function* () {
127 yield* stack.destroy();
128
129 const { permission } = yield* stack.deploy(
130 permissionStack({ actions: ["appsync:SourceGraphQL"] }),
131 );
132
133 const provider = yield* Provider.findProvider(Permission);
134 const all = yield* provider.list();
135 expect(all.some((x) => x.permissionArn === permission.permissionArn)).toBe(true);
136
137 yield* stack.destroy();
138 yield* assertDeleted(permission.permissionArn);
139 }).pipe(logLevel),
140 { tags: ["provider:aws", "provider:aws:ram", "live"], timeout: 240_000 },
141 );