|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package service |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// Run with: |
| 13 | +// SCRAPINGBEE_API_KEY=<key> go test -v -tags integration -run TestScrapingBee ./internal/service/ |
| 14 | +// SCRAPINGBEE_API_KEY=<key> go test -v -tags integration -timeout 120s ./internal/service/ |
| 15 | + |
| 16 | +func testClient(t *testing.T) *ScrapingBeeClient { |
| 17 | + t.Helper() |
| 18 | + key := os.Getenv("SCRAPINGBEE_API_KEY") |
| 19 | + if key == "" { |
| 20 | + t.Fatal("SCRAPINGBEE_API_KEY env var not set") |
| 21 | + } |
| 22 | + return NewScrapingBeeClient(key, 2) |
| 23 | +} |
| 24 | + |
| 25 | +func TestScrapingBee_FetchUsage(t *testing.T) { |
| 26 | + client := testClient(t) |
| 27 | + usage, err := client.FetchUsage(context.Background()) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("FetchUsage error: %v", err) |
| 30 | + } |
| 31 | + t.Logf("Usage: %d/%d credits (%.1f%%), renews %s", |
| 32 | + usage.UsedCredits, usage.MaxCredits, |
| 33 | + float64(usage.UsedCredits)/float64(usage.MaxCredits)*100, |
| 34 | + usage.RenewalDate, |
| 35 | + ) |
| 36 | + if usage.MaxCredits <= 0 { |
| 37 | + t.Errorf("expected positive MaxCredits, got %d", usage.MaxCredits) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestScrapingBee_FetchHTMLInSession costs 1 credit. |
| 42 | +func TestScrapingBee_FetchHTMLInSession(t *testing.T) { |
| 43 | + client := testClient(t) |
| 44 | + targetURL := "https://www.kickstarter.com/discover/advanced?category_id=16&sort=magic" |
| 45 | + html, err := client.FetchHTMLInSession(context.Background(), targetURL, 42) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("FetchHTMLInSession error: %v", err) |
| 48 | + } |
| 49 | + if len(html) < 1000 { |
| 50 | + t.Errorf("HTML response suspiciously short: %d bytes", len(html)) |
| 51 | + } |
| 52 | + if !strings.Contains(html, "kickstarter") { |
| 53 | + t.Errorf("response does not look like a Kickstarter page") |
| 54 | + } |
| 55 | + t.Logf("FetchHTMLInSession: got %d bytes", len(html)) |
| 56 | +} |
| 57 | + |
| 58 | +// TestScrapingBee_DiscoverCampaigns costs 1 credit and validates HTML parsing. |
| 59 | +func TestScrapingBee_DiscoverCampaigns(t *testing.T) { |
| 60 | + key := os.Getenv("SCRAPINGBEE_API_KEY") |
| 61 | + if key == "" { |
| 62 | + t.Fatal("SCRAPINGBEE_API_KEY env var not set") |
| 63 | + } |
| 64 | + svc := NewKickstarterScrapingService(key, 2) |
| 65 | + // category_id=16 = Technology; sort=magic; page=1 |
| 66 | + campaigns, err := svc.DiscoverCampaigns("16", "magic", 1, 42) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("DiscoverCampaigns error: %v", err) |
| 69 | + } |
| 70 | + if len(campaigns) == 0 { |
| 71 | + t.Fatal("expected at least one campaign, got 0") |
| 72 | + } |
| 73 | + t.Logf("DiscoverCampaigns: got %d campaigns", len(campaigns)) |
| 74 | + for i, c := range campaigns { |
| 75 | + if c.PID == "" { |
| 76 | + t.Errorf("campaign[%d] has empty PID", i) |
| 77 | + } |
| 78 | + if c.ProjectURL == "" { |
| 79 | + t.Errorf("campaign[%d] has empty ProjectURL", i) |
| 80 | + } |
| 81 | + t.Logf(" [%d] %s — %s", i, c.PID, c.Name) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestScrapingBee_Search costs 1 credit and validates the Search() method. |
| 86 | +func TestScrapingBee_Search(t *testing.T) { |
| 87 | + key := os.Getenv("SCRAPINGBEE_API_KEY") |
| 88 | + if key == "" { |
| 89 | + t.Fatal("SCRAPINGBEE_API_KEY env var not set") |
| 90 | + } |
| 91 | + svc := NewKickstarterScrapingService(key, 2) |
| 92 | + result, err := svc.Search("keyboard", "16", "magic", "", 12) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Search error: %v", err) |
| 95 | + } |
| 96 | + if len(result.Campaigns) == 0 { |
| 97 | + t.Fatal("expected at least one campaign, got 0") |
| 98 | + } |
| 99 | + t.Logf("Search: got %d campaigns, hasNextPage=%v", len(result.Campaigns), result.HasNextPage) |
| 100 | + for i, c := range result.Campaigns { |
| 101 | + t.Logf(" [%d] %s — %s", i, c.PID, c.Name) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// TestScrapingBee_ExtractWithAI_RawAPI documents that AI extraction returns |
| 106 | +// EMPTY_RESPONSE for [data-project] because project data is stored in HTML |
| 107 | +// attributes (not text nodes). This test is informational — it verifies the |
| 108 | +// raw API call succeeds and logs what ScrapingBee actually returns. |
| 109 | +func TestScrapingBee_ExtractWithAI_RawAPI(t *testing.T) { |
| 110 | + client := testClient(t) |
| 111 | + targetURL := "https://www.kickstarter.com/discover/advanced?category_id=16&sort=magic" |
| 112 | + // Note: [data-project] holds JSON in an attribute, not text — AI sees no text to extract. |
| 113 | + result, err := client.ExtractWithAI(context.Background(), targetURL, |
| 114 | + "Extract all project names visible on the page.", "h2 a") |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("ExtractWithAI error: %v", err) |
| 117 | + } |
| 118 | + preview := result |
| 119 | + if len(preview) > 300 { |
| 120 | + preview = preview[:300] |
| 121 | + } |
| 122 | + t.Logf("ExtractWithAI (h2 a selector) result (%d bytes): %s", len(result), preview) |
| 123 | +} |
0 commit comments