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

main
ts 177 lines 7.59 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { SearchJob } from "@alchemy.run/aws/BackupSearch";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as backupsearch from "@distilled.cloud/aws/backupsearch";
6 import * as Effect from "effect/Effect";
7
8 const { test } = Test.make({ providers: AWS.providers() });
9
10 // Ungated typed-error probes: prove the distilled error union carries the
11 // not-found tag this provider's read/reconcile/delete paths depend on. Runs
12 // in every CI pass at near-zero cost, unlike the gated lifecycle below.
13 test.provider(
14 "getSearchJob on a nonexistent identifier fails with ResourceNotFoundException",
15 () =>
16 Effect.gen(function* () {
17 // Identifiers must be UUID-shaped — anything else is a ValidationException.
18 const error = yield* Effect.flip(
19 backupsearch.getSearchJob({ SearchJobIdentifier: "00000000-0000-0000-0000-000000000000" }),
20 );
21 expect(error._tag).toBe("ResourceNotFoundException");
22 }),
23 { tags: ["provider:aws", "provider:aws:backupsearch", "live"] },
24 );
25
26 test.provider(
27 "getSearchResultExportJob on a nonexistent identifier fails with ResourceNotFoundException",
28 () =>
29 Effect.gen(function* () {
30 // Identifiers must be UUID-shaped — anything else is a ValidationException.
31 const error = yield* Effect.flip(
32 backupsearch.getSearchResultExportJob({
33 ExportJobIdentifier: "00000000-0000-0000-0000-000000000000",
34 }),
35 );
36 expect(error._tag).toBe("ResourceNotFoundException");
37 }),
38 { tags: ["provider:aws", "provider:aws:backupsearch", "live"] },
39 );
40
41 test.provider(
42 "listSearchJobResults on a nonexistent identifier fails with ResourceNotFoundException",
43 () =>
44 Effect.gen(function* () {
45 // Underlying operation of the ListSearchJobResults runtime binding.
46 const error = yield* Effect.flip(
47 backupsearch.listSearchJobResults({
48 SearchJobIdentifier: "00000000-0000-0000-0000-000000000000",
49 }),
50 );
51 expect(error._tag).toBe("ResourceNotFoundException");
52 }),
53 { tags: ["provider:aws", "provider:aws:backupsearch", "live"] },
54 );
55
56 test.provider(
57 "listSearchJobBackups on a nonexistent identifier fails with ResourceNotFoundException",
58 () =>
59 Effect.gen(function* () {
60 // Underlying operation of the ListSearchJobBackups runtime binding.
61 const error = yield* Effect.flip(
62 backupsearch.listSearchJobBackups({
63 SearchJobIdentifier: "00000000-0000-0000-0000-000000000000",
64 }),
65 );
66 expect(error._tag).toBe("ResourceNotFoundException");
67 }),
68 { tags: ["provider:aws", "provider:aws:backupsearch", "live"] },
69 );
70
71 test.provider(
72 "listSearchJobs succeeds on an account-level scan",
73 () =>
74 Effect.gen(function* () {
75 const page = yield* backupsearch.listSearchJobs({ MaxResults: 10 });
76 expect(Array.isArray(page.SearchJobs)).toBe(true);
77 }),
78 { tags: ["provider:aws", "provider:aws:backupsearch", "live"] },
79 );
80
81 // Search jobs CANNOT be deleted — the API is Start/Stop/Get/List only, and
82 // AWS retains terminal job records server-side for ~7 days before they age
83 // out of listSearchJobs. "Orphan-free" for this resource therefore means no
84 // job is ever left RUNNING: this sweeper stops any still-running job created
85 // by this test (crash-safe — covers a run that died before stack.destroy()).
86 // It is idempotent: stopping a job that already reached a terminal state is
87 // a ConflictException, which means there is nothing to do.
88 const stopLeakedSearchJobs = Effect.gen(function* () {
89 const page = yield* backupsearch.listSearchJobs({ ByStatus: "RUNNING", MaxResults: 25 });
90 yield* Effect.forEach(
91 (page.SearchJobs ?? []).filter((job) => job.Name?.startsWith("start-S3-search-job")),
92 (job) =>
93 backupsearch.stopSearchJob({ SearchJobIdentifier: job.SearchJobIdentifier! }).pipe(
94 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
95 Effect.catchTag("ConflictException", () => Effect.void),
96 ),
97 );
98 }).pipe(Effect.orDie);
99
100 // A search job only produces results when the account has recovery points
101 // with ACTIVE backup indexes to search — the full lifecycle is gated behind
102 // AWS_TEST_BACKUP_SEARCH=1. Search jobs cannot be deleted (7-day server-side
103 // retention); destroy stops the job if it is still RUNNING, and terminal
104 // job records aging out server-side are the BackupSearch equivalent of a
105 // KMS key in PENDING_DELETION — handled, not leaked.
106 test.provider.skipIf(!process.env.AWS_TEST_BACKUP_SEARCH)(
107 "start S3 search job, verify, destroy (stop)",
108 (stack) =>
109 Effect.gen(function* () {
110 // Pre-clean: stop any RUNNING job left over from a crashed prior run.
111 yield* stopLeakedSearchJobs;
112 yield* stack.destroy();
113
114 const { job } = yield* stack.deploy(
115 Effect.gen(function* () {
116 const job = yield* SearchJob("Search", {
117 searchScope: { backupResourceTypes: ["S3"] },
118 itemFilters: {
119 s3ItemFilters: [{ objectKeys: [{ value: "alchemy-", operator: "BEGINS_WITH" }] }],
120 },
121 tags: { fixture: "backup-search" },
122 });
123 return { job };
124 }),
125 );
126
127 expect(job.searchJobIdentifier).toBeDefined();
128 expect(job.searchJobArn).toContain("search-job");
129 // In an account without indexed recovery points the job fails fast;
130 // the resource lifecycle (start/observe/stop) is identical either way.
131 expect(["RUNNING", "COMPLETED", "FAILED"]).toContain(job.status);
132
133 // Out-of-band verification via distilled.
134 const observed = yield* backupsearch.getSearchJob({
135 SearchJobIdentifier: job.searchJobIdentifier,
136 });
137 expect(observed.SearchJobArn).toBe(job.searchJobArn);
138 const tags = yield* backupsearch.listTagsForResource({ ResourceArn: job.searchJobArn });
139 expect(tags.Tags?.fixture).toBe("backup-search");
140
141 // Replacement: the search scope/filters are immutable — changing them
142 // must start a NEW search job with a new identifier.
143 const { job: replaced } = yield* stack.deploy(
144 Effect.gen(function* () {
145 const job = yield* SearchJob("Search", {
146 searchScope: { backupResourceTypes: ["S3"] },
147 itemFilters: {
148 s3ItemFilters: [{ objectKeys: [{ value: "replaced-", operator: "BEGINS_WITH" }] }],
149 },
150 tags: { fixture: "backup-search" },
151 });
152 return { job };
153 }),
154 );
155 expect(replaced.searchJobIdentifier).not.toBe(job.searchJobIdentifier);
156
157 // NOTE: a live ExportJob lifecycle additionally requires a search job
158 // in COMPLETED or STOPPED status. In an account without indexed
159 // recovery points every search job ends FAILED ("Export is only
160 // supported for search job with COMPLETED,STOPPED statuses. SearchJob
161 // has status FAILED"), so export coverage needs real, indexed backups.
162
163 // Destroy stops the job (or no-ops if it already completed); the job
164 // record itself is retained by AWS for 7 days and cannot be deleted.
165 yield* stack.destroy();
166 const after = yield* backupsearch.getSearchJob({
167 SearchJobIdentifier: replaced.searchJobIdentifier,
168 });
169 expect(["STOPPING", "STOPPED", "COMPLETED", "FAILED"]).toContain(after.Status);
170 }).pipe(
171 // Crash-safe teardown: even if any assertion above fails mid-flight,
172 // never leave a search job RUNNING. (Terminal records are un-deletable
173 // and age out server-side within ~7 days.)
174 Effect.ensuring(stopLeakedSearchJobs),
175 ),
176 { tags: ["provider:aws", "provider:aws:backupsearch", "live"], timeout: 120_000 },
177 );