refactor: make ebay auth env-only

This commit is contained in:
2026-04-21 21:46:40 -04:00
parent a7a5eca7ad
commit 918ee92441
2 changed files with 50 additions and 22 deletions

View File

@@ -0,0 +1,41 @@
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
import fetchEbayItems from "../src/scrapers/ebay";
const originalFetch = global.fetch;
const originalWarn = console.warn;
describe("eBay Scraper Cookie Handling", () => {
beforeEach(() => {
global.fetch = mock(() =>
Promise.resolve({
ok: true,
text: () => Promise.resolve("<html><body></body></html>"),
}),
) as typeof fetch;
});
afterEach(() => {
global.fetch = originalFetch;
console.warn = originalWarn;
delete process.env.EBAY_COOKIE;
});
test("should ignore request cookie overrides and rely on EBAY_COOKIE", async () => {
const warnMock = mock(() => {});
console.warn = warnMock;
await fetchEbayItems("laptop", 1000, {
cookies: "s=from-request",
});
expect(global.fetch).toHaveBeenCalledTimes(1);
const [, init] = (global.fetch as ReturnType<typeof mock>).mock.calls[0];
const headers = (init as RequestInit).headers as Record<string, string>;
expect(headers.Cookie).toBeUndefined();
expect(warnMock).toHaveBeenCalledWith(
"No valid eBay cookies found in EBAY_COOKIE. eBay may block requests without a raw Cookie header string.",
);
});
});