This commit is contained in:
Yvan
2026-09-04 16:51:03 +08:00
parent dce95412dd
commit d83720c8fd
3 changed files with 83 additions and 2 deletions
+49
View File
@@ -15,6 +15,27 @@ type securityUpdateRepo struct {
persistedCopy *SecurityConfig
}
type bootstrapSecurityRepo struct {
initialized bool
calls int
}
func (r *bootstrapSecurityRepo) SecurityConfig(context.Context) (*SecurityConfig, error) {
r.calls++
if !r.initialized {
return nil, ErrDatabaseNotInitialized
}
return &SecurityConfig{ID: 1, LimitCount: 42}, nil
}
func (*bootstrapSecurityRepo) SaveSecurityConfig(context.Context, *SecurityConfig) error {
return nil
}
func (*bootstrapSecurityRepo) BackfillPasswordUpdatedAt(context.Context, time.Time) error {
return nil
}
func (r *securityUpdateRepo) SecurityConfig(context.Context) (*SecurityConfig, error) {
r.callOrder = append(r.callOrder, "get")
copy := *r.current
@@ -110,3 +131,31 @@ func TestSecurityReadsPersistedValueWhileCurrentUsesCache(t *testing.T) {
t.Fatalf("Security() returned stale value: %d", fresh.LimitCount)
}
}
func TestCurrentUsesBootstrapDefaultsWithoutCachingThem(t *testing.T) {
repo := &bootstrapSecurityRepo{}
uc := NewSecurityUsecase(repo, nil, nil, nil)
bootstrap, err := uc.Current(context.Background())
if err != nil {
t.Fatalf("Current() during bootstrap error = %v", err)
}
if bootstrap.ID != 1 || bootstrap.KeyLong != 6 || bootstrap.ImgWidth != 240 || bootstrap.ImgHeight != 80 || bootstrap.LimitEnable {
t.Fatalf("bootstrap config = %+v", bootstrap)
}
if repo.calls != 1 {
t.Fatalf("bootstrap repository calls = %d, want 1", repo.calls)
}
repo.initialized = true
active, err := uc.Current(context.Background())
if err != nil {
t.Fatalf("Current() after initialization error = %v", err)
}
if active.LimitCount != 42 {
t.Fatalf("active config = %+v, want persisted config", active)
}
if repo.calls != 2 {
t.Fatalf("repository calls = %d, want fallback to be uncached", repo.calls)
}
}