Fix & test /movies/random

This commit is contained in:
Zoe Roux 2025-01-13 12:52:39 +01:00
parent b6f996139f
commit 1cdb372079
No known key found for this signature in database
2 changed files with 17 additions and 5 deletions

View File

@ -155,18 +155,18 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
.get(
"random",
async ({ error, redirect }) => {
const [id] = await db
const [movie] = await db
.select({ id: shows.id })
.from(shows)
.where(eq(shows.kind, "movie"))
.orderBy(sql`random()`)
.limit(1);
if (!id)
if (!movie)
return error(404, {
status: 404,
message: "No movies in the database",
});
return redirect(`/movies/${id}`);
return redirect(`/movies/${movie.id}`);
},
{
detail: {
@ -175,7 +175,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
response: {
302: t.Void({
description:
"Redirected to the [/movies/id](#tag/movies/GET/movies/{id}) route.",
"Redirected to the [/movies/{id}](#tag/movies/GET/movies/{id}) route.",
}),
404: {
...KError,

View File

@ -6,8 +6,9 @@ import { shows } from "~/db/schema";
import { bubble } from "~/models/examples";
import { dune1984 } from "~/models/examples/dune-1984";
import { dune } from "~/models/examples/dune-2021";
import { getMovies, movieApp } from "./movies-helper";
import { getMovie, getMovies, movieApp } from "./movies-helper";
import type { Movie } from "~/models/movie";
import { isUuid } from "~/models/utils";
beforeAll(async () => {
await db.delete(shows);
@ -186,5 +187,16 @@ describe("Get all movies", () => {
body2.items[0].slug,
);
});
it("Get /random", async () => {
const resp = await movieApp.handle(
new Request("http://localhost/movies/random"),
);
expect(resp.status).toBe(302);
const location = resp.headers.get("location")!;
expect(location).toStartWith("/movies/");
const id = location.substring("/movies/".length);
expect(isUuid(id)).toBe(true);
});
});
});