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

main
ts 110 lines 4.03 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Database } from "@alchemy.run/aws/Glue";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as glue from "@distilled.cloud/aws/glue";
6 import * as Effect from "effect/Effect";
7 import { driftOf, repairDrift, waitForDrift } from "../drift.ts";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 const getDatabase = (name: string) =>
12 glue.getDatabase({ Name: name }).pipe(
13 Effect.map((r) => r.Database),
14 Effect.catchTag("EntityNotFoundException", () => Effect.succeed(undefined)),
15 );
16
17 test.provider(
18 "create, update, delete Glue database",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 // create
24 const created = yield* stack.deploy(
25 Effect.gen(function* () {
26 return yield* Database("AnalyticsDb", {
27 description: "analytics tables",
28 parameters: { classification: "parquet" },
29 });
30 }),
31 );
32
33 expect(created.databaseName).toBeDefined();
34 expect(created.databaseArn).toContain(`:database/${created.databaseName}`);
35 expect(created.catalogId).toBeDefined();
36
37 // out-of-band verification
38 const observed = yield* getDatabase(created.databaseName);
39 expect(observed?.Name).toEqual(created.databaseName);
40 expect(observed?.Description).toEqual("analytics tables");
41 expect(observed?.Parameters?.classification).toEqual("parquet");
42 // ownership markers live in the Parameters map (databases aren't taggable)
43 expect(observed?.Parameters?.["alchemy::id"]).toBeDefined();
44
45 // update: description + parameters
46 const updated = yield* stack.deploy(
47 Effect.gen(function* () {
48 return yield* Database("AnalyticsDb", {
49 description: "curated analytics tables",
50 locationUri: "s3://example-bucket/analytics/",
51 parameters: { classification: "json" },
52 });
53 }),
54 );
55
56 expect(updated.databaseName).toEqual(created.databaseName);
57 const reobserved = yield* getDatabase(created.databaseName);
58 expect(reobserved?.Description).toEqual("curated analytics tables");
59 expect(reobserved?.LocationUri).toEqual("s3://example-bucket/analytics/");
60 expect(reobserved?.Parameters?.classification).toEqual("json");
61
62 // delete
63 yield* stack.destroy();
64 const gone = yield* getDatabase(created.databaseName);
65 expect(gone).toBeUndefined();
66 }),
67 { tags: ["provider:aws", "provider:aws:glue", "live"] },
68 );
69
70 // #2688: `read` must observe the description, location and parameters, or
71 // an out-of-band change reads as `unchanged`.
72 test.provider(
73 "drift detects and repairs out-of-band database changes",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 const db = yield* stack.deploy(
79 Database("DriftDb", {
80 description: "declared",
81 parameters: { classification: "parquet" },
82 }),
83 );
84 expect((yield* driftOf(stack, "DriftDb"))?.action).toBe("unchanged");
85
86 const observed = yield* getDatabase(db.databaseName);
87 yield* glue.updateDatabase({
88 Name: db.databaseName,
89 DatabaseInput: {
90 Name: db.databaseName,
91 Description: "out-of-band",
92 Parameters: { ...observed?.Parameters, classification: "json" },
93 },
94 });
95
96 const drifted = yield* waitForDrift(stack, "DriftDb", "drifted");
97 expect(drifted?.attr?.configuration?.description).toBe("out-of-band");
98
99 const repaired = yield* repairDrift(stack);
100 expect(repaired.resources.DriftDb?.action).toBe("repaired");
101 expect((yield* driftOf(stack, "DriftDb"))?.action).toBe("unchanged");
102 const restored = yield* getDatabase(db.databaseName);
103 expect(restored?.Description).toBe("declared");
104 expect(restored?.Parameters?.classification).toBe("parquet");
105
106 yield* stack.destroy();
107 expect(yield* getDatabase(db.databaseName)).toBeUndefined();
108 }),
109 { tags: ["provider:aws", "provider:aws:glue", "live"] },
110 );