]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server_test.go
Extensive refactor and clean up
[rbdr/mobius] / hotline / server_test.go
index bf608e1c3da5992255d55b6a1d44ff74ba143b70..0487d2e586a650b7e268138f99f86491586767a4 100644 (file)
@@ -5,10 +5,9 @@ import (
        "context"
        "fmt"
        "github.com/stretchr/testify/assert"
        "context"
        "fmt"
        "github.com/stretchr/testify/assert"
-       "go.uber.org/zap"
        "io"
        "io"
+       "log/slog"
        "os"
        "os"
-       "sync"
        "testing"
 )
 
        "testing"
 )
 
@@ -27,24 +26,13 @@ func (mrw mockReadWriter) Write(p []byte) (n int, err error) {
 
 func TestServer_handleFileTransfer(t *testing.T) {
        type fields struct {
 
 func TestServer_handleFileTransfer(t *testing.T) {
        type fields struct {
-               Port          int
-               Accounts      map[string]*Account
-               Agreement     []byte
-               Clients       map[uint16]*ClientConn
-               ThreadedNews  *ThreadedNews
-               fileTransfers map[[4]byte]*FileTransfer
-               Config        *Config
-               ConfigDir     string
-               Logger        *zap.SugaredLogger
-               PrivateChats  map[uint32]*PrivateChat
-               NextGuestID   *uint16
-               TrackerPassID [4]byte
-               Stats         *Stats
-               FS            FileStore
-               outbox        chan Transaction
-               mux           sync.Mutex
-               flatNewsMux   sync.Mutex
-               FlatNews      []byte
+               ThreadedNews    *ThreadedNews
+               FileTransferMgr FileTransferMgr
+               Config          Config
+               ConfigDir       string
+               Stats           *Stats
+               Logger          *slog.Logger
+               FS              FileStore
        }
        type args struct {
                ctx context.Context
        }
        type args struct {
                ctx context.Context
@@ -82,7 +70,10 @@ func TestServer_handleFileTransfer(t *testing.T) {
                        wantErr: assert.Error,
                },
                {
                        wantErr: assert.Error,
                },
                {
-                       name: "with invalid transfer ID",
+                       name: "with invalid transfer Type",
+                       fields: fields{
+                               FileTransferMgr: NewMemFileTransferMgr(),
+                       },
                        args: args{
                                ctx: func() context.Context {
                                        ctx := context.Background()
                        args: args{
                                ctx: func() context.Context {
                                        ctx := context.Background()
@@ -109,31 +100,34 @@ func TestServer_handleFileTransfer(t *testing.T) {
                        name: "file download",
                        fields: fields{
                                FS: &OSFileStore{},
                        name: "file download",
                        fields: fields{
                                FS: &OSFileStore{},
-                               Config: &Config{
+                               Config: Config{
                                        FileRoot: func() string {
                                                path, _ := os.Getwd()
                                                return path + "/test/config/Files"
                                        }()},
                                Logger: NewTestLogger(),
                                        FileRoot: func() string {
                                                path, _ := os.Getwd()
                                                return path + "/test/config/Files"
                                        }()},
                                Logger: NewTestLogger(),
-                               Stats:  &Stats{},
-                               fileTransfers: map[[4]byte]*FileTransfer{
-                                       [4]byte{0, 0, 0, 5}: {
-                                               ReferenceNumber: []byte{0, 0, 0, 5},
-                                               Type:            FileDownload,
-                                               FileName:        []byte("testfile-8b"),
-                                               FilePath:        []byte{},
-                                               ClientConn: &ClientConn{
-                                                       Account: &Account{
-                                                               Login: "foo",
-                                                       },
-                                                       transfersMU: sync.Mutex{},
-                                                       transfers: map[int]map[[4]byte]*FileTransfer{
-                                                               FileDownload: {
-                                                                       [4]byte{0, 0, 0, 5}: &FileTransfer{},
+                               Stats:  NewStats(),
+                               FileTransferMgr: &MemFileTransferMgr{
+                                       fileTransfers: map[FileTransferID]*FileTransfer{
+                                               {0, 0, 0, 5}: {
+                                                       refNum:   [4]byte{0, 0, 0, 5},
+                                                       Type:     FileDownload,
+                                                       FileName: []byte("testfile-8b"),
+                                                       FilePath: []byte{},
+                                                       ClientConn: &ClientConn{
+                                                               Account: &Account{
+                                                                       Login: "foo",
+                                                               },
+                                                               ClientFileTransferMgr: ClientFileTransferMgr{
+                                                                       transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{
+                                                                               FileDownload: {
+                                                                                       [4]byte{0, 0, 0, 5}: &FileTransfer{},
+                                                                               },
+                                                                       },
                                                                },
                                                        },
                                                                },
                                                        },
+                                                       bytesSentCounter: &WriteCounter{},
                                                },
                                                },
-                                               bytesSentCounter: &WriteCounter{},
                                        },
                                },
                        },
                                        },
                                },
                        },
@@ -175,21 +169,75 @@ func TestServer_handleFileTransfer(t *testing.T) {
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
                        s := &Server{
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
                        s := &Server{
-                               Port:          tt.fields.Port,
-                               Accounts:      tt.fields.Accounts,
-                               Agreement:     tt.fields.Agreement,
-                               Clients:       tt.fields.Clients,
-                               ThreadedNews:  tt.fields.ThreadedNews,
-                               fileTransfers: tt.fields.fileTransfers,
-                               Config:        tt.fields.Config,
-                               ConfigDir:     tt.fields.ConfigDir,
-                               Logger:        tt.fields.Logger,
-                               Stats:         tt.fields.Stats,
-                               FS:            tt.fields.FS,
+                               FileTransferMgr: tt.fields.FileTransferMgr,
+                               Config:          tt.fields.Config,
+                               ConfigDir:       tt.fields.ConfigDir,
+                               Logger:          tt.fields.Logger,
+                               Stats:           tt.fields.Stats,
+                               FS:              tt.fields.FS,
                        }
                        }
+
                        tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
 
                        assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())
                })
        }
 }
                        tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
 
                        assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())
                })
        }
 }
+
+type TestData struct {
+       Name  string `yaml:"name"`
+       Value int    `yaml:"value"`
+}
+
+func TestLoadFromYAMLFile(t *testing.T) {
+       tests := []struct {
+               name     string
+               fileName string
+               content  string
+               wantData TestData
+               wantErr  bool
+       }{
+               {
+                       name:     "Valid YAML file",
+                       fileName: "valid.yaml",
+                       content:  "name: Test\nvalue: 123\n",
+                       wantData: TestData{Name: "Test", Value: 123},
+                       wantErr:  false,
+               },
+               {
+                       name:     "File not found",
+                       fileName: "nonexistent.yaml",
+                       content:  "",
+                       wantData: TestData{},
+                       wantErr:  true,
+               },
+               {
+                       name:     "Invalid YAML content",
+                       fileName: "invalid.yaml",
+                       content:  "name: Test\nvalue: invalid_int\n",
+                       wantData: TestData{},
+                       wantErr:  true,
+               },
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       // Setup: Create a temporary file with the provided content if content is not empty
+                       if tt.content != "" {
+                               err := os.WriteFile(tt.fileName, []byte(tt.content), 0644)
+                               assert.NoError(t, err)
+                               defer os.Remove(tt.fileName) // Cleanup the file after the test
+                       }
+
+                       var data TestData
+                       err := loadFromYAMLFile(tt.fileName, &data)
+
+                       if tt.wantErr {
+                               assert.Error(t, err)
+                       } else {
+                               assert.NoError(t, err)
+                               assert.Equal(t, tt.wantData, data)
+                       }
+               })
+       }
+}