The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import ForecastTestFunctionLive, { ForecastTestFunction } from "./handler"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 13 | const sharedStack = Core.scratchStack(testOptions, "ForecastBindings"); |
| 14 | |
| 15 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 16 | // over 60s on a fresh deploy. |
| 17 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 18 | |
| 19 | let baseUrl: string; |
| 20 | |
| 21 | // Well-formed-but-nonexistent ARNs the probe routes are driven against. |
| 22 | // Computed inside test bodies (test.provider provides AWSEnvironment). |
| 23 | const probeArns = Effect.gen(function* () { |
| 24 | const { accountId, region } = yield* AWSEnvironment.current; |
| 25 | return { |
| 26 | importJob: `arn:aws:forecast:${region}:${accountId}:dataset-import-job/alchemy_probe/alchemy_probe`, |
| 27 | predictor: `arn:aws:forecast:${region}:${accountId}:predictor/alchemy_probe`, |
| 28 | forecast: `arn:aws:forecast:${region}:${accountId}:forecast/alchemy_probe`, |
| 29 | forecastExport: `arn:aws:forecast:${region}:${accountId}:forecast-export-job/alchemy_probe/alchemy_probe`, |
| 30 | whatIfAnalysis: `arn:aws:forecast:${region}:${accountId}:what-if-analysis/alchemy_probe`, |
| 31 | whatIfForecast: `arn:aws:forecast:${region}:${accountId}:what-if-forecast/alchemy_probe`, |
| 32 | whatIfForecastExport: `arn:aws:forecast:${region}:${accountId}:what-if-forecast-export/alchemy_probe/alchemy_probe`, |
| 33 | }; |
| 34 | }); |
| 35 | |
| 36 | // Amazon Forecast is closed to new customers: a grandfathered account |
| 37 | // answers a nonexistent ARN with ResourceNotFoundException, while a blocked |
| 38 | // account rejects the call with AccessDeniedException ("Amazon Forecast is |
| 39 | // no longer available to new customers"). Both are typed tags decoded inside |
| 40 | // the Lambda — an IAM gap or schema break would surface a different tag and |
| 41 | // fail the assertion. |
| 42 | const EXPECTED_PROBE_TAGS = [ |
| 43 | "ResourceNotFoundException", |
| 44 | "AccessDeniedException", |
| 45 | "InvalidInputException", |
| 46 | ]; |
| 47 | |
| 48 | const getJson = (path: string) => |
| 49 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 50 | Effect.flatMap((response) => |
| 51 | response.status >= 500 |
| 52 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 53 | : Effect.succeed(response), |
| 54 | ), |
| 55 | Effect.retry({ |
| 56 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 57 | }), |
| 58 | Effect.flatMap((r) => r.json), |
| 59 | ); |
| 60 | |
| 61 | const probeTag = (path: string, arn: string) => |
| 62 | getJson(`${path}?arn=${encodeURIComponent(arn)}`).pipe( |
| 63 | Effect.map((response) => (response as { tag: string }).tag), |
| 64 | ); |
| 65 | |
| 66 | describe.sequential( |
| 67 | "Forecast Bindings", |
| 68 | { tags: ["provider:aws", "provider:aws:forecast", "provider:aws:lambda", "live"] }, |
| 69 | () => { |
| 70 | beforeAll( |
| 71 | Effect.gen(function* () { |
| 72 | yield* Effect.logInfo("Forecast test setup: destroying previous resources"); |
| 73 | yield* sharedStack.destroy(); |
| 74 | |
| 75 | yield* Effect.logInfo("Forecast test setup: deploying fixture"); |
| 76 | const { functionUrl } = yield* sharedStack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* ForecastTestFunction; |
| 79 | }).pipe(Effect.provide(ForecastTestFunctionLive)), |
| 80 | ); |
| 81 | |
| 82 | expect(functionUrl).toBeTruthy(); |
| 83 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 84 | |
| 85 | const readinessUrl = `${baseUrl}/bindings`; |
| 86 | yield* HttpClient.get(readinessUrl).pipe( |
| 87 | Effect.flatMap((response) => |
| 88 | response.status === 200 |
| 89 | ? Effect.succeed(response) |
| 90 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 91 | ), |
| 92 | Effect.retry({ schedule: readinessPolicy }), |
| 93 | ); |
| 94 | }), |
| 95 | { timeout: 240_000 }, |
| 96 | ); |
| 97 | |
| 98 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 99 | |
| 100 | describe("binding registration", () => { |
| 101 | test.provider("all twenty capabilities initialize in the runtime", () => |
| 102 | Effect.gen(function* () { |
| 103 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 104 | expect(response.bound).toHaveLength(20); |
| 105 | }), |
| 106 | ); |
| 107 | }); |
| 108 | |
| 109 | describe("DescribeDatasetImportJob", () => { |
| 110 | test.provider("surfaces a typed tag for a nonexistent import job", () => |
| 111 | Effect.gen(function* () { |
| 112 | const arns = yield* probeArns; |
| 113 | const tag = yield* probeTag("/import-probe", arns.importJob); |
| 114 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 115 | }), |
| 116 | ); |
| 117 | }); |
| 118 | |
| 119 | describe("DescribeAutoPredictor", () => { |
| 120 | test.provider("surfaces a typed tag for a nonexistent predictor", () => |
| 121 | Effect.gen(function* () { |
| 122 | const arns = yield* probeArns; |
| 123 | const tag = yield* probeTag("/predictor-probe", arns.predictor); |
| 124 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 125 | }), |
| 126 | ); |
| 127 | }); |
| 128 | |
| 129 | describe("GetAccuracyMetrics", () => { |
| 130 | test.provider("surfaces a typed tag for a nonexistent predictor", () => |
| 131 | Effect.gen(function* () { |
| 132 | const arns = yield* probeArns; |
| 133 | const tag = yield* probeTag("/accuracy-probe", arns.predictor); |
| 134 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 135 | }), |
| 136 | ); |
| 137 | }); |
| 138 | |
| 139 | describe("DescribeForecast", () => { |
| 140 | test.provider("surfaces a typed tag for a nonexistent forecast", () => |
| 141 | Effect.gen(function* () { |
| 142 | const arns = yield* probeArns; |
| 143 | const tag = yield* probeTag("/forecast-probe", arns.forecast); |
| 144 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 145 | }), |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | describe("CreateForecast", () => { |
| 150 | test.provider("rejects a nonexistent predictor with a typed tag", () => |
| 151 | Effect.gen(function* () { |
| 152 | const arns = yield* probeArns; |
| 153 | const tag = yield* probeTag("/create-forecast-probe", arns.predictor); |
| 154 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 155 | }), |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | describe("StopResource", () => { |
| 160 | test.provider("surfaces a typed tag for a nonexistent job", () => |
| 161 | Effect.gen(function* () { |
| 162 | const arns = yield* probeArns; |
| 163 | const tag = yield* probeTag("/stop-probe", arns.predictor); |
| 164 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 165 | }), |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("ResumeResource", () => { |
| 170 | test.provider("surfaces a typed tag for a nonexistent monitor", () => |
| 171 | Effect.gen(function* () { |
| 172 | const arns = yield* probeArns; |
| 173 | const tag = yield* probeTag("/resume-probe", arns.predictor); |
| 174 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 175 | }), |
| 176 | ); |
| 177 | }); |
| 178 | |
| 179 | describe("QueryForecast", () => { |
| 180 | test.provider("surfaces a typed tag for a nonexistent forecast", () => |
| 181 | Effect.gen(function* () { |
| 182 | const arns = yield* probeArns; |
| 183 | const tag = yield* probeTag("/query-probe", arns.forecast); |
| 184 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 185 | }), |
| 186 | ); |
| 187 | }); |
| 188 | |
| 189 | describe("QueryWhatIfForecast", () => { |
| 190 | test.provider("surfaces a typed tag for a nonexistent forecast", () => |
| 191 | Effect.gen(function* () { |
| 192 | const arns = yield* probeArns; |
| 193 | const tag = yield* probeTag("/whatif-query-probe", arns.whatIfForecast); |
| 194 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 195 | }), |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | describe("CreateForecastExportJob", () => { |
| 200 | test.provider("rejects a nonexistent forecast with a typed tag", () => |
| 201 | Effect.gen(function* () { |
| 202 | const arns = yield* probeArns; |
| 203 | const tag = yield* probeTag("/export-create-probe", arns.forecast); |
| 204 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 205 | }), |
| 206 | ); |
| 207 | }); |
| 208 | |
| 209 | describe("DescribeForecastExportJob", () => { |
| 210 | test.provider("surfaces a typed tag for a nonexistent export job", () => |
| 211 | Effect.gen(function* () { |
| 212 | const arns = yield* probeArns; |
| 213 | const tag = yield* probeTag("/export-probe", arns.forecastExport); |
| 214 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 215 | }), |
| 216 | ); |
| 217 | }); |
| 218 | |
| 219 | describe("CreateWhatIfAnalysis", () => { |
| 220 | test.provider("rejects a nonexistent forecast with a typed tag", () => |
| 221 | Effect.gen(function* () { |
| 222 | const arns = yield* probeArns; |
| 223 | const tag = yield* probeTag("/whatif-analysis-create-probe", arns.forecast); |
| 224 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 225 | }), |
| 226 | ); |
| 227 | }); |
| 228 | |
| 229 | describe("DescribeWhatIfAnalysis", () => { |
| 230 | test.provider("surfaces a typed tag for a nonexistent analysis", () => |
| 231 | Effect.gen(function* () { |
| 232 | const arns = yield* probeArns; |
| 233 | const tag = yield* probeTag("/whatif-analysis-probe", arns.whatIfAnalysis); |
| 234 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 235 | }), |
| 236 | ); |
| 237 | }); |
| 238 | |
| 239 | describe("CreateWhatIfForecast", () => { |
| 240 | test.provider("rejects a nonexistent analysis with a typed tag", () => |
| 241 | Effect.gen(function* () { |
| 242 | const arns = yield* probeArns; |
| 243 | const tag = yield* probeTag("/whatif-create-probe", arns.whatIfAnalysis); |
| 244 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 245 | }), |
| 246 | ); |
| 247 | }); |
| 248 | |
| 249 | describe("DescribeWhatIfForecast", () => { |
| 250 | test.provider("surfaces a typed tag for a nonexistent scenario forecast", () => |
| 251 | Effect.gen(function* () { |
| 252 | const arns = yield* probeArns; |
| 253 | const tag = yield* probeTag("/whatif-forecast-probe", arns.whatIfForecast); |
| 254 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 255 | }), |
| 256 | ); |
| 257 | }); |
| 258 | |
| 259 | describe("CreateWhatIfForecastExport", () => { |
| 260 | test.provider("rejects a nonexistent scenario forecast with a typed tag", () => |
| 261 | Effect.gen(function* () { |
| 262 | const arns = yield* probeArns; |
| 263 | const tag = yield* probeTag("/whatif-export-create-probe", arns.whatIfForecast); |
| 264 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 265 | }), |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | describe("DescribeWhatIfForecastExport", () => { |
| 270 | test.provider("surfaces a typed tag for a nonexistent export", () => |
| 271 | Effect.gen(function* () { |
| 272 | const arns = yield* probeArns; |
| 273 | const tag = yield* probeTag("/whatif-export-probe", arns.whatIfForecastExport); |
| 274 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 275 | }), |
| 276 | ); |
| 277 | }); |
| 278 | |
| 279 | describe("DeleteResourceTree", () => { |
| 280 | test.provider("surfaces a typed tag for a nonexistent parent", () => |
| 281 | Effect.gen(function* () { |
| 282 | const arns = yield* probeArns; |
| 283 | const tag = yield* probeTag("/delete-tree-probe", arns.predictor); |
| 284 | expect(EXPECTED_PROBE_TAGS).toContain(tag); |
| 285 | }), |
| 286 | ); |
| 287 | }); |
| 288 | }, |
| 289 | ); |