testutil

package
v0.0.0-...-676fa8b Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testutil provides shared test helpers: ephemeral SQLite databases with migrations applied, a MockProvider for the LLM interface, and a FakeTelegram httptest server that captures bot API calls. Production code never imports this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockResponseJSON

func MockResponseJSON(body string) llm.Response

MockResponseJSON wraps a raw JSON body in an llm.Response. Convenience for the most common assertion shape — canned parse output.

func NewDB

func NewDB(t *testing.T) *sql.DB

NewDB returns a fresh *sql.DB with all migrations applied, on a temp file in t.TempDir(). The database is closed via t.Cleanup. Production-equivalent: WAL, foreign keys on, single-writer connection (matches db.Open).

func SeedDefaultCategories

func SeedDefaultCategories(t *testing.T, q *sqlcgen.Queries) []sqlcgen.Category

SeedDefaultCategories inserts the same 8 categories the production first-run setup creates: Food, Transport, Utilities, Housing, Health, Entertainment, Shopping, Other. Returns them in display order.

func SeedLLMConfig

func SeedLLMConfig(t *testing.T, q *sqlcgen.Queries, provider, model string) sqlcgen.LlmConfig

SeedLLMConfig inserts a usable llm_config row with the given provider and model. The encrypted-key column gets a non-empty placeholder so LLMEngine doesn't bail with errLLMNotConfigured — but tests using a MockProvider override never decrypt this value.

func SeedOwner

func SeedOwner(t *testing.T, q *sqlcgen.Queries) sqlcgen.Owner

SeedOwner inserts the singleton owner row with usable defaults. Returns the freshly-loaded Owner so the test has the post-insert state (including the auto-defaulted trash_retention_days and dashboard_url).

func SeedTelegramConfig

func SeedTelegramConfig(t *testing.T, q *sqlcgen.Queries, chatID int64) sqlcgen.TelegramConfig

SeedTelegramConfig inserts a usable telegram_config row paired to the given chat ID. The bot_token column gets a placeholder.

Types

type FakeCall

type FakeCall struct {
	Method string         // bot API method, e.g. "sendMessage"
	Body   map[string]any // decoded JSON body
}

type FakeTelegram

type FakeTelegram struct {
	Server *httptest.Server

	Calls []FakeCall
	// contains filtered or unexported fields
}

FakeTelegram is an httptest.Server that mimics the Telegram Bot API for the methods the bot uses. Every captured call is appended to Calls so tests can assert outbound sendMessage / editMessageText / getFile / answerCallbackQuery payloads.

Constructor wires it as the BaseURL of a *telegram.Client; the client's callJSON path then hits the fake instead of api.telegram.org.

func NewFakeTelegram

func NewFakeTelegram(t *testing.T) *FakeTelegram

NewFakeTelegram starts the httptest server. Always call Close in t.Cleanup (already wired here).

func (*FakeTelegram) Captured

func (f *FakeTelegram) Captured() []FakeCall

Captured returns a snapshot of all calls so far, in order.

func (*FakeTelegram) CountCalls

func (f *FakeTelegram) CountCalls(method string) int

CountCalls returns how many calls landed for the given method name.

func (*FakeTelegram) LastEditMessage

func (f *FakeTelegram) LastEditMessage() map[string]any

LastEditMessage returns the most recent editMessageText body, or nil.

func (*FakeTelegram) LastSendMessage

func (f *FakeTelegram) LastSendMessage() map[string]any

LastSendMessage returns the most recent sendMessage body, or nil.

func (*FakeTelegram) WithFile

func (f *FakeTelegram) WithFile(fileID, mime string, body []byte)

WithFile registers binary content for a given file_id. The dispatcher's getFile + downloadFile sequence will return these bytes when the test pushes a Photo or Document Update with this file_id.

type MockProvider

type MockProvider struct {
	NameStr string
	Resp    llm.Response
	Err     error

	Calls []llm.Request
	// contains filtered or unexported fields
}

MockProvider implements llm.Provider with canned responses for tests. All Complete calls are recorded in Calls (mutex-guarded) so tests can assert what the dispatcher actually sent — system prompt content, images attached, model selected, etc.

Inject via DispatcherOpts.LLMProvider or Handler.LLMProvider.

func (*MockProvider) CallCount

func (m *MockProvider) CallCount() int

CallCount returns the number of Complete invocations.

func (*MockProvider) Complete

func (m *MockProvider) Complete(_ context.Context, r llm.Request) (llm.Response, error)

func (*MockProvider) LastCall

func (m *MockProvider) LastCall() llm.Request

LastCall returns the most recently recorded Request, or zero value if none.

func (*MockProvider) Name

func (m *MockProvider) Name() string

func (*MockProvider) Test

func (m *MockProvider) Test(_ context.Context, _ string) error

Jump to

Keyboard shortcuts

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