server/packages/media-manager/tests/media-manager.test.ts

66 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-03-09 00:14:45 +01:00
// FILEPATH: /home/jessew/Dev/lysand/packages/media-manager/media-converter.test.ts
2024-04-07 07:30:49 +02:00
import { beforeEach, describe, expect, it } from "bun:test";
import { ConvertableMediaFormats, MediaConverter } from "../media-converter";
2024-03-09 00:14:45 +01:00
describe("MediaConverter", () => {
2024-04-07 07:30:49 +02:00
let mediaConverter: MediaConverter;
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
beforeEach(() => {
mediaConverter = new MediaConverter(
ConvertableMediaFormats.JPG,
ConvertableMediaFormats.PNG,
);
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
it("should initialize with correct formats", () => {
expect(mediaConverter.fromFormat).toEqual(ConvertableMediaFormats.JPG);
expect(mediaConverter.toFormat).toEqual(ConvertableMediaFormats.PNG);
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
it("should check if media is convertable", () => {
expect(mediaConverter.isConvertable()).toBe(true);
mediaConverter.toFormat = ConvertableMediaFormats.JPG;
expect(mediaConverter.isConvertable()).toBe(false);
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
it("should replace file name extension", () => {
const fileName = "test.jpg";
const expectedFileName = "test.png";
// Written like this because it's a private function
expect(mediaConverter.getReplacedFileName(fileName)).toEqual(
expectedFileName,
);
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
describe("Filename extractor", () => {
it("should extract filename from path", () => {
const path = "path/to/test.jpg";
const expectedFileName = "test.jpg";
expect(mediaConverter.extractFilenameFromPath(path)).toEqual(
expectedFileName,
);
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
it("should handle escaped slashes", () => {
const path = "path/to/test\\/test.jpg";
const expectedFileName = "test\\/test.jpg";
expect(mediaConverter.extractFilenameFromPath(path)).toEqual(
expectedFileName,
);
});
});
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
it("should convert media", async () => {
const file = Bun.file(`${__dirname}/megamind.jpg`);
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
const convertedFile = await mediaConverter.convert(
file as unknown as File,
);
2024-03-09 00:14:45 +01:00
2024-04-07 07:30:49 +02:00
expect(convertedFile.name).toEqual("megamind.png");
expect(convertedFile.type).toEqual(
`image/${ConvertableMediaFormats.PNG}`,
);
});
2024-03-09 00:14:45 +01:00
});