From 492b852a1f6e89eff1b74442fe2e7bc4396e4d28 Mon Sep 17 00:00:00 2001 From: shaw Date: Tue, 24 Feb 2026 12:18:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B9=82=E7=AD=89=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=93=88=E5=B8=8C=E5=80=BC=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E8=B6=85=E5=87=BA=20VARCHAR(64)=20=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit idempotency_key_hash 和 request_fingerprint 列为 VARCHAR(64), 而 uniqueTestValue 生成的字符串含完整测试名可能超过 64 字符。 新增 hashedTestValue 辅助函数对测试值做 SHA-256 哈希, 与生产逻辑一致且严格符合列宽限制。 --- .../idempotency_repo_integration_test.go | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/backend/internal/repository/idempotency_repo_integration_test.go b/backend/internal/repository/idempotency_repo_integration_test.go index 021530bd..23b52726 100644 --- a/backend/internal/repository/idempotency_repo_integration_test.go +++ b/backend/internal/repository/idempotency_repo_integration_test.go @@ -4,6 +4,8 @@ package repository import ( "context" + "crypto/sha256" + "encoding/hex" "testing" "time" @@ -11,6 +13,13 @@ import ( "github.com/stretchr/testify/require" ) +// hashedTestValue returns a unique SHA-256 hex string (64 chars) that fits VARCHAR(64) columns. +func hashedTestValue(t *testing.T, prefix string) string { + t.Helper() + sum := sha256.Sum256([]byte(uniqueTestValue(t, prefix))) + return hex.EncodeToString(sum[:]) +} + func TestIdempotencyRepo_CreateProcessing_CompeteSameKey(t *testing.T) { tx := testTx(t) repo := &idempotencyRepository{sql: tx} @@ -19,8 +28,8 @@ func TestIdempotencyRepo_CreateProcessing_CompeteSameKey(t *testing.T) { now := time.Now().UTC() record := &service.IdempotencyRecord{ Scope: uniqueTestValue(t, "idem-scope-create"), - IdempotencyKeyHash: uniqueTestValue(t, "idem-hash"), - RequestFingerprint: uniqueTestValue(t, "idem-fp"), + IdempotencyKeyHash: hashedTestValue(t, "idem-hash"), + RequestFingerprint: hashedTestValue(t, "idem-fp"), Status: service.IdempotencyStatusProcessing, LockedUntil: ptrTime(now.Add(30 * time.Second)), ExpiresAt: now.Add(24 * time.Hour), @@ -33,7 +42,7 @@ func TestIdempotencyRepo_CreateProcessing_CompeteSameKey(t *testing.T) { duplicate := &service.IdempotencyRecord{ Scope: record.Scope, IdempotencyKeyHash: record.IdempotencyKeyHash, - RequestFingerprint: uniqueTestValue(t, "idem-fp-other"), + RequestFingerprint: hashedTestValue(t, "idem-fp-other"), Status: service.IdempotencyStatusProcessing, LockedUntil: ptrTime(now.Add(30 * time.Second)), ExpiresAt: now.Add(24 * time.Hour), @@ -51,8 +60,8 @@ func TestIdempotencyRepo_TryReclaim_StatusAndLockWindow(t *testing.T) { now := time.Now().UTC() record := &service.IdempotencyRecord{ Scope: uniqueTestValue(t, "idem-scope-reclaim"), - IdempotencyKeyHash: uniqueTestValue(t, "idem-hash-reclaim"), - RequestFingerprint: uniqueTestValue(t, "idem-fp-reclaim"), + IdempotencyKeyHash: hashedTestValue(t, "idem-hash-reclaim"), + RequestFingerprint: hashedTestValue(t, "idem-fp-reclaim"), Status: service.IdempotencyStatusProcessing, LockedUntil: ptrTime(now.Add(10 * time.Second)), ExpiresAt: now.Add(24 * time.Hour), @@ -116,8 +125,8 @@ func TestIdempotencyRepo_StatusTransition_ToSucceeded(t *testing.T) { now := time.Now().UTC() record := &service.IdempotencyRecord{ Scope: uniqueTestValue(t, "idem-scope-success"), - IdempotencyKeyHash: uniqueTestValue(t, "idem-hash-success"), - RequestFingerprint: uniqueTestValue(t, "idem-fp-success"), + IdempotencyKeyHash: hashedTestValue(t, "idem-hash-success"), + RequestFingerprint: hashedTestValue(t, "idem-fp-success"), Status: service.IdempotencyStatusProcessing, LockedUntil: ptrTime(now.Add(10 * time.Second)), ExpiresAt: now.Add(24 * time.Hour),