diff --git a/api/tests/helpers/index.ts b/api/tests/helpers/index.ts index e2898d52..f9111196 100644 --- a/api/tests/helpers/index.ts +++ b/api/tests/helpers/index.ts @@ -1,5 +1,6 @@ export * from "./movies-helper"; export * from "./series-helper"; +export * from "./shows-helper"; export * from "./studio-helper"; export * from "./staff-helper"; export * from "./videos-helper"; diff --git a/api/tests/helpers/movies-helper.ts b/api/tests/helpers/movies-helper.ts index 85b09916..f0b1711f 100644 --- a/api/tests/helpers/movies-helper.ts +++ b/api/tests/helpers/movies-helper.ts @@ -1,6 +1,7 @@ import { buildUrl } from "tests/utils"; import { app } from "~/base"; import type { SeedMovie } from "~/models/movie"; +import type { MovieWatchStatus } from "~/models/watchlist"; import { getJwtHeaders } from "./jwt"; export const getMovie = async ( @@ -66,3 +67,21 @@ export const createMovie = async (movie: SeedMovie) => { const body = await resp.json(); return [resp, body] as const; }; + +export const setMovieStatus = async ( + id: string, + status: Omit, +) => { + const resp = await app.handle( + new Request(buildUrl(`movies/${id}/watchstatus`), { + method: "POST", + body: JSON.stringify(status), + headers: { + "Content-Type": "application/json", + ...(await getJwtHeaders()), + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/helpers/series-helper.ts b/api/tests/helpers/series-helper.ts index 7004278e..843ff375 100644 --- a/api/tests/helpers/series-helper.ts +++ b/api/tests/helpers/series-helper.ts @@ -2,6 +2,7 @@ import { buildUrl } from "tests/utils"; import { app } from "~/base"; import type { SeedSerie } from "~/models/serie"; import { getJwtHeaders } from "./jwt"; +import { SerieWatchStatus } from "~/models/watchlist"; export const createSerie = async (serie: SeedSerie) => { const resp = await app.handle( @@ -162,3 +163,21 @@ export const getNews = async ({ const body = await resp.json(); return [resp, body] as const; }; + +export const setSerieStatus = async ( + id: string, + status: SerieWatchStatus +) => { + const resp = await app.handle( + new Request(buildUrl(`movies/${id}/watchstatus`), { + method: "POST", + body: JSON.stringify(status), + headers: { + "Content-Type": "application/json", + ...(await getJwtHeaders()), + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/helpers/shows-helper.ts b/api/tests/helpers/shows-helper.ts new file mode 100644 index 00000000..f1a138dd --- /dev/null +++ b/api/tests/helpers/shows-helper.ts @@ -0,0 +1,60 @@ +import { buildUrl } from "tests/utils"; +import { app } from "~/base"; +import { getJwtHeaders } from "./jwt"; + +export const getShows = async ({ + langs, + ...query +}: { + filter?: string; + limit?: number; + after?: string; + sort?: string | string[]; + query?: string; + langs?: string; + preferOriginal?: boolean; +}) => { + const resp = await app.handle( + new Request(buildUrl("shows", query), { + method: "GET", + headers: langs + ? { + "Accept-Language": langs, + ...(await getJwtHeaders()), + } + : await getJwtHeaders(), + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; + +export const getWatchlist = async ( + id: string, + { + langs, + ...query + }: { + filter?: string; + limit?: number; + after?: string; + sort?: string | string[]; + query?: string; + langs?: string; + preferOriginal?: boolean; + }, +) => { + const resp = await app.handle( + new Request(buildUrl(`profiles/${id}/watchlist`, query), { + method: "GET", + headers: langs + ? { + "Accept-Language": langs, + ...(await getJwtHeaders()), + } + : await getJwtHeaders(), + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/movies/watchstatus.test.ts b/api/tests/movies/watchstatus.test.ts new file mode 100644 index 00000000..98caa014 --- /dev/null +++ b/api/tests/movies/watchstatus.test.ts @@ -0,0 +1,94 @@ +import { beforeAll, describe, expect, it } from "bun:test"; +import { + createMovie, + getMovie, + getShows, + getWatchlist, + setMovieStatus, +} from "tests/helpers"; +import { expectStatus } from "tests/utils"; +import { db } from "~/db"; +import { shows } from "~/db/schema"; +import { bubble } from "~/models/examples"; + +beforeAll(async () => { + await db.delete(shows); + const [ret, body] = await createMovie(bubble); + expectStatus(ret, body).toBe(201); +}); + +describe("Set & get watch status", () => { + it("Creates watchlist entry", async () => { + let [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(0); + + const [r, b] = await setMovieStatus(bubble.slug, { + status: "completed", + completedAt: "2024-12-21", + score: 85, + }); + expectStatus(r, b).toBe(201); + + [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 100, + }); + }); + + it("Edit watchlist entry", async () => { + let [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + + const [r, b] = await setMovieStatus(bubble.slug, { + status: "rewatching", + // we still need to specify all values + completedAt: "2024-12-21", + score: 85, + }); + expectStatus(r, b).toBe(201); + + [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); + + it("Return watchstatus in /shows", async () => { + const [resp, body] = await getShows({}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); + + it("Return watchstatus in /movies/:id", async () => { + const [resp, body] = await getMovie(bubble.slug, {}); + expectStatus(resp, body).toBe(200); + expect(body.slug).toBe(bubble.slug); + expect(body.watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); +});