synctest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At(year int, month time.Month, day, hour, min, sec int) time.Time

At is a test helper for fixed scenario clocks.

func CanonicalSequenceGrowth

func CanonicalSequenceGrowth(hub *MemHub) int64

CanonicalSequenceGrowth returns the highest canonical sequence in the hub log.

func FixedClock

func FixedClock(t time.Time) hasync.Clock

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 NewDevice

func NewDevice(nodeID, tenantID string, hub hasync.Hub, clock hasync.Clock) *Device

NewDevice constructs a device node wired to the given hub.

func (*Device) Pull

Pull runs one pull pass on the device.

func (*Device) Push

Push runs one push pass on the device.

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

func (d *Device) ResourceExists(ctx context.Context, resourceType, id string) (bool, error)

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.

func (*Device) WithConflictEngine

func (d *Device) WithConflictEngine(engine *conflict.Engine) *Device

WithConflictEngine attaches a conflict engine to the device sync config.

type MemHub

type MemHub struct {
	// contains filtered or unexported fields
}

MemHub is an in-process fake hub implementing sync.Hub.

func NewMemHub

func NewMemHub(tenantIDs ...string) *MemHub

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) Pull

func (h *MemHub) Pull(_ context.Context, afterSequence int64, limit int) ([]hasync.CanonicalEvent, error)

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

func (h *MemHub) SetClock(clock hasync.Clock)

SetClock configures the timestamp source used for accepted canonical versions.

func (*MemHub) SetPushError

func (h *MemHub) SetPushError(err error)

SetPushError configures the hub to return an error on the next Push call.

func (*MemHub) SetStaleOnMismatch

func (h *MemHub) SetStaleOnMismatch(enabled bool)

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

func NewScenario(tenantID string, clock hasync.Clock) *Scenario

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.

Jump to

Keyboard shortcuts

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