With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
368 lines
11 KiB
Go
368 lines
11 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 TestSessionNeedsUsername(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"
|
|
cart_mode = "session"
|
|
`, map[string]string{"DIBS_AMAZON_PASS": "hunter2"})
|
|
wantErr(t, errs(t, err), "accounts.amazon", 7, "username is missing")
|
|
}
|
|
|
|
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) }
|
|
}
|
|
}
|