Files
jobhunt/scan_test.go
T

342 lines
13 KiB
Go

package main
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestWanted(t *testing.T) {
tests := []struct {
title, location string
want bool
}{
{"Backend Engineer", "Amsterdam, Netherlands", true},
{"Senior Back-end Developer", "Utrecht", true},
{"Full Stack Software Engineer II", "Amsterdam", true},
{"Fullstack Engineer (frontend-leaning)", "Rotterdam", true},
{"Python Software Engineer - Product team", "Utrecht, Netherlands", true},
{"Software Engineer - Research Infrastructure", "Amsterdam, North Holland, Netherlands", true},
{"Software Engineer, Frontend", "Amsterdam", false},
{"Frontend Engineer", "Amsterdam", false},
{"Backend Engineer", "London, United Kingdom", false},
{"Backend Engineer", "", false},
{"Engineering Manager, Backend", "Amsterdam", false},
{"Software Engineer Intern", "Amsterdam", false},
{"Staff Software Engineer", "Amsterdam", false},
{"Android Software Engineer", "Amsterdam", false},
{"Software Engineer in Test", "Amsterdam", false},
{"Account Executive", "Amsterdam", false},
{"Senior Java Software Engineer - Payments", "Amsterdam", false},
{"Software Engineer (Java) - Screening Team", "Amsterdam", false},
{"Haskell Software Engineer - Creatives Team", "Utrecht", false},
{"Senior Software Engineer, C++", "Amsterdam", false},
{"C++ Software Engineer", "Amsterdam", false},
{"Backend Engineer (.NET)", "Amsterdam", false},
{"Software Engineer C#/Python", "Amsterdam", false},
{"Forward Deployed Engineer - full Stack", "Amsterdam", false},
{"Fullstack Engineer (JavaScript, Python)", "Amsterdam", true},
{"Software Engineer - Scalability", "Amsterdam", true},
{"Backend Engineer, Rubyists welcome", "Amsterdam", true},
{"Data Analyst", "Utrecht", false},
}
for _, tt := range tests {
t.Run(tt.title+"/"+tt.location, func(t *testing.T) {
if got := wanted(Job{Title: tt.title, Location: tt.location}); got != tt.want {
t.Errorf("wanted = %v, want %v", got, tt.want)
}
})
}
}
func TestParseFeed(t *testing.T) {
tests := []struct {
name string
src Source
body string
want Job
wantText string // substring of Text
}{
{
name: "greenhouse, escaped html and numeric id",
src: Source{"Adyen", "greenhouse", "adyen"},
body: `{"jobs":[{"id":123,"title":"Backend Engineer","absolute_url":"https://careers.adyen.com/1","location":{"name":"Amsterdam"},"content":"<p>Build & run payments</p>"}]}`,
want: Job{Key: "greenhouse:adyen:123", Company: "Adyen", Title: "Backend Engineer", Location: "Amsterdam", URL: "https://careers.adyen.com/1"},
wantText: "Build & run payments",
},
{
name: "lever, plain text plus lists",
src: Source{"Mendix", "lever", "mendix"},
body: `[{"id":"ab-1","text":"Software Engineer","hostedUrl":"https://jobs.lever.co/mendix/ab-1","categories":{"location":"Rotterdam"},"descriptionPlain":"About the role","lists":[{"text":"You have","content":"<li>Go experience</li>"}],"additionalPlain":"Perks"}]`,
want: Job{Key: "lever:mendix:ab-1", Company: "Mendix", Title: "Software Engineer", Location: "Rotterdam", URL: "https://jobs.lever.co/mendix/ab-1"},
wantText: "Go experience",
},
{
name: "ashby, unlisted jobs are skipped",
src: Source{"Mollie", "ashby", "mollie"},
body: `{"jobs":[{"id":"hidden","title":"Secret","location":"Amsterdam","isListed":false},{"id":"u1","title":"Backend Engineer","location":"Amsterdam","jobUrl":"https://jobs.ashbyhq.com/mollie/u1","descriptionPlain":"Payments for everyone"}]}`,
want: Job{Key: "ashby:mollie:u1", Company: "Mollie", Title: "Backend Engineer", Location: "Amsterdam", URL: "https://jobs.ashbyhq.com/mollie/u1"},
wantText: "Payments for everyone",
},
{
name: "recruitee, description and requirements",
src: Source{"bunq", "recruitee", "bunq"},
body: `{"offers":[{"id":77,"title":"Python Developer","city":"Amsterdam","country":"Netherlands","careers_url":"https://careers.bunq.com/o/py","description":"<p>The bank of the free</p>","requirements":"<ul><li>Python</li></ul>"}]}`,
want: Job{Key: "recruitee:bunq:77", Company: "bunq", Title: "Python Developer", Location: "Amsterdam, Netherlands", URL: "https://careers.bunq.com/o/py"},
wantText: "Python",
},
{
name: "smartrecruiters, text comes from the detail document",
src: Source{"Picnic", "smartrecruiters", "picnic"},
body: `{"content":[{"id":"744","name":"Backend Engineer","location":{"city":"Eindhoven","country":"nl"}}]}`,
want: Job{Key: "smartrecruiters:picnic:744", Company: "Picnic", Title: "Backend Engineer", Location: "Eindhoven, NL",
URL: "https://jobs.smartrecruiters.com/picnic/744", DetailURL: "https://api.smartrecruiters.com/v1/companies/picnic/postings/744"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jobs, err := parseFeed(tt.src, []byte(tt.body))
if err != nil {
t.Fatal(err)
}
if len(jobs) != 1 {
t.Fatalf("got %d jobs, want 1: %+v", len(jobs), jobs)
}
got := jobs[0]
if !strings.Contains(got.Text, tt.wantText) {
t.Errorf("Text = %q, want it to contain %q", got.Text, tt.wantText)
}
got.Text = ""
if got != tt.want {
t.Errorf("got %+v\nwant %+v", got, tt.want)
}
if !wanted(got) {
t.Errorf("the parsed location %q should count as the Netherlands", got.Location)
}
})
}
if _, err := parseFeed(Source{"X", "greenhouse", "x"}, []byte("<html>blocked</html>")); err == nil {
t.Error("a non-JSON feed must be an error")
}
if _, err := parseFeed(Source{"X", "workday", "x"}, []byte("{}")); err == nil {
t.Error("an unknown ATS must be an error")
}
}
func TestParseSmartDetail(t *testing.T) {
text, err := parseSmartDetail([]byte(`{"jobAd":{"sections":{"qualifications":{"title":"Qualifications","text":"<li>Java or Python</li>"},"jobDescription":{"title":"Job Description","text":"<p>Build the app</p>"}}}}`))
if err != nil {
t.Fatal(err)
}
if d, q := strings.Index(text, "Build the app"), strings.Index(text, "Java or Python"); d < 0 || q < d {
t.Errorf("description should come before qualifications, got %q", text)
}
}
// scanFixture is a Scanner with every outside dependency faked.
type scanFixture struct {
*Scanner
assessed []string
posts []string
files []string
}
func newScanFixture(t *testing.T, jobs []Job, assess func(Posting) (*Package, error)) *scanFixture {
t.Helper()
f := &scanFixture{}
f.Scanner = &Scanner{
cfg: Config{NotesDir: t.TempDir(), StateDir: t.TempDir(), ScanMax: 10, ScanMinFit: 60},
sources: []Source{{"Acme", "greenhouse", "acme"}, {"Broken", "greenhouse", "broken"}},
list: func(_ context.Context, s Source) ([]Job, error) {
if s.Slug == "broken" {
return nil, errors.New("HTTP 500")
}
return jobs, nil
},
text: func(_ context.Context, j Job) (string, error) { return j.Text, nil },
assess: func(_ context.Context, p Posting) (*Package, error) {
f.assessed = append(f.assessed, p.URL)
return assess(p)
},
post: func(_ context.Context, content, fileName string, _ []byte) error {
f.posts = append(f.posts, content)
if fileName != "" {
f.files = append(f.files, fileName)
}
return nil
},
now: func() time.Time { return time.Date(2026, 11, 25, 9, 0, 0, 0, time.UTC) },
}
return f
}
func scanJob(id, title string) Job {
return Job{Key: "greenhouse:acme:" + id, Company: "Acme", Title: title, Location: "Utrecht, Netherlands",
URL: "https://acme.example/jobs/" + id, Text: longDescription}
}
func okPackage(p Posting) (*Package, error) {
return &Package{Company: "Acme", Title: "Backend Engineer " + filepath.Base(p.URL), FitScore: 80, Verdict: "apply"}, nil
}
func TestScanRun(t *testing.T) {
t.Run("posts matches once, drops language blockers, ignores the rest", func(t *testing.T) {
jobs := []Job{scanJob("1", "Backend Engineer"), scanJob("2", "Backend Engineer (Dutch speaking)"), scanJob("3", "Account Executive")}
f := newScanFixture(t, jobs, func(p Posting) (*Package, error) {
pkg, _ := okPackage(p)
pkg.LanguageBlocker = strings.HasSuffix(p.URL, "/2")
if !strings.HasPrefix(p.Text, "Company: Acme\nTitle: Backend Engineer") {
t.Errorf("posting text should lead with the feed's facts, got %q", truncate(p.Text, 60))
}
return pkg, nil
})
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 2 {
t.Errorf("assessed %v, want jobs 1 and 2 only", f.assessed)
}
if len(f.files) != 1 || len(f.posts) != 2 {
t.Fatalf("want one package and one summary, got files %v posts %q", f.files, f.posts)
}
if !strings.Contains(f.posts[0], "<https://acme.example/jobs/1>") || !strings.Contains(f.posts[0], "Fit 80/100") {
t.Errorf("package message = %q", f.posts[0])
}
for _, want := range []string{"1 posted", "1 dropped", "0 still queued", "Broken"} {
if !strings.Contains(f.posts[1], want) {
t.Errorf("summary %q should contain %q", f.posts[1], want)
}
}
notes, _ := filepath.Glob(filepath.Join(f.cfg.NotesDir, "*.md"))
if len(notes) != 1 {
t.Errorf("want exactly one note in the vault, got %v", notes)
}
// Second run: everything is seen, so nothing is assessed again.
f.assessed, f.posts = nil, nil
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 0 {
t.Errorf("second run assessed %v again", f.assessed)
}
})
t.Run("stays silent about postings below the minimum fit", func(t *testing.T) {
jobs := []Job{scanJob("1", "Backend Engineer"), scanJob("2", "Software Engineer"), scanJob("3", "Fullstack Engineer")}
f := newScanFixture(t, jobs, func(p Posting) (*Package, error) {
pkg, _ := okPackage(p)
pkg.FitScore = map[string]int{"1": 60, "2": 59, "3": 41}[filepath.Base(p.URL)]
return pkg, nil
})
f.sources = f.sources[:1]
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
notes, _ := filepath.Glob(filepath.Join(f.cfg.NotesDir, "*.md"))
if len(f.files) != 1 || len(notes) != 1 || !strings.Contains(f.posts[0], "Fit 60/100") {
t.Fatalf("only the job at the cutoff should be posted and filed: files %v notes %v", f.files, notes)
}
summary := f.posts[len(f.posts)-1]
for _, want := range []string{"1 posted", "2 below fit 60", "Acme, Software Engineer (59)", "Acme, Fullstack Engineer (41)"} {
if !strings.Contains(summary, want) {
t.Errorf("summary %q should contain %q", summary, want)
}
}
f.assessed = nil
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 0 {
t.Errorf("low-fit jobs were assessed again: %v", f.assessed)
}
})
t.Run("caps a run and leaves the rest for the next one", func(t *testing.T) {
jobs := []Job{scanJob("1", "Backend Engineer"), scanJob("2", "Backend Engineer"), scanJob("3", "Backend Engineer")}
f := newScanFixture(t, jobs, okPackage)
f.cfg.ScanMax = 2
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 2 || !strings.Contains(f.posts[len(f.posts)-1], "1 still queued") {
t.Errorf("assessed %v, summary %q", f.assessed, f.posts[len(f.posts)-1])
}
f.assessed = nil
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 1 || !strings.HasSuffix(f.assessed[0], "/3") {
t.Errorf("second run assessed %v, want only job 3", f.assessed)
}
})
t.Run("a Claude failure stops the run and the job is retried later", func(t *testing.T) {
jobs := []Job{scanJob("1", "Backend Engineer"), scanJob("2", "Backend Engineer")}
fail := true
f := newScanFixture(t, jobs, func(p Posting) (*Package, error) {
if fail {
return nil, errors.New("claude reported an error: not logged in")
}
return okPackage(p)
})
if err := f.run(t.Context()); err == nil {
t.Fatal("want the run to fail")
}
if len(f.assessed) != 1 || len(f.files) != 0 || !strings.Contains(f.posts[0], "not logged in") {
t.Errorf("assessed %v files %v posts %q", f.assessed, f.files, f.posts)
}
fail, f.assessed = false, nil
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.assessed) != 2 {
t.Errorf("after recovery assessed %v, want both jobs", f.assessed)
}
})
t.Run("quiet when there is nothing new", func(t *testing.T) {
f := newScanFixture(t, nil, okPackage)
f.sources = f.sources[:1] // no broken feed
if err := f.run(t.Context()); err != nil {
t.Fatal(err)
}
if len(f.posts) != 0 {
t.Errorf("want no message, got %q", f.posts)
}
})
t.Run("a corrupt seen file stops the run instead of re-posting everything", func(t *testing.T) {
f := newScanFixture(t, []Job{scanJob("1", "Backend Engineer")}, okPackage)
if err := os.WriteFile(f.seenPath(), []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if err := f.run(t.Context()); err == nil || len(f.assessed) != 0 {
t.Errorf("err = %v, assessed %v", err, f.assessed)
}
})
}
func TestParsePackageMalformedAnswer(t *testing.T) {
// What the first real scan hit: an object that closes early.
_, err := parsePackage([]byte(`{"is_error":false,"result":"{\"company\":\"Adyen\"}, \"title\":\"Engineer\"}"}`))
if !errors.Is(err, errBadAnswer) {
t.Fatalf("err = %v, want errBadAnswer so the assessment is retried", err)
}
if !strings.Contains(err.Error(), `Adyen`) {
t.Errorf("the error should quote the answer near the fault, got %v", err)
}
}
func TestYAMLString(t *testing.T) {
// A hostile or just odd title must stay one quoted scalar in the frontmatter.
if got, want := yamlString("Engineer: \"Core\"\n---\nfit: 100"), `"Engineer: \"Core\" --- fit: 100"`; got != want {
t.Errorf("yamlString = %s, want %s", got, want)
}
}