Documentation
¶
Overview ¶
Package opencodetest builds OpenCode-shaped SQLite fixture databases for tests that exercise the OpenCode SQLite reader and downstream collector.
The DB this package writes uses the *real* OpenCode schema (session, message, part tables + the indices the reader's query plan depends on), backed by the same modernc.org/sqlite driver production uses. Tests build scenarios by chaining AddSession / AddMessage / AddPart calls or by using the higher-level shape helpers (UserTextMessage, ToolPartCompleted, ...).
No fixture DB is checked in. Each test seeds a fresh DB at <t.TempDir()>/opencode.db; the helper takes care of teardown via the testing.T's cleanup hooks.
Notes on row shape (verified against a real OpenCode v1.15.13 DB):
- message.data and part.data JSON do NOT carry id/sessionID/messageID; those live only in row columns. The reader's job is to inject them. The Add* helpers below preserve that property so the reader's injection path is what the tests actually exercise.
- time_created is set to 0 across all fixture rows. The reader's ORDER BY (time_created, m.id, p.id) clause falls through to id ordering when time_created is constant, matching real production semantics (where ULIDs and time_created are co-monotonic) and keeping fixture rows order-independent of insertion order.
Index ¶
- func AssistantMessageFinished(finish string) map[string]any
- func AssistantMessageStreaming() map[string]any
- func ReasoningPart(text string) map[string]any
- func StepFinishPart() map[string]any
- func TextPart(text string) map[string]any
- func ToolPartCompleted(tool string, input map[string]any, output string) map[string]any
- func UserTextMessage(text string) map[string]any
- type Builder
- func (b *Builder) AddMessage(sessionID, msgID string, data map[string]any) *Builder
- func (b *Builder) AddPart(messageID, partID string, data map[string]any) *Builder
- func (b *Builder) AddSession(id, parentID string) *Builder
- func (b *Builder) AddSessionWithDir(id, parentID, directory string) *Builder
- func (b *Builder) Path() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssistantMessageFinished ¶
AssistantMessageFinished returns a settled assistant message envelope. finish must be one of "stop", "tool-calls", "max_tokens", "length" — the finish field's presence is what gates ocIsComplete for assistant rows.
func AssistantMessageStreaming ¶
AssistantMessageStreaming returns an unsettled assistant message envelope (no finish, no error). The collector must NOT emit this and must stop walking newer messages once it encounters one.
func ReasoningPart ¶
ReasoningPart returns a reasoning part envelope.
func StepFinishPart ¶
StepFinishPart returns a step-finish part envelope.
func ToolPartCompleted ¶
ToolPartCompleted returns a settled tool part envelope.
func UserTextMessage ¶
UserTextMessage returns a user-role message envelope with the minimal fields real OpenCode emits for user turns. Caller adds a TextPart separately to give it a content part.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder seeds a temporary SQLite DB with the OpenCode schema.
func NewDB ¶
NewDB creates a temp SQLite DB at <t.TempDir()>/opencode.db with the OpenCode schema applied, and returns a Builder for adding rows. The DB handle is closed at the end of the test via t.Cleanup.
func (*Builder) AddMessage ¶
AddMessage inserts a message row. data is the JSON envelope minus id/sessionID — the reader reconstructs those from row columns. The helper marshals the map to JSON before insert.
func (*Builder) AddPart ¶
AddPart inserts a part row under a message. data is the part envelope minus id/sessionID/messageID — the reader reconstructs those from columns. The session_id column is denormalized (matches production); it is derived from the parent message row.
func (*Builder) AddSession ¶
AddSession inserts a session row. parentID="" for a root session. NOT NULL columns get placeholder values; tests that exercise the session.directory or session.parent_id columns should use AddSessionWithDir instead.
func (*Builder) AddSessionWithDir ¶
AddSessionWithDir is the full-form session insert: it lets the caller set the session.directory column, which the reader's ReadSessionInfo surfaces. Used by CF-549 tests that assert on directory + parent_id.