重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+39 -39
View File
@@ -10,18 +10,18 @@ import (
// TestOrderDetailNotFound 查询不存在的订单应 404。
func TestOrderDetailNotFound(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodGet, "/api/orders/no-such-order", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
@@ -31,20 +31,20 @@ func TestOrderDetailNotFound(t *testing.T) {
// TestCardEndpointsErrorBranches 卡券相关错误分支:
// 无效 ID → 400;不存在 → 404;请求体非法 → 400。
// TestCardEndpointsErrorBranches 负责Test卡密Endpoints错误Branches相关处理。
// TestCardEndpointsErrorBranches 封装Test卡密Endpoints错误Branches业务协调。
func TestCardEndpointsErrorBranches(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// 无效 card_id(非数字)→ 400。
req := httptest.NewRequest(http.MethodGet, "/cards/abc", nil)
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -54,7 +54,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
// 合法但不存在的 ID → 404。
req2 := httptest.NewRequest(http.MethodGet, "/cards/999999", nil)
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
// rec2 用于本次流程后续判断的rec2
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != http.StatusNotFound {
@@ -64,7 +64,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
// 创建卡券请求体非法 → 400。
req3 := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader("not-json"))
req3.AddCookie(cookie)
// rec3 保存rec3,供当前处理流程使用
// rec3 用于本次流程后续判断的rec3
rec3 := httptest.NewRecorder()
h.ServeHTTP(rec3, req3)
if rec3.Code != http.StatusBadRequest {
@@ -74,7 +74,7 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
// 创建卡券缺类型 → 400(decodeCard 校验)。
req4 := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader(`{"name":"无类型卡"}`))
req4.AddCookie(cookie)
// rec4 保存rec4,供当前处理流程使用
// rec4 用于本次流程后续判断的rec4
rec4 := httptest.NewRecorder()
h.ServeHTTP(rec4, req4)
if rec4.Code != http.StatusBadRequest {
@@ -82,14 +82,14 @@ func TestCardEndpointsErrorBranches(t *testing.T) {
}
}
// TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay 负责TestCreate卡密RejectsUnsupportedAPI类型AndInvalid延迟相关处理。
// TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay 封装TestCreate卡密RejectsUnsupportedAPI类型AndInvalid延迟业务协调。
func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// body 表示当前遍历过程中的请求体
@@ -101,10 +101,10 @@ func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
`{"name":"空库存","type":"data","data_content":"\n","enabled":true}`,
`{"name":"空图片","type":"image","image_url":"","enabled":true}`,
} {
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/cards", strings.NewReader(body))
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -115,10 +115,10 @@ func TestCreateCardRejectsUnsupportedAPITypeAndInvalidDelay(t *testing.T) {
// TestProtectedEndpointsRequireAuth 受保护端点未登录应 401。
func TestProtectedEndpointsRequireAuth(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// path 表示当前遍历过程中的路径
@@ -131,9 +131,9 @@ func TestProtectedEndpointsRequireAuth(t *testing.T) {
"/admin/stats",
"/ai-reply-settings",
} {
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodGet, path, nil)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
@@ -144,31 +144,31 @@ func TestProtectedEndpointsRequireAuth(t *testing.T) {
// TestAdminEndpointRejectsNonAdmin 非 admin 用户访问管理端点应 403。
func TestAdminEndpointRejectsNonAdmin(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, store, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// 创建普通用户并登录。
store.Users.Create(context.Background(), "user2", "u2@e.com", "pw")
// body 保存请求体,供当前处理流程使用
// body 用于本次流程后续判断的请求体
body := `{"username":"user2","password":"pw"}`
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(body))
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("普通用户登录 status=%d", rec.Code)
}
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := rec.Result().Cookies()[0]
// 访问 admin 端点 → 403。
req2 := httptest.NewRequest(http.MethodGet, "/admin/stats", nil)
req2.AddCookie(cookie)
// rec2 保存rec2,供当前处理流程使用
// rec2 用于本次流程后续判断的rec2
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
if rec2.Code != http.StatusForbidden {
@@ -178,19 +178,19 @@ func TestAdminEndpointRejectsNonAdmin(t *testing.T) {
// TestOrderImportEmptyBody 空导入内容应 400。
func TestOrderImportEmptyBody(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// cookie 保存登录凭证,供当前处理流程使用
// cookie 用于本次流程后续判断的登录凭证
cookie := loginHelper(t, h)
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/api/orders/import", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
req.AddCookie(cookie)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
@@ -200,15 +200,15 @@ func TestOrderImportEmptyBody(t *testing.T) {
// TestLoginMalformedJSON 登录请求体非法 JSON 应不致命(400 或失败响应)。
func TestLoginMalformedJSON(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
// srv、cleanup 用于本次流程后续判断的srv、cleanup
srv, _, cleanup := newTestServer(t)
defer cleanup()
// h 保存h,供当前处理流程使用
// h 用于本次流程后续判断的h
h := srv.Router()
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader("not-json"))
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {