mirror of
https://gitlab.com/Zer0interrupt/recipe-manager.git
synced 2026-11-01 23:24:11 -05:00
64 lines
2.7 KiB
JavaScript
64 lines
2.7 KiB
JavaScript
const assert = require('assert');
|
|
|
|
// 1. Test URL detection helper
|
|
function isFacebookUrl(url) {
|
|
try {
|
|
const parsed = new URL(url);
|
|
const host = parsed.hostname.toLowerCase();
|
|
const isFb = host.includes('facebook.com') || host.includes('fb.watch') || host.includes('fb.com');
|
|
if (!isFb) return false;
|
|
return host.includes('fb.watch') ||
|
|
parsed.pathname.includes('/reel') ||
|
|
parsed.pathname.includes('/watch') ||
|
|
parsed.pathname.includes('/videos') ||
|
|
parsed.pathname.includes('/share/');
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function cleanSocialCaption(raw) {
|
|
if (!raw) return '';
|
|
let text = raw.replace(/(?:\s*#[^\s#]+)+\s*$/g, '').trim();
|
|
text = text.replace(/[\uFE00-\uFE0F\uD83C-\uDBFF\uDC00-\uDFFF\u2600-\u26FF\u2700-\u27BF]+$/, '').trim();
|
|
return text;
|
|
}
|
|
|
|
function cleanFacebookCaption(raw) {
|
|
if (!raw) return '';
|
|
let text = raw.trim();
|
|
const quotedMatch = text.match(/(?:on Facebook:\s*["“]|:\s*["“])([\s\S]*?)["”]?$/i);
|
|
if (quotedMatch && quotedMatch[1]) {
|
|
text = quotedMatch[1];
|
|
} else {
|
|
const colonQuote = text.indexOf(': "');
|
|
if (colonQuote !== -1) {
|
|
text = text.slice(colonQuote + 3).replace(/["”]$/, '');
|
|
}
|
|
}
|
|
text = text.replace(/\s*\|\s*By\s+[^|]+\|\s*Facebook.*$/i, '');
|
|
text = text.replace(/\s*\|\s*Facebook.*$/i, '');
|
|
return cleanSocialCaption(text);
|
|
}
|
|
|
|
console.log('Testing isFacebookUrl...');
|
|
assert.strictEqual(isFacebookUrl('https://www.facebook.com/reel/123456789'), true);
|
|
assert.strictEqual(isFacebookUrl('https://fb.watch/abcdef123/'), true);
|
|
assert.strictEqual(isFacebookUrl('https://m.facebook.com/reel/987654321/'), true);
|
|
assert.strictEqual(isFacebookUrl('https://www.facebook.com/watch/?v=11223344'), true);
|
|
assert.strictEqual(isFacebookUrl('https://www.facebook.com/share/r/abc123xyz/'), true);
|
|
assert.strictEqual(isFacebookUrl('https://www.instagram.com/reel/xyz123'), false);
|
|
assert.strictEqual(isFacebookUrl('https://www.allrecipes.com/recipe/123/cookies'), false);
|
|
console.log('✔ isFacebookUrl tests passed!');
|
|
|
|
console.log('\nTesting cleanFacebookCaption...');
|
|
const raw1 = 'Chef Gordon on Facebook: "Crispy Garlic Butter Steak Bites. 1 lb sirloin steak, 2 tbsp butter. Cook for 5 mins #food #steak"';
|
|
const cleaned1 = cleanFacebookCaption(raw1);
|
|
assert.strictEqual(cleaned1, 'Crispy Garlic Butter Steak Bites. 1 lb sirloin steak, 2 tbsp butter. Cook for 5 mins');
|
|
|
|
const raw2 = 'Delicious Homemade Pasta Recipe | 2 cups flour, 3 eggs | By Italian Kitchen | Facebook';
|
|
const cleaned2 = cleanFacebookCaption(raw2);
|
|
assert.strictEqual(cleaned2, 'Delicious Homemade Pasta Recipe | 2 cups flour, 3 eggs');
|
|
console.log('✔ cleanFacebookCaption tests passed!');
|
|
|
|
console.log('\nAll Facebook Reel unit tests passed successfully!');
|