ScuttleBot

scuttlebot / pkg / client / discovery_test.go
Source Blame History 105 lines
b2b8269… lmata 1 package client_test
b2b8269… lmata 2
b2b8269… lmata 3 import (
b2b8269… lmata 4 "context"
b2b8269… lmata 5 "testing"
b2b8269… lmata 6 "time"
b2b8269… lmata 7
b2b8269… lmata 8 "github.com/conflicthq/scuttlebot/pkg/client"
b2b8269… lmata 9 )
b2b8269… lmata 10
b2b8269… lmata 11 // TestDiscoveryNotConnected verifies all methods return an error when the
b2b8269… lmata 12 // client is not connected to IRC. The full request→response path requires a
b2b8269… lmata 13 // live Ergo instance (integration test).
b2b8269… lmata 14 func TestDiscoveryNotConnected(t *testing.T) {
b2b8269… lmata 15 c, _ := client.New(client.Options{
b2b8269… lmata 16 ServerAddr: "localhost:6667",
b2b8269… lmata 17 Nick: "agent-01",
b2b8269… lmata 18 Password: "secret",
b2b8269… lmata 19 })
b2b8269… lmata 20 d := client.NewDiscovery(c, client.DiscoveryOptions{})
b2b8269… lmata 21 ctx := context.Background()
b2b8269… lmata 22
b2b8269… lmata 23 t.Run("ListChannels", func(t *testing.T) {
b2b8269… lmata 24 if _, err := d.ListChannels(ctx); err == nil {
b2b8269… lmata 25 t.Error("expected error when not connected")
b2b8269… lmata 26 }
b2b8269… lmata 27 })
b2b8269… lmata 28 t.Run("ChannelMembers", func(t *testing.T) {
b2b8269… lmata 29 if _, err := d.ChannelMembers(ctx, "#fleet"); err == nil {
b2b8269… lmata 30 t.Error("expected error when not connected")
b2b8269… lmata 31 }
b2b8269… lmata 32 })
b2b8269… lmata 33 t.Run("GetTopic", func(t *testing.T) {
b2b8269… lmata 34 if _, err := d.GetTopic(ctx, "#fleet"); err == nil {
b2b8269… lmata 35 t.Error("expected error when not connected")
b2b8269… lmata 36 }
b2b8269… lmata 37 })
b2b8269… lmata 38 t.Run("WhoIs", func(t *testing.T) {
b2b8269… lmata 39 if _, err := d.WhoIs(ctx, "someone"); err == nil {
b2b8269… lmata 40 t.Error("expected error when not connected")
b2b8269… lmata 41 }
b2b8269… lmata 42 })
b2b8269… lmata 43 }
b2b8269… lmata 44
b2b8269… lmata 45 func TestDiscoveryCancellation(t *testing.T) {
b2b8269… lmata 46 c, _ := client.New(client.Options{
b2b8269… lmata 47 ServerAddr: "localhost:6667",
b2b8269… lmata 48 Nick: "agent-01",
b2b8269… lmata 49 Password: "secret",
b2b8269… lmata 50 })
b2b8269… lmata 51 d := client.NewDiscovery(c, client.DiscoveryOptions{})
b2b8269… lmata 52
b2b8269… lmata 53 ctx, cancel := context.WithCancel(context.Background())
b2b8269… lmata 54 cancel()
b2b8269… lmata 55
b2b8269… lmata 56 // All methods should return context.Canceled, not hang.
b2b8269… lmata 57 _, err := d.ListChannels(ctx)
b2b8269… lmata 58 // Either "not connected" (faster path) or context.Canceled is acceptable.
b2b8269… lmata 59 if err == nil {
b2b8269… lmata 60 t.Error("expected error on cancelled context")
b2b8269… lmata 61 }
b2b8269… lmata 62 }
b2b8269… lmata 63
b2b8269… lmata 64 func TestDiscoveryCacheTTL(t *testing.T) {
b2b8269… lmata 65 // Verify that cache entries expire after TTL.
b2b8269… lmata 66 // We test the cache layer directly by setting a very short TTL.
b2b8269… lmata 67 c, _ := client.New(client.Options{
b2b8269… lmata 68 ServerAddr: "localhost:6667",
b2b8269… lmata 69 Nick: "agent-01",
b2b8269… lmata 70 Password: "secret",
b2b8269… lmata 71 })
b2b8269… lmata 72
b2b8269… lmata 73 // Zero TTL disables caching — discovery always hits the server.
b2b8269… lmata 74 d := client.NewDiscovery(c, client.DiscoveryOptions{CacheTTL: 0})
b2b8269… lmata 75 if d == nil {
b2b8269… lmata 76 t.Fatal("NewDiscovery returned nil")
b2b8269… lmata 77 }
b2b8269… lmata 78 }
b2b8269… lmata 79
b2b8269… lmata 80 func TestDiscoveryDefaultOptions(t *testing.T) {
b2b8269… lmata 81 c, _ := client.New(client.Options{
b2b8269… lmata 82 ServerAddr: "localhost:6667",
b2b8269… lmata 83 Nick: "agent-01",
b2b8269… lmata 84 Password: "secret",
b2b8269… lmata 85 })
b2b8269… lmata 86 // Default TTL should be 30s — just verify it doesn't panic.
b2b8269… lmata 87 d := client.NewDiscovery(c, client.DiscoveryOptions{})
b2b8269… lmata 88 if d == nil {
b2b8269… lmata 89 t.Fatal("NewDiscovery returned nil")
b2b8269… lmata 90 }
b2b8269… lmata 91 }
b2b8269… lmata 92
b2b8269… lmata 93 func TestDiscoveryInvalidate(t *testing.T) {
b2b8269… lmata 94 c, _ := client.New(client.Options{
b2b8269… lmata 95 ServerAddr: "localhost:6667",
b2b8269… lmata 96 Nick: "agent-01",
b2b8269… lmata 97 Password: "secret",
b2b8269… lmata 98 })
b2b8269… lmata 99 d := client.NewDiscovery(c, client.DiscoveryOptions{CacheTTL: 10 * time.Minute})
b2b8269… lmata 100 // Invalidate should not panic on unknown keys.
b2b8269… lmata 101 d.Invalidate("list_channels")
b2b8269… lmata 102 d.Invalidate("members:#fleet")
b2b8269… lmata 103 d.Invalidate("topic:#fleet")
b2b8269… lmata 104 d.Invalidate("whois:nobody")
b2b8269… lmata 105 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button