Files
hireme/pkg/crawler/stripe/stripe_test.go
T

100 lines
2.6 KiB
Go

package stripe
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 TestStripeCrawler_DiscoverAndFetch(t *testing.T) {
mockClient := &http.Client{
Transport: roundTripFunc(func(r *http.Request) *http.Response {
if strings.HasSuffix(r.URL.Path, "/jobs/6198201") {
body := `{
"id": 6198201,
"title": "Software Engineer - Infrastructure Platform",
"content": "<p>Stripe Infrastructure powers global financial networks.</p>",
"location": {
"name": "Seattle, WA"
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
if strings.HasSuffix(r.URL.Path, "/jobs") {
body := `{
"jobs": [
{
"id": 6198201,
"title": "Software Engineer - Infrastructure Platform",
"absolute_url": "https://boards.greenhouse.io/stripe/jobs/6198201",
"location": {
"name": "Seattle, WA"
}
}
]
}`
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 := NewStripeCrawlerWithClient(mockClient)
if !c.CanHandle("stripe") {
t.Errorf("CanHandle should support stripe")
}
discovered, err := c.DiscoverJobs(context.Background(), "https://boards-api.greenhouse.io/v1/boards/stripe/jobs")
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 != "stripe:6198201" {
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
}
extracted, err := c.FetchJobDetail(context.Background(), "https://boards.greenhouse.io/stripe/jobs/6198201")
if err != nil {
t.Fatalf("FetchJobDetail failed: %v", err)
}
if extracted.Title != "Software Engineer - Infrastructure Platform" {
t.Errorf("unexpected title: %s", extracted.Title)
}
if extracted.Company != "Stripe" {
t.Errorf("unexpected company: %s", extracted.Company)
}
if extracted.Location != "Seattle, WA" {
t.Errorf("unexpected location: %s", extracted.Location)
}
}