package governance import ( "testing" "time" ) // 证据能不能支撑"当前目标可恢复"只由这四个条件回答: // 状态 passed、scope 相同、绑定了快照、有完成时间。少任何一条都不算。 func TestLibraryVerificationSatisfied(t *testing.T) { now := time.Now().UTC() cases := []struct { name string verification LibraryRestoreVerification scope string want bool reason string }{ {name: "passed with snapshot", scope: "internal", want: true, verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal", SnapshotID: "snap-1", FinishedAt: now}}, {name: "failed carries failure", scope: "internal", want: false, reason: "canary 后校验仍通过", verification: LibraryRestoreVerification{Status: LibraryVerificationFailed, Scope: "internal", SnapshotID: "snap-1", Failure: "canary 后校验仍通过", FinishedAt: now}}, {name: "wrong scope", scope: "internal", want: false, reason: "另一个 scope", verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "external", SnapshotID: "snap-1", FinishedAt: now}}, {name: "no snapshot", scope: "internal", want: false, reason: "没有绑定快照", verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal", FinishedAt: now}}, {name: "no finish time", scope: "internal", want: false, reason: "没有完成时间", verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal", SnapshotID: "snap-1"}}, } for _, testCase := range cases { t.Run(testCase.name, func(t *testing.T) { ok, reason := LibraryVerificationSatisfied(testCase.verification, testCase.scope) if ok != testCase.want { t.Fatalf("satisfied=%v want=%v reason=%q", ok, testCase.want, reason) } if !testCase.want && testCase.reason != "" && !containsText(reason, testCase.reason) { t.Fatalf("reason=%q want contains %q", reason, testCase.reason) } }) } } func containsText(haystack, needle string) bool { return len(haystack) >= len(needle) && (haystack == needle || indexOf(haystack, needle) >= 0) } func indexOf(haystack, needle string) int { for i := 0; i+len(needle) <= len(haystack); i++ { if haystack[i:i+len(needle)] == needle { return i } } return -1 }