Add a fake-only end-to-end stack, Firefox journey, and bounded process cleanup for normal, failed, and interrupted runs. Refs #12 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
194 lines
6.1 KiB
Go
194 lines
6.1 KiB
Go
package e2estack
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/tradarr/backend/internal/ai"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
type Fixtures struct {
|
|
IndexQuery string `json:"indexQuery"`
|
|
IndexName string `json:"indexName"`
|
|
IndexSymbol string `json:"indexSymbol"`
|
|
IndexFIGI string `json:"indexFIGI"`
|
|
SummarySentinel string `json:"summarySentinel"`
|
|
ArticleTitle string `json:"articleTitle"`
|
|
ArticleURL string `json:"articleURL"`
|
|
ArticleContent string `json:"articleContent"`
|
|
CandidateID string `json:"candidateID"`
|
|
}
|
|
|
|
func DefaultFixtures() Fixtures {
|
|
return Fixtures{"E2E index", "E2E Financial Index", "E2EIDX", "BBG000E2E001", "E2E synthesized financial summary", "E2E private article title", "https://fixture.invalid/private-article", "E2E private article body", "pending-candidate-id"}
|
|
}
|
|
|
|
type Event struct {
|
|
Kind string `json:"kind"`
|
|
Symbols []string `json:"symbols,omitempty"`
|
|
CandidateID string `json:"candidate_id,omitempty"`
|
|
}
|
|
type Fakes struct {
|
|
mu sync.Mutex
|
|
Fixture Fixtures
|
|
Events []Event
|
|
counts map[string]int
|
|
}
|
|
|
|
func NewFakes() *Fakes {
|
|
return &Fakes{Fixture: DefaultFixtures(), Events: []Event{}, counts: map[string]int{}}
|
|
}
|
|
func (f *Fakes) record(event Event) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.Events = append(f.Events, event)
|
|
f.counts[event.Kind]++
|
|
}
|
|
func (f *Fakes) Handler() http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/audit", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"openfigiSearches": f.counts["openfigiSearches"], "scraperCalls": f.counts["scraperCalls"], "layaRankingCalls": f.counts["layaRankingCalls"], "layaSummaryCalls": f.counts["layaSummaryCalls"], "events": f.Events})
|
|
})
|
|
mux.HandleFunc("/v3/search", func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Query string `json:"query"`
|
|
Market string `json:"marketSecDes"`
|
|
}
|
|
if r.Method != "POST" || json.NewDecoder(r.Body).Decode(&req) != nil || req.Market != "Index" {
|
|
http.Error(w, "invalid fixture search", 400)
|
|
return
|
|
}
|
|
f.record(Event{Kind: "openfigiSearches"})
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": []map[string]string{{"name": f.Fixture.IndexName, "ticker": f.Fixture.IndexSymbol, "figi": f.Fixture.IndexFIGI, "marketSector": "Index"}}})
|
|
})
|
|
mux.HandleFunc("/scrape", func(w http.ResponseWriter, r *http.Request) {
|
|
var symbols []string
|
|
if r.Method != "POST" || json.NewDecoder(r.Body).Decode(&symbols) != nil {
|
|
http.Error(w, "invalid fixture scrape", 400)
|
|
return
|
|
}
|
|
f.record(Event{Kind: "scraperCalls", Symbols: symbols})
|
|
_ = json.NewEncoder(w).Encode([]scraper.Article{{Title: f.Fixture.ArticleTitle, Content: f.Fixture.ArticleContent, URL: f.Fixture.ArticleURL}})
|
|
})
|
|
mux.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Prompt string `json:"prompt"`
|
|
Format string `json:"format"`
|
|
}
|
|
if r.Method != "POST" || json.NewDecoder(r.Body).Decode(&req) != nil {
|
|
http.Error(w, "invalid fixture provider", 400)
|
|
return
|
|
}
|
|
response := f.Fixture.SummarySentinel
|
|
if req.Format == "json" {
|
|
// The candidate UUID is generated by the real use case; never hard-code it.
|
|
start := strings.LastIndex(req.Prompt, "\n{")
|
|
if start < 0 {
|
|
http.Error(w, "ranking payload missing", 400)
|
|
return
|
|
}
|
|
var payload struct {
|
|
Articles []struct {
|
|
ID string `json:"id"`
|
|
} `json:"articles"`
|
|
}
|
|
if json.Unmarshal([]byte(req.Prompt[start+1:]), &payload) != nil || len(payload.Articles) != 1 {
|
|
http.Error(w, "ranking candidate missing", 400)
|
|
return
|
|
}
|
|
id := payload.Articles[0].ID
|
|
f.mu.Lock()
|
|
f.Fixture.CandidateID = id
|
|
f.mu.Unlock()
|
|
f.record(Event{Kind: "layaRankingCalls", CandidateID: id})
|
|
encoded, _ := json.Marshal(map[string]any{"ranked_article_ids": []string{id}})
|
|
response = string(encoded)
|
|
} else {
|
|
f.record(Event{Kind: "layaSummaryCalls"})
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"response": response})
|
|
})
|
|
return mux
|
|
}
|
|
func Listen(handler http.Handler) (string, func(), error) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
server := &http.Server{Handler: handler}
|
|
go server.Serve(listener)
|
|
return "http://" + listener.Addr().String(), func() { _ = server.Close() }, nil
|
|
}
|
|
|
|
type FakeScraper struct {
|
|
URL string
|
|
Client *http.Client
|
|
}
|
|
|
|
func (FakeScraper) Name() string { return "e2e" }
|
|
func (s FakeScraper) Scrape(ctx context.Context, symbols []string) ([]scraper.Article, error) {
|
|
if err := ValidateURL(s.URL, false); err != nil {
|
|
return nil, err
|
|
}
|
|
raw, _ := json.Marshal(symbols)
|
|
req, err := http.NewRequestWithContext(ctx, "POST", s.URL+"/scrape", bytes.NewReader(raw))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := s.Client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return nil, fmt.Errorf("fake scraper failed")
|
|
}
|
|
var articles []scraper.Article
|
|
err = json.NewDecoder(resp.Body).Decode(&articles)
|
|
return articles, err
|
|
}
|
|
|
|
type FakeProvider struct {
|
|
URL string
|
|
Client *http.Client
|
|
}
|
|
|
|
func (FakeProvider) Name() string { return "fake-laya" }
|
|
func (FakeProvider) ListModels(context.Context) ([]string, error) { return []string{"fake-laya"}, nil }
|
|
func (p FakeProvider) Summarize(ctx context.Context, prompt string, opts ai.GenOptions) (string, error) {
|
|
if err := ValidateURL(p.URL, false); err != nil {
|
|
return "", err
|
|
}
|
|
body := map[string]any{"prompt": prompt, "model": "fake-laya", "stream": false}
|
|
if opts.JSONMode {
|
|
body["format"] = "json"
|
|
}
|
|
raw, _ := json.Marshal(body)
|
|
req, err := http.NewRequestWithContext(ctx, "POST", p.URL+"/api/generate", bytes.NewReader(raw))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp, err := p.Client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return "", fmt.Errorf("fake provider failed")
|
|
}
|
|
var result struct {
|
|
Response string `json:"response"`
|
|
}
|
|
err = json.NewDecoder(resp.Body).Decode(&result)
|
|
return result.Response, err
|
|
}
|