108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"veola/internal/apify"
|
|
)
|
|
|
|
func ptr(f float64) *float64 { return &f }
|
|
|
|
func TestShouldAlert(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
target *float64
|
|
price float64
|
|
want bool
|
|
}{
|
|
{"no target alerts on any positive price", nil, 12.34, true},
|
|
{"no target alerts even on zero price", nil, 0, true},
|
|
{"price below target", ptr(60), 42, true},
|
|
{"price equal to target", ptr(60), 60, true},
|
|
{"price above target", ptr(60), 70, false},
|
|
{"target set but price unknown", ptr(60), 0, false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
got := ShouldAlert(c.target, c.price)
|
|
if got != c.want {
|
|
t.Errorf("got %v want %v", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFilterResults(t *testing.T) {
|
|
in := []apify.UnifiedResult{
|
|
{URL: "a", Price: 10, MatchConfidence: 0.9},
|
|
{URL: "b", Price: 10, MatchConfidence: 0.4},
|
|
{URL: "c", Price: 10, OutOfStock: true},
|
|
{URL: "", Price: 10},
|
|
{URL: "e", Price: 0},
|
|
{URL: "f", Price: 12},
|
|
}
|
|
got := FilterResults(in, 0.6, false)
|
|
if len(got) != 2 {
|
|
t.Fatalf("expected 2 results, got %d", len(got))
|
|
}
|
|
if got[0].URL != "a" || got[1].URL != "f" {
|
|
t.Errorf("unexpected filter output: %+v", got)
|
|
}
|
|
got2 := FilterResults(in, 0.6, true)
|
|
if len(got2) != 3 {
|
|
t.Errorf("expected 3 with OOS, got %d", len(got2))
|
|
}
|
|
}
|
|
|
|
func TestApplyItemFilters(t *testing.T) {
|
|
in := []apify.UnifiedResult{
|
|
{URL: "a", Title: "Sony A7 III body", Price: 1200},
|
|
{URL: "b", Title: "Sony A7 III battery grip", Price: 45},
|
|
{URL: "c", Title: "Sony A7 III lens cap", Price: 12},
|
|
{URL: "d", Title: "Sony A7 III with strap", Price: 1100},
|
|
{URL: "e", Title: "for parts not working", Price: 800},
|
|
}
|
|
got := ApplyItemFilters(in, ptr(100), []string{"grip", "lens cap", "for parts"})
|
|
if len(got) != 2 {
|
|
t.Fatalf("expected 2 results after filter, got %d: %+v", len(got), got)
|
|
}
|
|
if got[0].URL != "a" || got[1].URL != "d" {
|
|
t.Errorf("unexpected filter output: %+v", got)
|
|
}
|
|
|
|
// Nil/empty filters are no-ops.
|
|
got = ApplyItemFilters(in, nil, nil)
|
|
if len(got) != len(in) {
|
|
t.Errorf("nil filters dropped rows: got %d want %d", len(got), len(in))
|
|
}
|
|
}
|
|
|
|
func TestDedupByURL(t *testing.T) {
|
|
in := []apify.UnifiedResult{
|
|
{Source: "ebay", URL: "https://a", MatchedQuery: "alpha"},
|
|
{Source: "ebay", URL: "https://b", MatchedQuery: "alpha"},
|
|
{Source: "ebay", URL: "https://a", MatchedQuery: "beta"}, // dup of #0
|
|
{Source: "yahoo-auctions-jp", URL: "https://a"}, // different source, same url -> kept
|
|
}
|
|
got := DedupByURL(in)
|
|
if len(got) != 3 {
|
|
t.Fatalf("expected 3 deduped, got %d: %+v", len(got), got)
|
|
}
|
|
if got[0].MatchedQuery != "alpha" {
|
|
t.Errorf("first-occurrence MatchedQuery lost: %+v", got[0])
|
|
}
|
|
}
|
|
|
|
func TestPickBest(t *testing.T) {
|
|
rs := []apify.UnifiedResult{
|
|
{Price: 50}, {Price: 30}, {Price: 90}, {Price: 30},
|
|
}
|
|
got := PickBest(rs)
|
|
if got != 1 {
|
|
t.Errorf("expected index 1, got %d", got)
|
|
}
|
|
if PickBest(nil) != -1 {
|
|
t.Error("expected -1 for empty")
|
|
}
|
|
}
|