Documentation
¶
Overview ¶
Package synctest provides fake sync infrastructure and reusable push/pull scenario runners for tests.
Components ¶
- MemHub — in-process sync.Hub with idempotent push, optional stale-base conflict simulation (SetStaleOnMismatch), and canonical event logging
- Device — one fake device node with storetest.Backend stores and a wired sync.Engine
- Scenario — two-device setup (device A + device B) sharing one MemHub
- FixedClock, At — deterministic time helpers
Scenario flows ¶
SeedLocalCreate / SeedLocalUpdate write offline state (resources, history, outbox). RunPushPull pushes from device A and pulls on device B. OfflineCreateAndSync combines seeding and sync for the common offline-create → cloud → second-device flow.
ScenarioResult exposes push summary, pull summary, hub canonical events, device B resources, conflicts, and audit records for assertions.
ReferenceResolved checks that a FHIR reference in a pulled envelope points to a resource that exists in the target device's local store. Paths support dotted object traversal and arbitrary array indices, such as participant[0].actor.
Index ¶
- func At(year int, month time.Month, day, hour, min, sec int) time.Time
- func CanonicalSequenceGrowth(hub *MemHub) int64
- func FixedClock(t time.Time) hasync.Clock
- func ReferenceResolved(ctx context.Context, device *Device, source *types.ResourceEnvelope, ...) (bool, error)
- type Device
- func (d *Device) Pull(ctx context.Context) (*hasync.PullResultSummary, error)
- func (d *Device) Push(ctx context.Context) (*hasync.PushResultSummary, error)
- func (d *Device) ReadResource(ctx context.Context, resourceType, id string) (*types.ResourceEnvelope, error)
- func (d *Device) ResourceExists(ctx context.Context, resourceType, id string) (bool, error)
- func (d *Device) SeedLocalCreate(ctx context.Context, res *types.ResourceEnvelope, ts time.Time) error
- func (d *Device) SeedLocalUpdate(ctx context.Context, res *types.ResourceEnvelope, ts time.Time) error
- func (d *Device) WithConflictEngine(engine *conflict.Engine) *Device
- type MemHub
- func (h *MemHub) CanonicalEvents() []hasync.CanonicalEvent
- func (h *MemHub) Pull(_ context.Context, afterSequence int64, limit int) ([]hasync.CanonicalEvent, error)
- func (h *MemHub) Push(_ context.Context, events []hasync.LocalEvent) ([]hasync.PushResult, error)
- func (h *MemHub) Resources() map[string]*types.ResourceEnvelope
- func (h *MemHub) SeedResource(res *types.ResourceEnvelope)
- func (h *MemHub) SetClock(clock hasync.Clock)
- func (h *MemHub) SetPushError(err error)
- func (h *MemHub) SetStaleOnMismatch(enabled bool)
- type Scenario
- type ScenarioResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanonicalSequenceGrowth ¶
CanonicalSequenceGrowth returns the highest canonical sequence in the hub log.
func FixedClock ¶
FixedClock returns a deterministic sync.Clock pinned to t.
func ReferenceResolved ¶
func ReferenceResolved(ctx context.Context, device *Device, source *types.ResourceEnvelope, refPath string, targetType, targetID string) (bool, error)
ReferenceResolved reports whether targetType/targetID exists on device B when source references it via a FHIR reference string in JSON.
Types ¶
type Device ¶
type Device struct {
NodeID string
TenantID string
Backend *storetest.Backend
Engine *hasync.Engine
Clock hasync.Clock
}
Device is a fake sync device node with in-memory stores and an engine.
func (*Device) ReadResource ¶
func (d *Device) ReadResource(ctx context.Context, resourceType, id string) (*types.ResourceEnvelope, error)
ReadResource reads a resource from the local store.
func (*Device) ResourceExists ¶
ResourceExists reports whether a resource is stored locally.
func (*Device) SeedLocalCreate ¶
func (d *Device) SeedLocalCreate(ctx context.Context, res *types.ResourceEnvelope, ts time.Time) error
SeedLocalCreate records an offline resource create on the device outbox.
func (*Device) SeedLocalUpdate ¶
func (d *Device) SeedLocalUpdate(ctx context.Context, res *types.ResourceEnvelope, ts time.Time) error
SeedLocalUpdate records an offline resource update on the device outbox.
type MemHub ¶
type MemHub struct {
// contains filtered or unexported fields
}
MemHub is an in-process fake hub implementing sync.Hub.
func NewMemHub ¶
NewMemHub returns an empty in-memory sync hub. An optional tenant ID enables the same tenant-scope validation used by the production hub.
func (*MemHub) CanonicalEvents ¶
func (h *MemHub) CanonicalEvents() []hasync.CanonicalEvent
CanonicalEvents returns a copy of the hub canonical event log.
func (*MemHub) Push ¶
func (h *MemHub) Push(_ context.Context, events []hasync.LocalEvent) ([]hasync.PushResult, error)
func (*MemHub) Resources ¶
func (h *MemHub) Resources() map[string]*types.ResourceEnvelope
Resources returns hub-side resource snapshots keyed by ResourceType/id.
func (*MemHub) SeedResource ¶
func (h *MemHub) SeedResource(res *types.ResourceEnvelope)
SeedResource pre-populates a resource in the hub canonical state.
func (*MemHub) SetClock ¶
SetClock configures the timestamp source used for accepted canonical versions.
func (*MemHub) SetPushError ¶
SetPushError configures the hub to return an error on the next Push call.
func (*MemHub) SetStaleOnMismatch ¶
SetStaleOnMismatch enables stale-base conflict detection when BaseCloudVersion does not match the current hub resource version.
type Scenario ¶
type Scenario struct {
Hub *MemHub
DeviceA *Device
DeviceB *Device
TenantID string
Clock hasync.Clock
}
Scenario wires two device nodes to a shared fake hub.
func NewScenario ¶
NewScenario creates a two-device sync test scenario with a fresh hub.
func (*Scenario) RunPushPull ¶
func (s *Scenario) RunPushPull(ctx context.Context) (*ScenarioResult, error)
RunPushPull seeds nothing; it pushes from device A then pulls on device B.
type ScenarioResult ¶
type ScenarioResult struct {
PushSummary *hasync.PushResultSummary
PullSummary *hasync.PullResultSummary
HubEvents []hasync.CanonicalEvent
DeviceBResources []*types.ResourceEnvelope
Conflicts []store.ConflictRecord
AuditRecords []store.AuditRecord
}
ScenarioResult captures artifacts from a sync scenario run.
func OfflineCreateAndSync ¶
func OfflineCreateAndSync(ctx context.Context, s *Scenario, resources ...*types.ResourceEnvelope) (*ScenarioResult, error)
OfflineCreateAndSync is a reusable flow: seed on device A, push, pull on device B.