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 ¶
- type CallRecord
- type DeviceCode
- type EndpointFault
- type Repo
- type Session
- type Team
- type Twin
- func (tw *Twin) AdvanceTime(d time.Duration)
- func (tw *Twin) AssertCalled(t testing.TB, method, path string)
- func (tw *Twin) AssertNotCalled(t testing.TB, method, path string)
- func (tw *Twin) CallCount(method, path string) int
- func (tw *Twin) Calls() []CallRecord
- func (tw *Twin) ClearFaults()
- func (tw *Twin) CreateOrphanedSession(fakeUserID string) *Session
- func (tw *Twin) CreateSession(userID string) *Session
- func (tw *Twin) CreateUser(id, email, name string) *User
- func (tw *Twin) Env() []string
- func (tw *Twin) InjectFault(path string, statusCode int)
- func (tw *Twin) InjectFaultAfter(path string, statusCode int, after int)
- func (tw *Twin) URL() string
- func (tw *Twin) WithAuthenticatedUser(email, name string) *UserFixture
- type User
- type UserFixture
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 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 ¶
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 ¶
AdvanceTime moves the fake clock forward by d.
func (*Twin) AssertCalled ¶
AssertCalled fails the test if the given method+path was never called.
func (*Twin) AssertNotCalled ¶
AssertNotCalled fails the test if 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) CreateOrphanedSession ¶
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 ¶
CreateSession creates a session for the given user.
func (*Twin) CreateUser ¶
CreateUser adds a user to the store.
func (*Twin) InjectFault ¶
InjectFault configures a fault for the given path. All requests to that path will return the specified status code.
func (*Twin) InjectFaultAfter ¶
InjectFaultAfter configures a fault that only triggers after n successful calls.
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.