twinapi

package
v0.7.1 Latest Latest
Warning

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

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

Documentation

Overview

Package twinapi provides a digital twin of the SageOx cloud API for testing.

Each Twin instance runs two HTTP servers on ephemeral ports: an API server that replicates the real SageOx auth surface (device flow, JWT exchange, userinfo, token refresh, revocation) and an admin server for test control (user creation, fault injection, clock manipulation, call recording).

The ox CLI points at the API port via SAGEOX_ENDPOINT and cannot distinguish the twin from the real cloud API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallRecord

type CallRecord struct {
	Method     string    `json:"method"`
	Path       string    `json:"path"`
	StatusCode int       `json:"status_code"`
	Timestamp  time.Time `json:"timestamp"`
}

CallRecord captures an API call for test assertions.

type DeviceCode

type DeviceCode struct {
	DeviceCode string
	UserCode   string
	Approved   bool
	UserID     string // set when approved
	ExpiresAt  time.Time
}

DeviceCode tracks a pending device authorization flow.

type EndpointFault

type EndpointFault struct {
	StatusCode int           `json:"status_code"`
	Body       string        `json:"body"`
	Latency    time.Duration `json:"latency"`
	After      int           `json:"after"` // only fault after N successful calls
	// contains filtered or unexported fields
}

EndpointFault configures synthetic failures for a path.

type Repo

type Repo struct {
	ID          string `json:"id"`
	TeamID      string `json:"team_id"`
	Fingerprint string `json:"fingerprint"`
}

Repo represents a SageOx-tracked repository.

type Session

type Session struct {
	Token        string    `json:"token"`
	UserID       string    `json:"user_id"`
	RefreshToken string    `json:"refresh_token"`
	ExpiresAt    time.Time `json:"expires_at"`
}

Session represents an active session bound to a user.

type Team

type Team struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Slug string `json:"slug"`
}

Team represents a SageOx team.

type Twin

type Twin struct {
	APIURL   string // "http://127.0.0.1:PORT" — what ox points at
	AdminURL string // "http://127.0.0.1:PORT2" — test control
	// contains filtered or unexported fields
}

Twin is a test double for the SageOx cloud API.

func Start

func Start(t testing.TB) *Twin

Start creates and starts a new Twin. Both servers are shut down via t.Cleanup. Each invocation gets isolated state and a fresh JWT signing key.

func (*Twin) AdvanceTime

func (tw *Twin) AdvanceTime(d time.Duration)

AdvanceTime moves the fake clock forward by d.

func (*Twin) AssertCalled

func (tw *Twin) AssertCalled(t testing.TB, method, path string)

AssertCalled fails the test if the given method+path was never called.

func (*Twin) AssertNotCalled

func (tw *Twin) AssertNotCalled(t testing.TB, method, path string)

AssertNotCalled fails the test if the given method+path was called.

func (*Twin) CallCount

func (tw *Twin) CallCount(method, path string) int

CallCount returns how many times the given method+path was called.

func (*Twin) Calls

func (tw *Twin) Calls() []CallRecord

Calls returns a copy of all recorded API call records.

func (*Twin) ClearFaults

func (tw *Twin) ClearFaults()

ClearFaults removes all configured faults.

func (*Twin) CreateOrphanedSession

func (tw *Twin) CreateOrphanedSession(fakeUserID string) *Session

CreateOrphanedSession creates a session pointing to a user ID that does not exist in the store. Useful for testing JWT exchange error paths.

func (*Twin) CreateSession

func (tw *Twin) CreateSession(userID string) *Session

CreateSession creates a session for the given user.

func (*Twin) CreateUser

func (tw *Twin) CreateUser(id, email, name string) *User

CreateUser adds a user to the store.

func (*Twin) Env

func (tw *Twin) Env() []string

Env returns environment variables that point ox at this twin.

func (*Twin) InjectFault

func (tw *Twin) InjectFault(path string, statusCode int)

InjectFault configures a fault for the given path. All requests to that path will return the specified status code.

func (*Twin) InjectFaultAfter

func (tw *Twin) InjectFaultAfter(path string, statusCode int, after int)

InjectFaultAfter configures a fault that only triggers after n successful calls.

func (*Twin) URL

func (tw *Twin) URL() string

URL is an alias for APIURL.

func (*Twin) WithAuthenticatedUser

func (tw *Twin) WithAuthenticatedUser(email, name string) *UserFixture

WithAuthenticatedUser creates a user, session, and JWT in one call. Returns a fixture with all credentials needed for authenticated API calls.

type User

type User struct {
	ID    string `json:"id"`
	Email string `json:"email"`
	Name  string `json:"name"`
	Tier  string `json:"tier"`
}

User represents an authenticated user in the twin.

type UserFixture

type UserFixture struct {
	UserID       string
	Email        string
	Name         string
	SessionToken string
	RefreshToken string
	JWT          string
}

UserFixture holds all tokens and IDs for a fully authenticated user.

Jump to

Keyboard shortcuts

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