feat: add retention policy and stability verification
This commit is contained in:
65
internal/retention/policy_test.go
Normal file
65
internal/retention/policy_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package retention
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
core "git.yuntuo.hk/k2safe/orbit/model"
|
||||
)
|
||||
|
||||
func TestDefaultsAndValidation(t *testing.T) {
|
||||
ns, err := core.NewID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := Defaults(ns)
|
||||
if p.Enabled || p.Source != "default" || p.IdempotencyDays != 90 || p.TaskHistoryDays != 365 || p.AuditLogDays != 2555 {
|
||||
t.Fatalf("unexpected defaults: %+v", p)
|
||||
}
|
||||
if err := p.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.IdempotencyDays = 89
|
||||
if err := p.Validate(); err == nil {
|
||||
t.Fatal("accepted an idempotency window shorter than the safety minimum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateInputRequiresReasonAndKeepsSafetyMinimum(t *testing.T) {
|
||||
ns, _ := core.NewID()
|
||||
in := UpdateInput{
|
||||
IdempotencyDays: 90, TaskHistoryDays: 30, WorkflowHistoryDays: 30,
|
||||
EventLogDays: 30, DeliveryHistoryDays: 30, InboxHistoryDays: 30,
|
||||
AuditLogDays: 365, Reason: "quarterly retention review",
|
||||
}
|
||||
if _, err := in.ToPolicy(ns); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
in.Reason = ""
|
||||
if _, err := in.ToPolicy(ns); err == nil {
|
||||
t.Fatal("accepted an empty audit reason")
|
||||
}
|
||||
in.Reason = "shorten"
|
||||
in.IdempotencyDays = 89
|
||||
if _, err := in.ToPolicy(ns); err == nil {
|
||||
t.Fatal("accepted an unsafe idempotency window")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromEnvOverridesOnlyDeploymentDefaults(t *testing.T) {
|
||||
values := map[string]string{
|
||||
"ORBIT_RETENTION_IDEMPOTENCY_DAYS": "120",
|
||||
"ORBIT_RETENTION_TASK_DAYS": "180",
|
||||
"ORBIT_RETENTION_AUDIT_DAYS": "730",
|
||||
}
|
||||
p, err := FromEnv(func(k string) string { return values[k] })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.IdempotencyDays != 120 || p.TaskHistoryDays != 180 || p.AuditLogDays != 730 || p.EventLogDays != DefaultEventLogDays {
|
||||
t.Fatalf("environment defaults not applied: %+v", p)
|
||||
}
|
||||
values["ORBIT_RETENTION_IDEMPOTENCY_DAYS"] = "89"
|
||||
if _, err := FromEnv(func(k string) string { return values[k] }); err == nil {
|
||||
t.Fatal("accepted unsafe environment idempotency window")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user