|
| 1 | +import { GetObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; |
| 2 | +import { sdkStreamMixin } from "@smithy/util-stream"; |
| 3 | +import { fromAny } from "@total-typescript/shoehorn"; |
| 4 | +import { mockClient } from "aws-sdk-client-mock"; |
| 5 | +import { compressSync, strToU8 } from "fflate"; |
| 6 | +import { goTry } from "go-go-try"; |
| 7 | +import { beforeEach } from "node:test"; |
| 8 | +import { Readable } from "stream"; |
| 9 | +import { expect, test } from "vitest"; |
| 10 | +import { PackageApiR2Bucket } from "./package-api-r2-bucket"; |
| 11 | + |
| 12 | +const s3Mock = mockClient(S3Client); |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + s3Mock.reset(); |
| 16 | +}); |
| 17 | + |
| 18 | +test("setPackageApi fail", async () => { |
| 19 | + s3Mock.on(PutObjectCommand).rejects("test"); |
| 20 | + const db = new PackageApiR2Bucket(); |
| 21 | + const [err] = await goTry(db.setPackageApi("foo", fromAny({ name: "foo" }))); |
| 22 | + expect(err).toBeDefined(); |
| 23 | +}); |
| 24 | + |
| 25 | +test("setPackageApi success", async () => { |
| 26 | + s3Mock.on(PutObjectCommand).resolves({}); |
| 27 | + const db = new PackageApiR2Bucket(); |
| 28 | + const [err] = await goTry(db.setPackageApi("foo", fromAny({ name: "foo" }))); |
| 29 | + expect(err).toBeUndefined(); |
| 30 | +}); |
| 31 | + |
| 32 | +test("getPackageApi fail", async () => { |
| 33 | + s3Mock.on(GetObjectCommand).rejects("test"); |
| 34 | + const db = new PackageApiR2Bucket(); |
| 35 | + const [err] = await goTry(db.getPackageApi("foo")); |
| 36 | + expect(err).toBeDefined(); |
| 37 | +}); |
| 38 | + |
| 39 | +test("getPackageApi success", async () => { |
| 40 | + const pkgApiGz = compressSync(strToU8(JSON.stringify({ name: "foo" }))); |
| 41 | + const stream = new Readable(); |
| 42 | + stream.push(pkgApiGz); |
| 43 | + stream.push(null); |
| 44 | + s3Mock.on(GetObjectCommand).resolves({ Body: sdkStreamMixin(stream) }); |
| 45 | + const db = new PackageApiR2Bucket(); |
| 46 | + const [err, pkgApi] = await goTry(db.getPackageApi("foo")); |
| 47 | + expect(err).toBeUndefined(); |
| 48 | + expect(pkgApi).toMatchInlineSnapshot(` |
| 49 | + { |
| 50 | + "name": "foo", |
| 51 | + } |
| 52 | + `); |
| 53 | +}); |
0 commit comments