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 {
8 firstCustomerName,
9 logLevel,
10 runLifecycle,
11 signedDeviceProbe,
12} from "./common.ts";
13
14const { test } = Test.make({ providers: GCP.providers() });
15
16const waitUntilGone = (name: string) =>
17 sasportal.getDeploymentsDevices({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 8,
24 }),
25 );
26
27test.provider(
28 "getDeploymentsDevices on a missing device fails with a typed tag",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const error = yield* Effect.flip(
34 sasportal.getDeploymentsDevices({
35 name: "nodes/missing/deployments/missing/devices/alchemy-missing",
36 }),
37 );
38 expect(error._tag).toEqual("NotFound");
39
40 yield* stack.destroy();
41 }).pipe(logLevel),
42 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
43);
44
45test.provider.skipIf(runLifecycle)(
46 "createSignedNodesDeploymentsDevices without entitlement fails with a typed tag",
47 (stack) =>
48 Effect.gen(function* () {
49 yield* stack.destroy();
50
51 const error = yield* Effect.flip(
52 sasportal.createSignedNodesDeploymentsDevices({
53 parent: "nodes/missing/deployments/missing",
54 body: signedDeviceProbe,
55 }),
56 );
57 expect(error._tag).toEqual("ServiceDisabled");
58
59 yield* stack.destroy();
60 }).pipe(logLevel),
61 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
62);
63
64test.provider.skipIf(!runLifecycle)(
65 "create, update, and delete a signed device",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 const customer = yield* firstCustomerName;
71 expect(customer.length).toBeGreaterThan(0);
72
73 const created = yield* stack.deploy(
74 Effect.gen(function* () {
75 const node = yield* GCP.SasPortal.CustomersNode("Site", {
76 parent: customer,
77 displayName: "site-a",
78 });
79 const deployment = yield* GCP.SasPortal.CustomersNodesDeployment(
80 "Campus",
81 {
82 parent: node.name,
83 displayName: "downtown",
84 },
85 );
86 const device = yield* GCP.SasPortal.SignedNodesDeploymentsDevice(
87 "Cbsd",
88 {
89 parent: deployment.name,
90 displayName: "sector-a",
91 encodedDevice: signedDeviceProbe.encodedDevice,
92 installerId: signedDeviceProbe.installerId,
93 },
94 );
95 return { node, deployment, device };
96 }),
97 );
98
99 expect(created.device.name.length).toBeGreaterThan(0);
100 expect(created.device.displayName).toEqual("sector-a");
101
102 const fetched = yield* sasportal.getDeploymentsDevices({
103 name: created.device.name,
104 });
105 expect(fetched.name).toEqual(created.device.name);
106 expect(fetched.displayName).toContain("[alchemy ");
107
108 const updated = yield* stack.deploy(
109 Effect.gen(function* () {
110 const node = yield* GCP.SasPortal.CustomersNode("Site", {
111 parent: customer,
112 name: created.node.name,
113 displayName: "site-a",
114 });
115 const deployment = yield* GCP.SasPortal.CustomersNodesDeployment(
116 "Campus",
117 {
118 parent: node.name,
119 name: created.deployment.name,
120 displayName: "downtown",
121 },
122 );
123 const device = yield* GCP.SasPortal.SignedNodesDeploymentsDevice(
124 "Cbsd",
125 {
126 parent: deployment.name,
127 name: created.device.name,
128 displayName: "sector-b",
129 encodedDevice: signedDeviceProbe.encodedDevice,
130 installerId: signedDeviceProbe.installerId,
131 },
132 );
133 return { node, deployment, device };
134 }),
135 );
136
137 expect(updated.device.name).toEqual(created.device.name);
138 expect(updated.device.displayName).toEqual("sector-b");
139
140 yield* stack.destroy();
141
142 const gone = yield* waitUntilGone(created.device.name);
143 expect(gone).toEqual("gone");
144 }).pipe(logLevel),
145 { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 },
146);