Authorisation: @admin and @signedIn schema directives replace the 25 hand-copied resolver checks. A test fails if a mutation has neither and is not on the public allow-list, so a new mutation cannot ship unguarded. IDs: uint model IDs bind through graphql.UintID, removing the ten int(obj.ID) resolvers. Job status is a GraphQL enum, tied to services.statusOrder by a test. Input is strict, so the mutations no longer coerce an unknown status to "applied"; output is lenient so one legacy row cannot fail the list. deleteJobApplication and deleteJobAppReference now error on a missing id like the other deletes. The rowing reader, the job extractor and now the email classifier reach the Python service through one services.CallPython helper. The email pipeline's prompt and fence stripping moved to Python, and anthropic-sdk-go is gone from go.mod. Fix the auth test fixture, whose 11-byte secret had been panicking the services test package since InitAuth began requiring 32 bytes. Add a test that keeps the python service's compose environment list in step with the env registry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
211 lines
6.3 KiB
Go
211 lines
6.3 KiB
Go
package services
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
// These tests cover the pure helpers of the email sync: body cleaning,
|
|
// truncation, timestamp parsing, the retry predicate and the filter. The
|
|
// classify step, which is an HTTP call to the Python service, is covered in
|
|
// email_classify_test.go. What is left uncovered — token acquisition, the
|
|
// Graph paging loop, and the create/update paths processEmail ends in — needs
|
|
// either a live *gorm.DB or a Microsoft Graph endpoint, neither of which
|
|
// exists in a test run.
|
|
//
|
|
// matchesFilter is a method but reads no field of the receiver (it matches
|
|
// against the package-level subjectKeywords/senderDomains), which is why a
|
|
// zero-value &EmailSyncService{} is a legitimate receiver below.
|
|
|
|
func TestCleanEmailBody(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
content string
|
|
contentType string
|
|
want string
|
|
}{
|
|
{
|
|
"html tags are stripped and entities decoded",
|
|
"<p>Hi Adam & team</p><b>thanks</b>",
|
|
"html",
|
|
"Hi Adam & team thanks",
|
|
},
|
|
{
|
|
"contentType match is case-insensitive",
|
|
"<p>hello</p>",
|
|
"HTML",
|
|
"hello",
|
|
},
|
|
{
|
|
// Entities are only decoded on the HTML path, so plain text keeps
|
|
// them verbatim rather than being silently rewritten.
|
|
"text bodies are not entity-decoded",
|
|
"a & b",
|
|
"text",
|
|
"a & b",
|
|
},
|
|
{
|
|
"runs of spaces and tabs collapse to one space",
|
|
"one \t two",
|
|
"text",
|
|
"one two",
|
|
},
|
|
{
|
|
"three or more newlines collapse to a blank line",
|
|
"a\n\n\n\n\nb",
|
|
"text",
|
|
"a\n\nb",
|
|
},
|
|
{
|
|
"signature marker cuts the tail",
|
|
"body text\n-- \nAdam French\n07000",
|
|
"text",
|
|
"body text",
|
|
},
|
|
{
|
|
"Sent from my iPhone is treated as a signature",
|
|
"see attached\nSent from my iPhone",
|
|
"text",
|
|
"see attached",
|
|
},
|
|
{
|
|
// idx > 0, not >= 0: a body that is nothing but a signature would
|
|
// otherwise be blanked entirely, losing the only content there is.
|
|
"a body starting with a signature marker is left intact",
|
|
"Sent from my iPhone\nreal content",
|
|
"text",
|
|
"Sent from my iPhone\nreal content",
|
|
},
|
|
{
|
|
"quoted lines are dropped",
|
|
"my reply\n> their mail\n > indented quote\nmore reply",
|
|
"text",
|
|
"my reply\nmore reply",
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := cleanEmailBody(c.content, c.contentType); got != c.want {
|
|
t.Fatalf("cleanEmailBody(%q, %q) = %q, want %q", c.content, c.contentType, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The 4000-byte cap is what bounds the prompt cost, so it gets its own test:
|
|
// the cut is by bytes and is applied before the final TrimSpace.
|
|
func TestCleanEmailBodyCap(t *testing.T) {
|
|
got := cleanEmailBody(strings.Repeat("a", 5000), "text")
|
|
if len(got) != 4000 {
|
|
t.Fatalf("len = %d, want 4000", len(got))
|
|
}
|
|
|
|
// A body exactly at the cap must pass through untouched.
|
|
exact := strings.Repeat("b", 4000)
|
|
if got := cleanEmailBody(exact, "text"); got != exact {
|
|
t.Fatalf("body at the cap was modified: len = %d", len(got))
|
|
}
|
|
}
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
s string
|
|
maxLen int
|
|
want string
|
|
}{
|
|
{"shorter than the cap is unchanged", "abc", 10, "abc"},
|
|
{"exactly at the cap is unchanged", "abcde", 5, "abcde"},
|
|
{"longer is cut to the cap", "abcdef", 3, "abc"},
|
|
{"zero cap yields the empty string", "abc", 0, ""},
|
|
{"empty input", "", 5, ""},
|
|
// Bytes, not runes: the cut can land mid-rune. Asserted so the
|
|
// behaviour is a documented choice rather than a lurking surprise.
|
|
{"multi-byte runes are cut by byte", "é", 1, "\xc3"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := truncate(c.s, c.maxLen); got != c.want {
|
|
t.Fatalf("truncate(%q, %d) = %q, want %q", c.s, c.maxLen, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseReceivedAt(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
raw string
|
|
want time.Time
|
|
}{
|
|
{"valid RFC3339", "2024-03-15T10:30:00Z", time.Date(2024, 3, 15, 10, 30, 0, 0, time.UTC)},
|
|
{"empty means no timestamp", "", time.Time{}},
|
|
{"malformed falls back to zero rather than failing the sync", "15/03/2024", time.Time{}},
|
|
{"a date without a time is not RFC3339", "2024-03-15", time.Time{}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := parseReceivedAt(c.raw); !got.Equal(c.want) {
|
|
t.Fatalf("parseReceivedAt(%q) = %v, want %v", c.raw, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRetryableFailure(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
action string
|
|
attempts int
|
|
want bool
|
|
}{
|
|
{"a failure with attempts left is retried", "error", maxEmailAttempts - 1, true},
|
|
{"a failure at the cap is abandoned", "error", maxEmailAttempts, false},
|
|
{"a failure past the cap is abandoned", "error", maxEmailAttempts + 1, false},
|
|
{"a success is terminal even with attempts left", "created", 0, false},
|
|
{"a skip is terminal too", "skipped", 0, false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
row := &models.ProcessedEmail{Action: c.action, Attempts: c.attempts}
|
|
if got := retryableFailure(row); got != c.want {
|
|
t.Fatalf("retryableFailure(%s, %d) = %v, want %v", c.action, c.attempts, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMatchesFilter(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
subject string
|
|
sender string
|
|
want bool
|
|
}{
|
|
{"subject keyword anywhere in the line", "Your application to Acme", "[email protected]", true},
|
|
{"subject keyword is case-insensitive", "INTERVIEW CONFIRMED", "[email protected]", true},
|
|
{"sender on a tracked ATS domain", "hello", "[email protected]", true},
|
|
{"sender on a subdomain of a tracked domain", "hello", "[email protected]", true},
|
|
{"sender domain match is case-insensitive", "hello", "[email protected]", true},
|
|
// The rule is an @ or . boundary, so a lookalike domain that merely
|
|
// ends in the same letters must not match.
|
|
{"lookalike domain does not match", "hello", "[email protected]", false},
|
|
{"neither subject nor sender matches", "lunch tomorrow?", "[email protected]", false},
|
|
}
|
|
|
|
s := &EmailSyncService{}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
email := graphMessage{Subject: c.subject}
|
|
email.From.EmailAddress.Address = c.sender
|
|
if got := s.matchesFilter(email); got != c.want {
|
|
t.Fatalf("matchesFilter(%q, %q) = %v, want %v", c.subject, c.sender, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|