-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot_scope_test.go
More file actions
192 lines (170 loc) · 5.23 KB
/
Copy pathbot_scope_test.go
File metadata and controls
192 lines (170 loc) · 5.23 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"net/http"
"path/filepath"
"testing"
"example.org/dc-logger/internal/config"
"github.com/bwmarrin/discordgo"
)
func TestParseGuildSyncFilter_MultiGuild(t *testing.T) {
filter := parseGuildSyncFilter("g1, g2, g1")
if filter.allowAll {
t.Fatal("expected allowAll=false")
}
if !filter.allows("g1") || !filter.allows("g2") {
t.Fatal("expected g1 and g2 to be allowed")
}
if filter.allows("g3") {
t.Fatal("expected g3 to be disallowed")
}
}
func TestParseGuildSyncFilter_Wildcard(t *testing.T) {
filter := parseGuildSyncFilter(" * ")
if !filter.allowAll {
t.Fatal("expected allowAll=true")
}
if !filter.allows("any-guild") {
t.Fatal("expected any guild to be allowed")
}
}
func TestLoadGuildSyncFilter_Default(t *testing.T) {
t.Setenv(config.EnvDiscordSyncGuildIDs, "")
filter := loadGuildSyncFilter()
if !filter.allowAll {
t.Fatal("expected default filter to allow all guilds")
}
if !filter.allows("some-other-guild") {
t.Fatal("expected unrelated guild to be allowed by default")
}
}
func TestLoadGuildSyncFilter_Override(t *testing.T) {
t.Setenv(config.EnvDiscordSyncGuildIDs, "guild-x")
filter := loadGuildSyncFilter()
if !filter.allows("guild-x") {
t.Fatal("expected override guild to be allowed")
}
if filter.allows("guild-y") {
t.Fatal("expected non-configured guild to be disallowed when override is set")
}
}
func TestSuggestedGuildIDEnvValue(t *testing.T) {
got := suggestedGuildIDEnvValue([]*discordgo.Guild{
{ID: "2"},
nil,
{ID: "1"},
{ID: "2"},
{ID: ""},
})
if got != "1,2" {
t.Fatalf("suggestedGuildIDEnvValue mismatch: got %q want %q", got, "1,2")
}
}
func TestDescribeTrackedReadyGuilds(t *testing.T) {
got := describeTrackedReadyGuilds(nil, nil, []*discordgo.Guild{
{ID: "2", Name: "Beta"},
{ID: "1", Name: "Alpha"},
{ID: "3", Name: ""},
})
want := "3 (3), Alpha (1), Beta (2)"
if got != want {
t.Fatalf("describeTrackedReadyGuilds mismatch: got %q want %q", got, want)
}
}
func TestDescribeTrackedReadyGuilds_UsesMappedGuildNameFallback(t *testing.T) {
db, stmts := openTestDB(t)
now := "2026-02-20T00:00:00Z"
if err := upsertNameMappingRow(stmts.upsertIDNameMapping, nameMappingEntityGuild, "111111111111111111", "111111111111111111", "LOREM", now); err != nil {
t.Fatalf("seed guild mapping failed: %v", err)
}
got := describeTrackedReadyGuilds(nil, db, []*discordgo.Guild{
{ID: "111111111111111111", Name: ""},
})
want := "LOREM (111111111111111111)"
if got != want {
t.Fatalf("describeTrackedReadyGuilds fallback mismatch: got %q want %q", got, want)
}
}
func TestResolveGuildDisplayName_UsesGuildDetailsFetcherFallback(t *testing.T) {
prevFetcher := guildDetailsFetcher
t.Cleanup(func() { guildDetailsFetcher = prevFetcher })
guildDetailsFetcher = func(s *discordgo.Session, guildID string) (*discordgo.Guild, error) {
return &discordgo.Guild{ID: guildID, Name: "LOREM"}, nil
}
got := resolveGuildDisplayName(nil, nil, &discordgo.Guild{ID: "111111111111111111", Name: ""})
if got != "LOREM" {
t.Fatalf("resolveGuildDisplayName mismatch: got %q want %q", got, "LOREM")
}
}
func TestGuildDatabasePath_UsesReadableNameAndID(t *testing.T) {
got := guildDatabasePath(filepath.Join("database", "database.db"), "Example Server!/Ops", "12345")
want := filepath.Join("database", "db_Example_Server_Ops_12345.db")
if got != want {
t.Fatalf("guildDatabasePath mismatch: got %q want %q", got, want)
}
}
func TestPreflightGuildReadAccess_PassesWhenAccessible(t *testing.T) {
prevFetcher := channelMessagesFetcher
t.Cleanup(func() { channelMessagesFetcher = prevFetcher })
channelMessagesFetcher = func(
s *discordgo.Session,
channelID string,
limit int,
beforeID, afterID, aroundID string,
) ([]*discordgo.Message, error) {
return []*discordgo.Message{}, nil
}
ok := preflightGuildReadAccess(
nil,
nil,
[]*discordgo.Guild{{ID: "g1", Name: "GuildOne"}},
map[string][]*discordgo.Channel{
"g1": {
{ID: "c1", Name: "general", Type: discordgo.ChannelTypeGuildText},
{ID: "cat", Name: "General", Type: discordgo.ChannelTypeGuildCategory},
},
},
)
if !ok {
t.Fatal("expected preflight to pass")
}
}
func TestPreflightGuildReadAccess_FailsOnMissingAccess(t *testing.T) {
prevFetcher := channelMessagesFetcher
t.Cleanup(func() { channelMessagesFetcher = prevFetcher })
missingAccessErr := &discordgo.RESTError{
Response: &http.Response{
StatusCode: http.StatusForbidden,
Status: "403 Forbidden",
},
ResponseBody: []byte(`{"message":"Missing Access","code":50001}`),
Message: &discordgo.APIErrorMessage{
Code: discordgo.ErrCodeMissingAccess,
Message: "Missing Access",
},
}
channelMessagesFetcher = func(
s *discordgo.Session,
channelID string,
limit int,
beforeID, afterID, aroundID string,
) ([]*discordgo.Message, error) {
if channelID == "c-missing" {
return nil, missingAccessErr
}
return []*discordgo.Message{}, nil
}
ok := preflightGuildReadAccess(
nil,
nil,
[]*discordgo.Guild{{ID: "g1", Name: "GuildOne"}},
map[string][]*discordgo.Channel{
"g1": {
{ID: "c-ok", Name: "general", Type: discordgo.ChannelTypeGuildText},
{ID: "c-missing", Name: "announcements", Type: discordgo.ChannelTypeGuildText},
},
},
)
if ok {
t.Fatal("expected preflight to fail on missing access")
}
}