|
| 1 | +import { Actor } from 'apify'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { findActorTip, storeActorTip } from '../src/tips.js'; |
| 5 | + |
| 6 | +describe('findActorTip', () => { |
| 7 | + it.each([ |
| 8 | + ['https://www.facebook.com/apify/posts', 'For scraping facebook.com, we recommend using [Facebook Posts Scraper](https://apify.com/apify/facebook-posts-scraper)'], |
| 9 | + ['https://www.facebook.com/groups/1234567890', 'For scraping Facebook groups, we recommend using [Facebook Groups Scraper](https://apify.com/apify/facebook-groups-scraper)'], |
| 10 | + ['https://www.zillow.com/homedetails/123', 'For scraping zillow.com, we recommend using [Zillow Detail Scraper](https://apify.com/maxcopell/zillow-detail-scraper)'], |
| 11 | + ['https://www.instagram.com/apify/', 'For scraping instagram.com, we recommend using [Instagram Scraper](https://apify.com/apify/instagram-scraper)'], |
| 12 | + ['https://www.google.com/maps/place/Prague', 'For scraping Google Maps, we recommend using [Google Maps Scraper](https://apify.com/compass/crawler-google-places)'], |
| 13 | + ['https://maps.google.com/?q=prague', 'For scraping Google Maps, we recommend using [Google Maps Scraper](https://apify.com/compass/crawler-google-places)'], |
| 14 | + ['https://www.booking.com/hotel/cz/prague.html', 'For scraping booking.com, we recommend using [Booking Scraper](https://apify.com/voyager/booking-scraper)'], |
| 15 | + ['https://www.tripadvisor.com/Hotel_Review-g274707', 'For scraping tripadvisor.com, we recommend using [Tripadvisor Scraper](https://apify.com/maxcopell/tripadvisor)'], |
| 16 | + ['https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'For scraping youtube.com, we recommend using [YouTube Scraper](https://apify.com/streamers/youtube-scraper)'], |
| 17 | + ['https://youtu.be/dQw4w9WgXcQ', 'For scraping youtube.com, we recommend using [YouTube Scraper](https://apify.com/streamers/youtube-scraper)'], |
| 18 | + ['https://www.tiktok.com/@apify', 'For scraping tiktok.com, we recommend using [TikTok Scraper](https://apify.com/clockworks/tiktok-scraper)'], |
| 19 | + ['https://www.amazon.com/dp/B08N5WRWNW', 'For scraping amazon.com, we recommend using [Amazon Crawler](https://apify.com/junglee/amazon-crawler)'], |
| 20 | + ])('recommends the Actor listed in the issue for %s', (query, message) => { |
| 21 | + expect(findActorTip({ query })?.message).toBe(message); |
| 22 | + }); |
| 23 | + |
| 24 | + it('marks the tip as informational', () => { |
| 25 | + expect(findActorTip({ query: 'https://www.tiktok.com/@apify' })?.level).toBe('info'); |
| 26 | + }); |
| 27 | + |
| 28 | + it.each([ |
| 29 | + // The site name appears in the path, not in the host. |
| 30 | + 'https://example.com/facebook', |
| 31 | + 'https://example.com/redirect?to=https://www.facebook.com/apify', |
| 32 | + // Plain Google Search is what this Actor does itself. |
| 33 | + 'https://www.google.com/search?q=prague+restaurants', |
| 34 | + // Hosts that only look like a listed domain. |
| 35 | + 'https://notfacebook.com/apify', |
| 36 | + 'https://facebook.com.evil.example/apify', |
| 37 | + ])('does not recommend anything for %s', (query) => { |
| 38 | + expect(findActorTip({ query })).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it.each([ |
| 42 | + ['site:instagram.com nike', 'Instagram Scraper'], |
| 43 | + ['cheap hotels booking.com prague', 'Booking Scraper'], |
| 44 | + ['google.com/maps prague restaurants', 'Google Maps Scraper'], |
| 45 | + ['"tripadvisor.com" prague', 'Tripadvisor Scraper'], |
| 46 | + ])('finds a listed domain mentioned in the search query %s', (query, actorTitle) => { |
| 47 | + expect(findActorTip({ query })?.message).toContain(actorTitle); |
| 48 | + }); |
| 49 | + |
| 50 | + it.each([ |
| 51 | + 'best hotels in prague', |
| 52 | + 'site:google.com apify', |
| 53 | + 'nike -www.instagram.com', |
| 54 | + ])('does not recommend anything for the search query %s', (query) => { |
| 55 | + expect(findActorTip({ query })).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + ['https://www.amazon.co.uk/dp/B08N5WRWNW', 'Amazon Crawler'], |
| 60 | + ['https://www.amazon.de/dp/B08N5WRWNW', 'Amazon Crawler'], |
| 61 | + ['https://www.amazon.com.mx/dp/B08N5WRWNW', 'Amazon Crawler'], |
| 62 | + ['https://www.google.co.uk/maps/place/Prague', 'Google Maps Scraper'], |
| 63 | + ['https://maps.google.co.uk/?q=prague', 'Google Maps Scraper'], |
| 64 | + ['https://www.tripadvisor.co.uk/Hotel_Review-g274707', 'Tripadvisor Scraper'], |
| 65 | + ])('recognizes the country domain %s', (query, actorTitle) => { |
| 66 | + expect(findActorTip({ query })?.message).toContain(actorTitle); |
| 67 | + }); |
| 68 | + |
| 69 | + it.each([ |
| 70 | + // The brand is only a subdomain of someone else's site. |
| 71 | + 'https://amazon.abc.com/dp/1', |
| 72 | + 'https://amazon.example.com/dp/1', |
| 73 | + 'https://notamazon.de/dp/1', |
| 74 | + 'https://google.com.evil.example/maps', |
| 75 | + // A country domain does not turn plain Google Search into a Maps request. |
| 76 | + 'https://www.google.co.uk/search?q=prague', |
| 77 | + ])('does not read %s as a country domain of a listed site', (query) => { |
| 78 | + expect(findActorTip({ query })).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('keeps sites that have a single global domain pinned to it', () => { |
| 82 | + expect(findActorTip({ query: 'https://facebook.ru/apify' })).toBeNull(); |
| 83 | + expect(findActorTip({ query: 'https://instagram.de/apify' })).toBeNull(); |
| 84 | + expect(findActorTip({ query: 'https://booking.de/hotel/cz/prague.html' })).toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('matches subdomains, keeping the more specific rule first', () => { |
| 88 | + expect(findActorTip({ query: 'https://m.facebook.com/groups/123' })?.message).toContain('Facebook Groups Scraper'); |
| 89 | + expect(findActorTip({ query: 'https://m.facebook.com/apify' })?.message).toContain('Facebook Posts Scraper'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('matches a path segment exactly, not merely its prefix', () => { |
| 93 | + expect(findActorTip({ query: 'https://www.facebook.com/groups' })?.message).toContain('Facebook Groups Scraper'); |
| 94 | + expect(findActorTip({ query: 'https://www.google.com/maps' })?.message).toContain('Google Maps Scraper'); |
| 95 | + expect(findActorTip({ query: 'https://www.facebook.com/groupsomething' })?.message).toContain('Facebook Posts Scraper'); |
| 96 | + expect(findActorTip({ query: 'https://www.google.com/mapsomething' })).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it.each([ |
| 100 | + 'go to zillow.com for listings', |
| 101 | + 'go to zillow.com, and get the listings', |
| 102 | + 'go to zillow.com. thanks', |
| 103 | + 'go to zillow.com; now', |
| 104 | + 'go to zillow.com!', |
| 105 | + 'is zillow.com down?', |
| 106 | + 'see (zillow.com) for details', |
| 107 | + 'see [zillow.com] for details', |
| 108 | + 'go to zillow.com...', |
| 109 | + ])('finds the domain whatever punctuation surrounds it: %s', (query) => { |
| 110 | + expect(findActorTip({ query })?.message).toContain('Zillow Detail Scraper'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('strips the punctuation before matching a path segment', () => { |
| 114 | + expect(findActorTip({ query: 'google.com/maps, prague' })?.message).toContain('Google Maps Scraper'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('ignores the case of the input', () => { |
| 118 | + expect(findActorTip({ query: 'HTTPS://WWW.TIKTOK.COM/@APIFY' })?.message).toContain('TikTok Scraper'); |
| 119 | + expect(findActorTip({ query: 'Site:Instagram.com nike' })?.message).toContain('Instagram Scraper'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('never throws on tokens that cannot be parsed as a URL', () => { |
| 123 | + expect(findActorTip({ query: 'https:// http://[ .. :: %% "" -' })).toBeNull(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('reads `query` first and falls back to `url`', () => { |
| 127 | + expect(findActorTip({ url: 'https://www.tiktok.com/@apify' })?.message).toContain('TikTok Scraper'); |
| 128 | + expect(findActorTip({ query: '', url: 'https://www.tiktok.com/@apify' })?.message).toContain('TikTok Scraper'); |
| 129 | + expect(findActorTip({ query: 'https://www.tiktok.com/@apify', url: 'https://www.amazon.com/dp/1' })?.message).toContain('TikTok Scraper'); |
| 130 | + expect(findActorTip({})).toBeNull(); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('storeActorTip', () => { |
| 135 | + afterEach(() => { |
| 136 | + vi.restoreAllMocks(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('stores the tip under the reserved `TIP` key', async () => { |
| 140 | + const setValue = vi.spyOn(Actor, 'setValue').mockResolvedValue(undefined); |
| 141 | + const tip = { message: 'For scraping tiktok.com, ...', level: 'info' } as const; |
| 142 | + |
| 143 | + await storeActorTip(tip); |
| 144 | + |
| 145 | + expect(setValue).toHaveBeenCalledWith('TIP', tip); |
| 146 | + }); |
| 147 | + |
| 148 | + it('writes nothing when there is no tip', async () => { |
| 149 | + const setValue = vi.spyOn(Actor, 'setValue').mockResolvedValue(undefined); |
| 150 | + |
| 151 | + await storeActorTip(null); |
| 152 | + |
| 153 | + expect(setValue).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments