Files
hireme/pkg/crawler/linkedin/linkedin_test.go
T

105 lines
3.2 KiB
Go

package linkedin
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/hireme/hireme/internal/crawler/crawlertest"
)
type roundTripFunc func(req *http.Request) *http.Response
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func TestLinkedInCrawler_DiscoverAndFetch(t *testing.T) {
mockClient := &http.Client{
Transport: roundTripFunc(func(r *http.Request) *http.Response {
if strings.Contains(r.URL.Path, "seeMoreJobPostings") {
body := `<html>
<li data-row="1">
<div class="base-card" data-entity-urn="urn:li:jobPosting:3920192831">
<a class="base-card__full-link" href="https://www.linkedin.com/jobs/view/software-engineer-infra-at-linkedin-3920192831"></a>
</div>
</li>
</html>`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
if strings.Contains(r.URL.Path, "3920192831") {
body := `<html>
<h2 class="top-card-layout__title">Staff Software Engineer - Distributed Graph</h2>
<a class="topcard__org-name-link">LinkedIn</a>
<span class="topcard__flavor--bullet">Bellevue, WA</span>
<div class="show-more-less-html__markup">
<p>LinkedIn graph systems scale to billions of edge connections.</p>
</div>
</html>`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(bytes.NewBufferString("not found")),
Header: make(http.Header),
}
}),
}
c := NewLinkedInCrawlerWithClient(mockClient)
if !c.CanHandle("linkedin") {
t.Errorf("CanHandle should support linkedin")
}
discovered, err := c.DiscoverJobs(context.Background(), "https://www.linkedin.com/jobs-guest/jobs/api/seeMoreJobPostings/search?keywords=Software+Engineer")
if err != nil {
t.Fatalf("DiscoverJobs failed: %v", err)
}
if len(discovered) != 1 {
t.Fatalf("expected 1 discovered job, got %d", len(discovered))
}
if discovered[0].CanonicalID != "linkedin:3920192831" {
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
}
extracted, err := c.FetchJobDetail(context.Background(), "https://www.linkedin.com/jobs/view/3920192831")
if err != nil {
t.Fatalf("FetchJobDetail failed: %v", err)
}
if extracted.Title != "Staff Software Engineer - Distributed Graph" {
t.Errorf("unexpected title: %s", extracted.Title)
}
if extracted.Company != "LinkedIn" {
t.Errorf("unexpected company: %s", extracted.Company)
}
if extracted.Location != "Bellevue, WA" {
t.Errorf("unexpected location: %s", extracted.Location)
}
}
func TestLinkedInCrawler_LiveIntegration(t *testing.T) {
crawlertest.RunLiveIntegration(t, crawlertest.LiveTestConfig{
PlatformName: "LinkedIn",
Crawler: NewLinkedInCrawler(),
TargetURL: "https://www.linkedin.com/jobs-guest/jobs/api/seeMoreJobPostings/search?keywords=Software+Engineer&location=Seattle%2C+Washington%2C+United+States",
ExpectedCanonicalPrefix: "linkedin:",
ExpectedCompany: "",
})
}