From: Jeff Halter Date: Fri, 19 Jul 2024 23:50:28 +0000 (-0700) Subject: Appease linter X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/9f89cd9fbbcec9f7f42c87ee0adc21427aab9f1c?hp=50c837fe10df05b9cc08decb1add122fb935e8fa Appease linter --- diff --git a/hotline/field_test.go b/hotline/field_test.go index fc11c74..213d9c3 100644 --- a/hotline/field_test.go +++ b/hotline/field_test.go @@ -222,7 +222,7 @@ func TestField_DecodeInt(t *testing.T) { Data: tt.fields.Data, } got, err := f.DecodeInt() - if !tt.wantErr(t, err, fmt.Sprintf("DecodeInt()")) { + if !tt.wantErr(t, err) { return } assert.Equalf(t, tt.want, got, "DecodeInt()") diff --git a/hotline/file_resume_data.go b/hotline/file_resume_data.go index b5d980e..5dfd2d8 100644 --- a/hotline/file_resume_data.go +++ b/hotline/file_resume_data.go @@ -13,7 +13,7 @@ type FileResumeData struct { ForkCount [2]byte // Length of ForkInfoList. Either 2 or 3 depending on whether file has a resource fork ForkInfoList []ForkInfoList - readOffset int + //readOffset int // TODO } type ForkInfoList struct { diff --git a/hotline/file_transfer.go b/hotline/file_transfer.go index e37295a..806466b 100644 --- a/hotline/file_transfer.go +++ b/hotline/file_transfer.go @@ -289,14 +289,15 @@ func DownloadHandler(w io.Writer, fullPath string, fileTransfer *FileTransfer, f } } - rFile, err := fw.rsrcForkFile() - if err != nil { - // return fmt.Errorf("open resource fork file: %v", err) - } - - if _, err = io.Copy(w, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil { - // return fmt.Errorf("send resource fork data: %v", err) - } + rFile, _ := fw.rsrcForkFile() + //if err != nil { + // // return fmt.Errorf("open resource fork file: %v", err) + //} + + _, _ = io.Copy(w, io.TeeReader(rFile, fileTransfer.bytesSentCounter)) + //if err != nil { + // // return fmt.Errorf("send resource fork data: %v", err) + //} return nil } diff --git a/hotline/message_board_test.go b/hotline/message_board_test.go deleted file mode 100644 index 64db714..0000000 --- a/hotline/message_board_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package hotline - -import "github.com/stretchr/testify/mock" - -type mockReadWriteSeeker struct { - mock.Mock -} - -func (m *mockReadWriteSeeker) Read(p []byte) (int, error) { - args := m.Called(p) - - return args.Int(0), args.Error(1) -} - -func (m *mockReadWriteSeeker) Write(p []byte) (int, error) { - args := m.Called(p) - - return args.Int(0), args.Error(1) -} - -func (m *mockReadWriteSeeker) Seek(offset int64, whence int) (int64, error) { - args := m.Called(offset, whence) - - return args.Get(0).(int64), args.Error(1) -} 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[:]),