| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as sasportal from "@distilled.cloud/gcp/sasportal_v1alpha1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { |
| 8 | firstCustomerName, |
| 9 | logLevel, |
| 10 | runLifecycle, |
| 11 | signedDeviceProbe, |
| 12 | } from "./common.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: GCP.providers() }); |
| 15 | |
| 16 | const waitUntilGone = (name: string) => |
| 17 | sasportal.getNodesDevices({ 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 | |
| 27 | test.provider( |
| 28 | "getNodesDevices 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.getNodesDevices({ |
| 35 | name: "nodes/missing/nodes/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 | |
| 45 | test.provider.skipIf(runLifecycle)( |
| 46 | "createSignedNodesNodesDevices 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.createSignedNodesNodesDevices({ |
| 53 | parent: "nodes/missing/nodes/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 | |
| 64 | test.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 parent = yield* GCP.SasPortal.CustomersNode("Site", { |
| 76 | parent: customer, |
| 77 | displayName: "campus", |
| 78 | }); |
| 79 | const child = yield* GCP.SasPortal.NodesNode("Sector", { |
| 80 | parent: parent.name, |
| 81 | displayName: "sector-1", |
| 82 | }); |
| 83 | const device = yield* GCP.SasPortal.SignedNodesNodesDevice("Cbsd", { |
| 84 | parent: child.name, |
| 85 | displayName: "sector-a", |
| 86 | encodedDevice: signedDeviceProbe.encodedDevice, |
| 87 | installerId: signedDeviceProbe.installerId, |
| 88 | }); |
| 89 | return { parent, child, device }; |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(created.device.name.length).toBeGreaterThan(0); |
| 94 | expect(created.device.displayName).toEqual("sector-a"); |
| 95 | |
| 96 | const fetched = yield* sasportal.getNodesDevices({ |
| 97 | name: created.device.name, |
| 98 | }); |
| 99 | expect(fetched.name).toEqual(created.device.name); |
| 100 | expect(fetched.displayName).toContain("[alchemy "); |
| 101 | |
| 102 | const updated = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const parent = yield* GCP.SasPortal.CustomersNode("Site", { |
| 105 | parent: customer, |
| 106 | name: created.parent.name, |
| 107 | displayName: "campus", |
| 108 | }); |
| 109 | const child = yield* GCP.SasPortal.NodesNode("Sector", { |
| 110 | parent: parent.name, |
| 111 | name: created.child.name, |
| 112 | displayName: "sector-1", |
| 113 | }); |
| 114 | const device = yield* GCP.SasPortal.SignedNodesNodesDevice("Cbsd", { |
| 115 | parent: child.name, |
| 116 | name: created.device.name, |
| 117 | displayName: "sector-b", |
| 118 | encodedDevice: signedDeviceProbe.encodedDevice, |
| 119 | installerId: signedDeviceProbe.installerId, |
| 120 | }); |
| 121 | return { parent, child, device }; |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | expect(updated.device.name).toEqual(created.device.name); |
| 126 | expect(updated.device.displayName).toEqual("sector-b"); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | |
| 130 | const gone = yield* waitUntilGone(created.device.name); |
| 131 | expect(gone).toEqual("gone"); |
| 132 | }).pipe(logLevel), |
| 133 | { tags: ["provider:gcp", "provider:gcp:sasportal", "live"], timeout: 90_000 }, |
| 134 | ); |