File size: 2,856 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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])
    }
}