mirror of
https://github.com/versia-pub/server.git
synced 2026-04-28 05:09:16 +02:00
refactor: ♻️ Rewrite media management code
This commit is contained in:
parent
d09f74e58a
commit
faf829437d
34 changed files with 1195 additions and 904 deletions
71
classes/media/preprocessors/blurhash.test.ts
Normal file
71
classes/media/preprocessors/blurhash.test.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { beforeEach, describe, expect, it, mock } from "bun:test";
|
||||
import sharp from "sharp";
|
||||
import { BlurhashPreprocessor } from "./blurhash";
|
||||
|
||||
describe("BlurhashPreprocessor", () => {
|
||||
let preprocessor: BlurhashPreprocessor;
|
||||
|
||||
beforeEach(() => {
|
||||
preprocessor = new BlurhashPreprocessor();
|
||||
});
|
||||
|
||||
it("should calculate blurhash for a valid image", async () => {
|
||||
const inputBuffer = await sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 3,
|
||||
background: { r: 255, g: 0, b: 0 },
|
||||
},
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
|
||||
const inputFile = new File([inputBuffer], "test.png", {
|
||||
type: "image/png",
|
||||
});
|
||||
const result = await preprocessor.process(inputFile);
|
||||
|
||||
expect(result.file).toBe(inputFile);
|
||||
expect(result.blurhash).toBeTypeOf("string");
|
||||
expect(result.blurhash).not.toBe("");
|
||||
});
|
||||
|
||||
it("should return null blurhash for an invalid image", async () => {
|
||||
const invalidFile = new File(["invalid image data"], "invalid.png", {
|
||||
type: "image/png",
|
||||
});
|
||||
const result = await preprocessor.process(invalidFile);
|
||||
|
||||
expect(result.file).toBe(invalidFile);
|
||||
expect(result.blurhash).toBeNull();
|
||||
});
|
||||
|
||||
it("should handle errors during blurhash calculation", async () => {
|
||||
const inputBuffer = await sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 3,
|
||||
background: { r: 255, g: 0, b: 0 },
|
||||
},
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
|
||||
const inputFile = new File([inputBuffer], "test.png", {
|
||||
type: "image/png",
|
||||
});
|
||||
|
||||
mock.module("blurhash", () => ({
|
||||
encode: () => {
|
||||
throw new Error("Test error");
|
||||
},
|
||||
}));
|
||||
|
||||
const result = await preprocessor.process(inputFile);
|
||||
|
||||
expect(result.file).toBe(inputFile);
|
||||
expect(result.blurhash).toBeNull();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue