1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as sasportal from "@distilled.cloud/gcp/sasportal_v1alpha1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { firstCustomerName, logLevel, runLifecycle } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 sasportal.getNodesDevices({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("1 second"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22test.provider(
23 "getNodesDevices on a missing device fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27
28 const error = yield* Effect.flip(
29 sasportal.getNodesDevices({
30 name: "nodes/missing/devices/alchemy-missing",
31 }),
32 );
33 expect(error._tag).toEqual("ServiceDisabled");
34
35 yield* stack.destroy();
36 }).pipe(logLevel),
37 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
38);
39
40test.provider.skipIf(runLifecycle)(
41 "createNodesDevices without entitlement fails with a typed tag",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const error = yield* Effect.flip(
47 sasportal.createNodesDevices({
48 parent: "nodes/missing",
49 body: {
50 displayName: "alchemy-sasportal-probe",
51 fccId: "TESTFCC",
52 serialNumber: "ALCHEMYPROBE2",
53 },
54 }),
55 );
56 expect(error._tag).toEqual("ServiceDisabled");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
61);
62
63test.provider.skipIf(!runLifecycle)(
64 "create, update, and delete a node device",
65 (stack) =>
66 Effect.gen(function* () {
67 yield* stack.destroy();
68
69 const customer = yield* firstCustomerName;
70 expect(customer.length).toBeGreaterThan(0);
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 const node = yield* GCP.SasPortal.CustomersNode("Site", {
75 parent: customer,
76 displayName: "site-a",
77 });
78 const device = yield* GCP.SasPortal.NodesDevice("Cbsd", {
79 parent: node.name,
80 displayName: "sector-a",
81 fccId: "TESTFCC",
82 serialNumber: "ALCHSN2001",
83 });
84 return { node, device };
85 }),
86 );
87
88 expect(created.device.name.length).toBeGreaterThan(0);
89 expect(created.device.parent).toEqual(created.node.name);
90 expect(created.device.displayName).toEqual("sector-a");
91
92 const fetched = yield* sasportal.getNodesDevices({
93 name: created.device.name,
94 });
95 expect(fetched.name).toEqual(created.device.name);
96 expect(fetched.displayName).toContain("[alchemy ");
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 const node = yield* GCP.SasPortal.CustomersNode("Site", {
101 parent: customer,
102 name: created.node.name,
103 displayName: "site-a",
104 });
105 const device = yield* GCP.SasPortal.NodesDevice("Cbsd", {
106 parent: node.name,
107 name: created.device.name,
108 displayName: "sector-b",
109 fccId: "TESTFCC",
110 serialNumber: "ALCHSN2001",
111 });
112 return { node, device };
113 }),
114 );
115
116 expect(updated.device.name).toEqual(created.device.name);
117 expect(updated.device.displayName).toEqual("sector-b");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.device.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
125);