refactor: unit testable units and cleaner dep injection

This commit is contained in:
Arjun Patel
2026-04-27 16:07:28 -07:00
parent 013c453dd3
commit 48d6d5cb07
26 changed files with 761 additions and 617 deletions
@@ -0,0 +1,42 @@
package livestore
import (
"context"
"cloud.google.com/go/firestore"
)
//go:generate mockgen -source ./membershippublisher.go -destination ./mocks/membershippublisher.go
// MembershipPublisher publishes network membership changes to the live store
// (Firestore) that clients subscribe to. Postgres remains the source of truth;
// the membership reconciler heals any drift, so callers may log and ignore
// publish failures.
type MembershipPublisher interface {
Add(ctx context.Context, humanId, networkID string) error
Remove(ctx context.Context, humanId, networkID string) error
}
func NewMembershipPublisher(fs *firestore.Client) MembershipPublisher {
return &firestoreMembershipPublisher{fs: fs}
}
type firestoreMembershipPublisher struct {
fs *firestore.Client
}
func (p *firestoreMembershipPublisher) Add(ctx context.Context, humanId, networkID string) error {
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
"networks": firestore.ArrayUnion(networkID),
"updated_at": firestore.ServerTimestamp,
}, firestore.MergeAll)
return err
}
func (p *firestoreMembershipPublisher) Remove(ctx context.Context, humanId, networkID string) error {
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
"networks": firestore.ArrayRemove(networkID),
"updated_at": firestore.ServerTimestamp,
}, firestore.MergeAll)
return err
}