1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as bne from "@distilled.cloud/gcp/blockchainnodeengine_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// Blockchain nodes bill hourly and take 15-45 minutes to provision, so the
18// lifecycle is opt-in: set GCP_TEST_BLOCKCHAIN_NODE=1 together with
19// GCP_TEST_SLOW.
20const runLifecycle =
21 !!process.env.GCP_TEST_BLOCKCHAIN_NODE &&
22 !!process.env.GCP_TEST_SLOW &&
23 !process.env.FAST;
24
25const waitUntilGone = (name: string) =>
26 bne.getProjectsLocationsBlockchainNodes({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("2 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36test.provider(
37 "getProjectsLocationsBlockchainNodes on a missing node fails with a typed tag",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 const parent = `projects/${project}/locations/us-central1`;
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 bne.getProjectsLocationsBlockchainNodes({
46 name: `${parent}/blockchainNodes/alchemy-bne-missing`,
47 }),
48 );
49 expect(error._tag).toEqual("NotFound");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 {
54 tags: ["provider:gcp", "provider:gcp:blockchainnodeengine", "live"],
55 timeout: 90_000,
56 },
57);
58
59test.provider.skipIf(!runLifecycle)(
60 "create, update, and delete a blockchain node",
61 (stack) =>
62 Effect.gen(function* () {
63 const { project } = yield* GcpEnvironment.current;
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.BlockchainNodeEngine.BlockchainNode("Sepolia", {
69 location: "us-central1",
70 blockchainType: "ETHEREUM",
71 ethereumDetails: {
72 network: "TESTNET_SEPOLIA",
73 nodeType: "FULL",
74 executionClient: "GETH",
75 consensusClient: "LIGHTHOUSE",
76 },
77 labels: { env: "test" },
78 });
79 }),
80 );
81
82 expect(created.name).toContain("/blockchainNodes/");
83 expect(created.blockchainNodeId).toEqual(expect.any(String));
84 expect(created.location).toEqual("us-central1");
85 expect(created.project).toEqual(project);
86 expect(created.blockchainType).toEqual("ETHEREUM");
87 expect(created.labels).toMatchObject({ env: "test" });
88 expect(created.ethereumDetails?.network).toEqual("TESTNET_SEPOLIA");
89
90 const fetched = yield* bne.getProjectsLocationsBlockchainNodes({
91 name: created.name,
92 });
93 expect(fetched.name).toEqual(created.name);
94 expect(fetched.labels?.env).toEqual("test");
95 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
96 expect(fetched.ethereumDetails?.network).toEqual("TESTNET_SEPOLIA");
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 return yield* GCP.BlockchainNodeEngine.BlockchainNode("Sepolia", {
101 blockchainNodeId: created.blockchainNodeId,
102 location: "us-central1",
103 blockchainType: "ETHEREUM",
104 ethereumDetails: {
105 network: "TESTNET_SEPOLIA",
106 nodeType: "FULL",
107 executionClient: "GETH",
108 consensusClient: "LIGHTHOUSE",
109 },
110 labels: { env: "prod", role: "node" },
111 });
112 }),
113 );
114
115 expect(updated.name).toEqual(created.name);
116 expect(updated.blockchainNodeId).toEqual(created.blockchainNodeId);
117 expect(updated.labels).toMatchObject({ env: "prod", role: "node" });
118
119 const refetched = yield* bne.getProjectsLocationsBlockchainNodes({
120 name: created.name,
121 });
122 expect(refetched.labels?.env).toEqual("prod");
123 expect(refetched.labels?.role).toEqual("node");
124
125 yield* stack.destroy();
126
127 const gone = yield* waitUntilGone(created.name);
128 expect(gone).toEqual("gone");
129 }).pipe(logLevel),
130 {
131 tags: ["provider:gcp", "provider:gcp:blockchainnodeengine", "live"],
132 timeout: 3_600_000,
133 },
134);