Files
supply-intelligence/internal/app/app_test.go
2026-05-07 10:16:46 +08:00

86 lines
2.3 KiB
Go

package app
import (
"context"
"testing"
"time"
"supply-intelligence/internal/domain"
)
func TestNewApplication(t *testing.T) {
application := New()
if application == nil {
t.Fatalf("expected application")
}
if application.Repo == nil {
t.Fatalf("expected repository")
}
if application.ProbeService == nil {
t.Fatalf("expected probe service")
}
if application.PublishService == nil {
t.Fatalf("expected publish service")
}
if application.DiscoveryService == nil {
t.Fatalf("expected discovery service")
}
if application.GatewayConsumerService == nil {
t.Fatalf("expected gateway consumer service")
}
if application.GatewayPoller == nil {
t.Fatalf("expected gateway poller")
}
if application.GatewayRuntime == nil {
t.Fatalf("expected gateway runtime")
}
if application.Server == nil {
t.Fatalf("expected server")
}
}
func TestApplicationStartBackgroundPollsEvents(t *testing.T) {
application := New()
application.Repo.AppendPackageEvent(domain.PackageChangeEvent{
EventID: "evt-app-runtime-1",
EventType: "supply_package_published",
PackageID: 11,
Platform: "openai",
Model: "gpt-4.1-mini",
OccurredAt: time.Unix(2, 0).UTC(),
Version: 1,
GatewaySyncStatus: domain.GatewaySyncStatusPending,
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
application.StartBackground(ctx)
defer application.StopBackground()
deadline := time.Now().Add(1500 * time.Millisecond)
for time.Now().Before(deadline) {
items, _ := application.Repo.ListPackageEventsAfter("")
if len(items) == 1 && items[0].GatewaySyncStatus == domain.GatewaySyncStatusApplied {
return
}
time.Sleep(20 * time.Millisecond)
}
items, _ := application.Repo.ListPackageEventsAfter("")
t.Fatalf("expected background runtime to apply event, got %+v", items)
}
func TestApplicationStartBackgroundHandlesNilRuntime(t *testing.T) {
application := New()
application.GatewayRuntime = nil
application.StartBackground(context.Background())
if application.GatewayRuntime != nil {
t.Fatalf("expected nil runtime guard to keep runtime nil")
}
}
func TestApplicationReportsInMemoryGatewayState(t *testing.T) {
application := New()
if !application.IsInMemoryGatewayState() {
t.Fatalf("expected in-memory gateway state")
}
}