新增统一项目根实体及三库迁移并验证项目备份恢复

This commit is contained in:
xuans committed 2026-09-28 10:15:08 +08:00
1 parent adb7d7f536
commit 6b72ac2e20
20 files changed
+846 -4

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
package domain
import (
"errors"
"regexp"
"strings"
"time"
"unicode"
"unicode/utf8"
)
const DefaultProjectID = "default"
var projectIDPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,35}$`)
type Project struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func NewProject(name, description string) (Project, error) {
now := time.Now().UTC()
item := (Project{
ID: NewID(), Name: name, Description: description,
CreatedAt: now, UpdatedAt: now,
}).Normalize()
if err := item.Validate(); err != nil {
return Project{}, err
}
return item, nil
}
func (p Project) Normalize() Project {
p.ID = strings.TrimSpace(p.ID)
p.Name = strings.TrimSpace(p.Name)
p.Description = strings.TrimSpace(p.Description)
return p
}
func (p Project) Validate() error {
if !projectIDPattern.MatchString(p.ID) {
return errors.New("项目 ID 必须为 1~36 位小写字母、数字、连字符或下划线,并以字母或数字开头")
}
if !utf8.ValidString(p.Name) || strings.TrimSpace(p.Name) == "" || utf8.RuneCountInString(p.Name) > 128 {
return errors.New("项目名称必须为 1~128 个字符")
}
if strings.ContainsFunc(p.Name, unicode.IsControl) {
return errors.New("项目名称不能包含控制字符")
}
if !utf8.ValidString(p.Description) || utf8.RuneCountInString(p.Description) > 4096 {
return errors.New("项目说明不能超过 4096 个字符")
}
if strings.ContainsFunc(p.Description, func(r rune) bool {
return unicode.IsControl(r) && r != '\n' && r != '\r' && r != '\t'
}) {
return errors.New("项目说明包含无效控制字符")
}
return nil
}
+56
View File
@@ -0,0 +1,56 @@
package domain
import (
"strings"
"testing"
)
func TestNewProject(t *testing.T) {
item, err := NewProject(" 证书项目 ", " 项目说明\n支持换行 ")
if err != nil {
t.Fatal(err)
}
if item.ID == DefaultProjectID || item.Name != "证书项目" || item.Description != "项目说明\n支持换行" {
t.Fatalf("项目规范化失败:%+v", item)
}
if item.CreatedAt.IsZero() || !item.CreatedAt.Equal(item.UpdatedAt) || item.CreatedAt.Location().String() != "UTC" {
t.Fatal("项目时间戳未初始化为统一 UTC 时间")
}
other, err := NewProject(item.Name, "")
if err != nil || other.ID == item.ID {
t.Fatal("独立项目 ID 必须唯一,同名项目不应混淆")
}
}
func TestProjectValidation(t *testing.T) {
cases := []struct {
name string
item Project
valid bool
}{
{"default", Project{ID: DefaultProjectID, Name: "默认项目"}, true},
{"unicode_limit", Project{ID: "project-1", Name: strings.Repeat("证", 128), Description: strings.Repeat("书", 4096)}, true},
{"multiline", Project{ID: "project_2", Name: "项目", Description: "一\r\n二\t三"}, true},
{"empty_id", Project{Name: "项目"}, false},
{"long_id", Project{ID: strings.Repeat("a", 37), Name: "项目"}, false},
{"uppercase_id", Project{ID: "Default", Name: "项目"}, false},
{"unsafe_id", Project{ID: "../a", Name: "项目"}, false},
{"empty_name", Project{ID: "a", Name: " "}, false},
{"long_name", Project{ID: "a", Name: strings.Repeat("证", 129)}, false},
{"name_control", Project{ID: "a", Name: "证\x00书"}, false},
{"invalid_name_encoding", Project{ID: "a", Name: string([]byte{0xff})}, false},
{"long_description", Project{ID: "a", Name: "项目", Description: strings.Repeat("书", 4097)}, false},
{"description_control", Project{ID: "a", Name: "项目", Description: "证\x00书"}, false},
{"invalid_description_encoding", Project{ID: "a", Name: "项目", Description: string([]byte{0xff})}, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if err := tc.item.Validate(); (err == nil) != tc.valid {
t.Fatalf("项目校验结果不符:%v", err)
}
})
}
if _, err := NewProject(" ", ""); err == nil {
t.Fatal("构造函数未拒绝空项目名称")
}
}