Initial phase 1 baseline implementation
This commit is contained in:
parent
099c53badf
commit
077cf7601a
29 changed files with 1339 additions and 39 deletions
41
internal/application/system.go
Normal file
41
internal/application/system.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SystemRepository interface {
|
||||
DatabaseHealth(context.Context) error
|
||||
OwnerExists(context.Context) (bool, error)
|
||||
}
|
||||
|
||||
type SetupStatus struct {
|
||||
Initialized bool `json:"initialized"`
|
||||
Phase string `json:"phase"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type SystemService struct {
|
||||
repository SystemRepository
|
||||
version string
|
||||
}
|
||||
|
||||
func NewSystemService(repository SystemRepository, version string) *SystemService {
|
||||
return &SystemService{repository: repository, version: version}
|
||||
}
|
||||
|
||||
func (s *SystemService) Readiness(ctx context.Context) error {
|
||||
if err := s.repository.DatabaseHealth(ctx); err != nil {
|
||||
return fmt.Errorf("database health: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SystemService) SetupStatus(ctx context.Context) (SetupStatus, error) {
|
||||
initialized, err := s.repository.OwnerExists(ctx)
|
||||
if err != nil {
|
||||
return SetupStatus{}, fmt.Errorf("read owner setup state: %w", err)
|
||||
}
|
||||
return SetupStatus{Initialized: initialized, Phase: "phase-1", Version: s.version}, nil
|
||||
}
|
||||
44
internal/application/system_test.go
Normal file
44
internal/application/system_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type systemRepositoryStub struct {
|
||||
healthErr error
|
||||
ownerExists bool
|
||||
ownerErr error
|
||||
}
|
||||
|
||||
func (s systemRepositoryStub) DatabaseHealth(context.Context) error {
|
||||
return s.healthErr
|
||||
}
|
||||
|
||||
func (s systemRepositoryStub) OwnerExists(context.Context) (bool, error) {
|
||||
return s.ownerExists, s.ownerErr
|
||||
}
|
||||
|
||||
func TestSystemServiceReadinessPropagatesDatabaseFailure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
want := errors.New("database unavailable")
|
||||
service := NewSystemService(systemRepositoryStub{healthErr: want}, "test")
|
||||
if err := service.Readiness(context.Background()); !errors.Is(err, want) {
|
||||
t.Fatalf("Readiness error = %v, want wrapped %v", err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemServiceSetupStatusUsesPersistentOwnerState(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
service := NewSystemService(systemRepositoryStub{ownerExists: true}, "test")
|
||||
status, err := service.SetupStatus(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("SetupStatus returned an error: %v", err)
|
||||
}
|
||||
if !status.Initialized || status.Phase != "phase-1" || status.Version != "test" {
|
||||
t.Fatalf("unexpected setup status: %#v", status)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue