|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + toolhivetypes "github.com/stacklok/toolhive/pkg/registry/registry" |
| 9 | +) |
| 10 | + |
| 11 | +// ToolHiveRegistryOption is a function that configures a ToolHive Registry for testing |
| 12 | +type ToolHiveRegistryOption func(*toolhivetypes.Registry) |
| 13 | + |
| 14 | +// ImageServerOption is a function that configures an ImageMetadata (OCI server) for testing |
| 15 | +type ImageServerOption func(*toolhivetypes.ImageMetadata) |
| 16 | + |
| 17 | +// RemoteServerOption is a function that configures a RemoteServerMetadata for testing |
| 18 | +type RemoteServerOption func(*toolhivetypes.RemoteServerMetadata) |
| 19 | + |
| 20 | +// NewTestToolHiveRegistry creates a new ToolHive Registry for testing with default values |
| 21 | +// and applies any provided options |
| 22 | +func NewTestToolHiveRegistry(opts ...ToolHiveRegistryOption) *toolhivetypes.Registry { |
| 23 | + reg := &toolhivetypes.Registry{ |
| 24 | + Version: "1.0.0", |
| 25 | + LastUpdated: time.Now().Format(time.RFC3339), |
| 26 | + Servers: make(map[string]*toolhivetypes.ImageMetadata), |
| 27 | + RemoteServers: make(map[string]*toolhivetypes.RemoteServerMetadata), |
| 28 | + } |
| 29 | + |
| 30 | + for _, opt := range opts { |
| 31 | + opt(reg) |
| 32 | + } |
| 33 | + |
| 34 | + return reg |
| 35 | +} |
| 36 | + |
| 37 | +// WithToolHiveVersion sets the registry version |
| 38 | +func WithToolHiveVersion(version string) ToolHiveRegistryOption { |
| 39 | + return func(reg *toolhivetypes.Registry) { |
| 40 | + reg.Version = version |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// WithToolHiveLastUpdated sets the registry last updated timestamp |
| 45 | +func WithToolHiveLastUpdated(timestamp string) ToolHiveRegistryOption { |
| 46 | + return func(reg *toolhivetypes.Registry) { |
| 47 | + reg.LastUpdated = timestamp |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// WithImageServer adds an OCI/container image server to the registry |
| 52 | +func WithImageServer(name, image string, opts ...ImageServerOption) ToolHiveRegistryOption { |
| 53 | + return func(reg *toolhivetypes.Registry) { |
| 54 | + server := &toolhivetypes.ImageMetadata{ |
| 55 | + BaseServerMetadata: toolhivetypes.BaseServerMetadata{ |
| 56 | + Name: name, |
| 57 | + Description: fmt.Sprintf("Test server description for %s", name), |
| 58 | + Tier: "Community", |
| 59 | + Status: "Active", |
| 60 | + Transport: "stdio", |
| 61 | + Tools: []string{"test_tool"}, |
| 62 | + Tags: []string{"database"}, |
| 63 | + }, |
| 64 | + Image: image, |
| 65 | + } |
| 66 | + |
| 67 | + for _, opt := range opts { |
| 68 | + opt(server) |
| 69 | + } |
| 70 | + |
| 71 | + reg.Servers[name] = server |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// WithRemoteServerURL adds a remote (HTTP/SSE) server to the registry |
| 76 | +func WithRemoteServerURL(name, url string, opts ...RemoteServerOption) ToolHiveRegistryOption { |
| 77 | + return func(reg *toolhivetypes.Registry) { |
| 78 | + server := &toolhivetypes.RemoteServerMetadata{ |
| 79 | + BaseServerMetadata: toolhivetypes.BaseServerMetadata{ |
| 80 | + Name: name, |
| 81 | + Description: fmt.Sprintf("Test remote server description for %s", name), |
| 82 | + Tier: "Community", |
| 83 | + Status: "Active", |
| 84 | + Transport: "sse", |
| 85 | + Tools: []string{"remote_tool"}, |
| 86 | + }, |
| 87 | + URL: url, |
| 88 | + } |
| 89 | + |
| 90 | + for _, opt := range opts { |
| 91 | + opt(server) |
| 92 | + } |
| 93 | + |
| 94 | + reg.RemoteServers[name] = server |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// ImageServerOption helpers |
| 99 | + |
| 100 | +// WithImageDescription sets the server description |
| 101 | +func WithImageDescription(description string) ImageServerOption { |
| 102 | + return func(server *toolhivetypes.ImageMetadata) { |
| 103 | + server.Description = description |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// WithImageTier sets the server tier |
| 108 | +func WithImageTier(tier string) ImageServerOption { |
| 109 | + return func(server *toolhivetypes.ImageMetadata) { |
| 110 | + server.Tier = tier |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// WithImageStatus sets the server status |
| 115 | +func WithImageStatus(status string) ImageServerOption { |
| 116 | + return func(server *toolhivetypes.ImageMetadata) { |
| 117 | + server.Status = status |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// WithImageTransport sets the server transport |
| 122 | +func WithImageTransport(transport string) ImageServerOption { |
| 123 | + return func(server *toolhivetypes.ImageMetadata) { |
| 124 | + server.Transport = transport |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// WithImageTools sets the server tools |
| 129 | +func WithImageTools(tools ...string) ImageServerOption { |
| 130 | + return func(server *toolhivetypes.ImageMetadata) { |
| 131 | + server.Tools = tools |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// WithImageTags sets the server tags |
| 136 | +func WithImageTags(tags ...string) ImageServerOption { |
| 137 | + return func(server *toolhivetypes.ImageMetadata) { |
| 138 | + server.Tags = tags |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// RemoteServerOption helpers |
| 143 | + |
| 144 | +// WithRemoteDescription sets the remote server description |
| 145 | +func WithRemoteDescription(description string) RemoteServerOption { |
| 146 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 147 | + server.Description = description |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// WithRemoteTier sets the remote server tier |
| 152 | +func WithRemoteTier(tier string) RemoteServerOption { |
| 153 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 154 | + server.Tier = tier |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// WithRemoteStatus sets the remote server status |
| 159 | +func WithRemoteStatus(status string) RemoteServerOption { |
| 160 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 161 | + server.Status = status |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// WithRemoteTransport sets the remote server transport |
| 166 | +func WithRemoteTransport(transport string) RemoteServerOption { |
| 167 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 168 | + server.Transport = transport |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// WithRemoteTools sets the remote server tools |
| 173 | +func WithRemoteTools(tools ...string) RemoteServerOption { |
| 174 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 175 | + server.Tools = tools |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// WithRemoteTags sets the remote server tags |
| 180 | +func WithRemoteTags(tags ...string) RemoteServerOption { |
| 181 | + return func(server *toolhivetypes.RemoteServerMetadata) { |
| 182 | + server.Tags = tags |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// Helper functions for JSON generation |
| 187 | + |
| 188 | +// ToolHiveRegistryToJSON converts a ToolHive Registry to JSON bytes |
| 189 | +func ToolHiveRegistryToJSON(reg *toolhivetypes.Registry) []byte { |
| 190 | + data, err := json.Marshal(reg) |
| 191 | + if err != nil { |
| 192 | + panic(fmt.Sprintf("Failed to marshal ToolHive registry: %v", err)) |
| 193 | + } |
| 194 | + return data |
| 195 | +} |
| 196 | + |
| 197 | +// ToolHiveRegistryToPrettyJSON converts a ToolHive Registry to pretty-printed JSON bytes |
| 198 | +func ToolHiveRegistryToPrettyJSON(reg *toolhivetypes.Registry) []byte { |
| 199 | + data, err := json.MarshalIndent(reg, "", " ") |
| 200 | + if err != nil { |
| 201 | + panic(fmt.Sprintf("Failed to marshal ToolHive registry: %v", err)) |
| 202 | + } |
| 203 | + return data |
| 204 | +} |
| 205 | + |
| 206 | +// Helper functions for common test scenarios |
| 207 | + |
| 208 | +// InvalidJSON returns intentionally malformed JSON for testing error cases |
| 209 | +func InvalidJSON() []byte { |
| 210 | + return []byte("invalid json") |
| 211 | +} |
| 212 | + |
| 213 | +// EmptyToolHiveJSON returns an empty ToolHive registry JSON object |
| 214 | +func EmptyToolHiveJSON() []byte { |
| 215 | + return []byte("{}") |
| 216 | +} |
0 commit comments