| 1 | import { Action } from "@/Action"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | |
| 8 | const { test } = Test.make({ providers: GCP.providers() }); |
| 9 | |
| 10 | const logLevel = Effect.provideService( |
| 11 | MinimumLogLevel, |
| 12 | process.env.DEBUG ? "Debug" : "Info", |
| 13 | ); |
| 14 | |
| 15 | test.provider( |
| 16 | "ReadBucket, WriteBucket, and ReadWriteBucket clients", |
| 17 | (stack) => |
| 18 | Effect.gen(function* () { |
| 19 | yield* stack.destroy(); |
| 20 | |
| 21 | const out = yield* stack.deploy( |
| 22 | Effect.gen(function* () { |
| 23 | const bucket = yield* GCP.Storage.Bucket("Files", { |
| 24 | location: "US-CENTRAL1", |
| 25 | forceDestroy: true, |
| 26 | }); |
| 27 | const Probe = Action( |
| 28 | "Probe", |
| 29 | Effect.gen(function* () { |
| 30 | yield* bucket.bucketName; |
| 31 | const reader = yield* GCP.Storage.ReadBucket(bucket); |
| 32 | const writer = yield* GCP.Storage.WriteBucket(bucket); |
| 33 | const both = yield* GCP.Storage.ReadWriteBucket(bucket); |
| 34 | return Effect.fn(function* () { |
| 35 | const missing = yield* reader.get("docs/a.txt"); |
| 36 | const missingHead = yield* reader.head("docs/a.txt"); |
| 37 | yield* writer.put("docs/a.txt", "alpha", { |
| 38 | contentType: "text/plain", |
| 39 | }); |
| 40 | yield* both.put("docs/b.txt", new TextEncoder().encode("beta")); |
| 41 | yield* both.put("other/c.txt", "gamma"); |
| 42 | const a = yield* reader.get("docs/a.txt"); |
| 43 | const head = yield* reader.head("docs/a.txt"); |
| 44 | const listed = yield* both.list({ prefix: "docs/" }); |
| 45 | const grouped = yield* reader.list({ delimiter: "/" }); |
| 46 | yield* writer.delete("docs/a.txt"); |
| 47 | yield* writer.delete("never-existed.txt"); |
| 48 | const afterDelete = yield* both.get("docs/a.txt"); |
| 49 | return { |
| 50 | missing: missing === undefined, |
| 51 | missingHead: missingHead === undefined, |
| 52 | a: a && new TextDecoder().decode(a.body), |
| 53 | contentType: head?.contentType, |
| 54 | listed: listed.objects.map((object) => object.name).sort(), |
| 55 | prefixes: grouped.prefixes.sort(), |
| 56 | afterDelete: afterDelete === undefined, |
| 57 | }; |
| 58 | }); |
| 59 | }).pipe( |
| 60 | Effect.provide(GCP.Storage.ReadBucketHttp), |
| 61 | Effect.provide(GCP.Storage.WriteBucketHttp), |
| 62 | Effect.provide(GCP.Storage.ReadWriteBucketHttp), |
| 63 | ), |
| 64 | ); |
| 65 | return { probe: yield* Probe({}) }; |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(out.probe).toEqual({ |
| 70 | missing: true, |
| 71 | missingHead: true, |
| 72 | a: "alpha", |
| 73 | contentType: "text/plain", |
| 74 | listed: ["docs/a.txt", "docs/b.txt"], |
| 75 | prefixes: ["docs/", "other/"], |
| 76 | afterDelete: true, |
| 77 | }); |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | }).pipe(logLevel), |
| 81 | { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 180_000 }, |
| 82 | ); |