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: 8,
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/nodes/missing/devices/alchemy-missing",
31 }),
32 );
33 expect(error._tag).toEqual("NotFound");
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 "createNodesNodesDevices 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.createNodesNodesDevices({
48 parent: "nodes/missing/nodes/missing",
49 body: {
50 displayName: "alchemy-sasportal-probe",
51 fccId: "TESTFCC",
52 serialNumber: "ALCHEMYPROBE1",
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 nested 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 parent = yield* GCP.SasPortal.CustomersNode("Site", {
75 parent: customer,
76 displayName: "campus",
77 });
78 const child = yield* GCP.SasPortal.NodesNode("Sector", {
79 parent: parent.name,
80 displayName: "sector-1",
81 });
82 const device = yield* GCP.SasPortal.NodesNodesDevice("Cbsd", {
83 parent: child.name,
84 displayName: "sector-a",
85 fccId: "TESTFCC",
86 serialNumber: "ALCHSNNEST4",
87 });
88 return { parent, child, device };
89 }),
90 );
91
92 expect(created.device.name.length).toBeGreaterThan(0);
93 expect(created.device.displayName).toEqual("sector-a");
94
95 const fetched = yield* sasportal.getNodesDevices({
96 name: created.device.name,
97 });
98 expect(fetched.name).toEqual(created.device.name);
99 expect(fetched.displayName).toContain("[alchemy ");
100
101 const updated = yield* stack.deploy(
102 Effect.gen(function* () {
103 const parent = yield* GCP.SasPortal.CustomersNode("Site", {
104 parent: customer,
105 name: created.parent.name,
106 displayName: "campus",
107 });
108 const child = yield* GCP.SasPortal.NodesNode("Sector", {
109 parent: parent.name,
110 name: created.child.name,
111 displayName: "sector-1",
112 });
113 const device = yield* GCP.SasPortal.NodesNodesDevice("Cbsd", {
114 parent: child.name,
115 name: created.device.name,
116 displayName: "sector-b",
117 fccId: "TESTFCC",
118 serialNumber: "ALCHSNNEST4",
119 });
120 return { parent, child, device };
121 }),
122 );
123
124 expect(updated.device.name).toEqual(created.device.name);
125 expect(updated.device.displayName).toEqual("sector-b");
126
127 yield* stack.destroy();
128
129 const gone = yield* waitUntilGone(created.device.name);
130 expect(gone).toEqual("gone");
131 }).pipe(logLevel),
132 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
133);