]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server_test.go
Refactoring, cleanup, test backfilling
[rbdr/mobius] / hotline / server_test.go
index bf608e1c3da5992255d55b6a1d44ff74ba143b70..a29a4f8299d830dbcb30fa5537f8c3259fde1d85 100644 (file)
@@ -5,8 +5,8 @@ import (
        "context"
        "fmt"
        "github.com/stretchr/testify/assert"
        "context"
        "fmt"
        "github.com/stretchr/testify/assert"
-       "go.uber.org/zap"
        "io"
        "io"
+       "log/slog"
        "os"
        "sync"
        "testing"
        "os"
        "sync"
        "testing"
@@ -30,20 +30,17 @@ func TestServer_handleFileTransfer(t *testing.T) {
                Port          int
                Accounts      map[string]*Account
                Agreement     []byte
                Port          int
                Accounts      map[string]*Account
                Agreement     []byte
-               Clients       map[uint16]*ClientConn
+               Clients       map[[2]byte]*ClientConn
                ThreadedNews  *ThreadedNews
                fileTransfers map[[4]byte]*FileTransfer
                Config        *Config
                ConfigDir     string
                ThreadedNews  *ThreadedNews
                fileTransfers map[[4]byte]*FileTransfer
                Config        *Config
                ConfigDir     string
-               Logger        *zap.SugaredLogger
+               Logger        *slog.Logger
                PrivateChats  map[uint32]*PrivateChat
                NextGuestID   *uint16
                TrackerPassID [4]byte
                Stats         *Stats
                FS            FileStore
                PrivateChats  map[uint32]*PrivateChat
                NextGuestID   *uint16
                TrackerPassID [4]byte
                Stats         *Stats
                FS            FileStore
-               outbox        chan Transaction
-               mux           sync.Mutex
-               flatNewsMux   sync.Mutex
                FlatNews      []byte
        }
        type args struct {
                FlatNews      []byte
        }
        type args struct {
@@ -117,11 +114,11 @@ func TestServer_handleFileTransfer(t *testing.T) {
                                Logger: NewTestLogger(),
                                Stats:  &Stats{},
                                fileTransfers: map[[4]byte]*FileTransfer{
                                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{},
+                                       {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",
                                                ClientConn: &ClientConn{
                                                        Account: &Account{
                                                                Login: "foo",
@@ -193,3 +190,61 @@ func TestServer_handleFileTransfer(t *testing.T) {
                })
        }
 }
                })
        }
 }
+
+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)
+                       }
+               })
+       }
+}