| 1 | import * as AWS from "@/AWS"; |
| 2 | import { Graph } from "@/AWS/Detective/Graph.ts"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as detective from "@distilled.cloud/aws/detective"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { makeDetectiveTestLease } from "./TestLease.ts"; |
| 9 | |
| 10 | const { test, beforeAll, afterAll } = Test.make({ |
| 11 | providers: AWS.providers(), |
| 12 | }); |
| 13 | const testLease = makeDetectiveTestLease(); |
| 14 | |
| 15 | beforeAll(testLease.acquire, { timeout: 240_000 }); |
| 16 | afterAll(testLease.release); |
| 17 | |
| 18 | const firstGraphArn = detective |
| 19 | .listGraphs({}) |
| 20 | .pipe(Effect.map((r) => r.GraphList?.[0]?.Arn)); |
| 21 | |
| 22 | // The Detective behavior graph is an account/region singleton. This test only |
| 23 | // runs when the account has no graph — it must never delete a graph the user |
| 24 | // already operates (capture-and-restore safety). |
| 25 | test.provider( |
| 26 | "lifecycle: enable behavior graph, retag, disable", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const preexisting = yield* firstGraphArn; |
| 30 | if (preexisting) { |
| 31 | yield* Effect.logInfo( |
| 32 | `Detective graph ${preexisting} already exists — skipping destructive lifecycle test`, |
| 33 | ); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | // Create — enable the behavior graph with tags. |
| 40 | const created = yield* stack.deploy( |
| 41 | Effect.gen(function* () { |
| 42 | return yield* Graph("Graph", { tags: { env: "test" } }); |
| 43 | }), |
| 44 | ); |
| 45 | expect(created.graphArn).toContain(":graph:"); |
| 46 | |
| 47 | // Out-of-band verification. |
| 48 | const live = yield* firstGraphArn; |
| 49 | expect(live).toBe(created.graphArn); |
| 50 | const tags = yield* detective.listTagsForResource({ |
| 51 | ResourceArn: created.graphArn, |
| 52 | }); |
| 53 | expect(tags.Tags?.["env"]).toBe("test"); |
| 54 | expect(tags.Tags?.["alchemy::id"]).toBe("Graph"); |
| 55 | |
| 56 | // Canonical list() coverage. |
| 57 | const provider = yield* Provider.findProvider(Graph); |
| 58 | const all = yield* provider.list(); |
| 59 | expect(all.some((g) => g.graphArn === created.graphArn)).toBe(true); |
| 60 | |
| 61 | // Update — retag in place (no replacement; ARN is stable). |
| 62 | const updated = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* Graph("Graph", { tags: { env: "prod" } }); |
| 65 | }), |
| 66 | ); |
| 67 | expect(updated.graphArn).toBe(created.graphArn); |
| 68 | const updatedTags = yield* detective.listTagsForResource({ |
| 69 | ResourceArn: created.graphArn, |
| 70 | }); |
| 71 | expect(updatedTags.Tags?.["env"]).toBe("prod"); |
| 72 | |
| 73 | // Destroy — the graph is deleted and the region is clean again. |
| 74 | yield* stack.destroy(); |
| 75 | const after = yield* firstGraphArn; |
| 76 | expect(after).toBeUndefined(); |
| 77 | }), |
| 78 | { |
| 79 | tags: ["provider:aws", "provider:aws:detective", "live"], |
| 80 | timeout: 180_000, |
| 81 | }, |
| 82 | ); |