66 lines
2.4 KiB
JavaScript
66 lines
2.4 KiB
JavaScript
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
|
import getResolvedSrc from "../api/utils/getResolvedSrc.js";
|
|
import { setup, teardown } from "./commons.js";
|
|
import path from "node:path";
|
|
import fs from "node:fs";
|
|
import sharp from "sharp";
|
|
|
|
describe("getResolvedSrc", () => {
|
|
beforeAll(setup);
|
|
afterAll(teardown);
|
|
|
|
it("should return the default image for a non-existent URL", async () => {
|
|
const src = "https://example.com/non-existent-image.jpg";
|
|
const result = await getResolvedSrc(src);
|
|
expect(result.src.replace(/\\/g, "/")).toContain(
|
|
"/public/images/default.png"
|
|
);
|
|
});
|
|
|
|
it("should download and cache a remote image", async () => {
|
|
const src = "https://picsum.photos/200/300";
|
|
const result = await getResolvedSrc(src);
|
|
|
|
const expectedCachePath = path.join(process.cwd(), result.src);
|
|
|
|
expect(fs.existsSync(expectedCachePath)).toBe(true);
|
|
expect(result.base).toBe("300"); // picsum photo name seems to be the height
|
|
}, 15000); // Increase timeout for remote image download
|
|
|
|
it("should download an image and verify its dimensions", async () => {
|
|
const src =
|
|
"https://fastly.picsum.photos/id/343/200/300.jpg?hmac=_7ttvLezG-XONDvp0ILwQCv50ivQa_oewm7m6xV2uZA";
|
|
const result = await getResolvedSrc(src);
|
|
|
|
const imagePath = path.join(process.cwd(), result.src);
|
|
expect(fs.existsSync(imagePath)).toBe(true);
|
|
|
|
const metadata = await sharp(imagePath).metadata();
|
|
expect(metadata.width).toBe(200);
|
|
}, 15000);
|
|
|
|
it("should download another image and verify its height", async () => {
|
|
const src =
|
|
"https://assets.files.polymech.info//products/sheetpress/cassandra-edczmax-rc2/media/gallery/latest_controller.jpg";
|
|
const result = await getResolvedSrc(src);
|
|
|
|
const imagePath = path.join(process.cwd(), result.src);
|
|
expect(fs.existsSync(imagePath)).toBe(true);
|
|
|
|
const metadata = await sharp(imagePath).metadata();
|
|
expect(metadata.height).toBe(1980);
|
|
}, 15000);
|
|
|
|
it("should handle URLs with no file extension", async () => {
|
|
const src =
|
|
"https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=800&h=400&fit=crop";
|
|
const result = await getResolvedSrc(src);
|
|
|
|
const imagePath = path.join(process.cwd(), result.src);
|
|
expect(fs.existsSync(imagePath)).toBe(true);
|
|
|
|
const metadata = await sharp(imagePath).metadata();
|
|
expect(metadata.format).toBe("jpeg");
|
|
}, 15000);
|
|
});
|