package scn import ( "testing" "time" "git.blackforestbytes.com/BlackForestBytes/goext/tst" ) func TestNew(t *testing.T) { c := New("my-token") if c == nil { t.Fatal("New returned nil") } tst.AssertEqual(t, c.token, "my-token") } func TestNewEmptyToken(t *testing.T) { c := New("") if c == nil { t.Fatal("New returned nil") } tst.AssertEqual(t, c.token, "") } func TestNewReturnsDistinctInstances(t *testing.T) { c1 := New("token-a") c2 := New("token-b") if c1 == c2 { t.Fatal("New should return distinct instances") } tst.AssertEqual(t, c1.token, "token-a") tst.AssertEqual(t, c2.token, "token-b") } func TestConnectionMessage(t *testing.T) { c := New("tok") mb := c.Message("Hello") if mb == nil { t.Fatal("Message returned nil") } if mb.conn != c { t.Error("MessageBuilder.conn does not point to source Connection") } tst.AssertEqual(t, mb.title, "Hello") if mb.content != nil { t.Error("expected content to be nil") } if mb.channel != nil { t.Error("expected channel to be nil") } if mb.time != nil { t.Error("expected time to be nil") } if mb.sendername != nil { t.Error("expected sendername to be nil") } if mb.priority != nil { t.Error("expected priority to be nil") } } func TestConnectionTitle(t *testing.T) { c := New("tok") mb := c.Title("Hello") if mb == nil { t.Fatal("Title returned nil") } if mb.conn != c { t.Error("MessageBuilder.conn does not point to source Connection") } tst.AssertEqual(t, mb.title, "Hello") if mb.content != nil { t.Error("expected content to be nil") } } func TestMessageAndTitleAreEquivalent(t *testing.T) { c := New("tok") mbMsg := c.Message("X") mbTitle := c.Title("X") tst.AssertEqual(t, mbMsg.title, mbTitle.title) tst.AssertEqual(t, mbMsg.conn, mbTitle.conn) } func TestBuilderChannel(t *testing.T) { c := New("tok") mb := c.Message("t") res := mb.Channel("foo-channel") if res != mb { t.Error("Channel did not return same builder") } if mb.channel == nil { t.Fatal("expected channel to be set") } tst.AssertEqual(t, *mb.channel, "foo-channel") } func TestBuilderContent(t *testing.T) { c := New("tok") mb := c.Message("t") res := mb.Content("body") if res != mb { t.Error("Content did not return same builder") } if mb.content == nil { t.Fatal("expected content to be set") } tst.AssertEqual(t, *mb.content, "body") } func TestBuilderTime(t *testing.T) { c := New("tok") mb := c.Message("t") now := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC) res := mb.Time(now) if res != mb { t.Error("Time did not return same builder") } if mb.time == nil { t.Fatal("expected time to be set") } if !mb.time.Equal(now) { t.Errorf("expected %v, got %v", now, *mb.time) } } func TestBuilderSenderName(t *testing.T) { c := New("tok") mb := c.Message("t") res := mb.SenderName("alice") if res != mb { t.Error("SenderName did not return same builder") } if mb.sendername == nil { t.Fatal("expected sendername to be set") } tst.AssertEqual(t, *mb.sendername, "alice") } func TestBuilderPriority(t *testing.T) { c := New("tok") mb := c.Message("t") res := mb.Priority(2) if res != mb { t.Error("Priority did not return same builder") } if mb.priority == nil { t.Fatal("expected priority to be set") } tst.AssertEqual(t, *mb.priority, 2) } func TestBuilderChaining(t *testing.T) { c := New("tok") tt := time.Date(2030, 5, 6, 7, 8, 9, 0, time.UTC) mb := c.Message("hello"). Channel("ch"). Content("content"). Time(tt). SenderName("bob"). Priority(7) if mb == nil { t.Fatal("chained builder returned nil") } tst.AssertEqual(t, mb.title, "hello") if mb.channel == nil || *mb.channel != "ch" { t.Error("channel not set correctly") } if mb.content == nil || *mb.content != "content" { t.Error("content not set correctly") } if mb.time == nil || !mb.time.Equal(tt) { t.Error("time not set correctly") } if mb.sendername == nil || *mb.sendername != "bob" { t.Error("sendername not set correctly") } if mb.priority == nil || *mb.priority != 7 { t.Error("priority not set correctly") } } func TestBuilderOverwriteValues(t *testing.T) { c := New("tok") mb := c.Message("t"). Channel("first"). Content("first"). SenderName("first"). Priority(1) mb.Channel("second"). Content("second"). SenderName("second"). Priority(2) tst.AssertEqual(t, *mb.channel, "second") tst.AssertEqual(t, *mb.content, "second") tst.AssertEqual(t, *mb.sendername, "second") tst.AssertEqual(t, *mb.priority, 2) } func TestBuilderIndependentInstances(t *testing.T) { c := New("tok") a := c.Message("A").Content("aa").Priority(1) b := c.Message("B").Content("bb").Priority(9) if a == b { t.Fatal("expected distinct builders") } tst.AssertEqual(t, a.title, "A") tst.AssertEqual(t, b.title, "B") tst.AssertEqual(t, *a.content, "aa") tst.AssertEqual(t, *b.content, "bb") tst.AssertEqual(t, *a.priority, 1) tst.AssertEqual(t, *b.priority, 9) } func TestBuilderTimeZonePreserved(t *testing.T) { c := New("tok") loc, err := time.LoadLocation("Europe/Berlin") if err != nil { t.Skipf("timezone db not available: %v", err) } tt := time.Date(2025, 6, 15, 12, 0, 0, 0, loc) mb := c.Message("t").Time(tt) if mb.time == nil { t.Fatal("expected time to be set") } if !mb.time.Equal(tt) { t.Errorf("expected %v, got %v", tt, *mb.time) } if mb.time.Unix() != tt.Unix() { t.Errorf("expected unix %d, got %d", tt.Unix(), mb.time.Unix()) } } func TestBuilderNegativePriority(t *testing.T) { c := New("tok") mb := c.Message("t").Priority(-1) if mb.priority == nil { t.Fatal("expected priority to be set") } tst.AssertEqual(t, *mb.priority, -1) } func TestBuilderEmptyStrings(t *testing.T) { c := New("tok") mb := c.Message(""). Channel(""). Content(""). SenderName("") tst.AssertEqual(t, mb.title, "") if mb.channel == nil || *mb.channel != "" { t.Error("channel should be set to empty string (not nil)") } if mb.content == nil || *mb.content != "" { t.Error("content should be set to empty string (not nil)") } if mb.sendername == nil || *mb.sendername != "" { t.Error("sendername should be set to empty string (not nil)") } } func TestErrorTypesAreDistinct(t *testing.T) { errs := []any{ErrAuthFailed, ErrQuota, ErrBadRequest, ErrInternalServerErr, ErrOther} for i := range errs { if errs[i] == nil { t.Errorf("error type at index %d is nil", i) } } }