fix(core): handle partial listing data

This commit is contained in:
2026-04-28 21:34:45 -04:00
parent 7966073bf8
commit 3fe5fdb63f
10 changed files with 150 additions and 124 deletions

View File

@@ -15,7 +15,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
process.env.FACEBOOK_COOKIE = facebookCookie;
global.fetch = mock(() => {
throw new Error("fetch should be mocked in individual tests");
});
}) as unknown as typeof fetch;
});
afterEach(() => {
@@ -69,11 +69,11 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("iPhone", 1, "toronto", 25);
expect(results).toHaveLength(1);
expect(results[0].title).toBe("iPhone 13");
expect(results[0]?.title).toBe("iPhone 13");
});
test("should filter out items without price", async () => {
@@ -135,11 +135,11 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(results).toHaveLength(1);
expect(results[0].title).toBe("With Price");
expect(results[0]?.title).toBe("With Price");
});
test("should respect MAX_ITEMS parameter", async () => {
@@ -190,7 +190,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 5);
expect(results).toHaveLength(5);
@@ -231,7 +231,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems(
"nonexistent query",
@@ -252,7 +252,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(results).toEqual([]);
@@ -281,7 +281,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("lamp", 1, "toronto", 25);
expect(results).toEqual([]);
@@ -322,14 +322,16 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("lamp", 1, "toronto", 25);
expect(results).toEqual([]);
});
test("should handle network errors", async () => {
global.fetch = mock(() => Promise.reject(new Error("Network error")));
global.fetch = mock(() =>
Promise.reject(new Error("Network error")),
) as unknown as typeof fetch;
await expect(
fetchFacebookItems("test", 1, "toronto", 25),
@@ -400,7 +402,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
});
});
}) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(attempts).toBe(2);
@@ -473,13 +475,13 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("cars", 1, "toronto", 25);
expect(results).toHaveLength(2);
// Both should be classified as "item" type in search results (vehicle detection is for item details)
expect(results[0].title).toBe("2006 Honda Civic");
expect(results[1].title).toBe("iPhone 13");
expect(results[0]?.title).toBe("2006 Honda Civic");
expect(results[1]?.title).toBe("iPhone 13");
});
});
@@ -542,7 +544,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems(
"nintendo switch",
@@ -551,8 +553,8 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
25,
);
expect(results).toHaveLength(1);
expect(results[0].title).toBe("Nintendo Switch");
expect(results[0].categoryId).toBe("479353692612078");
expect(results[0]?.title).toBe("Nintendo Switch");
expect(results[0]?.categoryId).toBe("479353692612078");
});
test("should handle home goods/furniture listings", async () => {
@@ -613,12 +615,12 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("table", 1, "toronto", 25);
expect(results).toHaveLength(1);
expect(results[0].title).toBe("Dining Table");
expect(results[0].categoryId).toBe("1569171756675761");
expect(results[0]?.title).toBe("Dining Table");
expect(results[0]?.categoryId).toBe("1569171756675761");
});
});
@@ -635,7 +637,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(results).toEqual([]);
@@ -651,7 +653,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(results).toEqual([]);
@@ -667,7 +669,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const results = await fetchFacebookItems("test", 1, "toronto", 25);
expect(results).toEqual([]);
@@ -708,7 +710,7 @@ describe("Facebook Marketplace Scraper Integration Tests", () => {
get: () => null,
},
}),
);
) as unknown as typeof fetch;
const result = await fetchFacebookItem("123");
expect(result).toBeNull();