diff --git a/api/src/models/utils/page.ts b/api/src/models/utils/page.ts index daad081e..c7a18d1b 100644 --- a/api/src/models/utils/page.ts +++ b/api/src/models/utils/page.ts @@ -18,12 +18,17 @@ export const createPage = ( { url, sort, limit }: { url: string; sort: Sort; limit: number }, ) => { let next: string | null = null; + const uri = new URL(url); + + if (sort.random) { + uri.searchParams.set("sort", `random:${sort.random.seed}`); + url = uri.toString(); + } // we can't know for sure if there's a next page when the current page is full. // maybe the next page is empty, this is a bit weird but it allows us to handle pages // without making a new request to the db so it's fine. if (items.length === limit && limit > 0) { - const uri = new URL(url); uri.searchParams.set("after", generateAfter(items[items.length - 1], sort)); next = uri.toString(); } diff --git a/api/tests/movies/get-all-movies.test.ts b/api/tests/movies/get-all-movies.test.ts index c725d47e..e91c7be8 100644 --- a/api/tests/movies/get-all-movies.test.ts +++ b/api/tests/movies/get-all-movies.test.ts @@ -125,7 +125,7 @@ describe("Get all movies", () => { describe("Random sort", () => { it("No limit, compare order with same seeds", async () => { // First query - let [resp1, body1] = await getMovies({ + const [resp1, body1] = await getMovies({ sort: "random:100", }); expectStatus(resp1, body1).toBe(200); @@ -133,7 +133,7 @@ describe("Get all movies", () => { const items1Ids = items1.map(({ id }) => id); // Second query - let [resp2, body2] = await getMovies({ + const [resp2, body2] = await getMovies({ sort: "random:100", }); expectStatus(resp2, body2).toBe(200); @@ -170,5 +170,21 @@ describe("Get all movies", () => { expect(items.length).toBe(1); expect(items[0].id).toBe(expectedIds[1]); }); + it("Limit 1, pages 1 and 2, no seed ", async () => { + const [resp, body] = await getMovies({ + sort: "random", + limit: 2, + }); + expectStatus(resp, body).toBe(200); + + const resp2 = await movieApp.handle(new Request(body.next)); + const body2 = await resp2.json(); + expectStatus(resp2, body).toBe(200); + + expect(body2.items.length).toBe(1); + expect(body.items.map((x: Movie) => x.slug)).not.toContain( + body2.items[0].slug, + ); + }); }); });