21c895a008
改进内容: - sdogid 不再硬编码,优先从 $BYPOS_SDOGID 环境变量读取,其次 -sdogid 参数, 也可在 Web 控制台输入框填写;三者都未设时启动警告、采集报错 - 网络错误自动重试(最多 3 次,指数退避 500ms/1s/2s) - Web 控制台采集参数区增加 sdogid 输入框 - CSV 导出补全 retmsg 和 source 两列 - 新增 Go 单元测试(ean13Check、md5hex、sanitizeBarcodes、resolveSdogID 等) - README 更新配置说明 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestEan13Check_valid(t *testing.T) {
|
|
tests := []struct {
|
|
body string
|
|
want string
|
|
}{
|
|
{"692045990501", "6920459905012"},
|
|
{"690100000000", "6901000000004"},
|
|
{"690100000099", "6901000000998"},
|
|
}
|
|
for _, tt := range tests {
|
|
got, ok := ean13Check(tt.body)
|
|
if !ok {
|
|
t.Errorf("ean13Check(%q) returned not ok", tt.body)
|
|
continue
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ean13Check(%q) = %q, want %q", tt.body, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEan13Check_invalid(t *testing.T) {
|
|
cases := []string{"", "12345", "12345678901", "1234567890123", "69010000a000"}
|
|
for _, body := range cases {
|
|
_, ok := ean13Check(body)
|
|
if ok {
|
|
t.Errorf("ean13Check(%q) should return not ok", body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMd5hex(t *testing.T) {
|
|
got := md5hex("137966")
|
|
if len(got) != 32 {
|
|
t.Errorf("md5hex should return 32-char hex, got len=%d", len(got))
|
|
}
|
|
if got != md5hex("137966") {
|
|
t.Error("md5hex should be deterministic")
|
|
}
|
|
if got == md5hex("other") {
|
|
t.Error("md5hex should differ for different inputs")
|
|
}
|
|
}
|
|
|
|
func TestSanitizeBarcodes(t *testing.T) {
|
|
got := sanitizeBarcodes("6920459905012\n6901028941068 123")
|
|
want := []string{"6920459905012", "6901028941068", "123"}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("len = %d, want %d", len(got), len(want))
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Errorf("got[%d] = %q, want %q", i, got[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSanitizeBarcodes_empty(t *testing.T) {
|
|
got := sanitizeBarcodes(" \n\t ")
|
|
if len(got) != 0 {
|
|
t.Errorf("expected empty, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveSdogID_envOverride(t *testing.T) {
|
|
os.Setenv("BYPOS_SDOGID", "999999")
|
|
defer os.Unsetenv("BYPOS_SDOGID")
|
|
got := resolveSdogID("111111")
|
|
if got != "999999" {
|
|
t.Errorf("expected env var override, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveSdogID_fallback(t *testing.T) {
|
|
os.Unsetenv("BYPOS_SDOGID")
|
|
got := resolveSdogID("111111")
|
|
if got != "111111" {
|
|
t.Errorf("expected fallback to explicit, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveSdogID_empty(t *testing.T) {
|
|
os.Unsetenv("BYPOS_SDOGID")
|
|
got := resolveSdogID("")
|
|
if got != "" {
|
|
t.Errorf("expected empty, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestNewCollector_sdogFromEnv(t *testing.T) {
|
|
os.Setenv("BYPOS_SDOGID", "888888")
|
|
defer os.Unsetenv("BYPOS_SDOGID")
|
|
c := NewCollector("")
|
|
if c.sdogID != "888888" {
|
|
t.Errorf("expected sdogID from env, got %q", c.sdogID)
|
|
}
|
|
}
|
|
|
|
func TestStartRejectsEmptySdogID(t *testing.T) {
|
|
os.Unsetenv("BYPOS_SDOGID")
|
|
c := NewCollector("")
|
|
err := c.Start(JobReq{Mode: "list", List: "6920459905012"})
|
|
if err == nil {
|
|
t.Fatal("expected error for empty sdogid")
|
|
}
|
|
}
|