fix: preserve maxItems limit in unstable mode
This commit is contained in:
@@ -64,9 +64,23 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
canadaOnly,
|
||||
});
|
||||
|
||||
const results = hideUnstableResults || !maxItems ? items : items.slice(0, maxItems);
|
||||
let results;
|
||||
if (hideUnstableResults) {
|
||||
results = maxItems
|
||||
? {
|
||||
results: items.results.slice(0, maxItems),
|
||||
unstableResults: items.unstableResults.slice(0, maxItems),
|
||||
}
|
||||
: items;
|
||||
} else {
|
||||
results = maxItems ? items.slice(0, maxItems) : items;
|
||||
}
|
||||
|
||||
if (!results || results.length === 0)
|
||||
const isEmpty = hideUnstableResults
|
||||
? results.results.length === 0 && results.unstableResults.length === 0
|
||||
: !results || results.length === 0;
|
||||
|
||||
if (isEmpty)
|
||||
return Response.json(
|
||||
{ message: "Search didn't return any results!" },
|
||||
{ status: 404 },
|
||||
|
||||
@@ -109,6 +109,13 @@ describe("API routes", () => {
|
||||
test("ebayRoute forwards unstableFilter=true to core", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "item" }],
|
||||
unstableResults: [],
|
||||
}),
|
||||
);
|
||||
|
||||
await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&buyItNowOnly=true&unstableFilter=true",
|
||||
@@ -158,4 +165,149 @@ describe("API routes", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("facebookRoute does not forward unstableFilter when absent", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchFacebookItems).toHaveBeenCalledWith("laptop", 1, "toronto", 3);
|
||||
});
|
||||
|
||||
test("facebookRoute does not forward unstableFilter when false", async () => {
|
||||
const { facebookRoute } = await import("../src/routes/facebook");
|
||||
|
||||
await facebookRoute(
|
||||
new Request(
|
||||
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&unstableFilter=false",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchFacebookItems).toHaveBeenCalledWith("laptop", 1, "toronto", 3);
|
||||
});
|
||||
|
||||
test("ebayRoute does not forward unstableFilter when absent", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&buyItNowOnly=true",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, {
|
||||
minPrice: undefined,
|
||||
maxPrice: undefined,
|
||||
strictMode: false,
|
||||
exclusions: [],
|
||||
keywords: ["laptop"],
|
||||
buyItNowOnly: true,
|
||||
canadaOnly: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("ebayRoute does not forward unstableFilter when false", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&buyItNowOnly=true&unstableFilter=false",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, {
|
||||
minPrice: undefined,
|
||||
maxPrice: undefined,
|
||||
strictMode: false,
|
||||
exclusions: [],
|
||||
keywords: ["laptop"],
|
||||
buyItNowOnly: true,
|
||||
canadaOnly: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("kijijiRoute does not forward unstableFilter when absent", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
await kijijiRoute(
|
||||
new Request(
|
||||
"http://localhost/api/kijiji?q=laptop&maxPages=5",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchKijijiItems).toHaveBeenCalledWith(
|
||||
"laptop",
|
||||
4,
|
||||
"https://www.kijiji.ca",
|
||||
{
|
||||
location: undefined,
|
||||
category: undefined,
|
||||
keywords: undefined,
|
||||
sortBy: null,
|
||||
sortOrder: null,
|
||||
maxPages: 5,
|
||||
priceMin: undefined,
|
||||
priceMax: undefined,
|
||||
cookies: undefined,
|
||||
},
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
test("kijijiRoute does not forward unstableFilter when false", async () => {
|
||||
const { kijijiRoute } = await import("../src/routes/kijiji");
|
||||
|
||||
await kijijiRoute(
|
||||
new Request(
|
||||
"http://localhost/api/kijiji?q=laptop&maxPages=5&unstableFilter=false",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchKijijiItems).toHaveBeenCalledWith(
|
||||
"laptop",
|
||||
4,
|
||||
"https://www.kijiji.ca",
|
||||
{
|
||||
location: undefined,
|
||||
category: undefined,
|
||||
keywords: undefined,
|
||||
sortBy: null,
|
||||
sortOrder: null,
|
||||
maxPages: 5,
|
||||
priceMin: undefined,
|
||||
priceMax: undefined,
|
||||
cookies: undefined,
|
||||
},
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
test("ebayRoute applies maxItems in unstable mode", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "a" }, { title: "b" }, { title: "c" }],
|
||||
unstableResults: [{ title: "d" }, { title: "e" }],
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&unstableFilter=true&maxItems=2",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.results).toHaveLength(2);
|
||||
expect(body.unstableResults).toHaveLength(2);
|
||||
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