feat: add extensive test suite
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
"use server";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { adminCheck } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import type { TAccessKey } from "@/lib/db/schema";
|
||||
import { slipSync } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { retrieveMobileUploadService } from "./service";
|
||||
|
||||
export async function startMobileUpload() {
|
||||
return await db
|
||||
@@ -14,18 +16,13 @@ export async function startMobileUpload() {
|
||||
}
|
||||
|
||||
export async function retrieveMobileUpload(accessKey: TAccessKey) {
|
||||
const object = await db
|
||||
.delete(slipSync)
|
||||
.where(eq(slipSync.accessKey, accessKey))
|
||||
.returning()
|
||||
.then((r) => r.pop()!);
|
||||
const fd = new FormData();
|
||||
fd.set(
|
||||
"file",
|
||||
new File([Buffer.from(object.data!)], object.name!, {
|
||||
type: object.type!,
|
||||
}),
|
||||
);
|
||||
fd.set("name", object.name!)
|
||||
return fd;
|
||||
return retrieveMobileUploadService(accessKey, {
|
||||
adminCheck,
|
||||
consume: async (key) =>
|
||||
db
|
||||
.delete(slipSync)
|
||||
.where(eq(slipSync.accessKey, key as TAccessKey))
|
||||
.returning()
|
||||
.then((rows) => rows.pop()),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { type MobileUpload, retrieveMobileUploadService } from "./service";
|
||||
|
||||
describe("retrieveMobileUploadService", () => {
|
||||
test("requires admin before consuming an upload", async () => {
|
||||
const consume = mock(async (_key: string) => undefined);
|
||||
expect(
|
||||
retrieveMobileUploadService("key", {
|
||||
adminCheck: async () => false,
|
||||
consume,
|
||||
}),
|
||||
).rejects.toBe("Unauthorized");
|
||||
expect(consume).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("returns the uploaded file once and rejects later retrieval", async () => {
|
||||
let upload: MobileUpload | undefined = {
|
||||
data: Uint8Array.from([1, 2, 3]),
|
||||
name: "slip.png",
|
||||
type: "image/png",
|
||||
};
|
||||
const dependencies = {
|
||||
adminCheck: async () => true,
|
||||
consume: async () => {
|
||||
const result = upload;
|
||||
upload = undefined;
|
||||
return result;
|
||||
},
|
||||
};
|
||||
|
||||
const formData = await retrieveMobileUploadService("key", dependencies);
|
||||
const file = formData.get("file");
|
||||
expect(file).toBeInstanceOf(File);
|
||||
expect((file as File).name).toBe("slip.png");
|
||||
expect((file as File).type).toBe("image/png");
|
||||
expect(formData.get("name")).toBe("slip.png");
|
||||
expect(retrieveMobileUploadService("key", dependencies)).rejects.toThrow(
|
||||
"already retrieved",
|
||||
);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ data: null, name: "slip.png", type: "image/png" },
|
||||
{ data: Uint8Array.from([1]), name: null, type: "image/png" },
|
||||
{ data: Uint8Array.from([1]), name: "slip.png", type: null },
|
||||
])("rejects incomplete upload metadata: %p", async (upload) => {
|
||||
expect(
|
||||
retrieveMobileUploadService("key", {
|
||||
adminCheck: async () => true,
|
||||
consume: async () => upload,
|
||||
}),
|
||||
).rejects.toThrow("unavailable");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
export type MobileUpload = {
|
||||
data: Uint8Array | Buffer | null;
|
||||
name: string | null;
|
||||
type: string | null;
|
||||
};
|
||||
|
||||
export async function retrieveMobileUploadService(
|
||||
accessKey: string,
|
||||
dependencies: {
|
||||
adminCheck: () => Promise<unknown>;
|
||||
consume: (accessKey: string) => Promise<MobileUpload | undefined>;
|
||||
},
|
||||
) {
|
||||
if (!(await dependencies.adminCheck())) throw "Unauthorized";
|
||||
|
||||
const upload = await dependencies.consume(accessKey);
|
||||
if (!upload?.data || !upload.name || !upload.type) {
|
||||
throw new Error("Mobile upload is unavailable or already retrieved.");
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.set(
|
||||
"file",
|
||||
new File([Uint8Array.from(upload.data)], upload.name, {
|
||||
type: upload.type,
|
||||
}),
|
||||
);
|
||||
formData.set("name", upload.name);
|
||||
return formData;
|
||||
}
|
||||
Reference in New Issue
Block a user