feat: create route to handle updating watch status in AniList

This commit is contained in:
2024-06-12 09:33:55 -04:00
parent 09ffff7c56
commit 0a859e0f16
8 changed files with 284 additions and 10 deletions

View File

@@ -0,0 +1,99 @@
import { beforeEach, describe, expect, it } from "bun:test";
import app from "~/index";
import { server } from "~/mocks";
import { getDb, resetDb } from "~/models/db";
import { tokenTable } from "~/models/schema";
server.listen();
console.error = () => {};
describe("requests the /watch-status route", () => {
const db = getDb({
TURSO_URL: "http://127.0.0.1:3000",
TURSO_AUTH_TOKEN: "asd",
});
beforeEach(async () => {
await resetDb();
});
it("saving title, deviceId in db, should succeed", async () => {
await db.insert(tokenTable).values({ deviceId: "123", token: "asd" });
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: 10,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: true });
expect(res.status).toBe(200);
});
it("saving title, deviceId not in db, should fail", async () => {
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: 10,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: false });
expect(res.status).toBe(500);
});
it("saving title, Anilist request fails, should succeed", async () => {
await db.insert(tokenTable).values({ deviceId: "123", token: "asd" });
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: -1,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: true });
expect(res.status).toBe(200);
});
});