X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/48ecca21dde5b1c8a3f34bad60512503292c7141..6dabc6b44f27f0aa936a7f9518aa6bc1243d9001:/hotline/server_test.go?ds=sidebyside diff --git a/hotline/server_test.go b/hotline/server_test.go index 7e2608e..a29a4f8 100644 --- a/hotline/server_test.go +++ b/hotline/server_test.go @@ -5,8 +5,8 @@ import ( "context" "fmt" "github.com/stretchr/testify/assert" - "go.uber.org/zap" "io" + "log/slog" "os" "sync" "testing" @@ -30,12 +30,12 @@ func TestServer_handleFileTransfer(t *testing.T) { 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 - Logger *zap.SugaredLogger + Logger *slog.Logger PrivateChats map[uint32]*PrivateChat NextGuestID *uint16 TrackerPassID [4]byte @@ -114,11 +114,11 @@ func TestServer_handleFileTransfer(t *testing.T) { 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", @@ -190,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) + } + }) + } +}