Files
stpweb/backend/services/job_upsert_test.go
T
adamfandClaude Opus 5.5 af50e6c8ab
Test and deploy / test (push) Successful in 1m55s
Test and deploy / deploy (push) Successful in 59s
Add a job leads inbox that finds and scores postings
The tracker recorded applications but could not find jobs. A new
pipeline (services/job_leads.go, job_sources.go) polls a watch list of
sources on a timer or on request, files unseen postings as leads, marks
which pass a keyword and location pre-filter, and has Claude score those
against a single search profile through a fourth internal Python route,
/internal/jobs/score.

Sources are Greenhouse, Lever and Ashby company boards (public JSON) and
Adzuna and Reed keyword searches (API keys). The two search fetchers are
written from the providers' documentation and tested only against stubs.

Leads are their own table, not a job status, and are not soft deleted:
the (source_kind, external_id) index is the dedup, so a dismissed lead
has to remain a row. A lead reaches the tracker only by promotion, which
goes through UpsertJobApplication. That upsert moves from the capture
handler into services so promotion and the fetcher can share it; the
capture endpoint's behaviour is unchanged.

/cv/leads is the page: inbox, watch list and profile form. The timer is
off unless JOB_LEADS_ENABLED=true and never runs at start-up, so a
deploy spends no Claude calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 15:03:46 +01:00

122 lines
5.3 KiB
Go

package services
import (
"testing"
"time"
"adam-french.co.uk/backend/models"
)
func TestCanonicalJobURL(t *testing.T) {
const linkedIn = "https://www.linkedin.com/jobs/view/4012345678/"
cases := []struct {
name string
in string
want string // "" means rejected
}{
// One LinkedIn posting, reached four ways, must be one key.
{"linkedin direct", "https://www.linkedin.com/jobs/view/4012345678/?trk=public_jobs", linkedIn},
{"linkedin slug form", "https://uk.linkedin.com/jobs/view/backend-engineer-at-acme-4012345678?refId=abc", linkedIn},
{"linkedin search pane", "https://www.linkedin.com/jobs/search/?currentJobId=4012345678&keywords=go", linkedIn},
{"linkedin collections pane", "https://www.linkedin.com/jobs/collections/recommended/?currentJobId=4012345678", linkedIn},
{"linkedin page with no job id is left as a normal URL", "https://www.linkedin.com/jobs/search/?keywords=go", "https://www.linkedin.com/jobs/search?keywords=go"},
{"a non-numeric linkedin id is not trusted", "https://www.linkedin.com/jobs/search/?currentJobId=4/../x", "https://www.linkedin.com/jobs/search?currentJobId=4%2F..%2Fx"},
{"a lookalike host does not get the linkedin rule", "https://notlinkedin.com/jobs/view/42", "https://notlinkedin.com/jobs/view/42"},
{"tracking parameters and the fragment are dropped", "https://jobs.lever.co/acme/abc-123?lever-source=LinkedIn&utm_medium=x#apply", "https://jobs.lever.co/acme/abc-123"},
// gh_jid IS the posting on an embedded Greenhouse board.
{"a parameter that identifies the job is kept", "https://acme.com/careers?gh_jid=77&gh_src=abc", "https://acme.com/careers?gh_jid=77"},
{"parameter order does not matter", "https://x.com/job?b=2&a=1", "https://x.com/job?a=1&b=2"},
{"host case and a trailing slash do not matter", "https://Boards.Greenhouse.io/acme/jobs/55/", "https://boards.greenhouse.io/acme/jobs/55"},
{"a relative path is rejected", "/jobs/view/1", ""},
{"a non-http scheme is rejected", "javascript:alert(1)", ""},
{"empty is rejected", "", ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, ok := CanonicalJobURL(c.in)
if ok != (c.want != "") || got != c.want {
t.Fatalf("CanonicalJobURL(%q) = %q, %v; want %q", c.in, got, ok, c.want)
}
})
}
}
func strPtr(s string) *string { return &s }
func TestMergeCapture(t *testing.T) {
now := time.Date(2026, 10, 12, 9, 0, 0, 0, time.UTC)
earlier := now.Add(-48 * time.Hour)
t.Run("a new row takes every field and an applied date", func(t *testing.T) {
app := models.JobApplication{JobTitle: "Engineer", Company: "Acme"}
in := JobCaptureInput{Status: "applied", Location: "London", URL: "https://x.com/1", Notes: "Hybrid", Description: "Full text"}
if !MergeCapture(&app, in, now) {
t.Fatal("expected a change")
}
if app.Status != "applied" || app.AppliedAt == nil || !app.AppliedAt.Equal(now) {
t.Fatalf("status = %q, appliedAt = %v", app.Status, app.AppliedAt)
}
if *app.Location != "London" || *app.URL != "https://x.com/1" || *app.Notes != "Hybrid" || *app.Description != "Full text" {
t.Fatalf("fields not filled: %+v", app)
}
})
t.Run("a saved posting has no applied date", func(t *testing.T) {
app := models.JobApplication{}
MergeCapture(&app, JobCaptureInput{Status: "saved"}, now)
if app.Status != "saved" || app.AppliedAt != nil {
t.Fatalf("status = %q, appliedAt = %v", app.Status, app.AppliedAt)
}
})
t.Run("saved then applied advances and dates the application", func(t *testing.T) {
app := models.JobApplication{Status: "saved"}
if !MergeCapture(&app, JobCaptureInput{Status: "applied"}, now) {
t.Fatal("expected a change")
}
if app.Status != "applied" || app.AppliedAt == nil || !app.AppliedAt.Equal(now) {
t.Fatalf("status = %q, appliedAt = %v", app.Status, app.AppliedAt)
}
})
t.Run("a repeat capture cannot move an application backwards", func(t *testing.T) {
app := models.JobApplication{Status: "interviewing", AppliedAt: &earlier}
if MergeCapture(&app, JobCaptureInput{Status: "saved"}, now) {
t.Fatal("expected no change")
}
if app.Status != "interviewing" || !app.AppliedAt.Equal(earlier) {
t.Fatalf("status = %q, appliedAt = %v", app.Status, app.AppliedAt)
}
})
t.Run("fields already set are kept, empty ones are filled", func(t *testing.T) {
app := models.JobApplication{Status: "applied", AppliedAt: &earlier, Location: strPtr("Remote"), URL: strPtr("")}
in := JobCaptureInput{Status: "applied", Location: "London", URL: "https://x.com/1", Description: "Full text"}
if !MergeCapture(&app, in, now) {
t.Fatal("expected a change")
}
if *app.Location != "Remote" {
t.Fatalf("location overwritten: %q", *app.Location)
}
if *app.URL != "https://x.com/1" || *app.Description != "Full text" {
t.Fatalf("empty fields not filled: url=%q", *app.URL)
}
if !app.AppliedAt.Equal(earlier) {
t.Fatal("an existing applied date must not be replaced")
}
})
t.Run("notes append once", func(t *testing.T) {
app := models.JobApplication{Status: "applied", AppliedAt: &earlier, Notes: strPtr("Recruiter: Sam")}
in := JobCaptureInput{Status: "applied", Notes: "Hybrid, £70k"}
if !MergeCapture(&app, in, now) || *app.Notes != "Recruiter: Sam\nHybrid, £70k" {
t.Fatalf("notes = %q", *app.Notes)
}
if MergeCapture(&app, in, now) {
t.Fatalf("the same note was appended twice: %q", *app.Notes)
}
})
}