refactor: testing from the beginning. a journey in 150k lines of tests to none

This commit is contained in:
sam committed 2026-10-03 21:16:25 +10:00
1 parent ba37e37e53
commit ff424b70ac
651 files changed
-146443

No files matched your search

-290
View File
@@ -1,290 +0,0 @@
package memory
import (
"strings"
"testing"
)
func TestScopeValidate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
scope Scope
wantErr string // "" means valid; otherwise a substring of the error
}{
{"global", Scope{Kind: ScopeGlobal}, ""},
{"agent", Scope{Kind: ScopeAgent, Agent: "a1"}, ""},
{"user", Scope{Kind: ScopeUser, User: "u1"}, ""},
{"agent-user", Scope{Kind: ScopeAgentUser, Agent: "a1", User: "u1"}, ""},
{"unknown kind", Scope{Kind: "session"}, "unknown kind"},
{"empty kind", Scope{}, "unknown kind"},
{"global rejects agent", Scope{Kind: ScopeGlobal, Agent: "a1"}, "takes no agent or user"},
{"global rejects user", Scope{Kind: ScopeGlobal, User: "u1"}, "takes no agent or user"},
{"agent needs an agent", Scope{Kind: ScopeAgent}, "agent must not be empty"},
{"agent rejects a user", Scope{Kind: ScopeAgent, Agent: "a1", User: "u1"}, "takes no user"},
{"user needs a user", Scope{Kind: ScopeUser}, "user must not be empty"},
{"user rejects an agent", Scope{Kind: ScopeUser, Agent: "a1", User: "u1"}, "takes no agent"},
{"agent-user needs an agent", Scope{Kind: ScopeAgentUser, User: "u1"}, "both required"},
{"agent-user needs a user", Scope{Kind: ScopeAgentUser, Agent: "a1"}, "both required"},
{"agent-user needs both", Scope{Kind: ScopeAgentUser}, "both required"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.scope.Validate()
switch {
case tt.wantErr == "" && err != nil:
t.Fatalf("Validate() = %v, want nil", err)
case tt.wantErr != "" && err == nil:
t.Fatalf("Validate() = nil, want an error mentioning %q", tt.wantErr)
case tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr):
t.Fatalf("Validate() = %v, want an error mentioning %q", err, tt.wantErr)
}
})
}
}
func TestScopeKeyIsCanonical(t *testing.T) {
t.Parallel()
tests := []struct {
name string
scope Scope
want string
}{
{"global", Scope{Kind: ScopeGlobal}, "global"},
{"agent", Scope{Kind: ScopeAgent, Agent: "telegram"}, "agent:8:telegram"},
{"user", Scope{Kind: ScopeUser, User: "u1"}, "user:2:u1"},
{"agent-user", Scope{Kind: ScopeAgentUser, Agent: "a1", User: "u1"}, "agent-user:2:a1:2:u1"},
// An invalid scope has no storage key; "" is the signal, never a key
// a valid scope could produce.
{"invalid kind", Scope{Kind: "session", Agent: "a1"}, ""},
{"missing component", Scope{Kind: ScopeAgentUser, Agent: "a1"}, "agent-user:2:a1:0:"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.scope.Key(); got != tt.want {
t.Fatalf("Key() = %q, want %q", got, tt.want)
}
})
}
}
// TestScopeKeyCollisionFree is the property Key's length prefix exists for:
// AgentID and IdentityID are channel-native and opaque, so a separator-based
// key would let two different scopes share one storage key. Every scope over
// ids that embed the separator must still key distinctly.
func TestScopeKeyCollisionFree(t *testing.T) {
t.Parallel()
ids := []string{"", "a", "a:", ":a", "a:1", "1", "1:a", "agent-user:1:a"}
distinct := map[Scope]bool{{Kind: ScopeGlobal}: true}
for _, agent := range ids {
for _, user := range ids {
distinct[Scope{Kind: ScopeAgentUser, Agent: AgentID(agent), User: IdentityID(user)}] = true
distinct[Scope{Kind: ScopeAgent, Agent: AgentID(agent)}] = true
distinct[Scope{Kind: ScopeUser, User: IdentityID(user)}] = true
}
}
seen := make(map[string]Scope, len(distinct))
for scope := range distinct {
if err := scope.Validate(); err != nil {
continue // an incomplete scope has no key to collide with
}
key := scope.Key()
if key == "" {
t.Fatalf("valid scope %+v has an empty key", scope)
}
if other, dup := seen[key]; dup {
t.Fatalf("scopes %+v and %+v share key %q", other, scope, key)
}
seen[key] = scope
}
}
func TestSubjectScopesNarrowestFirst(t *testing.T) {
t.Parallel()
tests := []struct {
name string
subject Subject
want []string
}{
{
name: "agent and user",
subject: Subject{AgentID: "a1", UserID: "u1"},
want: []string{"agent-user:2:a1:2:u1", "agent:2:a1", "user:2:u1", "global"},
},
{
// A channel with no per-person identity (dashboard, webhook)
// gets agent and global only -- never a wider scope to stand in
// for the one it does not know.
name: "agent only",
subject: Subject{AgentID: "a1"},
want: []string{"agent:2:a1", "global"},
},
{
name: "user only",
subject: Subject{UserID: "u1"},
want: []string{"user:2:u1", "global"},
},
{
name: "neither",
subject: Subject{},
want: []string{"global"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := keys(tt.subject.Scopes())
if len(got) != len(tt.want) {
t.Fatalf("Scopes() = %v, want %v", got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Fatalf("Scopes() = %v, want %v", got, tt.want)
}
}
})
}
}
func TestSubjectWritableScopesExcludeGlobal(t *testing.T) {
t.Parallel()
subject := Subject{AgentID: "a1", UserID: "u1"}
writable := keys(subject.WritableScopes())
if len(writable) != 3 {
t.Fatalf("WritableScopes() = %v, want three scopes", writable)
}
for _, key := range writable {
if key == "global" {
t.Fatal("WritableScopes() includes global, which is never model-writable")
}
}
// Every writable scope is readable: the read set is a superset.
readable := map[string]bool{}
for _, key := range keys(subject.Scopes()) {
readable[key] = true
}
for _, key := range writable {
if !readable[key] {
t.Fatalf("writable scope %q is not in the read set", key)
}
}
}
func keys(scopes []Scope) []string {
out := make([]string, 0, len(scopes))
for _, scope := range scopes {
out = append(out, scope.Key())
}
return out
}
func TestNewRecordValidate(t *testing.T) {
t.Parallel()
valid := Scope{Kind: ScopeAgent, Agent: "a1"}
tests := []struct {
name string
in NewRecord
wantErr string
}{
{"valid", NewRecord{Scope: valid, Content: "x"}, ""},
{"valid without kind", NewRecord{Scope: valid, Content: "x"}, ""},
{"empty content", NewRecord{Scope: valid}, "content must not be empty"},
{"invalid scope", NewRecord{Scope: Scope{Kind: ScopeAgent}, Content: "x"}, "agent must not be empty"},
{"no scope", NewRecord{Content: "x"}, "unknown kind"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assertValidation(t, tt.in.Validate(), tt.wantErr)
})
}
}
func TestRecordUpdateValidate(t *testing.T) {
t.Parallel()
valid := Scope{Kind: ScopeAgent, Agent: "a1"}
tests := []struct {
name string
in RecordUpdate
wantErr string
}{
{"valid", RecordUpdate{Scope: valid, ID: "r1", Content: "x"}, ""},
{"valid with expected", RecordUpdate{Scope: valid, ID: "r1", Content: "x", Expected: 2}, ""},
{"empty id", RecordUpdate{Scope: valid, Content: "x"}, "id must not be empty"},
{"empty content", RecordUpdate{Scope: valid, ID: "r1"}, "content must not be empty"},
{"invalid scope", RecordUpdate{Scope: Scope{Kind: ScopeGlobal, Agent: "a1"}, ID: "r1", Content: "x"}, "takes no agent or user"},
{"negative expected", RecordUpdate{Scope: valid, ID: "r1", Content: "x", Expected: -1}, "must not be negative"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assertValidation(t, tt.in.Validate(), tt.wantErr)
})
}
}
func TestQueryValidate(t *testing.T) {
t.Parallel()
agent := Scope{Kind: ScopeAgent, Agent: "a1"}
tests := []struct {
name string
q Query
wantErr string
}{
{"one scope", Query{Scopes: []Scope{agent}}, ""},
{"two scopes", Query{Scopes: []Scope{agent, {Kind: ScopeGlobal}}}, ""},
{"scope only, no text", Query{Scopes: []Scope{agent}}, ""},
{"with limit", Query{Scopes: []Scope{agent}, Limit: 5}, ""},
{"no scopes", Query{}, "at least one scope"},
{"invalid scope", Query{Scopes: []Scope{{Kind: ScopeAgentUser, Agent: "a1"}}}, "both required"},
{"negative limit", Query{Scopes: []Scope{agent}, Limit: -1}, "must not be negative"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assertValidation(t, tt.q.Validate(), tt.wantErr)
})
}
}
func TestManifestValidateAcceptsZeroValue(t *testing.T) {
t.Parallel()
if err := (Manifest{}).Validate(); err != nil {
t.Fatalf("Validate() = %v, want nil: no field is required yet", err)
}
}
func assertValidation(t *testing.T, err error, wantErr string) {
t.Helper()
switch {
case wantErr == "" && err != nil:
t.Fatalf("Validate() = %v, want nil", err)
case wantErr != "" && err == nil:
t.Fatalf("Validate() = nil, want an error mentioning %q", wantErr)
case wantErr != "" && !strings.Contains(err.Error(), wantErr):
t.Fatalf("Validate() = %v, want an error mentioning %q", err, wantErr)
}
}
-464
View File
@@ -1,464 +0,0 @@
package memory
import (
"context"
"errors"
"slices"
"strconv"
"strings"
"sync"
"testing"
"time"
)
// --- fake engine -------------------------------------------------------------
type fakeEngine struct {
name string
manifest Manifest
recorder *[]string // shared lifecycle log
startErr error
stopErr error
stopBlock chan struct{}
bindPanic bool
view Registrar
records map[RecordID]Record
revisions map[RecordID][]Revision
nextID int
}
func newFake(name string, manifest Manifest) *fakeEngine {
return &fakeEngine{
name: name,
manifest: manifest,
records: map[RecordID]Record{},
revisions: map[RecordID][]Revision{},
}
}
func (f *fakeEngine) record(e string) {
if f.recorder != nil {
*f.recorder = append(*f.recorder, e)
}
}
func (f *fakeEngine) Name() string { return f.name }
func (f *fakeEngine) Version() string { return "test" }
func (f *fakeEngine) Manifest() Manifest { return f.manifest }
func (f *fakeEngine) Bind(host Registrar) {
f.view = host
f.record("bind:" + f.name)
if f.bindPanic {
panic("bind boom")
}
}
// The fake implements the full Store so it satisfies MemoryEngine, but these
// tests exercise registration and lifecycle, not storage semantics -- the
// builtin engine's own suite is the store's real coverage. The in-memory
// behaviour here is deliberately minimal and not a reference implementation.
func (f *fakeEngine) Create(_ context.Context, in NewRecord) (Record, error) {
f.nextID++
rec := Record{
ID: RecordID(f.name + "-" + strconv.Itoa(f.nextID)),
Scope: in.Scope,
Kind: in.Kind,
Content: in.Content,
Revision: 1,
Author: in.Author,
OriginUser: in.OriginUser,
Source: in.Source,
}
f.records[rec.ID] = rec
return rec, nil
}
func (f *fakeEngine) Get(_ context.Context, scope Scope, id RecordID) (Record, error) {
rec, ok := f.records[id]
if !ok || rec.Scope != scope {
return Record{}, ErrNotFound
}
return rec, nil
}
func (f *fakeEngine) Query(_ context.Context, q Query) ([]Record, error) {
wanted := make(map[Scope]bool, len(q.Scopes))
for _, scope := range q.Scopes {
wanted[scope] = true
}
var out []Record
for _, r := range f.records {
if q.Text != "" && !strings.Contains(r.Content, q.Text) {
continue
}
if wanted[r.Scope] {
out = append(out, r)
}
}
slices.SortFunc(out, func(a, b Record) int { return strings.Compare(string(a.ID), string(b.ID)) })
return limit(out, q.Limit), nil
}
func (f *fakeEngine) List(ctx context.Context, scope Scope) ([]Record, error) {
return f.Query(ctx, Query{Scopes: []Scope{scope}})
}
func (f *fakeEngine) Update(_ context.Context, in RecordUpdate) (Record, error) {
rec, ok := f.records[in.ID]
if !ok || rec.Scope != in.Scope {
return Record{}, ErrNotFound
}
if in.Expected != 0 && in.Expected != rec.Revision {
return Record{}, ErrStaleRevision
}
f.revisions[rec.ID] = append(f.revisions[rec.ID], Revision{Record: rec, Deleted: false})
rec.Content = in.Content
rec.Revision++
rec.Author = in.Author
rec.Source = in.Source
f.records[rec.ID] = rec
return rec, nil
}
func (f *fakeEngine) Forget(_ context.Context, scope Scope, id RecordID) error {
rec, ok := f.records[id]
if !ok || rec.Scope != scope {
return nil
}
f.revisions[id] = append(f.revisions[id], Revision{Record: rec, Deleted: true})
delete(f.records, id)
return nil
}
func (f *fakeEngine) Revisions(_ context.Context, _ Scope, id RecordID) ([]Revision, error) {
return f.revisions[id], nil
}
// limit trims out to n when n > 0, matching the Store contract's Limit.
func limit(out []Record, n int) []Record {
if n > 0 && len(out) > n {
return out[:n]
}
return out
}
func (f *fakeEngine) Start(context.Context) error {
if f.startErr != nil {
return f.startErr
}
f.record("start:" + f.name)
return nil
}
func (f *fakeEngine) Health(context.Context) Health { return Health{Status: HealthHealthy} }
func (f *fakeEngine) Stop(ctx context.Context) error {
if f.stopBlock != nil {
select {
case <-f.stopBlock:
case <-ctx.Done():
return ctx.Err()
}
}
if f.stopErr != nil {
return f.stopErr
}
f.record("stop:" + f.name)
return nil
}
// --- registry behavior --------------------------------------------------------
func TestRegistryRegisterAndDiscover(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
a := newFake("a", Manifest{})
b := newFake("b", Manifest{})
if err := r.Register(a); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
if err := r.Register(b); err != nil {
t.Fatalf("Register(b) = %v, want nil", err)
}
got, ok := r.Get("a")
if !ok || got != a {
t.Fatalf("Get(a) = (%v, %v), want (a, true)", got, ok)
}
if _, ok := r.Get("missing"); ok {
t.Fatal("Get(missing) = true, want false")
}
if got := r.Names(); len(got) != 2 || got[0] != "a" || got[1] != "b" {
t.Fatalf("Names() = %v, want [a b]", got)
}
}
func TestRegistryRegisterDuplicateRejected(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Register(newFake("a", Manifest{})); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
err := r.Register(newFake("a", Manifest{}))
if !errors.Is(err, ErrDuplicate) {
t.Fatalf("Register(dup) = %v, want ErrDuplicate", err)
}
if got := r.Names(); len(got) != 1 {
t.Fatalf("Names() = %v, want only the first registration", got)
}
}
func TestRegistryRegisterNilEngineRejected(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Register(nil); err == nil {
t.Fatal("Register(nil) = nil, want error")
}
}
func TestRegistryRegisterTypedNilEngineRejected(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
var e *fakeEngine
if err := r.Register(e); err == nil {
t.Fatal("Register(typed nil) = nil, want error")
}
}
func TestRegistryRegisterAfterStartRejected(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Register(newFake("a", Manifest{})); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
if err := r.Start(context.Background()); err != nil {
t.Fatalf("Start() = %v, want nil", err)
}
err := r.Register(newFake("b", Manifest{}))
if !errors.Is(err, ErrStarted) {
t.Fatalf("Register after Start = %v, want ErrStarted", err)
}
}
func TestRegistryBindAlwaysReceivesEventsAndClock(t *testing.T) {
t.Parallel()
sink := &testSink{}
clock := testClock{now: time.Unix(0, 0)}
r := NewRegistry(Registrar{Events: sink, Clock: clock})
e := newFake("a", Manifest{})
if err := r.Register(e); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
if e.view.Events == nil {
t.Error("view.Events = nil; activity must always be attributable")
}
if e.view.Clock == nil {
t.Error("view.Clock = nil; time is always available")
}
}
func TestRegistryBindPanicIsolated(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
panicky := newFake("panicky", Manifest{})
panicky.bindPanic = true
if err := r.Register(panicky); err == nil {
t.Fatal("Register(panicking Bind) = nil, want error")
}
if got := r.Names(); len(got) != 0 {
t.Fatalf("Names() = %v, want empty after a panicking Bind", got)
}
// The registry survives and still accepts healthy engines.
if err := r.Register(newFake("ok", Manifest{})); err != nil {
t.Fatalf("Register(ok) after panicking Bind = %v, want nil", err)
}
}
func TestRegistryStartStopOrdering(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
var log []string
a := newFake("a", Manifest{})
a.recorder = &log
b := newFake("b", Manifest{})
b.recorder = &log
c := newFake("c", Manifest{})
c.recorder = &log
for _, e := range []*fakeEngine{a, b, c} {
if err := r.Register(e); err != nil {
t.Fatalf("Register(%s) = %v, want nil", e.name, err)
}
}
if err := r.Start(context.Background()); err != nil {
t.Fatalf("Start() = %v, want nil", err)
}
if err := r.Stop(context.Background()); err != nil {
t.Fatalf("Stop() = %v, want nil", err)
}
want := []string{
"bind:a", "bind:b", "bind:c",
"start:a", "start:b", "start:c",
"stop:c", "stop:b", "stop:a",
}
if got := log; !equalStrings(got, want) {
t.Fatalf("lifecycle events = %v, want %v (start in registration order, stop reversed)", got, want)
}
}
func TestRegistryStartIsolatesFailure(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
var log []string
a := newFake("a", Manifest{})
a.recorder = &log
bad := newFake("bad", Manifest{})
bad.recorder = &log
bad.startErr = errBoom
c := newFake("c", Manifest{})
c.recorder = &log
for _, e := range []*fakeEngine{a, bad, c} {
if err := r.Register(e); err != nil {
t.Fatalf("Register(%s) = %v, want nil", e.name, err)
}
}
err := r.Start(context.Background())
if !errors.Is(err, errBoom) {
t.Fatalf("Start() error = %v, want the failing engine's error", err)
}
if !logHas(log, "start:a") || !logHas(log, "start:c") {
t.Fatalf("healthy engines did not start: %v", log)
}
if logHas(log, "start:bad") {
t.Fatalf("failing engine started despite Start error: %v", log)
}
health := r.Health(context.Background())
if health["bad"].Status != HealthUnhealthy {
t.Fatalf("Health(bad).Status = %v, want unhealthy", health["bad"].Status)
}
if health["a"].Status != HealthHealthy {
t.Fatalf("Health(a).Status = %v, want healthy", health["a"].Status)
}
if err := r.Stop(context.Background()); err != nil {
t.Fatalf("Stop() = %v, want nil", err)
}
want := []string{"bind:a", "bind:bad", "bind:c", "start:a", "start:c", "stop:c", "stop:a"}
if got := log; !equalStrings(got, want) {
t.Fatalf("lifecycle events = %v, want %v", got, want)
}
}
func TestRegistryStopBoundedByContext(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
blocked := newFake("blocked", Manifest{})
blocked.stopBlock = make(chan struct{})
if err := r.Register(blocked); err != nil {
t.Fatalf("Register(blocked) = %v, want nil", err)
}
if err := r.Start(context.Background()); err != nil {
t.Fatalf("Start() = %v, want nil", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
start := time.Now()
err := r.Stop(ctx)
elapsed := time.Since(start)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Stop() error = %v, want context.DeadlineExceeded", err)
}
if elapsed > 2*time.Second {
t.Fatalf("Stop() took %v; a blocking engine must not hang shutdown", elapsed)
}
}
func TestRegistryStopBeforeStartIsNoop(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Register(newFake("a", Manifest{})); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
if err := r.Stop(context.Background()); err != nil {
t.Fatalf("Stop() before Start = %v, want nil no-op", err)
}
}
func TestRegistryStartTwiceRejected(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Start(context.Background()); err != nil {
t.Fatalf("Start() = %v, want nil", err)
}
if err := r.Start(context.Background()); !errors.Is(err, ErrStarted) {
t.Fatalf("second Start() = %v, want ErrStarted", err)
}
}
func TestRegistryHealthReportsEveryEngine(t *testing.T) {
t.Parallel()
r := NewRegistry(Registrar{})
if err := r.Register(newFake("a", Manifest{})); err != nil {
t.Fatalf("Register(a) = %v, want nil", err)
}
health := r.Health(context.Background())
if len(health) != 1 || health["a"].Status != HealthHealthy {
t.Fatalf("Health() = %v, want one healthy entry for a", health)
}
}
// --- helpers -------------------------------------------------------------
type testSink struct {
mu sync.Mutex
kinds []string
}
func (s *testSink) Emit(kind, _ string, _ map[string]any) {
s.mu.Lock()
defer s.mu.Unlock()
s.kinds = append(s.kinds, kind)
}
type testClock struct{ now time.Time }
func (c testClock) Now() time.Time { return c.now }
var errBoom = errors.New("boom")
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func logHas(log []string, want string) bool {
return slices.Contains(log, want)
}