Repository navigation
Expand file tree
/
Copy pathcomposite_test.go
More file actions
62 lines (57 loc) · 2.67 KB
/
Copy pathcomposite_test.go
File metadata and controls
62 lines (57 loc) · 2.67 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
package main
import (
"path/filepath"
"reflect"
"testing"
"time"
)
func compositeFixture(t *testing.T) {
t.Helper()
oldConfig, oldServers, oldBans, oldComposite := serverConfig, serverList, ourBanData, compositeBanData
t.Cleanup(func() {
serverConfig, serverList, ourBanData, compositeBanData = oldConfig, oldServers, oldBans, oldComposite
})
serverConfig = serverConfigData{CommunityName: "Home", PathData: filePathData{CompositeBanFile: filepath.Join(t.TempDir(), "composite.json")}, ServerPrefs: serverPrefs{MaxBanOutputCount: 100}}
serverList = serverListData{}
ourBanData = nil
compositeBanData = nil
}
func TestCompositeBansMergesSubscribedSources(t *testing.T) {
compositeFixture(t)
ourBanData = []banDataType{{UserName: "ALICE", Reason: "local"}}
serverList.ServerList = []serverData{
{CommunityName: "Peer", LocalData: localData{Subscribed: true, BanList: []banDataType{{UserName: "alice", Reason: "remote"}, {UserName: "Revoked", Revoked: true}}}},
{CommunityName: "Ignored", LocalData: localData{BanList: []banDataType{{UserName: "ignored"}}}},
{CommunityName: "home", LocalData: localData{Subscribed: true, BanList: []banDataType{{UserName: "self"}}}},
}
compositeBans()
want := []minBanDataType{{UserName: "alice", Reason: "Home: local, Peer: remote"}}
if !reflect.DeepEqual(compositeBanData, want) {
t.Fatalf("composite = %#v, want %#v", compositeBanData, want)
}
}
func TestCompositeBansKeepsNewestAtOutputLimit(t *testing.T) {
compositeFixture(t)
serverConfig.ServerPrefs.MaxBanOutputCount = 2
base := time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)
ourBanData = []banDataType{
{UserName: "oldest", Added: base.Format(timeFormat)},
{UserName: "middle", Added: base.Add(time.Hour).Format(timeFormat)},
{UserName: "newest", Added: base.Add(2 * time.Hour).Format(timeFormat)},
}
compositeBans()
if len(compositeBanData) != 2 || compositeBanData[0].UserName != "newest" || compositeBanData[1].UserName != "middle" {
t.Fatalf("wrong bans retained: %#v", compositeBanData)
}
}
func TestCompositeBansUsesNewestDuplicateTimestamp(t *testing.T) {
compositeFixture(t)
serverConfig.ServerPrefs.MaxBanOutputCount = 1
base := time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)
ourBanData = []banDataType{{UserName: "alice", Added: base.Add(time.Hour).Format(timeFormat)}, {UserName: "bob", Added: base.Format(timeFormat)}}
serverList.ServerList = []serverData{{CommunityName: "Peer", LocalData: localData{Subscribed: true, BanList: []banDataType{{UserName: "bob", Added: base.Add(2 * time.Hour).Format(timeFormat)}}}}}
compositeBans()
if len(compositeBanData) != 1 || compositeBanData[0].UserName != "bob" {
t.Fatalf("newest remote timestamp ignored: %#v", compositeBanData)
}
}