Documentation
¶
Overview ¶
Package ktest provides high-level test helpers for the kernel action runtime.
Mirrors xtest:
Require* Fatal. Calls tb.Fatalf and stops the current test (or subtest).
Assert* Soft. Calls tb.Errorf and continues. Only AssertContracts uses
this form today (reports every structural problem in one run).
Run(...) Fluent chain. Every terminal method is fatal.
Parameters of type testing.TB are named tb (not t).
Index ¶
- func AssertContracts(t *testing.T, actions []action.AnyAction)
- func BenchAction[Req, Res any](b *testing.B, act *action.BuiltAction[Req, Res], req Req)
- func Ctx(tb testing.TB) (context.Context, *xctx.RequestScope)
- func CtxWithAuth(tb testing.TB, userID, tenantID string, roles ...string) context.Context
- func CtxWithPerm(tb testing.TB, perm string) context.Context
- func CtxWithRole(tb testing.TB, role string) context.Context
- func CtxWithTenant(tb testing.TB, tenantID string) context.Context
- func CtxWithUser(tb testing.TB, userID string) context.Context
- func Echo[T any](name string) *action.Builder[T, T]
- func Fails[Req, Res any](name string, err error) *action.Builder[Req, Res]
- func Fake(name string, res any) action.AnyAction
- func FakeErr(name string, err error) action.AnyAction
- func FakeSeq(name string, values ...any) action.AnyAction
- func Flaky[Req, Res any](name string, failures int, err error, value Res) *action.Builder[Req, Res]
- func MustError[Req, Res any](tb testing.TB, fn action.Fn[Req, Res], req Req, want error)
- func MustExecute[Req, Res any](tb testing.TB, fn action.Fn[Req, Res], req Req) Res
- func NewErrAction(name string, err error) *action.Builder[struct{}, string]
- func NewOkAction(name string) *action.Builder[struct{}, string]
- func RequestContext(tb testing.TB, requestID string) context.Context
- func RequireCondition(tb testing.TB, condition bool, format string, args ...any)
- func RequireEqual[T any](tb testing.TB, got, want T)
- func RequireErrorContains(tb testing.TB, err error, substr string)
- func RequireErrorIs(tb testing.TB, err, target error)
- func RequireErrorKind(tb testing.TB, err error, want xerr.Kind)
- func RequireNoError(tb testing.TB, err error)
- func RequirePermanent(tb testing.TB, err error)
- func RequireStringContains(tb testing.TB, value, substr string)
- func RequireTransient(tb testing.TB, err error)
- func Returns[Req, Res any](name string, value Res) *action.Builder[Req, Res]
- func Script[Req, Res any](results ...Result[Res]) action.Fn[Req, Res]
- func Sequence[Req, Res any](name string, values ...Res) *action.Builder[Req, Res]
- func Simulate[Req, Res any](tb testing.TB, act *action.BuiltAction[Req, Res], req Req, concurrency int, ...)
- type Call
- type Counter
- type Recorder
- func (r *Recorder[Req, Res]) OnCacheHit(context.Context, Req, Res)
- func (r *Recorder[Req, Res]) OnCacheMiss(context.Context, Req)
- func (r *Recorder[Req, Res]) OnCoalesced(context.Context, Req)
- func (r *Recorder[Req, Res]) OnDeduplicated(context.Context, Req)
- func (r *Recorder[Req, Res]) OnRetry(_ context.Context, req Req, attempt int, err error)
- type Result
- type RetryEvent
- type RunResult
- func (r *RunResult[Res]) Contains(substr string) *RunResult[Res]
- func (r *RunResult[Res]) Equals(want Res) *RunResult[Res]
- func (r *RunResult[Res]) ErrorContains(substr string) *RunResult[Res]
- func (r *RunResult[Res]) ErrorKind(kind xerr.Kind) *RunResult[Res]
- func (r *RunResult[Res]) IsError(target error) *RunResult[Res]
- func (r *RunResult[Res]) NoError() *RunResult[Res]
- func (r *RunResult[Res]) Satisfies(fn func(res Res) bool, msgAndArgs ...any) *RunResult[Res]
- func (r *RunResult[Res]) Value() Res
- type Trace
- func (t *Trace) Calls() []Call
- func (t *Trace) Count(name string) int
- func (t *Trace) Dump(tb testing.TB)
- func (t *Trace) Hook() action.AnyHook
- func (t *Trace) Names() []string
- func (t *Trace) RequireAllSucceeded(tb testing.TB)
- func (t *Trace) RequireCalled(tb testing.TB, name string)
- func (t *Trace) RequireErrorsAt(tb testing.TB, want ...string)
- func (t *Trace) RequireNotCalled(tb testing.TB, name string)
- func (t *Trace) RequireOrder(tb testing.TB, first, second string)
- func (t *Trace) RequireSequence(tb testing.TB, want ...string)
- func (t *Trace) RequireSubsequence(tb testing.TB, want ...string)
- func (t *Trace) Reset()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertContracts ¶
AssertContracts checks structural invariants across every action and is intentionally soft: each action is validated in its own subtest using t.Errorf so that one bad action does not hide the rest.
func BenchAction ¶
func BenchAction[Req, Res any](b *testing.B, act *action.BuiltAction[Req, Res], req Req)
BenchAction benchmarks a built action's pure execution speed without transport overhead. Use it to lock in the zero-allocation hot path on Do/DoAny: a regression shows up in allocs/op immediately.
func Ctx ¶
Ctx returns a fresh request scope bound to context.Background() and registered with tb.Cleanup so it is released back to the pool when the test ends.
func CtxWithAuth ¶
CtxWithAuth is the common composite: user + tenant + roles in a single call.
func CtxWithPerm ¶
CtxWithPerm returns a context carrying a scope with the given permission.
func CtxWithRole ¶
CtxWithRole returns a context carrying a scope with the given role.
func CtxWithTenant ¶
CtxWithTenant returns a context carrying a scope with the tenant ID.
func CtxWithUser ¶
CtxWithUser returns a context carrying a scope with the user ID.
func Fake ¶
Fake returns an untyped action that always succeeds with res. Prefer Returns when the request and response types are known.
func FakeErr ¶
FakeErr returns an untyped action that always fails with err. Prefer Fails when the request and response types are known.
func FakeSeq ¶
FakeSeq returns an untyped action that yields each value in order, then repeats the last one. Prefer Sequence when the response type is known.
func Flaky ¶
Flaky returns err for the first failures executions and then returns value. It is deterministic and safe for concurrent action execution.
func MustExecute ¶
func NewErrAction ¶
NewErrAction returns a new action builder that accepts struct{} and returns ("", err).
func NewOkAction ¶
NewOkAction returns a new action builder that accepts struct{} and returns ("ok", nil).
func RequestContext ¶
RequestContext returns the test context carrying requestID. The request ID is the idempotency key used by action middleware; cancellation and timeout remain explicit with context.WithCancel/WithTimeout at the call site.
func RequireCondition ¶
RequireCondition fails the test when condition is false.
func RequireEqual ¶
RequireEqual fails the test when got and want differ.
func RequireErrorContains ¶
RequireErrorContains fails the test when err is nil or lacks substr.
func RequireErrorIs ¶
RequireErrorIs fails the test when err does not wrap target.
func RequireErrorKind ¶
RequireErrorKind fails the test unless err is an *xerr.AppError with the given kind. It prints the full error on failure so the caller does not need a second assertion to see what actually happened.
func RequireNoError ¶
RequireNoError fails the test when err is non-nil.
func RequirePermanent ¶
RequirePermanent fails the test unless err is permanent by xerr rules (must not be retried).
func RequireStringContains ¶
RequireStringContains fails the test when value lacks substr.
func RequireTransient ¶
RequireTransient fails the test unless err is transient by xerr rules (safe to retry).
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter records action executions without requiring a custom atomic in each test.
type Recorder ¶
type Recorder[Req, Res any] struct { Retries []RetryEvent[Req] CacheHits int CacheMisses int Coalesced int Deduplicated int // contains filtered or unexported fields }
Recorder implements action.HookDispatcher with full goroutine concurrency safety.
func (*Recorder[Req, Res]) OnCacheHit ¶
func (*Recorder[Req, Res]) OnCacheMiss ¶
func (*Recorder[Req, Res]) OnCoalesced ¶
func (*Recorder[Req, Res]) OnDeduplicated ¶
type RetryEvent ¶
RetryEvent records one retry attempt.
type RunResult ¶
type RunResult[Res any] struct { // contains filtered or unexported fields }
RunResult is a chainable assertion wrapper. Every terminal method is fatal.
func (*RunResult[Res]) ErrorContains ¶
type Trace ¶
type Trace struct {
// contains filtered or unexported fields
}