feat: expose unstable mode in api routes

This commit is contained in:
2026-04-27 02:49:35 -04:00
parent 224e83ac4c
commit 3c38232cd5
4 changed files with 154 additions and 18 deletions

View File

@@ -2,10 +2,12 @@ import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
const fetchFacebookItems = mock(() => Promise.resolve([{ title: "item" }]));
const fetchEbayItems = mock(() => Promise.resolve([{ title: "item" }]));
const fetchKijijiItems = mock(() => Promise.resolve([{ title: "item" }]));
mock.module("@marketplace-scrapers/core", () => ({
fetchFacebookItems,
fetchEbayItems,
fetchKijijiItems,
}));
describe("API routes", () => {
@@ -18,11 +20,16 @@ describe("API routes", () => {
fetchEbayItems.mockImplementation(() =>
Promise.resolve([{ title: "item" }]),
);
fetchKijijiItems.mockReset();
fetchKijijiItems.mockImplementation(() =>
Promise.resolve([{ title: "item" }]),
);
});
afterEach(() => {
fetchFacebookItems.mockClear();
fetchEbayItems.mockClear();
fetchKijijiItems.mockClear();
});
test("facebookRoute ignores cookies query parameter", async () => {
@@ -56,4 +63,99 @@ describe("API routes", () => {
canadaOnly: true,
});
});
test("kijijiRoute ignores cookies query parameter", async () => {
const { kijijiRoute } = await import("../src/routes/kijiji");
await kijijiRoute(
new Request(
"http://localhost/api/kijiji?q=laptop&cookies=s%3D1&maxPages=3",
),
);
expect(fetchKijijiItems).toHaveBeenCalledWith(
"laptop",
4,
"https://www.kijiji.ca",
{
location: undefined,
category: undefined,
keywords: undefined,
sortBy: null,
sortOrder: null,
maxPages: 3,
priceMin: undefined,
priceMax: undefined,
cookies: "s=1",
},
{},
);
});
test("facebookRoute forwards unstableFilter=true to core", async () => {
const { facebookRoute } = await import("../src/routes/facebook");
await facebookRoute(
new Request(
"http://localhost/api/facebook?q=laptop&location=toronto&maxItems=3&unstableFilter=true",
),
);
expect(fetchFacebookItems).toHaveBeenCalledWith("laptop", 1, "toronto", 3, {
hideUnstableResults: true,
});
});
test("ebayRoute forwards unstableFilter=true to core", async () => {
const { ebayRoute } = await import("../src/routes/ebay");
await ebayRoute(
new Request(
"http://localhost/api/ebay?q=laptop&buyItNowOnly=true&unstableFilter=true",
),
);
expect(fetchEbayItems).toHaveBeenCalledWith("laptop", 1, {
minPrice: undefined,
maxPrice: undefined,
strictMode: false,
exclusions: [],
keywords: ["laptop"],
buyItNowOnly: true,
canadaOnly: true,
}, {
hideUnstableResults: true,
});
});
test("kijijiRoute forwards unstableFilter=true to core", async () => {
const { kijijiRoute } = await import("../src/routes/kijiji");
await kijijiRoute(
new Request(
"http://localhost/api/kijiji?q=laptop&maxPages=5&unstableFilter=true",
),
);
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,
},
{},
{
hideUnstableResults: true,
},
);
});
});