forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_test.go
82 lines (74 loc) · 1.82 KB
/
engine_test.go
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
71
72
73
74
75
76
77
78
79
80
81
82
package automod
import (
"context"
"log/slog"
"testing"
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/stretchr/testify/assert"
)
func simpleRule(evt *RecordEvent, post *appbsky.FeedPost) error {
for _, tag := range post.Tags {
if evt.InSet("banned-hashtags", tag) {
evt.AddRecordLabel("bad-hashtag")
break
}
}
for _, facet := range post.Facets {
for _, feat := range facet.Features {
if feat.RichtextFacet_Tag != nil {
tag := feat.RichtextFacet_Tag.Tag
if evt.InSet("banned-hashtags", tag) {
evt.AddRecordLabel("bad-hashtag")
break
}
}
}
}
return nil
}
func engineFixture() Engine {
rules := RuleSet{
PostRules: []PostRuleFunc{
simpleRule,
},
}
sets := NewMemSetStore()
sets.Sets["banned-hashtags"] = make(map[string]bool)
sets.Sets["banned-hashtags"]["slur"] = true
dir := identity.NewMockDirectory()
id1 := identity.Identity{
DID: syntax.DID("did:plc:abc111"),
Handle: syntax.Handle("handle.example.com"),
}
dir.Insert(id1)
engine := Engine{
Logger: slog.Default(),
Directory: &dir,
Counters: NewMemCountStore(),
Sets: sets,
Rules: rules,
}
return engine
}
func TestEngineBasics(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
engine := engineFixture()
id1 := identity.Identity{
DID: syntax.DID("did:plc:abc111"),
Handle: syntax.Handle("handle.example.com"),
}
path := "app.bsky.feed.post/abc123"
cid1 := "cid123"
p1 := appbsky.FeedPost{
Text: "some post blah",
}
assert.NoError(engine.ProcessRecord(ctx, id1.DID, path, cid1, &p1))
p2 := appbsky.FeedPost{
Text: "some post blah",
Tags: []string{"one", "slur"},
}
assert.NoError(engine.ProcessRecord(ctx, id1.DID, path, cid1, &p2))
}