test

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package test contains test helpers.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
	AddSource: true,
	Level:     slog.LevelDebug,
	ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
		if a.Key == "time" {
			return slog.Attr{}
		}
		if a.Key == "level" {
			return slog.Attr{}
		}
		if a.Key == slog.SourceKey {
			str := a.Value.String()
			split := strings.Split(str, "/")
			if len(split) > 2 {
				a.Value = slog.StringValue(strings.Join(split[len(split)-2:], "/"))
				a.Value = slog.StringValue(strings.Replace(a.Value.String(), "}", "", -1))
			}
		}
		return a
	}}))

DefaultLogger is a default logger.

Functions

func CreateTestDirectory

func CreateTestDirectory(t *testing.T) (path string, cleanup func())

CreateTestDirectory creates a temporary folder which will be deleted when cleanup is called.

func CreateTestFile

func CreateTestFile(t *testing.T, path string)

CreateTestFile creates a fake file with "hello" as the content.

func GetAPIKey

func GetAPIKey(key string) (string, error)

GetAPIKey returns the api key.

func GetTestToken

func GetTestToken() string

GetTestToken returns the test token.

func IsIntegrationTest added in v0.9.4

func IsIntegrationTest() bool

IsIntegrationTest returns true if the unit test environment variable is set.

Types

type ErrTestErrorAccumulatorWriteFailed

type ErrTestErrorAccumulatorWriteFailed struct{}

ErrTestErrorAccumulatorWriteFailed is the error returned by the failing error buffer.

func (ErrTestErrorAccumulatorWriteFailed) Error

type FailingErrorBuffer

type FailingErrorBuffer struct{}

FailingErrorBuffer is a buffer that always fails to write.

func (*FailingErrorBuffer) Bytes

func (b *FailingErrorBuffer) Bytes() []byte

Bytes always

func (*FailingErrorBuffer) Len

func (b *FailingErrorBuffer) Len() int

Len always returns 0.

func (*FailingErrorBuffer) Write

func (b *FailingErrorBuffer) Write(_ []byte) (n int, err error)

Write always fails.

It is used to test the behavior of the error accumulator.

type Handler

type Handler func(w http.ResponseWriter, r *http.Request)

Handler is a function that handles a request.

type ServerTest

type ServerTest struct {
	// contains filtered or unexported fields
}

ServerTest is a test server for the groq api.

func NewTestServer

func NewTestServer() *ServerTest

NewTestServer creates a new test server.

func (*ServerTest) ComposioTestServer

func (ts *ServerTest) ComposioTestServer() *httptest.Server

ComposioTestServer Creates a mocked Composer server which can pretend to handle requests during testing.

func (*ServerTest) E2bTestServer

func (ts *ServerTest) E2bTestServer() *httptest.Server

E2bTestServer creates a test server for emulating the e2b api.

func (*ServerTest) GroqTestServer

func (ts *ServerTest) GroqTestServer() *httptest.Server

GroqTestServer Creates a mocked Groq server which can pretend to handle requests during testing.

func (*ServerTest) JigsawStackTestServer added in v0.9.4

func (ts *ServerTest) JigsawStackTestServer() *httptest.Server

JigsawStackTestServer Creates a mocked JigsawStack server which can pretend to handle requests during testing.

func (*ServerTest) RegisterHandler

func (ts *ServerTest) RegisterHandler(path string, handler Handler)

RegisterHandler registers a handler for a path.

func (*ServerTest) ToolhouseTestServer

func (ts *ServerTest) ToolhouseTestServer() *httptest.Server

ToolhouseTestServer creates a test server for emulating the toolhouse api.

type TokenRoundTripper

type TokenRoundTripper struct {
	Token    string
	Fallback http.RoundTripper
}

TokenRoundTripper is a struct that implements the RoundTripper interface, specifically to handle the authentication token by adding a token to the request header. We need this because the API requires that each request include a valid API token in the headers for authentication and authorization.

func (*TokenRoundTripper) RoundTrip

func (t *TokenRoundTripper) RoundTrip(
	req *http.Request,
) (*http.Response, error)

RoundTrip takes an *http.Request as input and returns an *http.Response and an error.

It is expected to use the provided request to create a connection to an HTTP server and return the response, or an error if one occurred. The returned Response should have its Body closed. If the RoundTrip method returns an error, the Client's Get, Head, Post, and PostForm methods return the same error.

Jump to

Keyboard shortcuts

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