{"modulePath":"github.com/ghbvf/gocell","version":"v0.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package commandtest\n\nPackage commandtest provides shared conformance assertions for command.Queue + command.ActiveScanner implementations. Both the in-memory InMemQueue and the PostgreSQL command\\_queue adapter (adapters/postgres) enroll against the same suite so behavior stays in lock-step.\n\nref: runtime/audit/ledger/storetest (kernel-side conformance shape) ref: cells/accesscore/internal/ports/conformance (Factory + Features pattern)\n\nPackage commandtest provides in-memory implementations of the command package interfaces for use in unit tests and examples.\n\nNOTE: Not suitable for production deployments. Replace with a persistent adapter (e.g., adapters/postgres command store) for durable mode. This package is importable from non-test code intentionally — the guard against production misuse lives at the Cell wiring layer.\n\n## Functions\n\n```go\nfunc RunQueueConformance(t *testing.T, factory QueueFactory, features Features)\n```\nRunQueueConformance drives the shared state-machine assertions against the implementation produced by factory. Test names are scoped t.Run sub-tests so failures point at a single transition rather than a soup of assertions.\n\n## Types\n\n```go\ntype Features struct {\n\t// RequiresAmbientTx indicates writes must run inside a txRunner.RunInTx\n\t// scope (true for PG; false for mem). When true, the suite wraps each\n\t// mutating call in RunInTx; when false, it calls the Queue methods directly.\n\tRequiresAmbientTx bool\n\n\t// SupportsLeaseRenewal indicates ExtendLease is honored (true for both\n\t// today; the flag exists so future read-only mirrors can opt out without\n\t// silently passing).\n\tSupportsLeaseRenewal bool\n}\n```\nFeatures captures behavioral differences between in-memory and durable implementations that the conformance suite must accommodate without resorting to t.Skip. Features should describe \\*what\\* the implementation offers, never \\*whether to skip\\*.\n\n```go\ntype InMemQueue struct {\n\n\t// Now supplies the clock. Defaults to time.Now if nil.\n\tNow func() time.Time\n\t// contains filtered or unexported fields\n}\n```\nInMemQueue is a process-local, thread-safe implementation of command.Queue, command.ActiveScanner, and command.Writer backed by a map. It is NOT suitable for multi-replica coordination — use for tests and examples.\n\nImplements:\n\n  - command.Queue          (Enqueue/Dequeue/Report/Ack/ExtendLease/Cancel)\n  - command.ActiveScanner  (ScanActive/GetCommand)\n  - command.Writer         (WriteCommand — for test seeding)\n\n```go\nfunc NewInMemQueue() *InMemQueue\n```\nNewInMemQueue creates a new InMemQueue with the default wall clock.\n\n```go\nfunc (q *InMemQueue) Ack(_ context.Context, commandID string, reason command.AckReason, now time.Time) error\n```\nAck finalizes a command atomically in a single transition step. The AckReason maps directly to a terminal status:\n\n  - AckSuccess:  current → StatusSucceeded\n  - AckFailed:   current → StatusFailed\n  - AckTimeout:  current → StatusExpired (used by Sweeper on deadline elapse)\n  - AckRejected: current → StatusCanceled\n\nNo chaining: Ack does NOT advance through intermediate states. Acking from StatusSent directly to StatusSucceeded leaves DeliveredAt nil, which is the signal that the device skipped the optional Report step. Already-terminal entries are idempotent only when the requested reason maps to the existing terminal status. A different terminal target is rejected.\n\n```go\nfunc (q *InMemQueue) Cancel(_ context.Context, commandID string, now time.Time) error\n```\nCancel transitions a non-terminal command to StatusCanceled (operator action).\n\n```go\nfunc (q *InMemQueue) Dequeue(_ context.Context, targetID string, n int, leaseDuration time.Duration) ([]command.Entry, error)\n```\nDequeue returns up to n Pending entries for targetID, oldest first. Each returned entry is advanced to StatusSent (incrementing Attempt) and assigned a lease.\n\n```go\nfunc (q *InMemQueue) Enqueue(ctx context.Context, entry command.Entry, opts command.EnqueueOptions) error\n```\nEnqueue stores an entry atomically with optional authz and idempotency. If entry.ID is empty, a random ID is assigned. If opts.Authz is non-nil, it is called before any write; a non-nil return rejects the enqueue. If opts.IdempotencyKey is non-empty and an entry with that key already exists, this is a no-op (idempotent dedup). InMemQueue stores the key as metadata\\[\"\\_idempotency\\_key\"].\n\n```go\nfunc (q *InMemQueue) ExtendLease(_ context.Context, commandID string, extension time.Duration, now time.Time) error\n```\nExtendLease renews the lease for a command. Returns ErrNotFound if the command does not exist in the queue. Returns ErrValidationFailed (lease expired) if the command exists but its lease has expired or was never acquired (e.g. Pending, not yet Dequeued).\n\n```go\nfunc (q *InMemQueue) GetCommand(_ context.Context, id string) (*command.Entry, error)\n```\nGetCommand returns a single command by ID, or nil if not found. Internal metadata keys (prefix \"\\_\") are stripped from the returned copy.\n\n```go\nfunc (q *InMemQueue) RepoReady(_ context.Context) error\n```\nRepoReady always returns nil for the in-memory queue (no external dependency). Satisfies healthz.RepoProber so the same queue value can be registered as the \"command\\_queue\\_ready\" readiness probe in demo/test mode.\n\n```go\nfunc (q *InMemQueue) Report(_ context.Context, commandID string, now time.Time) error\n```\nReport advances a command from Sent to Delivered, recording that the device has acknowledged receipt and begun execution. No-op (nil) if the command is already Delivered (idempotent); returns ErrValidationFailed for any other non-Sent status.\n\n```go\nfunc (q *InMemQueue) ScanActive(_ context.Context, filter command.ScanFilter) ([]command.Entry, error)\n```\nScanActive returns all non-terminal entries matching filter, ordered by CreatedAt ascending. filter.DeviceID=\"\" means scan all devices; filter.Statuses=nil means all non-terminal statuses (Pending/Sent/Delivered). Terminal statuses in filter.Statuses are silently ignored.\n\n```go\nfunc (q *InMemQueue) WriteCommand(_ context.Context, entry command.Entry) error\n```\nWriteCommand stores an entry directly (bypasses Enqueue validation). Used by adapter-level tests that need to seed pre-existing entries.\n\n```go\ntype QueueFactory func(t *testing.T) (\n\tq command.Queue,\n\tscanner command.ActiveScanner,\n\ttxRunner TxRunner,\n\tnow func() time.Time,\n\tcleanup func(),\n)\n```\nQueueFactory builds a fresh Queue + ActiveScanner pair (typically the same concrete type) plus the matching TxRunner. The returned cleanup MUST be idempotent and tolerate being called even if no resources were acquired.\n\nThe returned clock is the time source the implementation must obey when the suite drives state transitions. Implementations that use wall time should return time.Now-equivalent here so the conformance \"now\" stays consistent.\n\n```go\ntype TxRunner interface {\n\tRunInTx(ctx context.Context, fn func(ctx context.Context) error) error\n}\n```\nTxRunner is the minimal transactional runner contract the conformance suite depends on. It mirrors kernel/persistence.TxRunner structurally — any implementation of that public interface satisfies this one — but is declared locally so kernel/command/commandtest does not import kernel/persistence (KERNEL-INTERNAL-DAG-01: command package would otherwise gain a cross-owner edge to persistence).\n\nThe Go idiom is \"accept interfaces, return concrete types\"; the conformance suite is the accept-interface site, and a one-method local definition keeps the kernel-internal DAG flat without adding a runtime dependency.\n\n","path":"github.com/ghbvf/gocell/kernel/command/commandtest","name":"commandtest","synopsis":"Package commandtest provides shared conformance assertions for command.Queue + command.ActiveScanner implementations.","isRedistributable":true}
