| 1 | package matrix |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | |
| 6 | "github.com/onsi/gomega" |
| 7 | |
| 8 | ginkgo "github.com/onsi/ginkgo/v2" |
| 9 | ) |
| 10 | |
| 11 | var _ = ginkgo.Describe("Service", func() { |
| 12 | ginkgo.Describe("GetID", func() { |
| 13 | ginkgo.It("should return the matrix scheme identifier", func() { |
| 14 | svc := &Service{} |
| 15 | gomega.Expect(svc.GetID()).To(gomega.Equal(Scheme)) |
| 16 | }) |
| 17 | |
| 18 | ginkgo.It("should always return 'matrix' regardless of service state", func() { |
| 19 | svc := &Service{ |
| 20 | Config: &Config{ |
| 21 | Host: "matrix.example.com", |
| 22 | }, |
| 23 | } |
| 24 | gomega.Expect(svc.GetID()).To(gomega.Equal("matrix")) |
| 25 | }) |
| 26 | }) |
| 27 | |
| 28 | ginkgo.Describe("Initialize", func() { |
| 29 | var svc *Service |
| 30 | |
| 31 | ginkgo.BeforeEach(func() { |
| 32 | svc = &Service{} |
| 33 | }) |
| 34 | |
| 35 | ginkgo.Context("with invalid URL", func() { |
| 36 | ginkgo.It("should return error when host is missing", func() { |
| 37 | invalidURL := &url.URL{ |
| 38 | Scheme: "matrix", |
| 39 | User: url.UserPassword("user", "password"), |
| 40 | } |
| 41 | err := svc.Initialize(invalidURL, nil) |
| 42 | gomega.Expect(err).To(gomega.MatchError(ErrMissingHost)) |
| 43 | }) |
| 44 | |
| 45 | ginkgo.It("should return error when password is missing", func() { |
| 46 | invalidURL := &url.URL{ |
| 47 | Scheme: "matrix", |
| 48 | Host: "matrix.example.com", |
| 49 | User: url.User("user"), |
| 50 | } |
| 51 | err := svc.Initialize(invalidURL, nil) |
| 52 | gomega.Expect(err).To(gomega.MatchError(ErrMissingCredentials)) |
| 53 | }) |
| 54 | }) |
| 55 | |
| 56 | ginkgo.Context("with valid URL but no client creation", func() { |
| 57 | // Test that we can set config without triggering client initialization |
| 58 | // The dummy URL check happens after config parsing, so we test config validation separately |
| 59 | ginkgo.It("should reject URL without user info when credentials required", func() { |
| 60 | // URL with host but no user - should fail at config validation |
| 61 | invalidURL := &url.URL{ |
| 62 | Scheme: "matrix", |
| 63 | Host: "matrix.example.com", |
| 64 | } |
| 65 | err := svc.Initialize(invalidURL, nil) |
| 66 | gomega.Expect(err).To(gomega.MatchError(ErrMissingCredentials)) |
| 67 | }) |
| 68 | }) |
| 69 | }) |
| 70 | |
| 71 | ginkgo.Describe("Send", func() { |
| 72 | var svc *Service |
| 73 | |
| 74 | ginkgo.BeforeEach(func() { |
| 75 | svc = &Service{} |
| 76 | }) |
| 77 | |
| 78 | ginkgo.Context("when client is not initialized", func() { |
| 79 | ginkgo.It("should return ErrClientNotInitialized", func() { |
| 80 | svc.Config = &Config{} |
| 81 | err := svc.Send("test message", nil) |
| 82 | gomega.Expect(err).To(gomega.MatchError(ErrClientNotInitialized)) |
| 83 | }) |
| 84 | }) |
| 85 | |
| 86 | ginkgo.Context("with initialized service but no client", func() { |
| 87 | ginkgo.BeforeEach(func() { |
| 88 | // Set config directly without initializing client |
| 89 | svc.Config = &Config{ |
| 90 | Host: "matrix.example.com", |
| 91 | } |
| 92 | // Forcefully set client to nil to test the error path |
| 93 | svc.client = nil |
| 94 | }) |
| 95 | |
| 96 | ginkgo.It("should return error when client is nil", func() { |
| 97 | err := svc.Send("test message", nil) |
| 98 | gomega.Expect(err).To(gomega.MatchError(ErrClientNotInitialized)) |
| 99 | }) |
| 100 | }) |
| 101 | }) |
| 102 | }) |