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

main
ts 193 lines 6.82 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import IoTSiteWiseTestFunctionLive, { IoTSiteWiseTestFunction } from "./fixtures/handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "IoTSiteWiseBindings");
15
16 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]);
17
18 let baseUrl: string;
19
20 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
21 readonly status: number;
22 readonly body: string;
23 }> {}
24
25 // POST a fixture route; retry 5xx (cold-start IAM propagation surfaces as a
26 // defect -> 500 until the fresh role policy is visible to IoT SiteWise).
27 const post = (path: string) =>
28 HttpClient.execute(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(
29 Effect.flatMap((response) =>
30 response.status >= 500
31 ? response.text.pipe(
32 Effect.flatMap((body) =>
33 Effect.fail(new TransientUpstream({ status: response.status, body })),
34 ),
35 )
36 : Effect.succeed(response),
37 ),
38 Effect.retry({
39 while: (e) => e._tag === "TransientUpstream",
40 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]),
41 }),
42 );
43
44 const postJson = (path: string) => post(path).pipe(Effect.flatMap((response) => response.json));
45
46 describe(
47 "IoTSiteWise Bindings",
48 {
49 tags: [
50 "provider:aws",
51 "provider:aws:batch",
52 "provider:aws:iotsitewise",
53 "provider:aws:lambda",
54 "live",
55 ],
56 },
57 () => {
58 beforeAll(
59 Effect.gen(function* () {
60 yield* Effect.logInfo("IoTSiteWise test setup: destroying previous resources");
61 yield* sharedStack.destroy();
62
63 yield* Effect.logInfo("IoTSiteWise test setup: deploying fixture");
64 const { functionUrl } = yield* sharedStack.deploy(
65 Effect.gen(function* () {
66 return yield* IoTSiteWiseTestFunction;
67 }).pipe(Effect.provide(IoTSiteWiseTestFunctionLive)),
68 );
69
70 expect(functionUrl).toBeTruthy();
71 baseUrl = functionUrl!.replace(/\/+$/, "");
72 const readinessUrl = `${baseUrl}/ping`;
73
74 yield* Effect.logInfo(`IoTSiteWise test setup: probing readiness at ${readinessUrl}`);
75 yield* HttpClient.get(readinessUrl).pipe(
76 Effect.flatMap((response) =>
77 response.status === 200
78 ? Effect.succeed(response)
79 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
80 ),
81 Effect.tapError((error) =>
82 Effect.logWarning(`IoTSiteWise test setup: fixture not ready yet (${String(error)})`),
83 ),
84 Effect.retry({ schedule: readinessPolicy }),
85 );
86 }),
87 { timeout: 300_000 },
88 );
89
90 afterAll(sharedStack.destroy(), { timeout: 180_000 });
91
92 describe("DescribeAsset", () => {
93 test.provider(
94 "describes the bound asset with its properties",
95 (_stack) =>
96 Effect.gen(function* () {
97 const response = (yield* postJson("/describe")) as {
98 assetId: string;
99 assetName: string;
100 propertyNames: string[];
101 };
102 expect(response.assetId).toBeTruthy();
103 expect(response.propertyNames).toContain("Temperature");
104 }),
105 { timeout: 120_000 },
106 );
107 });
108
109 describe("ListAssetProperties", () => {
110 test.provider(
111 "lists the bound asset's property summaries",
112 (_stack) =>
113 Effect.gen(function* () {
114 const response = (yield* postJson("/properties")) as { count: number };
115 expect(response.count).toBeGreaterThanOrEqual(1);
116 }),
117 { timeout: 120_000 },
118 );
119 });
120
121 describe("BatchPutAssetPropertyValue + GetAssetPropertyValue + GetAssetPropertyValueHistory", () => {
122 test.provider(
123 "ingests a TQV and reads it back (current value + history)",
124 (_stack) =>
125 Effect.gen(function* () {
126 const put = (yield* postJson("/put")) as { errorCount: number; errorCodes: string[] };
127 expect(put.errorCodes).toEqual([]);
128 expect(put.errorCount).toBe(0);
129
130 // Ingested values become readable within seconds — poll bounded.
131 const value = yield* postJson("/value").pipe(
132 Effect.map(
133 (body) => body as { doubleValue: number | null; timeInSeconds: number | null },
134 ),
135 Effect.repeat({
136 schedule: Schedule.spaced("3 seconds"),
137 until: (body): boolean => body.doubleValue !== null,
138 times: 20,
139 }),
140 );
141 expect(value.doubleValue).toBe(23.5);
142 expect(value.timeInSeconds).toBeGreaterThan(0);
143
144 const history = (yield* postJson("/history")) as { count: number };
145 expect(history.count).toBeGreaterThanOrEqual(1);
146 }),
147 { timeout: 180_000 },
148 );
149 });
150
151 describe("GetAssetPropertyAggregates", () => {
152 test.provider(
153 "reads aggregates over the last hour",
154 (_stack) =>
155 Effect.gen(function* () {
156 const response = (yield* postJson("/aggregates")) as { ok: boolean; count: number };
157 expect(response.ok).toBe(true);
158 expect(response.count).toBeGreaterThanOrEqual(0);
159 }),
160 { timeout: 120_000 },
161 );
162 });
163
164 describe("GetInterpolatedAssetPropertyValues", () => {
165 test.provider(
166 "computes interpolated values over the last hour",
167 (_stack) =>
168 Effect.gen(function* () {
169 const response = (yield* postJson("/interpolated")) as { ok: boolean; count: number };
170 expect(response.ok).toBe(true);
171 expect(response.count).toBeGreaterThanOrEqual(0);
172 }),
173 { timeout: 120_000 },
174 );
175 });
176
177 describe("ExecuteQuery", () => {
178 test.provider(
179 "runs a SiteWise SQL query against the asset index",
180 (_stack) =>
181 Effect.gen(function* () {
182 // The query index is eventually consistent for fresh assets — the
183 // call succeeding with a well-formed (possibly empty) row set is
184 // the contract under test.
185 const response = (yield* postJson("/query")) as { ok: boolean; rowCount: number };
186 expect(response.ok).toBe(true);
187 expect(response.rowCount).toBeGreaterThanOrEqual(0);
188 }),
189 { timeout: 120_000 },
190 );
191 });
192 },
193 );