socmind/test/facebook.test.ts
2026-09-11 23:33:20 -04:00

52 lines
1.9 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { extractFacebookLinks } from '../src/platforms/facebook-search.service.js';
import { FacebookSearchService } from '../src/platforms/facebook-search.service.js';
describe('extractFacebookLinks', () => {
it('extracts profile URLs, dedups, skips junk paths', () => {
const html = `
<a href="https://www.facebook.com/nasa/">NASA</a>
<a href="https://www.facebook.com/nasa">dup</a>
<a href="https://facebook.com/sharer/sharer.php">share</a>
<a href="https://m.facebook.com/esa/">ESA</a>
<a href="https://example.com/nasa">other</a>`;
const out = extractFacebookLinks(html, 'ddg');
assert.deepEqual(
out.map((r) => r.url),
['https://www.facebook.com/nasa', 'https://m.facebook.com/esa'],
);
assert.ok(out.every((r) => r.source === 'ddg'));
});
it('returns empty for no matches', () => {
assert.deepEqual(extractFacebookLinks('<p>nothing</p>', 'google'), []);
});
});
describe('FacebookSearchService.search', () => {
it('rejects multi-word usernames without fetching', async () => {
const svc = new FacebookSearchService();
const out = (await svc.search('two words')) as any;
assert.equal(out.found, false);
});
it('merges ddg + google, dedups by url', async () => {
const svc = new FacebookSearchService();
const real = globalThis.fetch;
(globalThis as any).fetch = async (url: string) => ({
ok: true,
text: async () =>
url.includes('duckduckgo')
? '<a href="https://facebook.com/nasa">a</a>'
: '<a href="https://facebook.com/nasa">a</a><a href="https://facebook.com/esa">b</a>',
});
try {
const out = (await svc.search('nasa')) as any;
assert.equal(out.found, true);
assert.equal(out.results.length, 2);
} finally {
globalThis.fetch = real;
}
});
});