Download task_039/environment/code/content_safety_test.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/environment/code/content_safety_test.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_039/environment/code/content_safety_test.go
-
curl -L -o content_safety_test.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/environment/code/content_safety_test.go
2.86 kB
| package contentsafety | |
| import ( | |
| "strings" | |
| "testing" | |
| ) | |
| func TestBlocksSensitiveWordsAcrossContentTypes(t *testing.T) { | |
| filter := NewFilter([]string{"forbidden", "辱骂"}) | |
| for _, contentType := range []string{"question", "answer", "comment"} { | |
| err := filter.Validate(contentType, "this contains forbidden content") | |
| if err == nil { | |
| t.Fatalf("%s containing sensitive word should be blocked", contentType) | |
| } | |
| if !strings.Contains(strings.ToLower(err.Error()), "sensitive") && !strings.Contains(err.Error(), "敏感") { | |
| t.Fatalf("error should clearly mention sensitive content, got %v", err) | |
| } | |
| } | |
| } | |
| func TestAllowsCleanContentAndSupportsConfigUpdates(t *testing.T) { | |
| filter := NewFilter([]string{"alpha"}) | |
| if err := filter.Validate("question", "clean text"); err != nil { | |
| t.Fatalf("clean content should pass: %v", err) | |
| } | |
| filter.SetWords([]string{"beta"}) | |
| if err := filter.Validate("answer", "alpha is no longer blocked"); err != nil { | |
| t.Fatalf("updated word list should replace old words: %v", err) | |
| } | |
| if err := filter.Validate("answer", "beta should now be blocked"); err == nil { | |
| t.Fatalf("updated word list should block beta") | |
| } | |
| } | |
| func TestMatchIsCaseInsensitive(t *testing.T) { | |
| // Validate lowercases both the configured word and the text before Contains, | |
| // so a lowercase word "forbidden" matches uppercase text "FORBIDDEN". | |
| filter := NewFilter([]string{"Forbidden"}) | |
| if err := filter.Validate("comment", "FORBIDDEN here"); err == nil { | |
| t.Fatalf("case-insensitive match should block FORBIDDEN") | |
| } | |
| logs := filter.AuditLog() | |
| if len(logs) != 1 || logs[0].Word != "Forbidden" { | |
| t.Fatalf("audit should record the configured word verbatim: %+v", logs) | |
| } | |
| } | |
| func TestNoAuditEntryForCleanContent(t *testing.T) { | |
| // Audit entries are appended only when a block occurs; clean content logs nothing. | |
| filter := NewFilter([]string{"forbidden"}) | |
| if err := filter.Validate("question", "perfectly clean text"); err != nil { | |
| t.Fatalf("clean content should pass: %v", err) | |
| } | |
| if logs := filter.AuditLog(); len(logs) != 0 { | |
| t.Fatalf("clean content should not create audit entries, got %d", len(logs)) | |
| } | |
| } | |
| func TestRecordsAuditEntriesForBlockedAttempts(t *testing.T) { | |
| filter := NewFilter([]string{"risk"}) | |
| _ = filter.Validate("comment", "risk appears here") | |
| logs := filter.AuditLog() | |
| if len(logs) != 1 { | |
| t.Fatalf("blocked attempt should create one audit entry, got %d", len(logs)) | |
| } | |
| if logs[0].ContentType != "comment" || logs[0].Word != "risk" { | |
| t.Fatalf("audit entry should capture content type and matched word: %+v", logs[0]) | |
| } | |
| } | |