63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
type coordinatorBootstrapFixture struct {
|
|
calls []string
|
|
organization *domain.OrganizationRecord
|
|
}
|
|
|
|
func (f *coordinatorBootstrapFixture) RunMigrations(context.Context) error {
|
|
f.calls = append(f.calls, "migrate")
|
|
return nil
|
|
}
|
|
func (f *coordinatorBootstrapFixture) EnsureIndexes(context.Context) error {
|
|
f.calls = append(f.calls, "indexes")
|
|
return nil
|
|
}
|
|
func (f *coordinatorBootstrapFixture) EnsureDefaultOrganization(context.Context) (*domain.OrganizationRecord, error) {
|
|
f.calls = append(f.calls, "create-org")
|
|
return f.organization, nil
|
|
}
|
|
func (f *coordinatorBootstrapFixture) GetOrganization(context.Context, string) (*domain.OrganizationRecord, error) {
|
|
f.calls = append(f.calls, "read-org")
|
|
return f.organization, nil
|
|
}
|
|
|
|
func TestCoordinatorBootstrapVerifyExistingDoesNotWrite(t *testing.T) {
|
|
for _, exists := range []bool{true, false} {
|
|
f := &coordinatorBootstrapFixture{}
|
|
if exists {
|
|
f.organization = &domain.OrganizationRecord{ID: domain.DefaultOrganizationID}
|
|
}
|
|
err := bootstrapCoordinator(t.Context(), f, "verify-existing")
|
|
if (err == nil) != exists {
|
|
t.Fatal(exists, err)
|
|
}
|
|
if !reflect.DeepEqual(f.calls, []string{"read-org"}) {
|
|
t.Fatal("read-only bootstrap mutated database", f.calls)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCoordinatorBootstrapPreservesDefaultAndRejectsUnknownMode(t *testing.T) {
|
|
for _, mode := range []string{"", "migrate", "unknown"} {
|
|
f := &coordinatorBootstrapFixture{}
|
|
err := bootstrapCoordinator(t.Context(), f, mode)
|
|
if mode == "unknown" {
|
|
if err == nil || len(f.calls) > 0 {
|
|
t.Fatal(err, f.calls)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || !reflect.DeepEqual(f.calls, []string{"migrate", "indexes"}) {
|
|
t.Fatal(mode, err, f.calls)
|
|
}
|
|
}
|
|
}
|