A table with one row per URL, a level-colored log pane and a status bar. New alerts flash their row and ring the bell. Keys pause, force checks, open the product page or cart link, and reload the config, which is validated first and applied in place, keeping each URL's timing. Headless mode and the TUI now share one watcher. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
5.6 KiB
Go
139 lines
5.6 KiB
Go
package alert
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/config"
|
|
"git.security-command.org/Overlord/dibs/internal/plugin"
|
|
)
|
|
|
|
// usdPerEUR converts USD and EUR at a fixed rate, or fails when down.
|
|
type usdPerEUR struct {
|
|
rate float64
|
|
down bool
|
|
}
|
|
|
|
func (c usdPerEUR) Convert(_ context.Context, amount float64, from, to string) (float64, error) {
|
|
switch {
|
|
case c.down:
|
|
return 0, errors.New("no rates")
|
|
case from == "USD" && to == "EUR":
|
|
return amount / c.rate, nil
|
|
case from == "EUR" && to == "USD":
|
|
return amount * c.rate, nil
|
|
default:
|
|
return 0, errors.New("unexpected conversion " + from + " to " + to)
|
|
}
|
|
}
|
|
|
|
func page(price float64, currency, seller string) *plugin.Report {
|
|
return &plugin.Report{
|
|
Shop: "amazon",
|
|
FetchURL: "https://www.amazon.de/dp/B0HMX2FSTF",
|
|
Result: &plugin.Result{Title: "Metro", Available: true, Price: &price, Currency: currency, Seller: seller},
|
|
}
|
|
}
|
|
|
|
func metro(maxPrice float64) config.Item {
|
|
return config.Item{Name: "metro", Shop: "amazon", MaxPrice: &maxPrice, Currency: "EUR", Seller: config.SellerFulfilled}
|
|
}
|
|
|
|
// TestCooldown follows one item through a day of restocks.
|
|
func TestCooldown(t *testing.T) {
|
|
ctx := context.Background()
|
|
start := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
fx := &usdPerEUR{rate: 1.25}
|
|
e := NewEngine(30*time.Minute, fx, nil)
|
|
it := metro(130)
|
|
|
|
steps := []struct {
|
|
name string
|
|
after time.Duration
|
|
report *plugin.Report
|
|
fires bool
|
|
prev string // the previous price a price-drop alert shows
|
|
} {
|
|
{"first alert", 0, page(129.99, "EUR", plugin.SellerFirstParty), true, ""},
|
|
{"same price in cooldown", 5 * time.Minute, page(129.99, "EUR", plugin.SellerFirstParty), false, ""},
|
|
{"higher price in cooldown", 10 * time.Minute, page(129.995, "EUR", plugin.SellerFulfilled), false, ""},
|
|
{"price drop in cooldown", 15 * time.Minute, page(119.99, "EUR", plugin.SellerFirstParty), true, "129.99 EUR"},
|
|
{"drop restarted the cooldown", 40 * time.Minute, page(119.99, "EUR", plugin.SellerFirstParty), false, ""},
|
|
{"cooldown over", 45 * time.Minute, page(125, "EUR", plugin.SellerFirstParty), true, ""},
|
|
// 140 USD is 112 EUR, cheaper than 125 EUR.
|
|
{"cheaper in another currency", 50 * time.Minute, page(140, "USD", plugin.SellerFirstParty), true, "125.00 EUR"},
|
|
// Same currency as the last alert: compared in USD, so the euro
|
|
// strengthening to 1.30 USD does not make it a drop to 107.69 EUR.
|
|
{"same USD price, new rate", 55 * time.Minute, page(140, "USD", plugin.SellerFirstParty), false, ""},
|
|
}
|
|
|
|
for _, st := range steps {
|
|
if strings.HasSuffix(st.name, "new rate") { fx.rate = 1.30 }
|
|
|
|
d, err := e.Consider(ctx, it, st.report, start.Add(st.after))
|
|
if err != nil { t.Fatalf("%s: %v", st.name, err) }
|
|
|
|
a := d.Alert
|
|
if fired := a != nil; fired != st.fires { t.Errorf("%s: fired = %v, want %v (%s)", st.name, fired, st.fires, d.Reason) }
|
|
if !d.Qualifies { t.Errorf("%s: does not qualify: %s", st.name, d.Reason) }
|
|
if a == nil { continue }
|
|
|
|
prev := ""
|
|
if a.PreviousPrice != nil { prev = Money(*a.PreviousPrice, a.PreviousCurrency) }
|
|
if prev != st.prev { t.Errorf("%s: previous price %q, want %q", st.name, prev, st.prev) }
|
|
}
|
|
}
|
|
|
|
func TestQualify(t *testing.T) {
|
|
ctx := context.Background()
|
|
now := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
|
|
noPrice := page(0, "EUR", plugin.SellerFirstParty)
|
|
noPrice.Result.Price = nil
|
|
|
|
withMargin := metro(120)
|
|
withMargin.FXMargin = 0.1
|
|
|
|
anySeller := metro(130)
|
|
anySeller.Seller = config.SellerAny
|
|
|
|
noLimit := config.Item{Name: "metro", Currency: "EUR", Seller: config.SellerAny}
|
|
|
|
cases := []struct {
|
|
name string
|
|
item config.Item
|
|
report *plugin.Report
|
|
fx usdPerEUR
|
|
want string // "fire", "error" or part of the reason
|
|
} {
|
|
{"at max_price", metro(129.99), page(129.99, "EUR", plugin.SellerFirstParty), usdPerEUR{}, "fire"},
|
|
{"above max_price", metro(129.99), page(130, "EUR", plugin.SellerFirstParty), usdPerEUR{}, "above max_price"},
|
|
{"marketplace seller", metro(130), page(99, "EUR", plugin.SellerThirdParty), usdPerEUR{}, "seller is third_party"},
|
|
{"unknown seller, any", anySeller, page(99, "EUR", plugin.SellerUnknown), usdPerEUR{}, "fire"},
|
|
{"no price", metro(130), noPrice, usdPerEUR{}, "no price"},
|
|
// 125 USD is 100 EUR, 110 EUR with the margin.
|
|
{"converted within", withMargin, page(125, "USD", plugin.SellerFirstParty), usdPerEUR{rate: 1.25}, "fire"},
|
|
// 140 USD is 112 EUR, 123.20 EUR with the margin.
|
|
{"margin tips it over", withMargin, page(140, "USD", plugin.SellerFirstParty), usdPerEUR{rate: 1.25}, "with fx_margin is above"},
|
|
{"no rates", metro(130), page(99, "USD", plugin.SellerFirstParty), usdPerEUR{down: true}, "error"},
|
|
{"no rates, no max_price", noLimit, page(99, "USD", plugin.SellerFirstParty), usdPerEUR{down: true}, "fire"},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
d, err := NewEngine(time.Hour, c.fx, nil).Consider(ctx, c.item, c.report, now)
|
|
a, why := d.Alert, d.Reason
|
|
|
|
switch {
|
|
case c.want == "fire" && a == nil:
|
|
t.Errorf("%s: did not fire: %s %v", c.name, why, err)
|
|
case c.want == "error" && err == nil:
|
|
t.Errorf("%s: no error (fired: %v, reason %q)", c.name, a != nil, why)
|
|
case c.want != "fire" && c.want != "error" && !strings.Contains(why, c.want):
|
|
t.Errorf("%s: reason %q, want %q (fired: %v, err %v)", c.name, why, c.want, a != nil, err)
|
|
}
|
|
}
|
|
}
|