fix: parse configured dispatch delay strings

This commit is contained in:
richjohnrambo
2026-10-13 17:05:53 +08:00
parent acbfaf97a6
commit 7eedbde4ab
2 changed files with 27 additions and 0 deletions
+5
View File
@@ -8,6 +8,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strconv"
"strings"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@@ -457,6 +459,9 @@ func jsonInt(value any) (int, bool) {
case json.Number: case json.Number:
parsed, err := typed.Int64() parsed, err := typed.Int64()
return int(parsed), err == nil return int(parsed), err == nil
case string:
parsed, err := strconv.Atoi(strings.TrimSpace(typed))
return parsed, err == nil
default: default:
return 0, false return 0, false
} }
+22
View File
@@ -2,6 +2,28 @@ package store
import "testing" import "testing"
func TestJSONInt(t *testing.T) {
tests := []struct {
name string
value any
want int
ok bool
}{
{name: "number", value: float64(900), want: 900, ok: true},
{name: "numeric string", value: "1200", want: 1200, ok: true},
{name: "trimmed numeric string", value: " 900 ", want: 900, ok: true},
{name: "invalid string", value: "15 minutes", ok: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, ok := jsonInt(test.value)
if got != test.want || ok != test.ok {
t.Fatalf("jsonInt(%v) = (%d, %v), want (%d, %v)", test.value, got, ok, test.want, test.ok)
}
})
}
}
func TestRandomDelaySeconds(t *testing.T) { func TestRandomDelaySeconds(t *testing.T) {
for index := 0; index < 100; index++ { for index := 0; index < 100; index++ {
value := randomDelaySeconds(900, 1200) value := randomDelaySeconds(900, 1200)