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

main
ts 131 lines 5.69 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as opensearch from "@distilled.cloud/aws/opensearch";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // Ungated typed-error probe: proves the distilled error union carries the
12 // not-found tag this provider's read/delete paths depend on.
13 test.provider(
14 "describeDomain on a nonexistent domain fails with ResourceNotFoundException",
15 () =>
16 Effect.gen(function* () {
17 const error = yield* Effect.flip(
18 opensearch.describeDomain({ DomainName: "alchemy-nonexistent-probe" }),
19 );
20 expect(error._tag).toBe("ResourceNotFoundException");
21 }),
22 { tags: ["provider:aws", "provider:aws:opensearch", "live"] },
23 );
24
25 // Deletion is verified as INITIATED (Deleted=true / Processing, irreversible)
26 // or fully gone. Full disappearance takes ~10-15 more minutes server-side;
27 // waiting for it would push the test into its timeout.
28 const assertDomainDeleting = (name: string) =>
29 Effect.gen(function* () {
30 const status = yield* opensearch.describeDomain({ DomainName: name }).pipe(
31 Effect.map((response) =>
32 response.DomainStatus.Deleted === true || response.DomainStatus.Processing === true
33 ? ("deleting" as const)
34 : ("active" as const),
35 ),
36 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
37 );
38 if (status === "active") {
39 return yield* Effect.fail(new Error(`domain '${name}' still exists and is not deleting`));
40 }
41 }).pipe(
42 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
43 );
44
45 // OpenSearch domains take ~15-25 minutes to provision and are billed per
46 // instance-hour while they exist. The full lifecycle is skipped under
47 // `--fast` and always destroys what it created.
48 test.provider.skipIf(!!process.env.FAST)(
49 "create encrypted single-node domain, verify, destroy",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const { accountId, region } = yield* AWSEnvironment.current;
55
56 // A structured (typed) PolicyDocument — deliberately NOT a raw JSON
57 // string — to prove PolicyDocument-valued access policies round-trip.
58 const accessPolicies: AWS.IAM.PolicyDocument = {
59 Version: "2012-10-17",
60 Statement: [
61 {
62 Effect: "Allow",
63 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
64 Action: ["es:*"],
65 Resource: `arn:aws:es:${region}:${accountId}:domain/*`,
66 },
67 ],
68 };
69
70 const program = Effect.gen(function* () {
71 const domain = yield* AWS.OpenSearch.Domain("Search", {
72 engineVersion: "OpenSearch_2.19",
73 clusterConfig: { instanceType: "t3.small.search", instanceCount: 1 },
74 ebsOptions: { volumeType: "gp3", volumeSize: 10 },
75 encryptionAtRest: { enabled: true },
76 nodeToNodeEncryption: true,
77 domainEndpointOptions: {
78 enforceHTTPS: true,
79 tlsSecurityPolicy: "Policy-Min-TLS-1-2-2019-07",
80 },
81 accessPolicies,
82 tags: { fixture: "opensearch-domain" },
83 });
84 return { domain };
85 });
86
87 const { domain } = yield* stack.deploy(program);
88
89 expect(domain.domainName).toBeDefined();
90 expect(domain.domainArn).toContain(":domain/");
91 expect(domain.created).toBe(true);
92 expect(domain.processing).toBe(false);
93 expect(domain.endpoint).toBeDefined();
94 expect(domain.engineVersion).toBe("OpenSearch_2.19");
95
96 // Out-of-band verification via distilled.
97 const described = yield* opensearch.describeDomain({ DomainName: domain.domainName });
98 expect(described.DomainStatus.EncryptionAtRestOptions?.Enabled).toBe(true);
99 expect(described.DomainStatus.NodeToNodeEncryptionOptions?.Enabled).toBe(true);
100 expect(described.DomainStatus.ClusterConfig.InstanceType).toBe("t3.small.search");
101 expect(described.DomainStatus.DomainEndpointOptions?.EnforceHTTPS).toBe(true);
102 expect(described.DomainStatus.AccessPolicies).toContain(accountId);
103 // The PolicyDocument round-trips: what OpenSearch echoes back is
104 // canonically equal to the structured document we deployed.
105 expect(AWS.IAM.normalizePolicyDocument(described.DomainStatus.AccessPolicies ?? "")).toBe(
106 AWS.IAM.normalizePolicyDocument(accessPolicies),
107 );
108
109 // Re-deploy the identical program — the normalized policy comparison
110 // must see no drift, so no updateDomainConfig (blue/green change) may
111 // fire. A config change would mint a new ChangeProgressDetails.ChangeId
112 // and flip Processing.
113 const changeIdBefore = described.DomainStatus.ChangeProgressDetails?.ChangeId;
114 const { domain: redeployed } = yield* stack.deploy(program);
115 expect(redeployed.domainArn).toBe(domain.domainArn);
116 expect(redeployed.processing).toBe(false);
117 const redescribed = yield* opensearch.describeDomain({ DomainName: domain.domainName });
118 expect(redescribed.DomainStatus.Processing).not.toBe(true);
119 expect(redescribed.DomainStatus.ChangeProgressDetails?.ChangeId).toBe(changeIdBefore);
120
121 // Destroy immediately — domains bill while they exist — and verify
122 // deletion was initiated out-of-band.
123 yield* stack.destroy();
124 yield* assertDomainDeleting(domain.domainName);
125 }),
126 // create (~15-25 min) + delete initiation, one test.
127 {
128 tags: ["provider:aws", "provider:aws:iam", "provider:aws:opensearch", "live"],
129 timeout: 3_000_000,
130 },
131 );