aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-07-19 16:50:28 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-07-19 16:50:28 -0700
commit9f89cd9fbbcec9f7f42c87ee0adc21427aab9f1c (patch)
tree35d0a6a1c7c3f875858dca98095adecade289975 /internal
parent50c837fe10df05b9cc08decb1add122fb935e8fa (diff)
Appease linter
Diffstat (limited to 'internal')
-rw-r--r--internal/mobius/ban_test.go1
-rw-r--r--internal/mobius/test/config/ThreadedNews.yaml56
-rw-r--r--internal/mobius/threaded_news.go11
-rw-r--r--internal/mobius/threaded_news_test.go149
-rw-r--r--internal/mobius/transaction_handlers.go10
-rw-r--r--internal/mobius/transaction_handlers_test.go19
6 files changed, 160 insertions, 86 deletions
diff --git a/internal/mobius/ban_test.go b/internal/mobius/ban_test.go
index 46860c6..f03f214 100644
--- a/internal/mobius/ban_test.go
+++ b/internal/mobius/ban_test.go
@@ -107,7 +107,6 @@ func TestAdd(t *testing.T) {
func TestBanFile_IsBanned(t *testing.T) {
type fields struct {
banList map[string]*time.Time
- Mutex sync.Mutex
}
type args struct {
ip string
diff --git a/internal/mobius/test/config/ThreadedNews.yaml b/internal/mobius/test/config/ThreadedNews.yaml
index 9f3fd65..ce8beee 100644
--- a/internal/mobius/test/config/ThreadedNews.yaml
+++ b/internal/mobius/test/config/ThreadedNews.yaml
@@ -28,61 +28,7 @@ Categories:
addsn: []
deletesn: []
guid: []
- TestSubCat:
- Type:
- - 0
- - 3
- Name: TestSubCat
- Articles:
- 1:
- Title: SubCatArt
- Poster: Halcyon 1.9.2
- Date:
- - 7
- - 228
- - 0
- - 0
- - 0
- - 254
- - 252
- - 246
- PrevArt:
- - 0
- - 0
- - 0
- - 0
- NextArt:
- - 0
- - 0
- - 0
- - 0
- ParentArt:
- - 0
- - 0
- - 0
- - 0
- FirstChildArtArt:
- - 0
- - 0
- - 0
- - 0
- DataFlav:
- - 116
- - 101
- - 120
- - 116
- - 47
- - 112
- - 108
- - 97
- - 105
- - 110
- Data: I'm an article in a subcategory!
- SubCats: {}
- count: []
- addsn: []
- deletesn: []
- guid: []
+
count: []
addsn: []
deletesn: []
diff --git a/internal/mobius/threaded_news.go b/internal/mobius/threaded_news.go
index a49055c..600006a 100644
--- a/internal/mobius/threaded_news.go
+++ b/internal/mobius/threaded_news.go
@@ -168,13 +168,13 @@ func (n *ThreadedNewsYAML) PostArticle(newsPath []string, parentArticleID uint32
return n.writeFile()
}
-func (n *ThreadedNewsYAML) DeleteArticle(newsPath []string, articleID uint32, recursive bool) error {
+func (n *ThreadedNewsYAML) DeleteArticle(newsPath []string, articleID uint32, _ bool) error {
n.mu.Lock()
defer n.mu.Unlock()
- if recursive {
- // TODO: Handle delete recursive
- }
+ //if recursive {
+ // // TODO: Handle delete recursive
+ //}
cats := n.getCatByPath(newsPath[:len(newsPath)-1])
@@ -214,8 +214,7 @@ func (n *ThreadedNewsYAML) Load() error {
n.ThreadedNews = hotline.ThreadedNews{}
- decoder := yaml.NewDecoder(fh)
- return decoder.Decode(&n.ThreadedNews)
+ return yaml.NewDecoder(fh).Decode(&n.ThreadedNews)
}
func (n *ThreadedNewsYAML) writeFile() error {
diff --git a/internal/mobius/threaded_news_test.go b/internal/mobius/threaded_news_test.go
index 9269a85..dd06a28 100644
--- a/internal/mobius/threaded_news_test.go
+++ b/internal/mobius/threaded_news_test.go
@@ -1,8 +1,12 @@
package mobius
import (
+ "fmt"
+ "github.com/jhalter/mobius/hotline"
"github.com/stretchr/testify/assert"
"os"
+ "path/filepath"
+ "sync"
"testing"
)
@@ -63,3 +67,148 @@ func TestLoadFromYAMLFile(t *testing.T) {
})
}
}
+
+func TestNewThreadedNewsYAML(t *testing.T) {
+ type args struct {
+ filePath string
+ }
+ tests := []struct {
+ name string
+ args args
+ want *ThreadedNewsYAML
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "Valid YAML file",
+ args: args{
+ filePath: "test/config/ThreadedNews.yaml",
+ },
+ want: &ThreadedNewsYAML{
+ filePath: "test/config/ThreadedNews.yaml",
+ ThreadedNews: hotline.ThreadedNews{
+ Categories: map[string]hotline.NewsCategoryListData15{
+ "TestBundle": {
+ Type: hotline.NewsBundle,
+ Name: "TestBundle",
+ Articles: make(map[uint32]*hotline.NewsArtData),
+ SubCats: map[string]hotline.NewsCategoryListData15{
+ "NestedBundle": {
+ Name: "NestedBundle",
+ Type: hotline.NewsBundle,
+ SubCats: map[string]hotline.NewsCategoryListData15{
+ "NestedCat": {
+ Name: "NestedCat",
+ Type: hotline.NewsCategory,
+ Articles: make(map[uint32]*hotline.NewsArtData),
+ SubCats: make(map[string]hotline.NewsCategoryListData15),
+ },
+ },
+ Articles: make(map[uint32]*hotline.NewsArtData),
+ },
+ },
+ },
+ "TestCat": {
+ Type: hotline.NewsCategory,
+ Name: "TestCat",
+ Articles: map[uint32]*hotline.NewsArtData{
+ 1: {
+ Title: "TestArt",
+ Poster: "Halcyon 1.9.2",
+ Date: [8]byte{0x07, 0xe4, 0x00, 0x00, 0x00, 0xfe, 0xfc, 0xcc},
+ NextArt: [4]byte{0, 0, 0, 2},
+ FirstChildArt: [4]byte{0, 0, 0, 2},
+ Data: "TestArt Body",
+ },
+ 2: {
+ Title: "Re: TestArt",
+ Poster: "Halcyon 1.9.2",
+ Date: [8]byte{0x07, 0xe4, 0x00, 0x00, 0x00, 0xfe, 0xfc, 0xd8},
+ PrevArt: [4]byte{0, 0, 0, 1},
+ ParentArt: [4]byte{0, 0, 0, 1},
+ NextArt: [4]byte{0, 0, 0, 3},
+ Data: "I'm a reply",
+ },
+ 3: {
+ Title: "TestArt 2",
+ Poster: "Halcyon 1.9.2",
+ Date: [8]byte{0x07, 0xe4, 0x00, 0x00, 0x00, 0xfe, 0xfd, 0x06},
+ PrevArt: [4]byte{0, 0, 0, 2},
+ Data: "Hello world",
+ },
+ },
+ SubCats: make(map[string]hotline.NewsCategoryListData15),
+ },
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := NewThreadedNewsYAML(tt.args.filePath)
+ if !tt.wantErr(t, err, fmt.Sprintf("NewThreadedNewsYAML(%v)", tt.args.filePath)) {
+ return
+ }
+ assert.Equalf(t, tt.want, got, "NewThreadedNewsYAML(%v)", tt.args.filePath)
+ })
+ }
+}
+
+func TestThreadedNewsYAML_CreateGrouping(t *testing.T) {
+ // Create a temporary directory.
+ tmpDir, err := os.MkdirTemp("", "createGrouping")
+ if err != nil {
+ t.Fatalf("Failed to create temp directory: %v", err)
+ }
+ defer os.RemoveAll(tmpDir) // Clean up the temporary directory.
+
+ // Path to the temporary ban file.
+ tmpFilePath := filepath.Join(tmpDir, "ThreadedNews.yaml")
+
+ type fields struct {
+ ThreadedNews hotline.ThreadedNews
+ filePath string
+ }
+ type args struct {
+ newsPath []string
+ name string
+ t [2]byte
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "new bundle",
+ fields: fields{
+ ThreadedNews: hotline.ThreadedNews{
+ Categories: map[string]hotline.NewsCategoryListData15{
+ "": {
+ SubCats: make(map[string]hotline.NewsCategoryListData15),
+ },
+ },
+ },
+ filePath: tmpFilePath,
+ },
+ args: args{
+ newsPath: []string{""},
+ name: "new bundle",
+ t: hotline.NewsBundle,
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ n := &ThreadedNewsYAML{
+ ThreadedNews: tt.fields.ThreadedNews,
+ filePath: tt.fields.filePath,
+ mu: sync.Mutex{},
+ }
+ tt.wantErr(t, n.CreateGrouping(tt.args.newsPath, tt.args.name, tt.args.t), fmt.Sprintf("CreateGrouping(%v, %v, %v)", tt.args.newsPath, tt.args.name, tt.args.t))
+ })
+ }
+}
diff --git a/internal/mobius/transaction_handlers.go b/internal/mobius/transaction_handlers.go
index 3b07d0e..399a8c0 100644
--- a/internal/mobius/transaction_handlers.go
+++ b/internal/mobius/transaction_handlers.go
@@ -981,12 +981,11 @@ func HandleDisconnectUser(cc *hotline.ClientConn, t *hotline.Transaction) (res [
err := cc.Server.BanList.Add(ip, nil)
if err != nil {
- // TODO
+ cc.Logger.Error("Error saving ban", "err", err)
}
}
}
- // TODO: remove this awful hack
go func() {
time.Sleep(1 * time.Second)
clientConn.Disconnect()
@@ -1005,14 +1004,15 @@ func HandleGetNewsCatNameList(cc *hotline.ClientConn, t *hotline.Transaction) (r
pathStrs, err := t.GetField(hotline.FieldNewsPath).DecodeNewsPath()
if err != nil {
-
+ cc.Logger.Error("get news path", "err", err)
+ return nil
}
var fields []hotline.Field
for _, cat := range cc.Server.ThreadedNewsMgr.GetCategories(pathStrs) {
b, err := io.ReadAll(&cat)
if err != nil {
- // TODO
+ cc.Logger.Error("get news categories", "err", err)
}
fields = append(fields, hotline.NewField(hotline.FieldNewsCatListData15, b))
@@ -1254,7 +1254,7 @@ func HandleGetMsgs(cc *hotline.ClientConn, t *hotline.Transaction) (res []hotlin
newsData, err := io.ReadAll(cc.Server.MessageBoard)
if err != nil {
- // TODO
+ cc.Logger.Error("Error reading messageboard", "err", err)
}
return append(res, cc.NewReply(t, hotline.NewField(hotline.FieldData, newsData)))
diff --git a/internal/mobius/transaction_handlers_test.go b/internal/mobius/transaction_handlers_test.go
index 7e9faef..63d2946 100644
--- a/internal/mobius/transaction_handlers_test.go
+++ b/internal/mobius/transaction_handlers_test.go
@@ -3,7 +3,6 @@ package mobius
import (
"cmp"
"encoding/binary"
- "encoding/hex"
"errors"
"github.com/jhalter/mobius/hotline"
"github.com/stretchr/testify/assert"
@@ -45,24 +44,6 @@ func NewTestLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, nil))
}
-// assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with
-// a hexdump for the bytes in got, after stripping the create/modify timestamps.
-// I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the
-// filesystem interactions or work around in this way.
-// TODO: figure out a better solution
-func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool {
- if wantHexDump == "" {
- return true
- }
-
- clean := slices.Concat(
- got[:92],
- make([]byte, 16),
- got[108:],
- )
- return assert.Equal(t, wantHexDump, hex.Dump(clean))
-}
-
var tranSortFunc = func(a, b hotline.Transaction) int {
return cmp.Compare(
binary.BigEndian.Uint16(a.ClientID[:]),