testutil

package
v0.0.0-...-fa64a56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CleanupLabel is used to identify resources created by tests
	CleanupLabel = "shelf-test"
)

Variables

This section is empty.

Functions

func CleanupAllTestContainers

func CleanupAllTestContainers(ctx context.Context) error

CleanupAllTestContainers removes ALL shelf-test containers. Use sparingly - mainly for cleaning up after interrupted test runs.

func ContainerLabels

func ContainerLabels(t TestingT) map[string]string

ContainerLabels returns labels to apply to test containers. These labels are used for cleanup.

func DockerClient

func DockerClient(t TestingT) *client.Client

DockerClient creates a Docker client and registers cleanup for test containers. It cleans up any orphaned containers from previous interrupted runs with the same test name.

func FindFreePort

func FindFreePort() (string, error)

FindFreePort finds an available TCP port and returns it as a string.

func HTTPClient

func HTTPClient() *http.Client

HTTPClient returns an HTTP client for making requests.

func UniqueContainerName

func UniqueContainerName(t TestingT, prefix string) string

UniqueContainerName generates a unique container name for a test. Format: shelf-test-<testname>-<random>

func WaitForServer

func WaitForServer(url string, timeout time.Duration) error

WaitForServer polls the /api/status endpoint until DefraDB is healthy.

func WaitForShutdown

func WaitForShutdown(done <-chan error, timeout time.Duration) error

WaitForShutdown waits for a channel to receive a value or timeout.

Types

type DefraTestConfig

type DefraTestConfig struct {
	ContainerName string
	HostPort      string
	Labels        map[string]string
}

DefraTestConfig holds DefraDB container configuration without importing defra package. This breaks the import cycle between testutil and defra.

type ServerConfig

type ServerConfig struct {
	Host          string
	Port          string
	DefraDataPath string
	ConfigFile    string
	DefraConfig   DefraTestConfig
	Logger        *slog.Logger
}

ServerConfig returns configuration values for creating a test server. This avoids importing the server package directly.

func NewServerConfig

func NewServerConfig(t *testing.T) ServerConfig

NewServerConfig creates configuration for a test server with unique ports.

func (ServerConfig) URL

func (c ServerConfig) URL() string

URL returns the server URL for the given config.

type StartServer

type StartServer struct {
	Cancel context.CancelFunc
	Done   <-chan error
}

StartServer is a helper type for managing server lifecycle in tests. Usage:

cfg := testutil.NewServerConfig(t)
srv, err := server.New(server.Config{...from cfg...})
starter := testutil.StartServer{Cancel: cancel, Done: done}
t.Cleanup(func() { starter.Stop() })

func (*StartServer) Stop

func (s *StartServer) Stop()

Stop cancels the server context and waits for shutdown.

type StatusResponse

type StatusResponse struct {
	Server    string `json:"server"`
	Providers struct {
		OCR []string `json:"ocr"`
		LLM []string `json:"llm"`
	} `json:"providers"`
	Defra struct {
		Container string `json:"container"`
		Health    string `json:"health"`
		URL       string `json:"url"`
	} `json:"defra"`
}

StatusResponse matches the server's StatusResponse structure.

func GetStatus

func GetStatus(url string) (*StatusResponse, error)

GetStatus fetches the /api/status endpoint and returns the parsed response.

type TestingT

type TestingT interface {
	Name() string
	Cleanup(func())
	Logf(format string, args ...any)
	Helper()
}

TestingT is a subset of testing.T used for Docker setup

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL