opencodetest

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssistantMessageFinished

func AssistantMessageFinished(finish string) map[string]any

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

func AssistantMessageStreaming() map[string]any

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

func ReasoningPart(text string) map[string]any

ReasoningPart returns a reasoning part envelope.

func StepFinishPart

func StepFinishPart() map[string]any

StepFinishPart returns a step-finish part envelope.

func TextPart

func TextPart(text string) map[string]any

TextPart returns a text part envelope.

func ToolPartCompleted

func ToolPartCompleted(tool string, input map[string]any, output string) map[string]any

ToolPartCompleted returns a settled tool part envelope.

func UserTextMessage

func UserTextMessage(text string) map[string]any

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

func NewDB(t *testing.T) *Builder

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

func (b *Builder) AddMessage(sessionID, msgID string, data map[string]any) *Builder

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

func (b *Builder) AddPart(messageID, partID string, data map[string]any) *Builder

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

func (b *Builder) AddSession(id, parentID string) *Builder

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

func (b *Builder) AddSessionWithDir(id, parentID, directory string) *Builder

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.

func (*Builder) Path

func (b *Builder) Path() string

Path returns the absolute path to the fixture DB.

Jump to

Keyboard shortcuts

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