testutil

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testutil hosts in-memory test fakes for the production ports defined in github.com/algo2go/kite-mcp-clockport. Production code does NOT import testutil; only _test.go files do.

FakeClock + fakeTicker provide a deterministic implementation of clockport.Clock + clockport.Ticker that advances only when Advance() is called. This lets rate-limit / scheduler-style goroutines be driven forward without wall-clock waits.

The matching production port + RealClock implementation live at github.com/algo2go/kite-mcp-clockport (Path A.27, 28th algo2go module). The split is documented at .research/testutil-clock-port-split-design.md (commit fa6c70a) and .research/path-a-27-clockport-pick.md (this commit).

What this fake does NOT help with:

  • Sleeps that wait for external I/O (TCP bind, HTTP server readiness, SQLite worker drain). A fake clock cannot make the OS bind faster; those sleeps stay and belong to integration-test scope.

Package testutil provides shared test infrastructure for the kite-mcp-server project. It is NOT a _test.go file so that any package can import it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureLogger

func CaptureLogger() *logport.CaptureLogger

CaptureLogger returns a fresh kc/logger.CaptureLogger so a test can assert "logger received X" without piping through io.Discard + regex matching. The returned value is both the *CaptureLogger (with a Records() method) and a Logger — the test can pass it into the system under test and read back observed records afterwards.

func DiscardLogger

func DiscardLogger() *slog.Logger

DiscardLogger returns a slog.Logger that discards all output. It is the canonical no-op logger used by every package's test suite, so tests can converge on a single fixture instead of re-building their own.

func NewSessionKiteServer

func NewSessionKiteServer(t *testing.T) *httptest.Server

NewSessionKiteServer returns a bare *httptest.Server that handles the Kite session lifecycle routes (POST /session/token, GET /user/profile, DELETE /session/token) plus all default MockKiteServer data routes. It is the shared replacement for per-package newMockKiteServer helpers that test the GenerateSession → CompleteSession → InvalidateAccessToken flow.

The server is automatically closed when the test finishes.

func NoopLogger

func NoopLogger() logport.Logger

NoopLogger returns the kc/logger.Logger port wired to a no-op implementation — the test-time companion to DiscardLogger for code that has been migrated to depend on the Logger port instead of the concrete *slog.Logger. Both fixtures coexist during the gradual migration: pick whichever matches the constructor signature you're testing.

Types

type FakeClock

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

FakeClock is a test clock whose time only moves when Advance is called. All Tickers registered via NewTicker observe the advances atomically.

FakeClock is safe for concurrent use. Ticker channels are buffered (1) so an Advance that crosses multiple tick boundaries delivers one tick per boundary without blocking; if the caller has not drained the channel, subsequent ticks across the same boundary coalesce (matching the stdlib time.Ticker semantics).

func NewFakeClock

func NewFakeClock(start time.Time) *FakeClock

NewFakeClock returns a FakeClock initialised to the given time. Pass time.Now() if the absolute value is irrelevant.

func (*FakeClock) Advance

func (f *FakeClock) Advance(d time.Duration) int

Advance moves the fake clock forward by d, delivering ticks for every registered ticker whose `next` boundary the advance crosses. Returns the count of ticks delivered across all tickers.

func (*FakeClock) NewTicker

func (f *FakeClock) NewTicker(d time.Duration) clockport.Ticker

NewTicker registers a ticker that fires when Advance crosses the given interval. Stop removes it from the registry.

func (*FakeClock) Now

func (f *FakeClock) Now() time.Time

Now returns the current fake time.

func (*FakeClock) Set

func (f *FakeClock) Set(to time.Time) int

Set moves the fake clock to the given time, delivering ticks for any boundary the jump crosses. Useful when tests want absolute control.

type MockKiteServer

type MockKiteServer struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

MockKiteServer wraps an httptest.Server that simulates the Kite Connect API. Tests configure responses via the Set* methods and then point a kiteconnect.Client at Server.URL.

IMPORTANT: The gokiteconnect SDK routes GetLTP(), GetOHLC(), and GetQuotes() all through the /quote endpoint (not /quote/ltp or /quote/ohlc). This mock handles /quote as the primary route. The /quote/ltp and /quote/ohlc paths are registered as aliases for direct HTTP testing convenience.

func NewMockKiteServer

func NewMockKiteServer(t *testing.T) *MockKiteServer

NewMockKiteServer creates a mock Kite HTTP server with realistic default responses for all required endpoints. The server is automatically closed when the test finishes.

func (*MockKiteServer) Client

func (m *MockKiteServer) Client(apiKey, accessToken string) *kiteconnect.Client

Client creates a kiteconnect.Client pointed at this mock server.

func (*MockKiteServer) SetBasketMargins

func (m *MockKiteServer) SetBasketMargins(v any)

func (*MockKiteServer) SetHoldings

func (m *MockKiteServer) SetHoldings(v any)

func (*MockKiteServer) SetMFHoldings

func (m *MockKiteServer) SetMFHoldings(v any)

func (*MockKiteServer) SetMFOrders

func (m *MockKiteServer) SetMFOrders(v any)

func (*MockKiteServer) SetMFSIPs

func (m *MockKiteServer) SetMFSIPs(v any)

func (*MockKiteServer) SetMargins

func (m *MockKiteServer) SetMargins(v any)

func (*MockKiteServer) SetOrderMargins

func (m *MockKiteServer) SetOrderMargins(v any)

func (*MockKiteServer) SetOrders

func (m *MockKiteServer) SetOrders(v any)

func (*MockKiteServer) SetPositions

func (m *MockKiteServer) SetPositions(v any)

func (*MockKiteServer) SetProfile

func (m *MockKiteServer) SetProfile(v any)

func (*MockKiteServer) SetQuotes

func (m *MockKiteServer) SetQuotes(v any)

func (*MockKiteServer) SetTrades

func (m *MockKiteServer) SetTrades(v any)

func (*MockKiteServer) SetTriggerRange

func (m *MockKiteServer) SetTriggerRange(v any)

func (*MockKiteServer) URL

func (m *MockKiteServer) URL() string

URL returns the base URL of the mock server.

Directories

Path Synopsis
Package kcfixture provides a shared factory for building *kc.Manager instances in tests.
Package kcfixture provides a shared factory for building *kc.Manager instances in tests.

Jump to

Keyboard shortcuts

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