Download task_039/tests/enhanced_cases_test.go.fixture from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 6.2 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/tests/enhanced_cases_test.go.fixture
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_039/tests/enhanced_cases_test.go.fixture
-
curl -L -o enhanced_cases_test.go.fixture https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/tests/enhanced_cases_test.go.fixture
6.2 kB
| package contentsafety | |
| import ( | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| "testing" | |
| ) | |
| // ENHANCED_CASE_1: configured non-placeholder words are blocked in all required content types. | |
| func TestEnhancedDistinctWordsAcrossAllContentTypes(t *testing.T) { | |
| words := []string{"question-risk-731", "回答风险词", "comment-risk-419"} | |
| filter := NewFilter(words) | |
| cases := []struct{ contentType, text, word string }{ | |
| {"question", "prefix question-risk-731 suffix", words[0]}, | |
| {"answer", "回答中包含回答风险词需要阻止", words[1]}, | |
| {"comment", "prefix comment-risk-419 suffix", words[2]}, | |
| } | |
| for _, tc := range cases { | |
| err := filter.Validate(tc.contentType, tc.text) | |
| if err == nil { | |
| t.Fatalf("%s containing configured word %q was not blocked", tc.contentType, tc.word) | |
| } | |
| if strings.TrimSpace(err.Error()) == "" { | |
| t.Errorf("%s block should return a non-empty error", tc.contentType) | |
| } | |
| } | |
| } | |
| // ENHANCED_CASE_2: administrator-style repeated configuration updates take effect immediately. | |
| func TestEnhancedRepeatedConfigurationReplacement(t *testing.T) { | |
| filter := NewFilter([]string{"retired-risk-101"}) | |
| filter.SetWords([]string{"current-risk-202"}) | |
| if err := filter.Validate("question", "retired-risk-101 is now clean"); err != nil { | |
| t.Fatalf("replaced word remained blocked: %v", err) | |
| } | |
| if err := filter.Validate("answer", "contains current-risk-202"); err == nil { | |
| t.Fatal("newly configured word was not blocked") | |
| } | |
| filter.SetWords([]string{"final-risk-303"}) | |
| if err := filter.Validate("comment", "current-risk-202 is now clean"); err != nil { | |
| t.Fatalf("second replacement did not remove prior configuration: %v", err) | |
| } | |
| if err := filter.Validate("comment", "contains final-risk-303"); err == nil { | |
| t.Fatal("second replacement did not add new configuration") | |
| } | |
| } | |
| // ENHANCED_CASE_3: every blocked attempt is auditable with its type, text, and matched word. | |
| func TestEnhancedAuditTrailForMultipleAttempts(t *testing.T) { | |
| filter := NewFilter([]string{"audit-risk-551"}) | |
| attempts := []struct{ contentType, text string }{ | |
| {"question", "question audit-risk-551 payload"}, | |
| {"answer", "answer audit-risk-551 payload"}, | |
| {"comment", "comment audit-risk-551 payload"}, | |
| } | |
| for _, attempt := range attempts { | |
| if err := filter.Validate(attempt.contentType, attempt.text); err == nil { | |
| t.Fatalf("blocked attempt for %s unexpectedly passed", attempt.contentType) | |
| } | |
| } | |
| if err := filter.Validate("comment", "a clean control message"); err != nil { | |
| t.Fatalf("clean control message was blocked: %v", err) | |
| } | |
| log := filter.AuditLog() | |
| if len(log) != len(attempts) { | |
| t.Fatalf("want %d audit entries, got %d", len(attempts), len(log)) | |
| } | |
| remaining := make(map[string]bool, len(attempts)) | |
| for _, attempt := range attempts { | |
| remaining[attempt.contentType+"\x00"+attempt.text] = true | |
| } | |
| for _, entry := range log { | |
| key := entry.ContentType + "\x00" + entry.Text | |
| if !remaining[key] || entry.Word != "audit-risk-551" { | |
| t.Errorf("audit entry does not preserve a blocked attempt: %+v", entry) | |
| } | |
| delete(remaining, key) | |
| } | |
| if len(remaining) != 0 { | |
| t.Fatalf("audit log omitted %d blocked attempts", len(remaining)) | |
| } | |
| } | |
| func TestEnhancedFileConfigAndSubmission(t *testing.T) { | |
| f, err := NewFilterFromFile("sensitive_words.txt") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := f.Submit("question", "A SPAM-LINK should not be accepted"); err == nil { | |
| t.Fatal("bundled word should block") | |
| } | |
| if len(f.Accepted()) != 0 { | |
| t.Fatal("blocked submission entered accepted list") | |
| } | |
| if err := f.Submit("answer", "This is a clean answer"); err != nil { | |
| t.Fatal(err) | |
| } | |
| if got := f.Accepted(); len(got) != 1 || got[0].ContentType != "answer" { | |
| t.Fatalf("accepted=%+v", got) | |
| } | |
| if got := f.AuditLog(); len(got) != 1 || got[0].Word != "spam-link" { | |
| t.Fatalf("audit=%+v", got) | |
| } | |
| path := filepath.Join(t.TempDir(), "words.txt") | |
| if err := os.WriteFile(path, []byte("# comment\n\n alpha \nBETA\n"), 0600); err != nil { | |
| t.Fatal(err) | |
| } | |
| words, err := LoadWordsFile(path) | |
| if err != nil || len(words) != 2 || words[0] != "alpha" || words[1] != "BETA" { | |
| t.Fatalf("loaded=%v err=%v", words, err) | |
| } | |
| f.SetWords(words) | |
| if err := f.Submit("comment", "beta mention"); err == nil { | |
| t.Fatal("updated file words did not block") | |
| } | |
| if err := f.Submit("comment", "spam-link is retired"); err != nil { | |
| t.Fatal("old word still active:", err) | |
| } | |
| if _, err := NewFilterFromFile(filepath.Join(t.TempDir(), "missing")); err == nil { | |
| t.Fatal("missing config must return error") | |
| } | |
| } | |
| func TestEnhancedInvalidTypeAndDefensiveCopies(t *testing.T) { | |
| f := NewFilter([]string{" ", "Risk", "risk"}) | |
| if err := f.Submit("question", "clean"); err != nil { | |
| t.Fatal("blank word blocked all text:", err) | |
| } | |
| if err := f.Submit("post", "risk"); err == nil { | |
| t.Fatal("unsupported type accepted") | |
| } | |
| if len(f.Accepted()) != 1 || len(f.AuditLog()) != 0 { | |
| t.Fatal("invalid type altered records") | |
| } | |
| if err := f.Submit("comment", "RISK"); err == nil { | |
| t.Fatal("case fold failed") | |
| } | |
| accepted := f.Accepted() | |
| accepted[0].Text = "mutated" | |
| audit := f.AuditLog() | |
| audit[0].Text = "mutated" | |
| if f.Accepted()[0].Text != "clean" || f.AuditLog()[0].Text != "RISK" { | |
| t.Fatal("record snapshots expose internal state") | |
| } | |
| } | |
| func TestEnhancedWordOwnershipAndEmptyReplacement(t *testing.T) { | |
| words := []string{"original"} | |
| f := NewFilter(words) | |
| words[0] = "outside-change" | |
| if f.Validate("question", "original") == nil { | |
| t.Fatal("constructor must own its word snapshot") | |
| } | |
| updated := []string{"replacement"} | |
| f.SetWords(updated) | |
| updated[0] = "outside-change" | |
| if f.Validate("answer", "replacement") == nil { | |
| t.Fatal("SetWords must own its word snapshot") | |
| } | |
| f.SetWords(nil) | |
| before := len(f.AuditLog()) | |
| if err := f.Submit("comment", "original replacement outside-change"); err != nil { | |
| t.Fatal("empty replacement must clear the word list:", err) | |
| } | |
| if len(f.AuditLog()) != before { | |
| t.Fatal("empty configuration created a block audit") | |
| } | |
| if err := f.Validate("post", "anything"); err == nil { | |
| t.Fatal("Validate must reject unsupported content types too") | |
| } | |
| if len(f.AuditLog()) != before { | |
| t.Fatal("invalid Validate call altered audit") | |
| } | |
| } | |