package billing import ( "os" "path/filepath" "strings" "testing" "time" ) const testWallet = "nostr+walletconnect://" + "1111111111111111111111111111111111111111111111111111111111111111" + "?relay=wss%3A%2F%2Frelay.example&secret=" + "2222222222222222222222222222222222222222222222222222222222222222" // TestTheOperatorSetsTheMargin: the fee to list a swap is the gateway operator's number, in basis // points of the BTC leg, and it never rounds down to nothing. func TestTheOperatorSetsTheMargin(t *testing.T) { var settings Settings if err := settings.Set("swap_fee_percent", "0.5"); err != nil { t.Fatalf("set percent: %v", err) } if settings.SwapFeeBPS != 50 { t.Fatalf("0.5%% stored as %d basis points", settings.SwapFeeBPS) } if fee := settings.ListingFee(1_000_000); fee != 5_000 { t.Fatalf("0.5%% of 1,000,000 sats is %d", fee) } // A small swap is charged the floor, not a rounding error. if err := settings.Set("min_fee_sats", "10"); err != nil { t.Fatalf("set floor: %v", err) } if fee := settings.ListingFee(100); fee != 10 { t.Fatalf("the floor was not applied: %d", fee) } // Rounding up: a fee that rounds to zero is a fee that is not charged. if fee := settings.ListingFee(199); fee != 10 { t.Fatalf("fee %d", fee) } if err := settings.Set("swap_fee_bps", "0"); err != nil { t.Fatalf("set zero: %v", err) } if fee := settings.ListingFee(1_000_000); fee != 0 { t.Fatalf("a zero margin charged %d", fee) } } // TestAMarginOutsideTheBoundIsRefused: a mistyped 50 instead of 0.5 is a fee nobody agreed to. func TestAMarginOutsideTheBoundIsRefused(t *testing.T) { var settings Settings if err := settings.Set("swap_fee_bps", "2500"); err == nil { t.Fatal("a 25% margin was accepted") } if err := settings.Set("swap_fee_percent", "25"); err == nil { t.Fatal("a 25% margin was accepted through the percent spelling") } if err := settings.Set("swap_fee_bps", "-1"); err == nil { t.Fatal("a negative margin was accepted") } if err := settings.Set("commision", "50"); err == nil || !strings.Contains(err.Error(), "unknown setting") { t.Fatalf("a mistyped setting was accepted: %v", err) } } // TestAccessIsPricedPerDay: relay access is sats per day, and a part day costs a day. func TestAccessIsPricedPerDay(t *testing.T) { var settings Settings if err := settings.Set("relay_sats_per_day", "1000"); err != nil { t.Fatalf("set rate: %v", err) } cases := map[time.Duration]int64{ 0: 0, time.Hour: 1000, 24 * time.Hour: 1000, 25 * time.Hour: 2000, 72 * time.Hour: 3000, 7 * 24 * time.Hour: 7000, } for period, want := range cases { if got := settings.AccessFee(period); got != want { t.Fatalf("%s: got %d, want %d", period, got, want) } } } // TestTheWalletStringIsACredential: it is stored, redacted when shown, and validated when set. func TestTheWalletStringIsACredential(t *testing.T) { var settings Settings if err := settings.Set("wallet", testWallet); err != nil { t.Fatalf("set wallet: %v", err) } if settings.Wallet != testWallet { t.Fatal("the connection string was not stored as given, so the gateway could not use it") } shown := settings.Redacted() if shown["wallet_configured"] != true { t.Fatalf("configured wallet not reported: %v", shown) } text := strings.ToLower(strings.Join(toStrings(shown), " ")) if strings.Contains(text, "2222222222") { t.Fatalf("the secret appeared in the redacted view: %v", shown) } if shown["wallet_pubkey"] != strings.Repeat("1", 64) { t.Fatalf("wallet key: %v", shown["wallet_pubkey"]) } if err := settings.Set("wallet", "nostr+walletconnect://nothex"); err == nil { t.Fatal("an invalid connection string was accepted") } if err := settings.Set("wallet", "none"); err != nil { t.Fatalf("clearing the wallet: %v", err) } if settings.Wallet != "" { t.Fatal("the wallet was not cleared") } } // TestSettingsSurviveARestart: the price an operator set is the price after a reboot, and the file // is not readable by other users. func TestSettingsSurviveARestart(t *testing.T) { path := filepath.Join(t.TempDir(), "billing.json") settings := Settings{SwapFeeBPS: 50, MinFeeSats: 10, RelaySatsPerDay: 1000} settings.Touch("test") settings.Wallet = testWallet if err := settings.Save(path); err != nil { t.Fatalf("save: %v", err) } info, err := os.Stat(path) if err != nil { t.Fatal(err) } if perm := info.Mode().Perm(); perm != 0o600 { t.Fatalf("settings file is %o, want 600", perm) } loaded, err := Load(path) if err != nil { t.Fatalf("load: %v", err) } if loaded.SwapFeeBPS != 50 || loaded.MinFeeSats != 10 || loaded.RelaySatsPerDay != 1000 { t.Fatalf("loaded %+v", loaded) } if loaded.UpdatedBy != "test" || loaded.UpdatedAt == "" { t.Fatalf("who changed it was not recorded: %+v", loaded) } // A new gateway has no file and no prices, which is not an error. missing, err := Load(filepath.Join(t.TempDir(), "missing.json")) if err != nil || missing.SwapFeeBPS != 0 || missing.Wallet != "" { t.Fatalf("missing file: %+v %v", missing, err) } } func toStrings(m map[string]any) []string { var out []string for key, value := range m { out = append(out, key+"="+strings.TrimSpace(strings.ReplaceAll(sprint(value), "\n", " "))) } return out } func sprint(v any) string { switch value := v.(type) { case string: return value case bool: if value { return "true" } return "false" case int: return strings.TrimSpace(strings.ReplaceAll(strings.Repeat("", 0)+itoa(int64(value)), "\n", "")) case int64: return itoa(value) case float64: return strings.TrimSpace(strings.ReplaceAll(ftoa(value), "\n", "")) } return "" } func itoa(n int64) string { if n == 0 { return "0" } negative := n < 0 if negative { n = -n } var digits []byte for n > 0 { digits = append([]byte{byte('0' + n%10)}, digits...) n /= 10 } if negative { return "-" + string(digits) } return string(digits) } func ftoa(f float64) string { whole := int64(f) frac := int64((f - float64(whole)) * 1000) return itoa(whole) + "." + itoa(frac) }