Files
go-iar-notificator/client/client_test.go
2025-09-08 13:27:09 -04:00

59 lines
1.3 KiB
Go

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
}