The applications tracker, leads inbox, email pipeline and capture userscript move here from web_server's backend, Python service and Vue app, as one Go binary that serves its own Vue SPA under /jobs/. - The Go services, resolvers and tests move over almost unchanged. - The three Claude calls are ported from Python to Go (internal/claude), with the same prompts, per-model request shaping and error messages. - There is no login: internal/auth asks the site backend's /auth/validate-admin with the visitor's cookies, and relays refreshed cookies back. - A Settings page replaces the admin-panel capture token card and adds the Microsoft sign-in link and a manual email sync. - The userscript is 2.0.0, at /jobs/, and migrates a saved API base. - Dockerfile, a standalone compose with Postgres, Makefile, and a Gitea CI workflow that tests and publishes the image. Co-Authored-By: Claude Opus 5.5 <[email protected]>
183 lines
6.5 KiB
Go
183 lines
6.5 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
|
|
"adam-french.co.uk/job-tracker/internal/claude"
|
|
)
|
|
|
|
// The classify step of the email pipeline: the message handed to Claude, and
|
|
// what processEmail does with each kind of answer. A fake classifier stands
|
|
// in for internal/claude, whose own tests cover the request and the parsing.
|
|
//
|
|
// processEmail is driven only as far as it can go without a database. That
|
|
// covers every failure (they all return before the first query) and the two
|
|
// "nothing to do" answers; an email that is about a job goes on to look the
|
|
// application up, which needs Postgres and is not exercised here.
|
|
|
|
// fakeClassifier answers every email with analysis and err, recording the
|
|
// messages it was given.
|
|
type fakeClassifier struct {
|
|
analysis EmailAnalysis
|
|
err error
|
|
messages []string
|
|
}
|
|
|
|
func (f *fakeClassifier) ClassifyEmail(ctx context.Context, message string) (EmailAnalysis, error) {
|
|
f.messages = append(f.messages, message)
|
|
if err := ctx.Err(); err != nil {
|
|
return EmailAnalysis{}, err
|
|
}
|
|
return f.analysis, f.err
|
|
}
|
|
|
|
func classifierStub(analysis EmailAnalysis, err error) (*EmailSyncService, *fakeClassifier) {
|
|
f := &fakeClassifier{analysis: analysis, err: err}
|
|
return &EmailSyncService{Classifier: f}, f
|
|
}
|
|
|
|
func testEmail() graphMessage {
|
|
email := graphMessage{
|
|
ID: "msg-1",
|
|
Subject: "Your application to Acme",
|
|
ReceivedDateTime: "2026-03-15T10:30:00Z",
|
|
Body: graphBody{ContentType: "html", Content: "<p>Thanks for applying</p>"},
|
|
}
|
|
email.From.EmailAddress = graphEmailAddress{Name: "Acme Hiring", Address: "[email protected]"}
|
|
return email
|
|
}
|
|
|
|
func TestClassifyEmailMessage(t *testing.T) {
|
|
want := EmailAnalysis{IsJobEmail: true, Action: "create", Company: "Acme", JobTitle: "Engineer", Status: "applied"}
|
|
s, f := classifierStub(want, nil)
|
|
|
|
analysis, err := s.classifyEmail(context.Background(), testEmail())
|
|
if err != nil || analysis != want {
|
|
t.Fatalf("classifyEmail = %+v, %v", analysis, err)
|
|
}
|
|
// The whole contract: the headers, a blank line and the cleaned body,
|
|
// which go to Claude unchanged.
|
|
msg := "Subject: Your application to Acme\nFrom: Acme Hiring <[email protected]>\nDate: 2026-03-15T10:30:00Z\n\nThanks for applying"
|
|
if !slices.Equal(f.messages, []string{msg}) {
|
|
t.Fatalf("messages = %q\nwant [%q]", f.messages, msg)
|
|
}
|
|
}
|
|
|
|
// The classifier prompt lists the statuses Claude may answer; they must be
|
|
// the tracker's vocabulary less "saved", which no email announces.
|
|
func TestClassifierStatusesMatchStatusOrder(t *testing.T) {
|
|
var want []string
|
|
for status := range statusOrder {
|
|
if status != "saved" {
|
|
want = append(want, status)
|
|
}
|
|
}
|
|
got := slices.Clone(claude.EmailStatuses)
|
|
sort.Strings(want)
|
|
sort.Strings(got)
|
|
if !slices.Equal(got, want) {
|
|
t.Fatalf("claude.EmailStatuses = %v\nstatusOrder less saved = %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestProcessEmailWithoutAJob(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
analysis EmailAnalysis
|
|
}{
|
|
{"not a job email", EmailAnalysis{}},
|
|
// (company, job title) is the only key an application has, so an
|
|
// answer missing either cannot be matched or created.
|
|
{"a job email with no company", EmailAnalysis{IsJobEmail: true, Action: "create", JobTitle: "Engineer", Status: "applied"}},
|
|
{"a job email with no title", EmailAnalysis{IsJobEmail: true, Action: "create", Company: "Acme", Status: "applied"}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
s, _ := classifierStub(c.analysis, nil)
|
|
action, jobAppID, err := s.processEmail(context.Background(), testEmail())
|
|
if err != nil || action != "skipped" || jobAppID != nil {
|
|
t.Fatalf("processEmail = (%q, %v, %v), want (skipped, nil, nil)", action, jobAppID, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Every way the classifier can fail must come back from processEmail as an
|
|
// error with no action: that is what makes SyncEmails record the email as
|
|
// Action "error" with one more attempt used, to be retried next sync.
|
|
func TestProcessEmailClassifierFailures(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
err error
|
|
}{
|
|
{"Claude failed", &claude.ReadError{Msg: "failed to classify the email"}},
|
|
{"Claude's reply was unparseable", &claude.ReadError{Msg: "failed to parse the email classification"}},
|
|
{"no API key", claude.ErrNotConfigured},
|
|
{"something else", fmt.Errorf("unexpected")},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
s, _ := classifierStub(EmailAnalysis{}, c.err)
|
|
action, jobAppID, err := s.processEmail(context.Background(), testEmail())
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
if action != "" || jobAppID != nil {
|
|
t.Fatalf("processEmail = (%q, %v) alongside an error", action, jobAppID)
|
|
}
|
|
// Not a cancellation: SyncEmails skips those without recording
|
|
// anything, which would leave a real failure retried for ever.
|
|
if errors.Is(err, context.Canceled) {
|
|
t.Fatalf("err = %v reads as a cancelled sync", err)
|
|
}
|
|
if !errors.Is(err, c.err) {
|
|
t.Fatalf("err = %v does not wrap %v", err, c.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The other half of the rule above: a sync abandoned mid-call (the admin
|
|
// closed the tab) must be recognisable, so the email's attempts do not advance.
|
|
func TestProcessEmailCancelled(t *testing.T) {
|
|
s, _ := classifierStub(EmailAnalysis{}, nil)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, _, err := s.processEmail(ctx, testEmail()); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("err = %v, want it to wrap context.Canceled", err)
|
|
}
|
|
}
|
|
|
|
func TestClassifierMessageIsCapped(t *testing.T) {
|
|
// An ordinary email is nowhere near the cap and is not touched.
|
|
if got := classifierMessage(testEmail()); !strings.HasSuffix(got, "\n\nThanks for applying") {
|
|
t.Fatalf("message = %q", got)
|
|
}
|
|
|
|
// cleanEmailBody bounds the body, so only the headers can push a message
|
|
// over. Multi-byte runes: the cap counts characters, and the cut must not split one.
|
|
long := testEmail()
|
|
long.Subject = strings.Repeat("é", maxClassifyRunes+50)
|
|
got := classifierMessage(long)
|
|
if n := utf8.RuneCountInString(got); n != maxClassifyRunes {
|
|
t.Fatalf("message is %d runes, want %d", n, maxClassifyRunes)
|
|
}
|
|
if !utf8.ValidString(got) {
|
|
t.Fatal("the cut split a rune")
|
|
}
|
|
|
|
// More bytes than the cap but fewer runes: within the limit, so intact.
|
|
wide := testEmail()
|
|
wide.Subject = strings.Repeat("é", maxClassifyRunes/2+10)
|
|
if got := classifierMessage(wide); !strings.HasSuffix(got, "\n\nThanks for applying") {
|
|
t.Fatal("a message under the rune cap was cut")
|
|
}
|
|
}
|