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

main
ts 185 lines 7.48 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Dataset, DatasetGroup } from "@alchemy.run/aws/Forecast";
3 import { toTagRecord } from "@alchemy.run/aws/Forecast/internal";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as forecast from "@distilled.cloud/aws/forecast";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 class DatasetGroupStillExists extends Data.TaggedError("DatasetGroupStillExists")<{
14 readonly arn: string;
15 }> {}
16
17 const assertDatasetGroupDeleted = (arn: string) =>
18 forecast.describeDatasetGroup({ DatasetGroupArn: arn }).pipe(
19 Effect.flatMap(() => Effect.fail(new DatasetGroupStillExists({ arn }))),
20 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
21 Effect.retry({
22 while: (e) => e._tag === "DatasetGroupStillExists",
23 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
24 }),
25 );
26
27 // Typed-error probe: proves the SDK decodes Forecast errors as typed tags.
28 // A missing dataset surfaces as ResourceNotFoundException. Training resources
29 // (predictors, forecasts) take ~an hour of paid training — they are
30 // intentionally NOT implemented; only the cheap definition resources below
31 // have a live lifecycle.
32 test.provider(
33 "describeDataset on a nonexistent ARN fails with a typed error",
34 () =>
35 Effect.gen(function* () {
36 const region = yield* yield* AWS.Region;
37 const error = yield* Effect.flip(
38 forecast.describeDataset({
39 DatasetArn: `arn:aws:forecast:${region}:000000000000:dataset/does_not_exist`,
40 }),
41 );
42 expect(["ResourceNotFoundException", "AccessDeniedException"].includes(error._tag)).toBe(
43 true,
44 );
45 }),
46 { tags: ["provider:aws", "provider:aws:forecast", "live"] },
47 );
48
49 // Entitlement probe: Amazon Forecast is closed to new customers — accounts
50 // without prior Forecast usage get a typed AccessDeniedException on every
51 // create ("Amazon Forecast is no longer available to new customers. Existing
52 // customers of Amazon Forecast can continue to use the service as normal.").
53 // In an entitled (grandfathered) account the create succeeds, so this probe
54 // accepts either outcome and cleans up after itself.
55 test.provider(
56 "createDatasetGroup is either entitled or a typed AccessDeniedException",
57 () =>
58 Effect.gen(function* () {
59 const arn = yield* forecast
60 .createDatasetGroup({
61 DatasetGroupName: "alchemy_forecast_entitlement_probe",
62 Domain: "CUSTOM",
63 })
64 .pipe(
65 Effect.map((r) => r.DatasetGroupArn),
66 Effect.catchTag(
67 // A leftover probe group from a crashed prior run is also fine.
68 ["AccessDeniedException", "ResourceAlreadyExistsException"],
69 () => Effect.succeed(undefined),
70 ),
71 );
72 if (arn !== undefined) {
73 // Entitled account — clean up the probe group.
74 yield* forecast
75 .deleteDatasetGroup({ DatasetGroupArn: arn })
76 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
77 }
78 }),
79 { tags: ["provider:aws", "provider:aws:forecast", "live"] },
80 );
81
82 // Forecast datasets and dataset groups are cheap, fast metadata objects, but
83 // the standing test account is blocked by the new-customer closure above —
84 // createDatasetGroup fails with AccessDeniedException ("Amazon Forecast is no
85 // longer available to new customers"). The lifecycle is implemented fully and
86 // gated behind AWS_TEST_FORECAST=1 so a grandfathered account can run it
87 // unchanged. The expensive training work (predictors, forecasts, import jobs)
88 // is out of scope.
89 test.provider.skipIf(!process.env.AWS_TEST_FORECAST)(
90 "create, attach, and destroy a dataset and dataset group",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94
95 const created = yield* stack.deploy(
96 Effect.gen(function* () {
97 const dataset = yield* Dataset("Demand", {
98 domain: "CUSTOM",
99 datasetType: "TARGET_TIME_SERIES",
100 dataFrequency: "D",
101 schema: {
102 attributes: [
103 { attributeName: "item_id", attributeType: "string" },
104 { attributeName: "timestamp", attributeType: "timestamp" },
105 { attributeName: "target_value", attributeType: "float" },
106 ],
107 },
108 tags: { Environment: "test" },
109 });
110 const group = yield* DatasetGroup("Sales", {
111 domain: "CUSTOM",
112 datasetArns: [dataset.datasetArn],
113 tags: { Environment: "test" },
114 });
115 return { dataset, group };
116 }),
117 );
118
119 expect(created.dataset.datasetArn).toContain(":dataset/");
120 expect(created.dataset.datasetType).toBe("TARGET_TIME_SERIES");
121 expect(created.group.datasetGroupArn).toContain(":dataset-group/");
122 expect(created.group.domain).toBe("CUSTOM");
123
124 // Out-of-band verification via distilled.
125 const describedDataset = yield* forecast.describeDataset({
126 DatasetArn: created.dataset.datasetArn,
127 });
128 expect(describedDataset.Domain).toBe("CUSTOM");
129 expect(describedDataset.DataFrequency).toBe("D");
130
131 const describedGroup = yield* forecast.describeDatasetGroup({
132 DatasetGroupArn: created.group.datasetGroupArn,
133 });
134 expect(describedGroup.DatasetArns).toContain(created.dataset.datasetArn);
135
136 const groupTags = yield* forecast.listTagsForResource({
137 ResourceArn: created.group.datasetGroupArn,
138 });
139 expect(toTagRecord(groupTags.Tags)["alchemy::id"]).toBe("Sales");
140
141 // Update: detach the dataset and change tags in place.
142 const updated = yield* stack.deploy(
143 Effect.gen(function* () {
144 const dataset = yield* Dataset("Demand", {
145 domain: "CUSTOM",
146 datasetType: "TARGET_TIME_SERIES",
147 dataFrequency: "D",
148 schema: {
149 attributes: [
150 { attributeName: "item_id", attributeType: "string" },
151 { attributeName: "timestamp", attributeType: "timestamp" },
152 { attributeName: "target_value", attributeType: "float" },
153 ],
154 },
155 tags: { Environment: "test" },
156 });
157 const group = yield* DatasetGroup("Sales", {
158 domain: "CUSTOM",
159 datasetArns: [],
160 tags: { Environment: "test", Extra: "yes" },
161 });
162 return { dataset, group };
163 }),
164 );
165 expect(updated.group.datasetGroupArn).toBe(created.group.datasetGroupArn);
166
167 const reDescribedGroup = yield* forecast.describeDatasetGroup({
168 DatasetGroupArn: created.group.datasetGroupArn,
169 });
170 expect(reDescribedGroup.DatasetArns ?? []).not.toContain(created.dataset.datasetArn);
171 const updatedTags = yield* forecast.listTagsForResource({
172 ResourceArn: created.group.datasetGroupArn,
173 });
174 expect(toTagRecord(updatedTags.Tags).Extra).toBe("yes");
175
176 // Destroy and verify deletion out-of-band.
177 yield* stack.destroy();
178 yield* assertDatasetGroupDeleted(created.group.datasetGroupArn);
179 const datasetError = yield* Effect.flip(
180 forecast.describeDataset({ DatasetArn: created.dataset.datasetArn }),
181 );
182 expect(datasetError._tag).toBe("ResourceNotFoundException");
183 }),
184 { tags: ["provider:aws", "provider:aws:forecast", "live"], timeout: 300_000 },
185 );