Files
stpweb/backend/handlers/handle_rowing_test.go
adamfandClaude Opus 5.5 71344a1f8d Declare GraphQL auth in the schema and drop the Go Claude client
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]>
2026-10-12 14:39:19 +01:00

97 lines
3.2 KiB
Go

package handlers
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"adam-french.co.uk/backend/services"
)
// readRowingDisplay is the Go half of the contract with the Python service's
// /internal/rowing/read. CreateRowing itself needs a database and a real
// multipart photo with EXIF, so what is covered is the call: the request the
// Python side receives, and how each kind of reply comes back.
func rowingReader(t *testing.T, handler http.HandlerFunc) *Store {
t.Helper()
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
return &Store{PythonURL: srv.URL}
}
func TestReadRowingDisplayRequestAndReply(t *testing.T) {
var got struct {
MediaType string `json:"media_type"`
Data string `json:"data"`
}
var path string
var forwarded bool
store := rowingReader(t, func(w http.ResponseWriter, r *http.Request) {
path = r.URL.Path
// The Python route treats either header as "came through nginx" and
// answers 404, so this call must never carry them.
forwarded = r.Header.Get("X-Real-IP") != "" || r.Header.Get("X-Forwarded-For") != ""
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
t.Errorf("decoding request: %v", err)
}
w.Write([]byte(`{"timeMinutes": 20, "timeSeconds": 5, "distance": 5000}`))
})
data, err := store.readRowingDisplay(context.Background(), "image/jpeg", []byte("hello"))
if err != nil {
t.Fatalf("readRowingDisplay: %v", err)
}
if data != (ExtractedRowingData{TimeMinutes: 20, TimeSeconds: 5, Distance: 5000}) {
t.Fatalf("got %+v", data)
}
if path != rowingReadPath {
t.Fatalf("path = %q, want %q", path, rowingReadPath)
}
if forwarded {
t.Fatal("request carried a proxy header the Python route rejects")
}
// "hello" in standard base64, the encoding the Anthropic API expects.
if got.MediaType != "image/jpeg" || got.Data != "aGVsbG8=" {
t.Fatalf("request body = %+v", got)
}
}
func TestReadRowingDisplayErrors(t *testing.T) {
cases := []struct {
name string
status int
body string
wantDetail string // non-empty: a services.PythonError with this message
}{
{"reader failure is relayed", http.StatusBadGateway, `{"detail": "failed to parse image data"}`, "failed to parse image data"},
{"validation error is not relayed", http.StatusUnprocessableEntity, `{"detail": [{"msg": "bad"}]}`, ""},
{"missing key is not relayed", http.StatusServiceUnavailable, `{"detail": "CLAUDE_API_KEY is not set"}`, ""},
{"non-JSON 502 is not relayed", http.StatusBadGateway, `<html>bad gateway</html>`, ""},
{"malformed success body", http.StatusOK, `not json`, ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
store := rowingReader(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(c.status)
w.Write([]byte(c.body))
})
_, err := store.readRowingDisplay(context.Background(), "image/jpeg", []byte("x"))
if err == nil {
t.Fatal("expected an error")
}
var readErr *services.PythonError
if relayed := errors.As(err, &readErr); relayed != (c.wantDetail != "") {
t.Fatalf("relayed = %v, err = %v", relayed, err)
}
if readErr != nil && readErr.Detail != c.wantDetail {
t.Fatalf("detail = %q, want %q", readErr.Detail, c.wantDetail)
}
})
}
}