Files

241 lines
8.4 KiB
Go

package daemon
import (
"context"
"log/slog"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/events"
)
// wired gives d the dependencies production always sets, with neutral
// stand-ins for any the test left nil. A test that does not exercise one of
// them then never reaches a nil branch in the code under test. A test that
// does exercise one sets its own fake.
func wired(d *Daemon) *Daemon {
if d.Log == nil {
d.Log = slog.New(slog.DiscardHandler)
}
if d.TaskTransport == nil {
d.TaskTransport = neutralTaskTransport{}
}
if d.Store == nil {
d.Store = neutralStore{}
}
if d.Bus == nil {
d.Bus = events.NewBus()
}
if d.RunCredentials == nil {
d.RunCredentials = neutralCredentials{}
}
if d.TaskModels == nil {
d.TaskModels = neutralTaskConfig{}
}
if d.TaskInstructions == nil {
d.TaskInstructions = neutralTaskConfig{}
}
if d.Identity == nil {
lister, _ := d.Store.(access.WorkspaceLister)
d.Identity = NewIdentityGate(neutralIdentities{}, neutralAssignments{}, allowAll{}, neutralPrincipals{}, neutralDenials{}, defaultOrgPrincipals, lister, d.Log)
}
if d.PackageAuthorities == nil {
d.PackageAuthorities = neutralAuthorities{}
}
if d.Playbooks == nil {
d.Playbooks = neutralPlaybooks{}
}
if d.WorkflowDefinitions == nil {
d.WorkflowDefinitions = neutralDefinitions{}
}
if d.AgentStatus == nil {
d.AgentStatus = &AgentStatus{}
}
if d.Cfg == nil {
d.Cfg = config.NewHolder(config.Config{})
}
if d.BindingDispatch == nil {
d.BindingDispatch = NewBindingDispatch(BindingDispatchDeps{
Store: d.Store, Mappings: neutralMappings{}, Bindings: neutralBindings{},
Dispatcher: neutralDispatcher{}, Matches: neutralMatches{}, Creator: neutralCreator{},
Enablement: neutralEnablement{}, Definitions: d.WorkflowDefinitions,
Access: d.Identity, Denials: d.Identity, Cfg: d.Cfg, Orgs: d.Identity,
OrgPrincipals: defaultOrgPrincipals,
Log: d.Log,
})
}
d.WireTaskBus()
d.IssuePoller = NewIssuePoller(d.Cfg, d.Store, d.Log,
func(context.Context) []IssueSource { return nil },
func(context.Context, int64, string) error { return nil },
d.TaskBus.PublishTask, d.Identity, d.BindingDispatch)
d.ReviewScanner = NewReviewScanner(d.Store, neutralReactions{}, d.TaskTransport.PublishUnique, d.Bus, d, d.Identity, d, nil, d.Log)
return d
}
// defaultOrgPrincipals is the one acting principal of the default org.
func defaultOrgPrincipals(context.Context) []access.Principal {
return []access.Principal{{Org: org.DefaultOrgID}}
}
// neutralMappings resolves no mapping, so a binding never reaches a capture.
type neutralMappings struct{ storecontract.MappingStore }
// neutralBindings lists no armed binding, so no capture is offered.
type neutralBindings struct{ storecontract.BindingStore }
func (neutralBindings) ListBindings(context.Context) ([]binding.Binding, error) { return nil, nil }
// neutralDispatcher lists no capture. Any other dispatch call panics, so a test
// that reaches one sets its own dispatcher.
type neutralDispatcher struct {
storecontract.BindingDispatcher
}
func (neutralDispatcher) ListUndispatchedCaptures(context.Context, []string, int) ([]storecontract.CapturedEvent, error) {
return nil, nil
}
// neutralCreator enqueues nothing. A test that reaches it sets its own creator.
type neutralCreator struct {
storecontract.BindingTaskCreator
}
// neutralTaskTransport takes no work and accepts every publish.
type neutralTaskTransport struct{}
func (neutralTaskTransport) PublishUnique(context.Context, string, string, []byte) error { return nil }
func (neutralTaskTransport) Fetch(context.Context) (eventbus.Message, error) {
return nil, eventbus.ErrNoMessage
}
func (neutralTaskTransport) Request(context.Context, string, []byte) ([]byte, error) { return nil, nil }
// neutralStore records no events. Any other store call panics, so a test that
// reaches one sets its own store.
type neutralStore struct{ storecontract.TaskStore }
func (neutralStore) InsertEvent(context.Context, events.Event) (int64, error) { return 0, nil }
// neutralReactions has no review reactions waiting.
type neutralReactions struct{}
func (neutralReactions) Fetch(context.Context) (eventbus.Message, error) {
return nil, eventbus.ErrNoMessage
}
// neutralCredentials issues an empty run credential.
type neutralCredentials struct{}
func (neutralCredentials) Issue(context.Context, *workflow.Task, time.Duration) (string, func(), error) {
return "", func() {}, nil
}
// neutralTaskConfig has no org model aliases and no instructions.
type neutralTaskConfig struct{}
func (neutralTaskConfig) TaskModels(context.Context, int64) (config.OrgModels, error) {
return config.OrgModels{}, nil
}
func (neutralTaskConfig) TaskInstructions(context.Context, int64) (string, string, error) {
return "", "", nil
}
// allowAll permits every policy request.
type allowAll struct{}
func (allowAll) Authorize(context.Context, access.Principal, access.Action, access.Resource, access.Context) access.Decision {
return access.Decision{Allowed: true}
}
// neutralPrincipals resolves every identity to the default org with no role.
type neutralPrincipals struct{}
func (neutralPrincipals) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: org.DefaultOrgID}, nil
}
type neutralDenials struct{}
func (neutralDenials) RecordDenial(context.Context, access.Denial) error { return nil }
// neutralIdentities resolves every identity as an active agent.
type neutralIdentities struct{}
func (neutralIdentities) List(context.Context) ([]identity.Identity, error) { return nil, nil }
func (neutralIdentities) Get(_ context.Context, id identity.IdentityID) (identity.Identity, error) {
return identity.Identity{ID: id, Kind: identity.KindBot, Lifecycle: identity.LifecycleActive}, nil
}
func (neutralIdentities) ResolveLegacyName(context.Context, string) (identity.Identity, error) {
return identity.Identity{}, identity.ErrNotFound
}
func (neutralIdentities) Create(_ context.Context, v identity.Identity, _ identity.Audit) (identity.Identity, error) {
return v, nil
}
func (neutralIdentities) Apply(_ context.Context, _ identity.IdentityID, _ int64, _ identity.Command, _ identity.Audit) (identity.Identity, error) {
return identity.Identity{}, nil
}
type neutralAssignments struct{}
func (neutralAssignments) ListOrgAgents(context.Context, org.OrgID) ([]org.AgentAssignment, error) {
return nil, nil
}
// neutralAuthorities accepts nothing for any package.
type neutralAuthorities struct{}
func (neutralAuthorities) AcceptedAuthority(context.Context, string) (storepkg.Authority, error) {
return storepkg.Authority{}, nil
}
// neutralPlaybooks matches no playbook.
type neutralPlaybooks struct{}
func (neutralPlaybooks) Dispatch(context.Context, playbook.DispatchInput) (playbook.Decision, bool) {
return playbook.Decision{}, false
}
func (neutralPlaybooks) Run(context.Context, storecontract.PlaybookDispatcher, *slog.Logger, playbook.DispatchInput) error {
return nil
}
// neutralMatches records no mapping match.
type neutralMatches struct{}
func (neutralMatches) RecordMappingMatch(context.Context, string, string) error { return nil }
// neutralDefinitions serves the shipped workflow definitions at version 0.
type neutralDefinitions struct{}
func (neutralDefinitions) WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
return workflow.ShippedDefinitions(), 0, nil
}
func (neutralDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) {
return workflow.ShippedIntakeRoutes(), nil
}
// neutralEnablement disables no workflow.
type neutralEnablement struct{}
func (neutralEnablement) WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error) {
return workflowtask.WorkflowEnablement{}, nil
}