fix: correct empty-result and maxItems handling in routes
This commit is contained in:
@@ -64,7 +64,7 @@ describe("API routes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("kijijiRoute ignores cookies query parameter", async () => {
|
||||
test("kijijiRoute passes cookies query parameter", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
await kijijiRoute(
|
||||
@@ -95,6 +95,13 @@ describe("API routes", () => {
|
||||
test("facebookRoute forwards unstableFilter=true to core", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
fetchFacebookItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "item" }],
|
||||
unstableResults: [],
|
||||
}),
|
||||
);
|
||||
|
||||
await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&unstableFilter=true",
|
||||
@@ -138,6 +145,13 @@ describe("API routes", () => {
|
||||
test("kijijiRoute forwards unstableFilter=true to core", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
fetchKijijiItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "item" }],
|
||||
unstableResults: [],
|
||||
}),
|
||||
);
|
||||
|
||||
await kijijiRoute(
|
||||
new Request(
|
||||
"http://localhost/api/kijiji?q=laptop&maxPages=5&unstableFilter=true",
|
||||
@@ -286,7 +300,112 @@ describe("API routes", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("ebayRoute applies maxItems in unstable mode", async () => {
|
||||
test("facebookRoute returns bucket shape when unstableFilter is enabled", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
fetchFacebookItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "a" }],
|
||||
unstableResults: [{ title: "b" }],
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&unstableFilter=true",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.results).toHaveLength(1);
|
||||
expect(body.unstableResults).toHaveLength(1);
|
||||
expect(body.results[0].title).toBe("a");
|
||||
expect(body.unstableResults[0].title).toBe("b");
|
||||
});
|
||||
|
||||
test("kijijiRoute returns bucket shape when unstableFilter is enabled", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
fetchKijijiItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "a" }],
|
||||
unstableResults: [{ title: "b" }],
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await kijijiRoute(
|
||||
new Request(
|
||||
"http://localhost/api/kijiji?q=laptop&maxPages=5&unstableFilter=true",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.results).toHaveLength(1);
|
||||
expect(body.unstableResults).toHaveLength(1);
|
||||
expect(body.results[0].title).toBe("a");
|
||||
expect(body.unstableResults[0].title).toBe("b");
|
||||
});
|
||||
|
||||
test("facebookRoute returns 404 when unstable results are empty", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
fetchFacebookItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [],
|
||||
unstableResults: [],
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&unstableFilter=true",
|
||||
),
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
const body = await response.json();
|
||||
expect(body.message).toBe("Search didn't return any results!");
|
||||
});
|
||||
|
||||
test("kijijiRoute returns 404 when unstable results are empty", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
fetchKijijiItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [],
|
||||
unstableResults: [],
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await kijijiRoute(
|
||||
new Request(
|
||||
"http://localhost/api/kijiji?q=laptop&maxPages=5&unstableFilter=true",
|
||||
),
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
const body = await response.json();
|
||||
expect(body.message).toBe("Search didn't return any results!");
|
||||
});
|
||||
|
||||
test("ebayRoute respects maxItems=0 in default mode", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
Promise.resolve([{ title: "a" }, { title: "b" }]),
|
||||
);
|
||||
|
||||
const response = await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&maxItems=0",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("ebayRoute limits total items with maxItems in unstable mode", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
@@ -298,16 +417,16 @@ describe("API routes", () => {
|
||||
|
||||
const response = await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&unstableFilter=true&maxItems=2",
|
||||
"http://localhost/api/ebay?q=laptop&unstableFilter=true&maxItems=4",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.results).toHaveLength(2);
|
||||
expect(body.unstableResults).toHaveLength(2);
|
||||
const total = body.results.length + body.unstableResults.length;
|
||||
expect(total).toBe(4);
|
||||
expect(body.results).toHaveLength(3);
|
||||
expect(body.unstableResults).toHaveLength(1);
|
||||
expect(body.results[0].title).toBe("a");
|
||||
expect(body.results[1].title).toBe("b");
|
||||
expect(body.unstableResults[0].title).toBe("d");
|
||||
expect(body.unstableResults[1].title).toBe("e");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user