import { parseOpenGraph } from '@crawlee/utils'; import { load } from 'cheerio'; describe('parseOpenGraph', () => { const case1 = load(` `); const case2 = load(` `); const case3 = load(` `); const case4 = load(` `); const case5 = load(``); const case6 = ` `; const case7 = load(` `); it('Should scrape properties', () => { expect(parseOpenGraph(case1)).toEqual({ title: 'Under Pressure', type: 'music.song', }); }); it('Should return a property as an array if there are multiple attributes under the same property name', () => { const parsed = parseOpenGraph(case2) as { videoInfo: { actor: { actorValue: string[] } }; }; expect(parsed).toHaveProperty('videoInfo'); expect(parsed.videoInfo.actor.actorValue).toContain('foo'); expect(parsed.videoInfo.actor.actorValue).toContain('bar'); expect(parsed.videoInfo.actor.actorValue).toContain('baz'); const parsed2 = parseOpenGraph(case3) as { locale: { localeValue: string; alternate: string[] }; }; expect(parsed2).toHaveProperty('locale'); expect(parsed2.locale.alternate).toContain('foo'); expect(parsed2.locale.alternate).toContain('bar'); }); it('Should parse properties regardless of how deeply they are nested', () => { expect(parseOpenGraph(case4)).toEqual({ musicInfo: { song: { disc: 'hello', track: 'world' } }, }); }); it('Should accept additional OpenGraphProperties', () => { const parsed = parseOpenGraph(case5, [ { name: 'og:custom', outputName: 'custom', children: [ { name: 'og:custom:test', outputName: 'test', children: [], }, ], }, ]); expect(parsed).toEqual({ custom: { test: 'hello' } }); }); it('Should accept strings as a substitute for CheerioAPI objects', () => { expect(parseOpenGraph(case6)).toEqual({ title: 'My Website', type: 'website', }); }); it('Should parse article properties into articleInfo', () => { expect(parseOpenGraph(case7)).toEqual({ type: 'article', articleInfo: { publishedTime: '2024-01-01T00:00:00Z', modifiedTime: '2024-01-02T00:00:00Z', expirationTime: '2024-01-03T00:00:00Z', author: 'Jane Doe', section: 'Tech', tag: 'ai', }, }); }); });