feat: 添加批量生成CDK功能
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
This commit is contained in:
76
modules/config/service/cdk_test.go
Normal file
76
modules/config/service/cdk_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerate16CharSecure(t *testing.T) {
|
||||
for i := 0; i < 100; i++ {
|
||||
code := Generate16CharSecure()
|
||||
if len(code) != cdkCodeLength {
|
||||
t.Fatalf("expected code length %d, got %d", cdkCodeLength, len(code))
|
||||
}
|
||||
|
||||
for _, ch := range code {
|
||||
if !containsRune(charsetWithSymbol, ch) {
|
||||
t.Fatalf("unexpected character %q in code %q", ch, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildUniqueCodesSkipsDuplicatesAndExisting(t *testing.T) {
|
||||
sequence := []string{
|
||||
"DUPLICATE-000001",
|
||||
"DUPLICATE-000001",
|
||||
"EXISTING-000001",
|
||||
"VALID-000000001",
|
||||
"VALID-000000002",
|
||||
"VALID-000000003",
|
||||
"VALID-000000004",
|
||||
"VALID-000000005",
|
||||
"VALID-000000006",
|
||||
}
|
||||
index := 0
|
||||
|
||||
codes, err := buildUniqueCodes(3, func() (string, error) {
|
||||
if index >= len(sequence) {
|
||||
return "", errors.New("generator exhausted")
|
||||
}
|
||||
code := sequence[index]
|
||||
index++
|
||||
return code, nil
|
||||
}, func(candidates []string) (map[string]struct{}, error) {
|
||||
return map[string]struct{}{
|
||||
"EXISTING-000001": {},
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("buildUniqueCodes returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(codes) != 3 {
|
||||
t.Fatalf("expected 3 codes, got %d", len(codes))
|
||||
}
|
||||
|
||||
want := []string{
|
||||
"VALID-000000001",
|
||||
"VALID-000000002",
|
||||
"VALID-000000003",
|
||||
}
|
||||
for i, code := range want {
|
||||
if codes[i] != code {
|
||||
t.Fatalf("expected code %q at index %d, got %q", code, i, codes[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func containsRune(value string, target rune) bool {
|
||||
for _, ch := range value {
|
||||
if ch == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user