kafka/internal/engines/github_test.go
Franz Kafka df8fe9474b feat: add DuckDuckGo, GitHub, Reddit, and Bing engines
- DuckDuckGo: scrapes Lite HTML endpoint for results
  - Language-aware region mapping (de→de-de, ja→jp-jp, etc.)
  - HTML parser extracts result links and snippets from DDG Lite markup
  - Shared html_helpers.go with extractAttr, stripHTML, htmlUnescape

- GitHub: uses public Search API (repos, sorted by stars)
  - No auth required (10 req/min unauthenticated)
  - Shows stars, language, topics, last updated date
  - Paginated via GitHub's page parameter

- Reddit: uses public JSON search API
  - Respects safesearch (skips over_18 posts)
  - Shows subreddit, score, comment count
  - Links self-posts to the thread URL

- Bing: scrapes web search HTML (b_algo containers)
  - Extracts titles, URLs, and snippets from Bing's result markup
  - Handles Bing's tracking URL encoding

- Updated factory, config defaults, and config.example.toml
- Full test suite: unit tests for all engines, HTML parsing tests,
  region mapping tests, live request tests (skipped in short mode)

9 engines total: wikipedia, arxiv, crossref, braveapi, qwant,
duckduckgo, github, reddit, bing
2026-03-21 16:52:11 +00:00

72 lines
1.7 KiB
Go

package engines
import (
"context"
"net/http"
"testing"
"time"
"github.com/ashie/gosearch/internal/contracts"
)
func TestGitHubEngine_EmptyQuery(t *testing.T) {
eng := &GitHubEngine{}
resp, err := eng.Search(context.Background(), contracts.SearchRequest{Query: ""})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Results) != 0 {
t.Errorf("expected 0 results for empty query, got %d", len(resp.Results))
}
}
func TestGitHubEngine_Name(t *testing.T) {
eng := &GitHubEngine{}
if eng.Name() != "github" {
t.Errorf("expected 'github', got %q", eng.Name())
}
}
func TestGitHubEngine_Uninitialized(t *testing.T) {
eng := &GitHubEngine{}
_, err := eng.Search(context.Background(), contracts.SearchRequest{Query: "test"})
if err == nil {
t.Error("expected error for uninitialized client")
}
}
func TestGitHubEngine_LiveRequest(t *testing.T) {
if testing.Short() {
t.Skip("skipping live request")
}
client := &http.Client{}
eng := &GitHubEngine{client: client}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
resp, err := eng.Search(ctx, contracts.SearchRequest{
Query: "golang cli",
})
if err != nil {
t.Fatalf("live search failed: %v", err)
}
if resp.NumberOfResults <= 0 {
t.Error("expected some results for 'golang cli'")
}
if len(resp.Results) == 0 {
t.Error("expected at least 1 result")
}
// Verify structure.
for _, r := range resp.Results {
if r.Engine != "github" {
t.Errorf("expected engine 'github', got %q", r.Engine)
}
if r.URL == nil || *r.URL == "" {
t.Error("expected non-empty URL")
}
}
t.Logf("github returned %d results (total: %d)", len(resp.Results), resp.NumberOfResults)
}