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

main
ts 159 lines 6.8 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { Link, Sink } from "@alchemy.run/aws/OAM";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as oam from "@distilled.cloud/aws/oam";
7 import * as Effect from "effect/Effect";
8 import * as Result from "effect/Result";
9 import * as Schedule from "effect/Schedule";
10 import { makeOamTestLease } from "./TestLease.ts";
11
12 const { test, beforeAll, afterAll } = Test.make({ providers: AWS.providers() });
13 const serviceLease = makeOamTestLease();
14
15 beforeAll(serviceLease.acquire, { timeout: 3_600_000 });
16 afterAll(serviceLease.release);
17
18 const assertSinkGone = (sinkArn: string) =>
19 oam.getSink({ Identifier: sinkArn }).pipe(
20 Effect.flatMap(() => Effect.fail(new Error(`sink ${sinkArn} still exists`))),
21 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
22 Effect.retry({
23 while: (e) => e instanceof Error,
24 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
25 }),
26 );
27
28 // Policy validation resolves principals, so the source account must be a
29 // REAL account — we use the caller's own. Nothing ever links to this sink
30 // (same-account links are rejected); the policy only exercises put/get/diff.
31 const sinkPolicy = (accountId: string, resourceTypes: string[]) => ({
32 Version: "2012-10-17",
33 Statement: [
34 {
35 Effect: "Allow",
36 Principal: { AWS: [accountId] },
37 Action: ["oam:CreateLink", "oam:UpdateLink"],
38 Resource: "*",
39 Condition: { "ForAllValues:StringEquals": { "oam:ResourceTypes": resourceTypes } },
40 },
41 ],
42 });
43
44 // AWS enforces ONE sink per account per region, so every test that deploys a
45 // sink must run sequentially — including the same-account Link probe, which
46 // shares the lifecycle test's sink instead of deploying its own.
47 describe.sequential("AWS.OAM.Sink", { tags: ["provider:aws", "provider:aws:oam", "live"] }, () => {
48 test.provider(
49 "sink lifecycle (policy + tags) and same-account link rejection",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* AWSEnvironment.current;
53 yield* stack.destroy();
54
55 const deploySink = (props: { policy: Record<string, any>; tags: Record<string, string> }) =>
56 stack.deploy(
57 Effect.gen(function* () {
58 const sink = yield* Sink("MonitoringSink", props);
59 return { sinkName: sink.sinkName, sinkArn: sink.sinkArn, sinkId: sink.sinkId };
60 }),
61 );
62
63 const created = yield* deploySink({
64 policy: sinkPolicy(accountId, ["AWS::CloudWatch::Metric"]),
65 tags: { purpose: "alchemy-test" },
66 });
67 expect(created.sinkArn).toContain(":sink/");
68 expect(created.sinkArn).toContain(created.sinkId);
69
70 // Out-of-band: the sink, its policy, and its tags are all live.
71 const observed = yield* oam.getSink({ Identifier: created.sinkArn });
72 expect(observed.Name).toBe(created.sinkName);
73 const policy = yield* oam.getSinkPolicy({ SinkIdentifier: created.sinkArn });
74 expect(policy.Policy).toContain("oam:CreateLink");
75 expect(policy.Policy).toContain("AWS::CloudWatch::Metric");
76 const tags = yield* oam.listTagsForResource({ ResourceArn: created.sinkArn });
77 expect(tags.Tags?.purpose).toBe("alchemy-test");
78 expect(tags.Tags?.["alchemy::id"]).toBe("MonitoringSink");
79
80 // Same-account link create-path probe: OAM rejects a link whose sink
81 // lives in the caller's own account with a typed
82 // InvalidParameterException. This proves the createLink wire shape
83 // (the full Link lifecycle needs a second account — gated below).
84 const linkAttempt = yield* Effect.result(
85 oam.createLink({
86 LabelTemplate: "$AccountName",
87 ResourceTypes: ["AWS::CloudWatch::Metric"],
88 SinkIdentifier: created.sinkArn,
89 }),
90 );
91 expect(Result.isFailure(linkAttempt)).toBe(true);
92 if (Result.isFailure(linkAttempt)) {
93 const failure = linkAttempt.failure;
94 expect(failure._tag).toBe("InvalidParameterException");
95 if (failure._tag === "InvalidParameterException") {
96 expect(failure.message ?? "").toContain("same account");
97 }
98 }
99
100 // Update: widen the sink policy and change tags; the sink itself
101 // (name/arn/id) is stable.
102 const updated = yield* deploySink({
103 policy: sinkPolicy(accountId, ["AWS::CloudWatch::Metric", "AWS::Logs::LogGroup"]),
104 tags: { purpose: "alchemy-test", updated: "true" },
105 });
106 expect(updated.sinkArn).toBe(created.sinkArn);
107 const updatedPolicy = yield* oam.getSinkPolicy({ SinkIdentifier: created.sinkArn });
108 expect(updatedPolicy.Policy).toContain("AWS::Logs::LogGroup");
109 const updatedTags = yield* oam.listTagsForResource({ ResourceArn: created.sinkArn });
110 expect(updatedTags.Tags?.updated).toBe("true");
111
112 yield* stack.destroy();
113 yield* assertSinkGone(created.sinkArn);
114 }),
115 { timeout: 180_000 },
116 );
117
118 // Full Link lifecycle requires a sink in a DIFFERENT account whose policy
119 // authorizes this one. Point AWS_TEST_OAM_SINK_ARN at such a sink to run.
120 test.provider.skipIf(!process.env.AWS_TEST_OAM_SINK_ARN)(
121 "link lifecycle against a cross-account sink",
122 (stack) =>
123 Effect.gen(function* () {
124 const sinkIdentifier = process.env.AWS_TEST_OAM_SINK_ARN!;
125 yield* stack.destroy();
126
127 const deployLink = (resourceTypes: string[]) =>
128 stack.deploy(
129 Effect.gen(function* () {
130 const link = yield* Link("CrossAccountLink", {
131 labelTemplate: "$AccountName",
132 resourceTypes,
133 sinkIdentifier,
134 tags: { purpose: "alchemy-test" },
135 });
136 return { linkArn: link.linkArn, label: link.label };
137 }),
138 );
139
140 const created = yield* deployLink(["AWS::CloudWatch::Metric"]);
141 const live = yield* oam.getLink({ Identifier: created.linkArn });
142 expect(live.ResourceTypes).toEqual(["AWS::CloudWatch::Metric"]);
143
144 // Update mutates resource types in place (no replacement).
145 const updated = yield* deployLink(["AWS::CloudWatch::Metric", "AWS::Logs::LogGroup"]);
146 expect(updated.linkArn).toBe(created.linkArn);
147 const after = yield* oam.getLink({ Identifier: created.linkArn });
148 expect([...(after.ResourceTypes ?? [])].sort()).toEqual([
149 "AWS::CloudWatch::Metric",
150 "AWS::Logs::LogGroup",
151 ]);
152
153 yield* stack.destroy();
154 const gone = yield* Effect.result(oam.getLink({ Identifier: created.linkArn }));
155 expect(Result.isFailure(gone)).toBe(true);
156 }),
157 { timeout: 180_000 },
158 );
159 });