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
+3 -4
View File
@@ -11,7 +11,6 @@ import (
"cloud.google.com/go/firestore"
pbaero "github.com/flowy-live/llink/genproto/aero"
pbpusher "github.com/flowy-live/llink/genproto/llink/pusher"
"github.com/flowy-live/llink/internal/billing"
"github.com/flowy-live/llink/internal/db"
"github.com/flowy-live/llink/internal/human"
"github.com/flowy-live/llink/internal/network"
@@ -68,7 +67,7 @@ func main() {
// Initialize services
humanSvc := human.NewService(db.Pool())
networkSvc := network.NewService(db.Pool(), aeroSvc, billing.Noop(), nil)
networkSvc := network.NewReader(db.Pool())
slog.Info("starting email notification cycle")
if err := runNotificationCycle(ctx, firestoreClient, aeroSvc, pusherSvc, humanSvc, networkSvc); err != nil {
@@ -84,12 +83,12 @@ func runNotificationCycle(
aeroSvc pbaero.PrimaryClient,
pusherSvc pbpusher.PusherServiceClient,
humanSvc human.Service,
networkSvc network.Service,
networkReader network.Reader,
) error {
now := time.Now()
// Load all networks
networks, err := networkSvc.ListAll(ctx)
networks, err := networkReader.ListAll(ctx)
if err != nil {
return fmt.Errorf("listing networks: %w", err)
}
+3 -5
View File
@@ -18,7 +18,6 @@ import (
"time"
"cloud.google.com/go/firestore"
"github.com/flowy-live/llink/internal/billing"
"github.com/flowy-live/llink/internal/db"
"github.com/flowy-live/llink/internal/human"
"github.com/flowy-live/llink/internal/network"
@@ -41,8 +40,7 @@ func main() {
defer fs.Close()
humanSvc := human.NewService(db.Pool())
// billing/aero/fs unused here — we only call ListAllMemberships.
networkSvc := network.NewService(db.Pool(), nil, billing.Noop(), nil)
networkSvc := network.NewReader(db.Pool())
started := time.Now()
written, scanned, err := reconcile(ctx, fs, humanSvc, networkSvc)
@@ -61,14 +59,14 @@ func reconcile(
ctx context.Context,
fs *firestore.Client,
humanSvc human.Service,
networkSvc network.Service,
networkReader network.Reader,
) (written, scanned int, err error) {
humans, err := humanSvc.ListAll(ctx)
if err != nil {
return 0, 0, err
}
memberships, err := networkSvc.ListAllMemberships(ctx)
memberships, err := networkReader.ListAllMemberships(ctx)
if err != nil {
return 0, 0, err
}
+2 -1
View File
@@ -19,6 +19,7 @@ import (
"github.com/flowy-live/llink/internal/handler"
"github.com/flowy-live/llink/internal/human"
"github.com/flowy-live/llink/internal/livekit"
"github.com/flowy-live/llink/internal/livestore"
"github.com/flowy-live/llink/internal/middleware"
"github.com/flowy-live/llink/internal/network"
"github.com/flowy-live/llink/internal/particle"
@@ -98,7 +99,7 @@ func main() {
}
defer firestoreClient.Close()
networkSvc := network.NewService(db.Pool(), aeroSvc, billingSvc, firestoreClient)
networkSvc := network.NewService(db.Pool(), aeroSvc, billingSvc, livestore.NewMembershipPublisher(firestoreClient))
particleSvc := particle.NewService(db.Pool(), networkSvc)
depotSvc := depot.NewService(db.Pool(), storageClient, depot.Config{
GoogleServiceAccountEmail: utils.MustGetEnv("GOOGLE_SERVICE_ACCOUNT_EMAIL"),
+3 -4
View File
@@ -12,7 +12,6 @@ import (
"github.com/flowy-live/llink/internal"
"github.com/flowy-live/llink/internal/auth"
"github.com/flowy-live/llink/internal/billing"
"github.com/flowy-live/llink/internal/db"
"github.com/flowy-live/llink/internal/network"
"github.com/flowy-live/llink/internal/pusher"
@@ -37,8 +36,8 @@ func main() {
pusherRedis := internal.ConnectAndTestRedis(db.RedisDBPusher)
// Services
authSvc := auth.NewAuthService(authRedis, nil, nil) // nil aeroSvc / fbAuth — pusher only calls GetSession
networkSvc := network.NewService(db.Pool(), nil, billing.Noop(), nil) // nil aeroSvc / noop billing / nil firestore — pusher never mutates membership
authSvc := auth.NewAuthService(authRedis, nil, nil) // nil aeroSvc / fbAuth — pusher only calls GetSession
networkReader := network.NewReader(db.Pool()) // pusher only checks membership
// Pod identity (use hostname in k8s, which is the pod name)
podID, err := os.Hostname()
@@ -52,7 +51,7 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
authorizer := pusher.NewAuthorizer(networkSvc)
authorizer := pusher.NewAuthorizer(networkReader)
hub := pusher.NewHub(bridge, authorizer)
bridge.SetHub(hub)
server := pusher.NewServer(ctx, hub, bridge, authSvc)