feat: add maxItems support to ebay scraper
This commit is contained in:
@@ -65,6 +65,7 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
keywords,
|
||||
buyItNowOnly,
|
||||
canadaOnly,
|
||||
maxItems,
|
||||
};
|
||||
const items = hideUnstableResults
|
||||
? await fetchEbayItems(SEARCH_QUERY, 1, opts, {
|
||||
@@ -82,13 +83,7 @@ export async function ebayRoute(req: Request): Promise<Response> {
|
||||
{ status: 404 },
|
||||
);
|
||||
|
||||
const results = hideUnstableResults
|
||||
? items
|
||||
: maxItems !== undefined
|
||||
? items.slice(0, maxItems)
|
||||
: items;
|
||||
|
||||
return Response.json(results, { status: 200 });
|
||||
return Response.json(items, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("eBay scraping error:", error);
|
||||
const errorMessage =
|
||||
|
||||
@@ -382,21 +382,20 @@ describe("API routes", () => {
|
||||
expect(body.message).toBe("Search didn't return any results!");
|
||||
});
|
||||
|
||||
test("ebayRoute respects maxItems=0 in default mode", async () => {
|
||||
test("ebayRoute forwards maxItems to core in default mode", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
Promise.resolve([{ title: "a" }, { title: "b" }]),
|
||||
Promise.resolve([{ title: "a" }]),
|
||||
);
|
||||
|
||||
const response = await ebayRoute(
|
||||
await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&maxItems=0",
|
||||
"http://localhost/api/ebay?q=laptop&maxItems=2",
|
||||
),
|
||||
);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveLength(0);
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, expect.objectContaining({ maxItems: 2 }));
|
||||
});
|
||||
|
||||
test("ebayRoute passes through scraper payload unchanged in unstable mode", async () => {
|
||||
@@ -420,6 +419,30 @@ describe("API routes", () => {
|
||||
expect(body.unstableResults).toHaveLength(2);
|
||||
expect(body.results[0].title).toBe("a");
|
||||
expect(body.unstableResults[0].title).toBe("d");
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, expect.objectContaining({ maxItems: 4 }), {
|
||||
hideUnstableResults: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("ebayRoute forwards maxItems to core in unstable mode", async () => {
|
||||
const { ebayRoute } = await import("../src/routes/ebay");
|
||||
|
||||
fetchEbayItems.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
results: [{ title: "a" }],
|
||||
unstableResults: [{ title: "b" }],
|
||||
}),
|
||||
);
|
||||
|
||||
await ebayRoute(
|
||||
new Request(
|
||||
"http://localhost/api/ebay?q=laptop&unstableFilter=true&maxItems=2",
|
||||
),
|
||||
);
|
||||
|
||||
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, expect.objectContaining({ maxItems: 2 }), {
|
||||
hideUnstableResults: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("ebayRoute returns 404 when unstable results are empty", async () => {
|
||||
|
||||
Reference in New Issue
Block a user