116 lines
4.2 KiB
Go
116 lines
4.2 KiB
Go
package web
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"cmautobuy/admin/service"
|
|
)
|
|
|
|
func TestParseInnerCodeIDs_过滤无效并保持首次选择顺序(t *testing.T) {
|
|
got := parseInnerCodeIDs([]string{" 8 ", "bad", "0", "8", "3"})
|
|
if len(got) != 2 || got[0] != 8 || got[1] != 3 {
|
|
t.Fatalf("解析结果不正确: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestInnerCodeRedirect_区分错误反馈并使用默认100条(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
recorder := httptest.NewRecorder()
|
|
context, _ := gin.CreateTestContext(recorder)
|
|
form := url.Values{"page_size": {""}}
|
|
context.Request = httptest.NewRequest(http.MethodPost, "/inner-codes/match", strings.NewReader(form.Encode()))
|
|
context.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
(&Handler{}).innerCodeRedirect(context, "2026-08-15", "pending", "ORDER-1", 2,
|
|
innerCodeFeedbackError, "匹配失败:测试错误")
|
|
if context.Writer.Status() != http.StatusSeeOther {
|
|
t.Fatalf("状态码=%d,期望 303", context.Writer.Status())
|
|
}
|
|
location, err := url.Parse(recorder.Header().Get("Location"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
query := location.Query()
|
|
if location.Path != "/inner-codes" || query.Get("feedback") != "error" ||
|
|
query.Get("message") != "匹配失败:测试错误" || query.Get("page_size") != "100" ||
|
|
query.Get("date") != "2026-08-15" || query.Get("status") != "pending" ||
|
|
query.Get("q") != "ORDER-1" || query.Get("page") != "2" {
|
|
t.Fatalf("反馈跳转参数不完整: %s", recorder.Header().Get("Location"))
|
|
}
|
|
}
|
|
|
|
func TestInnerCodeMatch_不再按20条提前拒绝只读匹配(t *testing.T) {
|
|
raw, err := os.ReadFile("inner_code.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
source := string(raw)
|
|
for _, forbidden := range []string{"InnerCodeRemoteBatchLimit", "一次最多匹配 20 条"} {
|
|
if strings.Contains(source, forbidden) {
|
|
t.Errorf("只读匹配仍包含旧限制 %q", forbidden)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEnsureInnerCodeMatchSession_有效会话不自动登录(t *testing.T) {
|
|
loginCalls := 0
|
|
autoLoggedIn, message := ensureInnerCodeMatchSession(
|
|
func() error { return nil },
|
|
func() (string, bool) { loginCalls++; return "", true },
|
|
)
|
|
if autoLoggedIn || message != "" || loginCalls != 0 {
|
|
t.Fatalf("有效会话不应自动登录: auto=%v message=%q calls=%d", autoLoggedIn, message, loginCalls)
|
|
}
|
|
}
|
|
|
|
func TestEnsureInnerCodeMatchSession_过期后自动登录成功(t *testing.T) {
|
|
loginCalls := 0
|
|
autoLoggedIn, message := ensureInnerCodeMatchSession(
|
|
func() error { return service.ErrSybLoginRequired },
|
|
func() (string, bool) { loginCalls++; return "", true },
|
|
)
|
|
if !autoLoggedIn || message != "" || loginCalls != 1 {
|
|
t.Fatalf("过期会话应自动登录一次: auto=%v message=%q calls=%d", autoLoggedIn, message, loginCalls)
|
|
}
|
|
}
|
|
|
|
func TestEnsureInnerCodeMatchSession_自动登录失败给出恢复路径(t *testing.T) {
|
|
autoLoggedIn, message := ensureInnerCodeMatchSession(
|
|
func() error { return service.ErrSybLoginRequired },
|
|
func() (string, bool) { return "验证码识别服务不可达", false },
|
|
)
|
|
if autoLoggedIn || !strings.Contains(message, "验证码识别服务不可达") ||
|
|
!strings.Contains(message, "顺运宝数据") || !strings.Contains(message, "没有执行匹配") {
|
|
t.Fatalf("自动登录失败提示不完整: auto=%v message=%q", autoLoggedIn, message)
|
|
}
|
|
}
|
|
|
|
func TestEnsureInnerCodeMatchSession_未配置OCR给出明确原因(t *testing.T) {
|
|
_, message := ensureInnerCodeMatchSession(
|
|
func() error { return service.ErrSybLoginRequired },
|
|
func() (string, bool) { return "", false },
|
|
)
|
|
if !strings.Contains(message, "未配置验证码识别服务") {
|
|
t.Fatalf("未配置 OCR 的提示不明确: %q", message)
|
|
}
|
|
}
|
|
|
|
func TestEnsureInnerCodeMatchSession_普通错误不尝试登录(t *testing.T) {
|
|
loginCalls := 0
|
|
autoLoggedIn, message := ensureInnerCodeMatchSession(
|
|
func() error { return errors.New("database unavailable") },
|
|
func() (string, bool) { loginCalls++; return "", true },
|
|
)
|
|
if autoLoggedIn || message != "校验顺运宝会话失败,没有执行匹配。" || loginCalls != 0 {
|
|
t.Fatalf("普通错误不应自动登录: auto=%v message=%q calls=%d", autoLoggedIn, message, loginCalls)
|
|
}
|
|
}
|