Items fire when a page is available from an accepted seller at or below max_price, converted with the ECB's daily rates when the page uses another currency. A per-item cooldown, kept in the state file, holds back repeat alerts unless the price drops. Alerts go to Discord, ntfy and JSON webhooks with retries, and to the desktop; 'dibs notify test' sends a sample.
354 lines
10 KiB
Go
354 lines
10 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const fullConfig = `
|
|
[[item]]
|
|
name = "Metro 2039 CE"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF", "https://www.amazon.co.uk/dp/B0HM497GJJ"]
|
|
max_price = 129.99
|
|
currency = "eur"
|
|
fx_margin = 0.1
|
|
seller = "any"
|
|
interval = "5m"
|
|
|
|
[[item]]
|
|
name = "Console"
|
|
shop = "mediamarkt"
|
|
seller = "fulfilled"
|
|
urls = ["https://www.mediamarkt.de/de/product/_console-123.html"]
|
|
|
|
[accounts.amazon]
|
|
username = "me@example.com"
|
|
password_env = "DIBS_AMAZON_PASS"
|
|
cart_mode = "session"
|
|
twofa = true
|
|
|
|
[notify]
|
|
webhooks = [
|
|
{ url = "https://discord.com/api/webhooks/1/abc", format = "discord" },
|
|
{ url = "https://ntfy.sh/my-topic", format = "ntfy" },
|
|
]
|
|
desktop = false
|
|
cooldown = "1h"
|
|
|
|
[plugins]
|
|
mediamarkt = "plugins/mediamarkt.lua"
|
|
|
|
[sandbox]
|
|
timeout = "500ms"
|
|
session_timeout = "2m"
|
|
memory_mb = 32
|
|
call_stack = 100
|
|
`
|
|
|
|
// load writes doc as dibs.toml in a temp dir, together with an empty
|
|
// plugins/mediamarkt.lua, and loads it.
|
|
func load(t *testing.T, doc string, env map[string]string) (*Config, error) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(dir, "plugins"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "plugins", "mediamarkt.lua"), nil, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(dir, "dibs.toml")
|
|
if err := os.WriteFile(path, []byte(doc), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return Load(path, Options{
|
|
Builtins: []string{"amazon"},
|
|
LookupEnv: func(k string) (string, bool) {
|
|
v, ok := env[k]
|
|
return v, ok
|
|
},
|
|
})
|
|
}
|
|
|
|
// errs asserts that err is Errors and returns it.
|
|
func errs(t *testing.T, err error) Errors {
|
|
t.Helper()
|
|
var es Errors
|
|
if !errors.As(err, &es) {
|
|
t.Fatalf("want config.Errors, got %T: %v", err, err)
|
|
}
|
|
return es
|
|
}
|
|
|
|
// wantErr asserts that es has an error for key on line whose message
|
|
// contains msg.
|
|
func wantErr(t *testing.T, es Errors, key string, line int, msg string) {
|
|
t.Helper()
|
|
for _, e := range es {
|
|
if e.Key == key && e.Line == line && strings.Contains(e.Msg, msg) {
|
|
return
|
|
}
|
|
}
|
|
t.Errorf("no error for %s on line %d containing %q; got:\n%v", key, line, msg, es)
|
|
}
|
|
|
|
func TestLoadFull(t *testing.T) {
|
|
cfg, err := load(t, fullConfig, map[string]string{"DIBS_AMAZON_PASS": "secret"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(cfg.Items) != 2 {
|
|
t.Fatalf("items: got %d, want 2", len(cfg.Items))
|
|
}
|
|
|
|
if s := cfg.Items[1].Seller; s != SellerFulfilled { t.Errorf("item[1].seller: got %q, want fulfilled", s) }
|
|
it := cfg.Items[0]
|
|
if it.Currency != "EUR" {
|
|
t.Errorf("currency: got %q, want it uppercased to EUR", it.Currency)
|
|
}
|
|
if it.MaxPrice == nil || *it.MaxPrice != 129.99 {
|
|
t.Errorf("max_price: got %v", it.MaxPrice)
|
|
}
|
|
if it.Seller != SellerAny || it.Interval.Duration != 5*time.Minute || it.FXMargin != 0.1 {
|
|
t.Errorf("item[0]: got %+v", it)
|
|
}
|
|
|
|
if acc := cfg.Accounts["amazon"]; acc.CartMode != CartModeSession || !acc.TwoFA {
|
|
t.Errorf("account: got %+v", acc)
|
|
}
|
|
if n := cfg.Notify; n.Desktop || n.Cooldown.Duration != time.Hour || len(n.Webhooks) != 2 {
|
|
t.Errorf("notify: got %+v", n)
|
|
}
|
|
want := filepath.Join(filepath.Dir(cfg.Path), "plugins", "mediamarkt.lua")
|
|
if got := cfg.Plugins["mediamarkt"]; got != want {
|
|
t.Errorf("plugin path: got %q, want %q", got, want)
|
|
}
|
|
s := cfg.Sandbox
|
|
if s.Timeout.Duration != 500*time.Millisecond || s.SessionTimeout.Duration != 2*time.Minute ||
|
|
s.MemoryMB != 32 || s.CallStack != 100 {
|
|
t.Errorf("sandbox: got %+v", s)
|
|
}
|
|
}
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
cfg, err := load(t, `
|
|
[[item]]
|
|
name = "x"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
|
|
[accounts.amazon]
|
|
`, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
it := cfg.Items[0]
|
|
if it.Seller != SellerFulfilled || it.Interval.Duration != DefaultInterval || it.MaxPrice != nil {
|
|
t.Errorf("item defaults: got %+v", it)
|
|
}
|
|
if cfg.Accounts["amazon"].CartMode != CartModeDeeplink {
|
|
t.Errorf("cart_mode default: got %q", cfg.Accounts["amazon"].CartMode)
|
|
}
|
|
if !cfg.Notify.Desktop || cfg.Notify.Cooldown.Duration != DefaultCooldown {
|
|
t.Errorf("notify defaults: got %+v", cfg.Notify)
|
|
}
|
|
s := cfg.Sandbox
|
|
if s.Timeout.Duration != DefaultSandboxTimeout || s.SessionTimeout.Duration != DefaultSessionTimeout ||
|
|
s.MemoryMB != DefaultMemoryMB || s.CallStack != DefaultCallStack {
|
|
t.Errorf("sandbox defaults: got %+v", s)
|
|
}
|
|
}
|
|
|
|
func TestUnknownKeys(t *testing.T) {
|
|
_, err := load(t, `
|
|
[[item]]
|
|
name = "x"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
quantity = 2
|
|
|
|
[notfy]
|
|
desktop = true
|
|
`, nil)
|
|
es := errs(t, err)
|
|
wantErr(t, es, "item[0].quantity", 6, "quantity is always 1")
|
|
wantErr(t, es, "notfy", 8, "unknown key")
|
|
}
|
|
|
|
func TestUnknownKeyStillValidatesRest(t *testing.T) {
|
|
_, err := load(t, `
|
|
[[item]]
|
|
name = "x"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
interval = "10s"
|
|
typo = 1
|
|
`, nil)
|
|
es := errs(t, err)
|
|
if len(es) != 2 {
|
|
t.Errorf("want 2 errors, got:\n%v", es)
|
|
}
|
|
wantErr(t, es, "item[0].interval", 6, "at least 1m0s")
|
|
}
|
|
|
|
func TestDecodeErrors(t *testing.T) {
|
|
for name, tc := range map[string]struct {
|
|
doc string
|
|
key string
|
|
line int
|
|
msg string
|
|
}{
|
|
"wrong type": {"[[item]]\nname = \"x\"\nmax_price = \"cheap\"\n", "item[0].max_price", 3, "expected a number, got a TOML string"},
|
|
"wrong element": {"[[item]]\nname = \"x\"\nurls = [1]\n", "item[0].urls[0]", 3, "expected a string, got a TOML integer"},
|
|
"bad duration": {"[[item]]\nname = \"x\"\ninterval = \"3x\"\n", "item[0].interval", 3, "invalid duration"},
|
|
"syntax": {"[[item]]\nname = \n", "", 2, ""},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, err := load(t, tc.doc, nil)
|
|
es := errs(t, err)
|
|
if len(es) != 1 {
|
|
t.Fatalf("want one error, got:\n%v", es)
|
|
}
|
|
if tc.key != "" {
|
|
wantErr(t, es, tc.key, tc.line, tc.msg)
|
|
} else if es[0].Line != tc.line {
|
|
t.Errorf("want error on line %d, got:\n%v", tc.line, es)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidationErrors(t *testing.T) {
|
|
_, err := load(t, `
|
|
[[item]]
|
|
shop = "amazn"
|
|
urls = ["ftp://example.com/x", "https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
max_price = -5
|
|
currency = "EURO"
|
|
seller = "amazon"
|
|
interval = "30s"
|
|
|
|
[[item]]
|
|
name = "dup"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
fx_margin = 0.2
|
|
|
|
[[item]]
|
|
name = "dup"
|
|
shop = "amazon"
|
|
urls = []
|
|
|
|
[accounts.amazon]
|
|
password_env = "DIBS_MISSING"
|
|
cart_mode = "auto"
|
|
login_url = "/signin"
|
|
|
|
[accounts.ebay]
|
|
|
|
[notify]
|
|
webhooks = [
|
|
{ url = "https://ntfy.sh/a", format = "ntfy" },
|
|
{ url = "https://example.com/hook", format = "slack" },
|
|
{ format = "json" },
|
|
]
|
|
cooldown = "-1m"
|
|
|
|
[plugins]
|
|
"Bad Name" = "x.lua"
|
|
missing = "plugins/missing.lua"
|
|
|
|
[sandbox]
|
|
timeout = "1m"
|
|
session_timeout = "1s"
|
|
memory_mb = 0
|
|
call_stack = 1
|
|
`, nil)
|
|
es := errs(t, err)
|
|
|
|
wantErr(t, es, "item[0].name", 2, "missing required key")
|
|
wantErr(t, es, "item[0].shop", 3, `unknown shop "amazn"`)
|
|
wantErr(t, es, "item[0].urls[0]", 4, "http:// or https://")
|
|
wantErr(t, es, "item[0].max_price", 5, "positive")
|
|
wantErr(t, es, "item[0].currency", 6, "ISO 4217")
|
|
wantErr(t, es, "item[0].seller", 7, `"first_party", "fulfilled" or "any"`)
|
|
wantErr(t, es, "item[0].interval", 8, "at least 1m0s")
|
|
wantErr(t, es, "item[1].urls[0]", 13, "duplicate URL (also item[0].urls[1])")
|
|
wantErr(t, es, "item[1].fx_margin", 14, "needs both currency and max_price")
|
|
wantErr(t, es, "item[2].name", 17, "duplicate item name")
|
|
wantErr(t, es, "item[2].urls", 19, "at least one URL")
|
|
wantErr(t, es, "accounts.amazon.password_env", 22, "DIBS_MISSING is not set")
|
|
wantErr(t, es, "accounts.amazon.cart_mode", 23, `"deeplink" or "session"`)
|
|
wantErr(t, es, "accounts.amazon.login_url", 24, "http:// or https://")
|
|
wantErr(t, es, "accounts.ebay", 26, `no plugin named "ebay"`)
|
|
wantErr(t, es, "notify.webhooks[1].format", 31, `got "slack"`)
|
|
wantErr(t, es, "notify.webhooks[2].url", 32, "missing required key")
|
|
wantErr(t, es, "notify.cooldown", 34, "between 0s and 168h")
|
|
wantErr(t, es, "plugins.Bad Name", 37, "plugin name")
|
|
wantErr(t, es, "plugins.missing", 38, "cannot read plugin")
|
|
wantErr(t, es, "sandbox.timeout", 41, "between 100ms and 30s")
|
|
wantErr(t, es, "sandbox.session_timeout", 42, "between 10s and 30m0s")
|
|
wantErr(t, es, "sandbox.memory_mb", 43, "between 4 and 1024")
|
|
wantErr(t, es, "sandbox.call_stack", 44, "between 50 and 10000")
|
|
if len(es) != 24 {
|
|
t.Errorf("want 24 errors, got %d:\n%v", len(es), es)
|
|
}
|
|
for i := 1; i < len(es); i++ {
|
|
if es[i].Line < es[i-1].Line {
|
|
t.Errorf("errors not sorted by line:\n%v", es)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNoItems(t *testing.T) {
|
|
_, err := load(t, "[notify]\ndesktop = true\n", nil)
|
|
es := errs(t, err)
|
|
if len(es) != 1 || !strings.Contains(es[0].Msg, "no [[item]]") {
|
|
t.Errorf("got:\n%v", es)
|
|
}
|
|
}
|
|
|
|
func TestEmptyPasswordEnv(t *testing.T) {
|
|
_, err := load(t, `
|
|
[[item]]
|
|
name = "x"
|
|
shop = "amazon"
|
|
urls = ["https://www.amazon.de/dp/B0HMX2FSTF"]
|
|
|
|
[accounts.amazon]
|
|
password_env = "DIBS_AMAZON_PASS"
|
|
`, map[string]string{"DIBS_AMAZON_PASS": ""})
|
|
wantErr(t, errs(t, err), "accounts.amazon.password_env", 8, "is not set")
|
|
}
|
|
|
|
func TestErrorFormat(t *testing.T) {
|
|
e := Error{File: "dibs.toml", Line: 14, Col: 3, Key: "item[1].interval", Msg: "must be at least 1m0s (got 30s)"}
|
|
if got, want := e.Error(), "dibs.toml:14:3: item[1].interval: must be at least 1m0s (got 30s)"; got != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
e = Error{File: "dibs.toml", Msg: "no [[item]] configured"}
|
|
if got, want := e.Error(), "dibs.toml: no [[item]] configured"; got != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestAcceptsSeller(t *testing.T) {
|
|
for policy, want := range map[string]map[string]bool{
|
|
SellerFirstParty: {"first_party": true, "fulfilled": false, "third_party": false, "unknown": false},
|
|
SellerFulfilled: {"first_party": true, "fulfilled": true, "third_party": false, "unknown": false},
|
|
SellerAny: {"first_party": true, "fulfilled": true, "third_party": true, "unknown": true},
|
|
} {
|
|
for seller, ok := range want {
|
|
if got := (Item{Seller: policy}).AcceptsSeller(seller); got != ok { t.Errorf("policy %s, seller %s: got %v, want %v", policy, seller, got, ok) }
|
|
}
|
|
|
|
if (Item{Seller: policy}).AcceptsSeller("amazon") { t.Errorf("policy %s accepts an invalid seller", policy) }
|
|
}
|
|
}
|