X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c6a3fa2cdbf7eb78964a5df7622482cdd8782d74..a2ef262a164fc735b9b8471ac0c8001eea2b9bf6:/hotline/server_test.go diff --git a/hotline/server_test.go b/hotline/server_test.go index bf608e1..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,20 +30,17 @@ 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 Stats *Stats FS FileStore - outbox chan Transaction - mux sync.Mutex - flatNewsMux sync.Mutex FlatNews []byte } type args struct { @@ -117,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", @@ -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) + } + }) + } +}