Files
descrybe/apps/api/internal/processing/eprel_test.go
T
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

79 lines
2.2 KiB
Go

package processing
import (
"context"
"testing"
"github.com/descrybe/descrybe-v2/apps/api/internal/eprel"
)
type stubEprel struct {
enabled bool
data *eprel.Data
err error
calls int
lastID string
}
func (s *stubEprel) Enabled() bool { return s.enabled }
func (s *stubEprel) Fetch(_ context.Context, id string) (*eprel.Data, error) {
s.calls++
s.lastID = id
return s.data, s.err
}
func TestRunSteps_EPREL_mergesAttributes(t *testing.T) {
st := &stubEprel{
enabled: true,
data: &eprel.Data{
ID: "246834",
Label: "https://eprel.ec.europa.eu/api/product/246834/labels?format=png",
PDF: "https://eprel.ec.europa.eu/fiches/x.pdf",
EnergyClass: "C",
EnergyScale: "A-G",
},
}
e := &Engine{EPREL: st, Completer: HeuristicCompleter{}, Vector: NoopVectorCategorizer{}}
out, err := e.RunSteps(context.Background(), "co", ProductInput{
Mapped: map[string]any{"name": "Fridge"},
Raw: map[string]any{"EPRELID": "246834"},
}, "eprel_only", nil, StepPolicy{AllowAI: true, AllowEPREL: true})
if err != nil {
t.Fatal(err)
}
if st.calls != 1 || st.lastID != "246834" {
t.Fatalf("calls=%d id=%q", st.calls, st.lastID)
}
if out.ProcessedAttributes["eprel_id"] != "246834" {
t.Fatalf("attrs=%v", out.ProcessedAttributes)
}
if out.ProcessedAttributes["eprel_energy_class"] != "C" {
t.Fatalf("class missing: %v", out.ProcessedAttributes)
}
}
func TestRunSteps_EPREL_disabledOrMissingID(t *testing.T) {
e := &Engine{EPREL: eprel.Disabled{}, Completer: HeuristicCompleter{}, Vector: NoopVectorCategorizer{}}
out, err := e.RunSteps(context.Background(), "co", ProductInput{
Raw: map[string]any{"EPRELID": "1"},
}, "eprel_only", nil, StepPolicy{AllowAI: true, AllowEPREL: true})
if err != nil {
t.Fatal(err)
}
// Disabled enricher still records the discovered id; it must not fetch remote data.
if out.ProcessedAttributes["eprel_energy_class"] != nil {
t.Fatal("should not fetch energy class when disabled")
}
st := &stubEprel{enabled: true}
e.EPREL = st
_, err = e.RunSteps(context.Background(), "co", ProductInput{}, "eprel_only", nil, StepPolicy{AllowAI: true, AllowEPREL: true})
if err != nil {
t.Fatal(err)
}
if st.calls != 0 {
t.Fatal("should not call fetch without id")
}
}