| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as migrationcenter from "@distilled.cloud/gcp/migrationcenter_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | migrationcenter.getProjectsLocationsImportJobsImportDataFiles({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsImportJobsImportDataFiles on a missing file fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | migrationcenter.getProjectsLocationsImportJobsImportDataFiles({ |
| 37 | name: `projects/${project}/locations/us-central1/importJobs/alchemy-missing-job/importDataFiles/alchemy-missing-file`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create and delete a migration center import data file", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const source = yield* GCP.MigrationCenter.Source("Upload", { |
| 59 | location: "us-central1", |
| 60 | type: "SOURCE_TYPE_UPLOAD", |
| 61 | displayName: "file-source", |
| 62 | }); |
| 63 | const job = yield* GCP.MigrationCenter.ImportJob("Rvtools", { |
| 64 | location: "us-central1", |
| 65 | assetSource: source.name, |
| 66 | displayName: "file-import", |
| 67 | }); |
| 68 | const file = yield* GCP.MigrationCenter.ImportJobsImportDataFile( |
| 69 | "Payload", |
| 70 | { |
| 71 | importJob: job.name, |
| 72 | location: "us-central1", |
| 73 | format: "IMPORT_JOB_FORMAT_RVTOOLS_CSV", |
| 74 | displayName: "inventory", |
| 75 | }, |
| 76 | ); |
| 77 | return { source, job, file }; |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(created.file.importDataFileId).toEqual(expect.any(String)); |
| 82 | expect(created.file.importJob).toEqual(created.job.name); |
| 83 | expect(created.file.format).toEqual("IMPORT_JOB_FORMAT_RVTOOLS_CSV"); |
| 84 | |
| 85 | const fetched = |
| 86 | yield* migrationcenter.getProjectsLocationsImportJobsImportDataFiles({ |
| 87 | name: created.file.name, |
| 88 | }); |
| 89 | expect(fetched.name).toEqual(created.file.name); |
| 90 | expect(fetched.format).toEqual("IMPORT_JOB_FORMAT_RVTOOLS_CSV"); |
| 91 | expect(fetched.displayName).toContain("alchemy-id="); |
| 92 | |
| 93 | yield* stack.destroy(); |
| 94 | const gone = yield* waitUntilGone(created.file.name); |
| 95 | expect(gone).toEqual("gone"); |
| 96 | }).pipe(logLevel), |
| 97 | { |
| 98 | tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"], |
| 99 | timeout: 180_000, |
| 100 | }, |
| 101 | ); |