2026-08-10 15:11:38 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-17 17:20:07 +08:00
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
2026-08-10 15:11:38 +08:00
|
|
|
"testing"
|
|
|
|
|
|
2026-08-17 17:20:07 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
2026-08-10 15:11:38 +08:00
|
|
|
"cmautobuy/admin/model"
|
2026-08-17 17:20:07 +08:00
|
|
|
"cmautobuy/admin/service"
|
2026-08-10 15:11:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestTaskPayload_包含不可变执行模式(t *testing.T) {
|
|
|
|
|
payload := taskPayload(&model.Task{TaskID: "T-1", TaskType: model.TaskPurchase, ExecutionMode: model.TaskExecutionLive})
|
|
|
|
|
if payload["execution_mode"] != model.TaskExecutionLive {
|
|
|
|
|
t.Fatalf("execution_mode=%v", payload["execution_mode"])
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-17 17:20:07 +08:00
|
|
|
|
|
|
|
|
func TestResolvePurchaseSpec_请求体上限在进入数据库前拒绝(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
router := gin.New()
|
|
|
|
|
Register(router, nil, nil, service.AIEndpointPolicy{})
|
|
|
|
|
body := bytes.Repeat([]byte("x"), service.MaxPurchaseSpecResolutionBodyBytes+1)
|
|
|
|
|
request := httptest.NewRequest(http.MethodPost,
|
|
|
|
|
"/api/v1/client/tasks/cg255/spec-resolution", bytes.NewReader(body))
|
|
|
|
|
request.Header.Set("X-Client-Id", "CLIENT-255")
|
|
|
|
|
request.Header.Set("Idempotency-Key", "spec-resolution-v1:"+strings.Repeat("0", 64))
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
router.ServeHTTP(recorder, request)
|
|
|
|
|
if recorder.Code != http.StatusBadRequest || !strings.Contains(recorder.Body.String(), `"code":"INVALID_BODY"`) {
|
|
|
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
|
|
|
}
|
|
|
|
|
}
|