refactor: 重构trustlog-sdk目录结构到trustlog/go-trustlog

- 将所有trustlog-sdk文件移动到trustlog/go-trustlog/目录
- 更新README中所有import路径从trustlog-sdk改为go-trustlog
- 更新cookiecutter配置文件中的项目名称
- 更新根目录.lefthook.yml以引用新位置的配置
- 添加go.sum文件到版本控制
- 删除过时的示例文件

这次重构与trustlog-server保持一致的目录结构,
为未来支持多语言SDK(Python、Java等)预留空间。
This commit is contained in:
ryan
2025-12-22 13:37:57 +08:00
commit d313449c5c
87 changed files with 20622 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
package helpers_test
import (
"regexp"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/trustlog-sdk/internal/helpers"
)
func TestNewUUIDv7(t *testing.T) {
// UUID v7 格式无连字符32个十六进制字符
uuidPattern := regexp.MustCompile(`^[0-9a-f]{32}$`)
t.Run("生成有效的UUID", func(t *testing.T) {
uuid := helpers.NewUUIDv7()
// 验证格式
assert.Len(t, uuid, 32, "UUID长度应该是32个字符")
assert.Regexp(t, uuidPattern, uuid, "UUID应该只包含小写十六进制字符")
})
t.Run("每次生成的UUID应该不同", func(t *testing.T) {
uuid1 := helpers.NewUUIDv7()
uuid2 := helpers.NewUUIDv7()
uuid3 := helpers.NewUUIDv7()
assert.NotEqual(t, uuid1, uuid2)
assert.NotEqual(t, uuid2, uuid3)
assert.NotEqual(t, uuid1, uuid3)
})
t.Run("UUID格式验证", func(t *testing.T) {
uuid := helpers.NewUUIDv7()
// UUID v7 应该是 32 个十六进制字符
require.Len(t, uuid, 32)
// 检查每个字符都是有效的十六进制
for i, c := range uuid {
assert.True(t,
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'),
"字符 %c 在位置 %d 不是有效的十六进制字符", c, i)
}
})
t.Run("并发生成UUID", func(t *testing.T) {
const concurrency = 100
uuids := make([]string, concurrency)
var wg sync.WaitGroup
wg.Add(concurrency)
for i := range concurrency {
go func(idx int) {
defer wg.Done()
uuids[idx] = helpers.NewUUIDv7()
}(i)
}
wg.Wait()
// 验证所有 UUID 都不为空且格式正确
for i, uuid := range uuids {
assert.NotEmpty(t, uuid, "UUID %d 不应该为空", i)
assert.Regexp(t, uuidPattern, uuid, "UUID %d 格式不正确", i)
}
// 验证所有 UUID 都是唯一的
uniqueMap := make(map[string]bool)
for _, uuid := range uuids {
assert.False(t, uniqueMap[uuid], "UUID重复: %s", uuid)
uniqueMap[uuid] = true
}
assert.Len(t, uniqueMap, concurrency, "应该生成%d个唯一的UUID", concurrency)
})
t.Run("UUID包含时间戳信息", func(t *testing.T) {
// 连续生成多个UUID它们的时间戳部分应该相近或递增
uuid1 := helpers.NewUUIDv7()
uuid2 := helpers.NewUUIDv7()
// UUID v7 的前12个字符主要是时间戳
// 在很短的时间内生成的UUID时间戳部分应该相同或非常接近
timePrefix1 := uuid1[:12]
timePrefix2 := uuid2[:12]
// 时间戳应该相同或第二个略大(因为时间在递增)
assert.True(t,
timePrefix1 == timePrefix2 || timePrefix1 <= timePrefix2,
"UUID的时间戳部分应该单调递增")
})
t.Run("批量生成UUID性能测试", func(t *testing.T) {
const iterations = 1000
uuids := make([]string, iterations)
for i := range iterations {
uuids[i] = helpers.NewUUIDv7()
}
// 验证所有UUID都有效
for i, uuid := range uuids {
assert.Regexp(t, uuidPattern, uuid, "UUID %d 格式不正确", i)
}
// 简单的唯一性检查
uniqueMap := make(map[string]bool)
for _, uuid := range uuids {
uniqueMap[uuid] = true
}
assert.Len(t, uniqueMap, iterations, "应该生成%d个唯一的UUID", iterations)
})
}
func TestNewUUIDv7_Format(t *testing.T) {
// 测试UUID v7的具体格式要求
uuid := helpers.NewUUIDv7()
// 总长度 32
assert.Len(t, uuid, 32)
// 全部小写
for _, c := range uuid {
if c >= 'a' && c <= 'f' {
assert.True(t, c >= 'a' && c <= 'f')
} else {
assert.True(t, c >= '0' && c <= '9')
}
}
}
func TestNewUUIDv7_EdgeCases(t *testing.T) {
t.Run("快速连续生成", func(t *testing.T) {
// 在极短时间内生成多个UUID
uuids := make([]string, 10)
for i := range 10 {
uuids[i] = helpers.NewUUIDv7()
}
// 所有UUID应该都有效且唯一
seen := make(map[string]bool)
for _, uuid := range uuids {
assert.Len(t, uuid, 32)
assert.False(t, seen[uuid], "UUID不应该重复")
seen[uuid] = true
}
})
}