first commit
This commit is contained in:
58
client/client_test.go
Normal file
58
client/client_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.savin.nyc/alex/go-iar-notificator/client"
|
||||
"git.savin.nyc/alex/go-iar-notificator/config"
|
||||
)
|
||||
|
||||
// mockLogger returns a no-op slog.Logger for testing
|
||||
func mockLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
||||
}
|
||||
|
||||
// mockCredentials returns dummy credentials for testing
|
||||
func mockCredentials() *config.Credentials {
|
||||
return &config.Credentials{
|
||||
SecretKey: "dummy-secret",
|
||||
TTDApiKey: "dummy-api-key",
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
logger := mockLogger()
|
||||
creds := mockCredentials()
|
||||
c, err := client.New(creds, logger)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if c == nil {
|
||||
t.Fatal("expected client, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAliveDefault(t *testing.T) {
|
||||
logger := mockLogger()
|
||||
creds := mockCredentials()
|
||||
c, _ := client.New(creds, logger)
|
||||
if c.IsAlive() {
|
||||
t.Error("expected isAlive to be false by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeKeepAliveCancel(t *testing.T) {
|
||||
logger := mockLogger()
|
||||
creds := mockCredentials()
|
||||
c, _ := client.New(creds, logger)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go c.ServeKeepAlive(ctx, 10*time.Millisecond)
|
||||
// Let it run briefly
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
cancel()
|
||||
// No panic or deadlock expected
|
||||
}
|
Reference in New Issue
Block a user