2024-04-17 18:04:18 +00:00
|
|
|
import { afterEach, describe, expect, it, mock } from "bun:test";
|
|
|
|
import BotApiService from "src/services/BotApiService";
|
|
|
|
import GelbooruApiService from "src/services/GelbooruApiService";
|
2024-04-17 17:23:39 +00:00
|
|
|
import ImageService from "src/services/ImageService";
|
|
|
|
import { BotApiResponse } from "src/types/BotApiResponse";
|
2024-04-17 18:04:18 +00:00
|
|
|
import GelbooruApiResponse from "src/types/GelbooruServiceResponse";
|
|
|
|
import Image from "src/types/Image";
|
2024-04-12 11:42:25 +00:00
|
|
|
|
2024-04-15 08:54:01 +00:00
|
|
|
const imageServiceOriginal = ImageService;
|
|
|
|
const gelbooruApiServiceOriginal = GelbooruApiService;
|
|
|
|
const botApiServiceOriginal = BotApiService;
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/ImageService", () => ({
|
2024-04-15 08:54:01 +00:00
|
|
|
default: imageServiceOriginal,
|
|
|
|
}));
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/GelbooruApiService", () => ({
|
2024-04-15 08:54:01 +00:00
|
|
|
default: gelbooruApiServiceOriginal,
|
|
|
|
}));
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/BotApiService", () => ({
|
2024-04-15 08:54:01 +00:00
|
|
|
default: botApiServiceOriginal
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
2024-04-12 11:42:25 +00:00
|
|
|
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";
|
|
|
|
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/GelbooruApiService", () => {
|
2024-04-12 11:42:25 +00:00
|
|
|
let alreadyCalled = false;
|
|
|
|
return {
|
|
|
|
default: {
|
|
|
|
get: (): GelbooruApiResponse => {
|
|
|
|
if (alreadyCalled) {
|
|
|
|
return { posts: [{ url: UNIQUE_URL, tags: [] }] };
|
|
|
|
} else {
|
|
|
|
alreadyCalled = true;
|
|
|
|
return { posts: [{ url: REPEATED_URL, tags: [] }] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/BotApiService", () => ({
|
2024-04-12 11:42:25 +00:00
|
|
|
default: {
|
|
|
|
getAll: (): BotApiResponse => ({
|
|
|
|
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);
|
|
|
|
});
|
2024-04-15 08:54:01 +00:00
|
|
|
});
|