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: "customers/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 "createCustomersNodesDevices 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.createCustomersNodesDevices({
48 parent: "customers/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: "site-a",
77 });
78 const device = yield* GCP.SasPortal.CustomersNodesDevice("Cbsd", {
79 parent: parent.name,
80 displayName: "sector-a",
81 fccId: "TESTFCC",
82 serialNumber: "ALCHSNNEST2",
83 });
84 return { parent, device };
85 }),
86 );
87
88 expect(created.device.name.length).toBeGreaterThan(0);
89 expect(created.device.displayName).toEqual("sector-a");
90
91 const fetched = yield* sasportal.getNodesDevices({
92 name: created.device.name,
93 });
94 expect(fetched.name).toEqual(created.device.name);
95 expect(fetched.displayName).toContain("[alchemy ");
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 const parent = yield* GCP.SasPortal.CustomersNode("Site", {
100 parent: customer,
101 name: created.parent.name,
102 displayName: "site-a",
103 });
104 const device = yield* GCP.SasPortal.CustomersNodesDevice("Cbsd", {
105 parent: parent.name,
106 name: created.device.name,
107 displayName: "sector-b",
108 fccId: "TESTFCC",
109 serialNumber: "ALCHSNNEST2",
110 });
111 return { parent, device };
112 }),
113 );
114
115 expect(updated.device.name).toEqual(created.device.name);
116 expect(updated.device.displayName).toEqual("sector-b");
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(created.device.name);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
124);