fe-middleware/test/ImageService/ImageService.test.ts

44 lines
1.6 KiB
TypeScript

import { afterAll, describe, expect, it, jest, mock, spyOn } from "bun:test";
import Image from "src/types/Image";
import * as gelbooruApiService from "src/services/gelbooruApiService";
import * as botApiService from "src/services/botApiService";
import * as imageService from "src/services/imageService";
afterAll(() => {
jest.restoreAllMocks();
})
describe("the service is thread-safe", () => {
it("should run normally when 2 processes call the get() method with 1 remaining image in the queue", async () => {
// TODO
})
})
describe("endpoint gets a non repeated image", () => {
it("should return an image that is not in the response of the /images endpoint of the bot API", async () => {
const REPEATED_URL =
"https://fastly.picsum.photos/id/1/10/20.jpg?hmac=gY6PvUXFacKfYpBpTTVcNLxumpyMmoCamM-J5DOPwNc";
const UNIQUE_URL =
"https://fastly.picsum.photos/id/2/10/20.jpg?hmac=zy6lz21CuRIstr9ETx9h5AuoH50s_L2uIEct3dROpY8";
const gelbooruApiServiceGet = spyOn(gelbooruApiService, "get");
gelbooruApiServiceGet.mockImplementationOnce(async () => ({ posts: [{ url: UNIQUE_URL, tags: [] }] }));
gelbooruApiServiceGet.mockImplementation(async () => ({ posts: [{ url: REPEATED_URL, tags: [] }] }));
spyOn(botApiService, "getAll").mockImplementation(async () => ({
images: [
{
_id: "0",
url: REPEATED_URL,
status: "consumed",
tags: ["pokemon", "computer"],
__v: 0,
},
],
}));
const image: Image = await imageService.get();
expect(image.url).not.toBe(REPEATED_URL);
});
});