Files
hireme/pkg/crawler/microsoft/microsoft_test.go
T

122 lines
3.5 KiB
Go

package microsoft
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
"git.pengzhan.dev/haopengzhan/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 TestMicrosoftCrawler_DiscoverAndFetch(t *testing.T) {
mockClient := &http.Client{
Transport: roundTripFunc(func(r *http.Request) *http.Response {
if strings.Contains(r.URL.Path, "/job/1789001") {
body := `{
"operationResult": {
"result": {
"jobId": "1789001",
"title": "Software Engineer II - Azure Core",
"description": "<p>Azure Core is building the backbone of cloud compute.</p>",
"responsibilities": "<li>Design and implement scalable distributed services.</li>",
"qualifications": "<li>Bachelor degree and 2+ years of software development experience.</li>",
"properties": {
"primaryLocation": "Redmond, WA, United States"
}
}
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
if strings.HasSuffix(r.URL.Path, "/search") || strings.Contains(r.URL.RawQuery, "pgSz") {
body := `{
"operationResult": {
"result": {
"totalJobs": 1,
"jobs": [
{
"jobId": "1789001",
"title": "Software Engineer II - Azure Core",
"properties": {
"primaryLocation": "Redmond, WA, United States",
"discipline": "Software Engineering"
}
}
]
}
}
}`
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 := NewMicrosoftCrawlerWithClient(mockClient)
if !c.CanHandle("microsoft") || !c.CanHandle("azure") {
t.Errorf("CanHandle should support microsoft and azure")
}
discovered, err := c.DiscoverJobs(context.Background(), "https://gcsservices.careers.microsoft.com/search/api/v1/search?q=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 != "microsoft:1789001" {
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
}
extracted, err := c.FetchJobDetail(context.Background(), "https://jobs.careers.microsoft.com/global/en/share/1789001")
if err != nil {
t.Fatalf("FetchJobDetail failed: %v", err)
}
if extracted.Title != "Software Engineer II - Azure Core" {
t.Errorf("unexpected title: %s", extracted.Title)
}
if extracted.Company != "Microsoft" {
t.Errorf("unexpected company: %s", extracted.Company)
}
if extracted.Location != "Redmond, WA, United States" {
t.Errorf("unexpected location: %s", extracted.Location)
}
}
func TestMicrosoftCrawler_LiveIntegration(t *testing.T) {
crawlertest.RunLiveIntegration(t, crawlertest.LiveTestConfig{
PlatformName: "Microsoft",
Crawler: NewMicrosoftCrawler(),
TargetURL: "https://gcsservices.careers.microsoft.com/search/api/v1/search?q=Software+Engineer&lc=Redmond%2C+Washington%2C+United+States&l=en_us",
ExpectedCanonicalPrefix: "microsoft:",
ExpectedCompany: "Microsoft",
})
}