Files
archie/internal/infrastructure/servicediscovery/nats/keys_test.go
T
sam 0bc65b680d feat(servicediscovery): scaffold ServiceRegistry contract and NATS KV implementation
Add a broker-neutral service registry contract (Endpoint, EventKind,
Event, ServiceRegistry, ErrNotInstalled) and a NATS JetStream KV
implementation, plus the registration side a service uses to announce itself.

The contract's governing semantic -- NotInstalled is not Down -- is modeled
by two buckets: a durable installed marker (never expires) distinct from
expiring heartbeat keys. Instances heartbeat their key on an interval; a
stopped process leaves via TTL expiry. Register writes the marker once then
heartbeats; Resolve returns ErrNotInstalled only when the marker is absent,
and an empty roster for installed-but-down. Watch dedupes heartbeat refreshes
so it emits membership changes, not liveness pings.

Unwired: no daemon/bootstrap path consumes it yet. Deliberately uses a dot
key separator instead of the research sketch's slash so per-service watch
wildcards match.
2026-09-05 03:11:43 +10:00

50 lines
1.4 KiB
Go

package nats
import "testing"
func TestEndpointKey(t *testing.T) {
tests := []struct {
name string
service string
id string
want string
}{
{name: "simple", service: "curator", id: "inst-1", want: "curator.inst-1"},
{name: "single-char id", service: "scheduler", id: "a", want: "scheduler.a"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := endpointKey(tt.service, tt.id); got != tt.want {
t.Errorf("endpointKey(%q, %q) = %q, want %q", tt.service, tt.id, got, tt.want)
}
})
}
}
func TestEndpointKeyPrefix(t *testing.T) {
if got := endpointKeyPrefix("curator"); got != "curator.*" {
t.Errorf("endpointKeyPrefix(curator) = %q, want %q", got, "curator.*")
}
}
func TestIDFromEndpointKey(t *testing.T) {
tests := []struct {
name string
service string
key string
want string
}{
{name: "own key returns id", service: "curator", key: "curator.inst-1", want: "inst-1"},
{name: "other service", service: "curator", key: "scheduler.inst", want: ""},
{name: "empty id", service: "curator", key: "curator.", want: ""},
{name: "key equals prefix", service: "curator", key: "curator", want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := idFromEndpointKey(tt.service, tt.key); got != tt.want {
t.Errorf("idFromEndpointKey(%q, %q) = %q, want %q", tt.service, tt.key, got, tt.want)
}
})
}
}