testkit

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testkit has the in-process fake of the Checkend API and the CLI harness for tests.

Index

Constants

This section is empty.

Variables

View Source
var Migrations = map[string]string{
	"migrations/0001_init.sql":     "CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);",
	"migrations/0002_projects.sql": "CREATE TABLE projects (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
}

Migrations are the migration files of a fake release.

Functions

func Code

func Code(t testing.TB, r Result, want int)

Code checks the exit code and shows stderr when it is wrong.

func Contains

func Contains(t testing.TB, text, part string)

Contains checks that text contains part.

func Equal

func Equal(t testing.TB, got, want any)

Equal compares two values as JSON (so 1 and 1.0 are equal).

func Exists

func Exists(path string) bool

Exists reports whether a file exists.

func IngestURL

func IngestURL(dsn *url.URL, endpoint string) string

IngestURL is the URL where a Sentry SDK sends events for a DSN: the path of the DSN without its last segment (the base path of a hosted install), then /api/<project>/<endpoint>/.

func Matches

func Matches(t testing.TB, text, pattern string)

Matches checks text against a regular expression.

func NotContains

func NotContains(t testing.TB, text, part string)

NotContains checks that text does not contain part.

func NotMatches

func NotMatches(t testing.TB, text, pattern string)

NotMatches checks that text does not match a regular expression.

func Remarshal

func Remarshal(v any) any

Remarshal returns v as the JSON decoder gives it (numbers as float64), to compare with output.

func Sum

func Sum(data []byte) string

Sum is the SHA-256 of data in hex.

func TarGz

func TarGz(files map[string]string) []byte

TarGz makes a .tar.gz with these files.

Types

type Call

type Call struct {
	Name  string
	Args  []string
	Stdin string
}

Call is one program that the CLI ran.

type FakeAPI

type FakeAPI struct {
	Token string
	// SetupKey opens the /v1/setup routes. Tokens are more valid API tokens (agent tokens).
	SetupKey string
	Tokens   []string
	// APITokens are the tokens of POST /v1/tokens (with "token"; the list never shows it).
	APITokens []J
	// AgentApps is the app (public project ID) of each agent token.
	AgentApps  map[string]string
	SetupMD    string
	TestEvents map[string]bool
	Reports    []string
	MCPHeaders []http.Header
	Uploads    []*Request
	// URL is the origin plus the base path. Origin is without the base path.
	URL    string
	Origin string
	// Outside are the requests outside the base path (the fake answers 404).
	Outside  []*Request
	Health   J
	Ingest   J
	Projects []J
	Issues   []J
	Events   []J
	Override func(r *Request) *Reply
	// contains filtered or unexported fields
}

FakeAPI is a fake of the Checkend API (GET /health and /v1). It follows guides/api.md of the server. Tests change its state and can replace any response with Override.

func NewFakeAPI

func NewFakeAPI(t testing.TB) *FakeAPI

NewFakeAPI starts a fake on 127.0.0.1. It stops at the end of the test.

func NewFakeAPIAt

func NewFakeAPIAt(t testing.TB, base string) *FakeAPI

NewFakeAPIAt starts a fake that serves under a base path ("/k7q2x9"), as a hosted install. Requests outside the base path get 404 and go to Outside.

func (*FakeAPI) AddEvent

func (f *FakeAPI) AddEvent(fields J) J

AddEvent adds an event. fields must have issue_id.

func (*FakeAPI) AddIssue

func (f *FakeAPI) AddIssue(fields J) J

AddIssue adds an issue. fields must have project_id.

func (*FakeAPI) AddProject

func (f *FakeAPI) AddProject(name string, platform ...string) J

AddProject adds a project with a 10-digit public ID and a DSN.

func (*FakeAPI) FindProject

func (f *FakeAPI) FindProject(ref string) J

FindProject finds a project by public ID or slug.

func (*FakeAPI) Handle

func (f *FakeAPI) Handle(method, pattern string, handle func(r *Request, match []string) *Reply)

Handle adds a route (for endpoints that only some tests need). pattern matches the path.

func (*FakeAPI) Last

func (f *FakeAPI) Last() *Request

Last returns the last request, or nil.

func (*FakeAPI) Paths

func (f *FakeAPI) Paths() []string

Paths returns "METHOD /path" for each request.

func (*FakeAPI) Requests

func (f *FakeAPI) Requests() []*Request

Requests returns the requests so far.

func (*FakeAPI) Reset

func (f *FakeAPI) Reset()

Reset forgets the requests so far.

func (*FakeAPI) Stop

func (f *FakeAPI) Stop()

Stop stops the server. Requests after Stop fail to connect.

type FakeCloudflare

type FakeCloudflare struct {
	URL       string
	Token     string
	Accounts  []J
	Subdomain string
	Databases map[string]*FakeD1
	Scripts   map[string]*FakeScript
	Buckets   map[string]bool
	// Calls are "METHOD /path" of each request.
	Calls []string
	// SetupOpen is the setup_open of /health on a workers.dev host.
	SetupOpen bool
	// Fail makes a request fail with HTTP 500 when it returns true ("METHOD /path").
	Fail func(call string) bool
	// contains filtered or unexported fields
}

FakeCloudflare is a fake of the parts of the Cloudflare API that `ce install cloudflare` uses: token verify, accounts, the workers.dev subdomain, D1 (create, query, Time Travel bookmarks), Worker uploads, secrets, schedules and R2 buckets.

func NewFakeCloudflare

func NewFakeCloudflare(t testing.TB) *FakeCloudflare

NewFakeCloudflare starts the fake with one account and a workers.dev subdomain.

func (*FakeCloudflare) Account

func (f *FakeCloudflare) Account() string

Account is the ID of the first account.

func (*FakeCloudflare) Client

func (f *FakeCloudflare) Client() *http.Client

Client is an HTTP client that sends requests for *.workers.dev to the fake (the installed Worker answers /health there) and all other requests to their host.

func (*FakeCloudflare) DB

func (f *FakeCloudflare) DB(name string) *FakeD1

DB finds a database by name.

type FakeD1

type FakeD1 struct {
	ID         string
	Name       string
	Migrations []string
	// SQL is each statement batch that ran, in order.
	SQL       []string
	Tables    []string
	Bookmarks int
}

FakeD1 is a D1 database: its migrations (d1_migrations) and other tables.

type FakeRelease

type FakeRelease struct {
	Tag        string
	Commit     string
	Draft      bool
	Prerelease bool
	// Manifest is the manifest as a value; Publish signs its JSON.
	Manifest map[string]any
	// Assets are the files, by name. ManifestBytes and Signature are in Assets too.
	Assets map[string][]byte
}

FakeRelease is one GitHub release.

type FakeReleases

type FakeReleases struct {
	URL string
	// API is the repo API, for example <URL>/repos/Checkend/checkend.
	API   string
	KeyID string
	Key   ed25519.PrivateKey
	// Releases, newest first.
	Releases []*FakeRelease
	// Downloads counts the asset downloads, by asset name.
	Downloads map[string]int
	// contains filtered or unexported fields
}

FakeReleases is a fake of the GitHub releases API of the server repo. Each release has a signed checkend-release.json (format 1), the Worker bundle, the migrations and SHA256SUMS.

func NewFakeReleases

func NewFakeReleases(t testing.TB) *FakeReleases

NewFakeReleases starts the fake with no release.

func (*FakeReleases) PublicKeys

func (f *FakeReleases) PublicKeys() map[string]string

PublicKeys are the trusted keys for the CLI: key ID to "ssh-ed25519 AAAA…".

func (*FakeReleases) Publish

func (f *FakeReleases) Publish(version string, change ...func(m map[string]any)) *FakeRelease

Publish adds a release (newest first) with a valid manifest for this version. change can change the manifest before it is signed.

func (*FakeReleases) Sign

func (f *FakeReleases) Sign(r *FakeRelease)

Sign writes checkend-release.json and its signature from r.Manifest.

type FakeScript

type FakeScript struct {
	Metadata   map[string]any
	Modules    map[string]string
	Types      map[string]string
	Secrets    map[string]string
	Schedules  []string
	WorkersDev bool
}

FakeScript is a Worker.

type Harness

type Harness struct {
	Home    string
	Repo    string
	Keyring *credstore.Memory
	Prompts []Prompt
	// Git has the output of git commands, by their arguments ("rev-parse HEAD").
	Git map[string]string
	// Releases is the fake of the server releases (nil: none is configured).
	Releases *FakeReleases
	// Exec fakes the programs that the CLI runs (ssh, docker). Calls records each call.
	Exec  func(stdin []byte, name string, args ...string) ([]byte, error)
	Calls []Call
	// DNS answers host lookups.
	DNS map[string][]string
	// HTTP is the HTTP client of the CLI (nil: the default).
	HTTP *http.Client
	// Executable is the path of ce for `ce update`; CLIReleaseAPI fakes its releases.
	Executable    string
	CLIReleaseAPI string
	// CEOnPath is the `ce` that PATH finds ("": none).
	CEOnPath string
	// Cloudflare is the fake of the Cloudflare API (nil: none).
	Cloudflare *FakeCloudflare
	// contains filtered or unexported fields
}

Harness runs the CLI in-process with a fake terminal, a temporary home, a temporary repo and a keychain in memory.

func NewHarness

func NewHarness(t testing.TB) *Harness

NewHarness makes a home and a repo (with .git) in a temporary directory.

func (*Harness) Run

func (h *Harness) Run(argv []string, opts ...Options) Result

Run runs `ce argv...`.

func (*Harness) Write

func (h *Harness) Write(name, content string)

Write writes a file in the repo.

type J

type J = map[string]any

J is a JSON object.

type Options

type Options struct {
	Env map[string]string
	TTY bool
	// Answers for prompts, in order.
	Answers []string
	Stdin   string
	Cwd     string
}

Options change one run.

type Prompt

type Prompt struct {
	Question string
	Secret   bool
}

Prompt is one question that the CLI asked.

type Reply

type Reply struct {
	Status  int
	Body    any
	Headers map[string]string
	Raw     string
}

Reply replaces a response of the fake.

type Request

type Request struct {
	Method        string
	Path          string
	Query         url.Values
	Authorization string
	ContentType   string
	Body          any
	Raw           []byte
	Header        http.Header
}

Request is one request that the fake received.

type Result

type Result struct {
	Code   int
	Stdout string
	Stderr string
	// All is stdout and stderr together.
	All string
}

Result is the result of one CLI run.

func (Result) ErrJSON

func (r Result) ErrJSON(t testing.TB) map[string]any

ErrJSON decodes stderr.

func (Result) JSON

func (r Result) JSON(t testing.TB) map[string]any

JSON decodes stdout.

Jump to

Keyboard shortcuts

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