refactor: make cookie loading env-only
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
||||
import {
|
||||
ensureFacebookCookies,
|
||||
extractFacebookItemData,
|
||||
extractFacebookMarketplaceData,
|
||||
fetchFacebookItem,
|
||||
@@ -84,15 +85,58 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
expect(result[1].name).toBe("xs");
|
||||
expect(result[1].value).toBe("abc");
|
||||
});
|
||||
|
||||
test("should load Facebook cookies from FACEBOOK_COOKIE env var", async () => {
|
||||
const previous = process.env.FACEBOOK_COOKIE;
|
||||
process.env.FACEBOOK_COOKIE = "c_user=123; xs=abc";
|
||||
|
||||
try {
|
||||
const cookies = await ensureFacebookCookies();
|
||||
|
||||
expect(cookies.map((cookie) => cookie.name)).toEqual(["c_user", "xs"]);
|
||||
} finally {
|
||||
if (previous === undefined) {
|
||||
delete process.env.FACEBOOK_COOKIE;
|
||||
} else {
|
||||
process.env.FACEBOOK_COOKIE = previous;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("should reject missing Facebook auth env var", async () => {
|
||||
const previous = process.env.FACEBOOK_COOKIE;
|
||||
delete process.env.FACEBOOK_COOKIE;
|
||||
|
||||
try {
|
||||
await expect(ensureFacebookCookies()).rejects.toThrow(
|
||||
"Provide cookies via FACEBOOK_COOKIE environment variable as a raw Cookie header string",
|
||||
);
|
||||
} finally {
|
||||
if (previous !== undefined) {
|
||||
process.env.FACEBOOK_COOKIE = previous;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Facebook Item Fetching", () => {
|
||||
describe("fetchFacebookItem", () => {
|
||||
const mockCookies = JSON.stringify([
|
||||
{ name: "c_user", value: "12345", domain: ".facebook.com" },
|
||||
{ name: "xs", value: "abc123", domain: ".facebook.com" },
|
||||
]);
|
||||
const mockCookies = "c_user=12345; xs=abc123";
|
||||
let previousCookie: string | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
previousCookie = process.env.FACEBOOK_COOKIE;
|
||||
process.env.FACEBOOK_COOKIE = mockCookies;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (previousCookie === undefined) {
|
||||
delete process.env.FACEBOOK_COOKIE;
|
||||
} else {
|
||||
process.env.FACEBOOK_COOKIE = previousCookie;
|
||||
}
|
||||
});
|
||||
|
||||
test("should handle authentication errors", async () => {
|
||||
global.fetch = mock(() =>
|
||||
@@ -234,16 +278,6 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
expect(result?.listingStatus).toBe("SOLD");
|
||||
});
|
||||
|
||||
test("should handle missing authentication cookies", async () => {
|
||||
// Use a test-specific cookie file that doesn't exist
|
||||
const testCookiePath = "./cookies/facebook-test.json";
|
||||
|
||||
// Test with no cookies available (test file doesn't exist)
|
||||
await expect(
|
||||
fetchFacebookItem("123", undefined, testCookiePath),
|
||||
).rejects.toThrow("No valid Facebook cookies found");
|
||||
});
|
||||
|
||||
test("should handle successful item extraction", async () => {
|
||||
const mockData = {
|
||||
require: [
|
||||
|
||||
Reference in New Issue
Block a user