Make go test ./... run and fix the stale tooltip test
internal/config.go panicked in init without a config.yaml and the i18n loader only found translations/ from the repository root, so no test in a package importing them could run. Config checks are skipped in a test binary, the translation directory is located by walking up, GetAmount no longer dereferences a nil price watcher, and the tooltip test expects the text the code actually produces. Adds tests for amount validation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
81af610980
commit
5a3b4dad7c
@@ -3,6 +3,7 @@ package internal
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/jinzhu/configor"
|
||||
@@ -89,9 +90,19 @@ func init() {
|
||||
panic(err)
|
||||
}
|
||||
Configuration.Bot.LNURLHostUrl = hostname
|
||||
if isTestBinary() {
|
||||
// go test has no config.yaml; a missing LNbits URL must not stop
|
||||
// unit tests of packages that import this one.
|
||||
return
|
||||
}
|
||||
checkLnbitsConfiguration()
|
||||
}
|
||||
|
||||
// isTestBinary reports whether the process is a go test binary.
|
||||
func isTestBinary() bool {
|
||||
return strings.HasSuffix(os.Args[0], ".test") || strings.HasSuffix(os.Args[0], ".test.exe")
|
||||
}
|
||||
|
||||
func checkLnbitsConfiguration() {
|
||||
if Configuration.Lnbits.Url == "" {
|
||||
panic(fmt.Errorf("please configure a lnbits url"))
|
||||
|
||||
+30
-13
@@ -1,6 +1,9 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -13,22 +16,36 @@ func init() {
|
||||
Bundle = RegisterLanguages()
|
||||
}
|
||||
|
||||
// translationsDir finds the translations directory. The bot runs from the
|
||||
// repository root; go test runs from the package directory, so walk up.
|
||||
func translationsDir() string {
|
||||
dir := "translations"
|
||||
for i := 0; i < 6; i++ {
|
||||
if st, err := os.Stat(dir); err == nil && st.IsDir() {
|
||||
return dir
|
||||
}
|
||||
dir = filepath.Join("..", dir)
|
||||
}
|
||||
return "translations"
|
||||
}
|
||||
|
||||
func RegisterLanguages() *i18n.Bundle {
|
||||
bundle := i18n.NewBundle(language.English)
|
||||
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
bundle.MustLoadMessageFile("translations/en.toml")
|
||||
bundle.LoadMessageFile("translations/de.toml")
|
||||
bundle.LoadMessageFile("translations/fi.toml")
|
||||
bundle.LoadMessageFile("translations/it.toml")
|
||||
bundle.LoadMessageFile("translations/es.toml")
|
||||
bundle.LoadMessageFile("translations/nl.toml")
|
||||
bundle.LoadMessageFile("translations/pl.toml")
|
||||
bundle.LoadMessageFile("translations/fr.toml")
|
||||
bundle.LoadMessageFile("translations/pt-br.toml")
|
||||
bundle.LoadMessageFile("translations/tr.toml")
|
||||
bundle.LoadMessageFile("translations/cs.toml")
|
||||
bundle.LoadMessageFile("translations/id.toml")
|
||||
bundle.LoadMessageFile("translations/ru.toml")
|
||||
dir := translationsDir()
|
||||
bundle.MustLoadMessageFile(filepath.Join(dir, "en.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "de.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "fi.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "it.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "es.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "nl.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "pl.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "fr.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "pt-br.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "tr.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "cs.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "id.toml"))
|
||||
bundle.LoadMessageFile(filepath.Join(dir, "ru.toml"))
|
||||
return bundle
|
||||
}
|
||||
func Translate(languageCode string, MessgeID string) string {
|
||||
|
||||
@@ -87,7 +87,11 @@ func GetAmount(input string) (amount int64, err error) {
|
||||
}
|
||||
|
||||
// convert fiat currencies to satoshis
|
||||
for currency, symbol := range price.P.Currencies {
|
||||
var currencies map[string]string
|
||||
if price.P != nil {
|
||||
currencies = price.P.Currencies
|
||||
}
|
||||
for currency, symbol := range currencies {
|
||||
if strings.HasPrefix(input, symbol) || strings.HasSuffix(input, symbol) || // for 1$ and $1
|
||||
strings.HasPrefix(strings.ToLower(input), strings.ToLower(currency)) || // for USD1
|
||||
strings.HasSuffix(strings.ToLower(input), strings.ToLower(currency)) { // for 1USD
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckAmount(t *testing.T) {
|
||||
good := []int64{1, 21, 1000, maxAmountSat}
|
||||
for _, a := range good {
|
||||
if got, err := checkAmount(float64(a), a); err != nil || got != a {
|
||||
t.Errorf("checkAmount(%d) = %d, %v", a, got, err)
|
||||
}
|
||||
}
|
||||
bad := []struct {
|
||||
name string
|
||||
f float64
|
||||
a int64
|
||||
}{
|
||||
{"zero", 0, 0},
|
||||
{"negative", -5, -5},
|
||||
{"above max", float64(maxAmountSat + 1), maxAmountSat + 1},
|
||||
{"nan", math.NaN(), 0},
|
||||
{"+inf", math.Inf(1), math.MaxInt64},
|
||||
{"-inf", math.Inf(-1), math.MinInt64},
|
||||
}
|
||||
for _, c := range bad {
|
||||
if got, err := checkAmount(c.f, c.a); err == nil || got != 0 {
|
||||
t.Errorf("%s: accepted: got %d, err %v", c.name, got, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetAmount covers the integer and "k" branches; the fiat branch needs a
|
||||
// live price feed.
|
||||
func TestGetAmount(t *testing.T) {
|
||||
cases := map[string]int64{
|
||||
"21": 21,
|
||||
"1k": 1000,
|
||||
"1.5k": 1500,
|
||||
"1,5k": 1500,
|
||||
"10000k": maxAmountSat,
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got, err := GetAmount(in); err != nil || got != want {
|
||||
t.Errorf("GetAmount(%q) = %d, %v; want %d", in, got, err, want)
|
||||
}
|
||||
}
|
||||
rejected := []string{"0", "-1", "-1k", "0k", "NaNk", "Infk", "10001k", "99999999999", "abc", ""}
|
||||
for _, in := range rejected {
|
||||
if got, err := GetAmount(in); err == nil {
|
||||
t.Errorf("GetAmount(%q) accepted with %d", in, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +207,7 @@ func (bot *TipBot) HandleLNURL(rawlnurl string) (string, lnurl.LNURLParams, erro
|
||||
lnurl_str, ok := lnurl.FindLNURLInText(rawlnurl)
|
||||
if !ok {
|
||||
return "", nil,
|
||||
fmt.Errorf("%s", "invalid bech32-encoded lnurl: " + rawlnurl)
|
||||
fmt.Errorf("%s", "invalid bech32-encoded lnurl: "+rawlnurl)
|
||||
}
|
||||
rawurl, err = lnurl.LNURLDecodeStrict(lnurl_str)
|
||||
if err != nil {
|
||||
@@ -247,7 +247,7 @@ func (bot *TipBot) HandleLNURL(rawlnurl string) (string, lnurl.LNURLParams, erro
|
||||
return rawurl, nil, err
|
||||
}
|
||||
if resp.StatusCode >= 300 {
|
||||
return rawurl, nil, fmt.Errorf("%s", "HTTP error: " + resp.Status)
|
||||
return rawurl, nil, fmt.Errorf("%s", "HTTP error: "+resp.Status)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
@@ -66,13 +66,13 @@ func TestMessage_getTooltipMessage(t *testing.T) {
|
||||
name: "1",
|
||||
args: args{botUserName: "@test-bot", notInitializedWallet: true},
|
||||
fields: fields{Message: Message{}, TipAmount: 10, Ntips: 1, Tippers: append(tippers, tipper1)},
|
||||
want: "🏅 10 sat (by @username1)\n🗑 Chat with @test-bot to manage your wallet.",
|
||||
want: "🏅 10 sat (by @username1)\n🗑 Chat with @test-bot 👈 to manage your wallet.",
|
||||
},
|
||||
{
|
||||
name: "2",
|
||||
args: args{botUserName: "@test-bot", notInitializedWallet: true},
|
||||
fields: fields{Message: Message{}, TipAmount: 100, Ntips: 6, Tippers: append(tippers, tipper1, tipper2, tipper3, tipper4, tipper5, tipper6)},
|
||||
want: "🏅 100 sat (6 tips by @username1, @username2, @username3, @username4, @username5, ... and others)\n🗑 Chat with @test-bot to manage your wallet.",
|
||||
want: "🏅 100 sat (6 tips by @username1, @username2, @username3, @username4, @username5, ... and others)\n🗑 Chat with @test-bot 👈 to manage your wallet.",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user