Add seed in response if it was not specified

This commit is contained in:
Zoe Roux 2025-01-12 16:31:43 +01:00
parent b9da57fd88
commit 46570410ea
No known key found for this signature in database
2 changed files with 24 additions and 3 deletions

View File

@ -18,12 +18,17 @@ export const createPage = <T>(
{ url, sort, limit }: { url: string; sort: Sort<any, any>; 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();
}

View File

@ -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,
);
});
});
});