Trigger dispatch, run start, every run claim, every delivery attempt and webhook intake now ask the policy chain. Background work acts as the workload principal; a refusal is final and recorded on the run or the delivery, and an undecidable chain leaves the work queued for another attempt. Intake applies the per-source delivery policy after source verification.
121 lines
4.1 KiB
Go
121 lines
4.1 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
"github.com/samcharles93/archie-core/modules/judgement"
|
|
"github.com/samcharles93/archie-core/modules/runs/internal/store"
|
|
)
|
|
|
|
type decidingJudge struct {
|
|
decision judgement.Decision
|
|
executed int
|
|
}
|
|
|
|
func (j *decidingJudge) Assess(context.Context, judgement.Request) (judgement.Decision, error) {
|
|
return j.decision, nil
|
|
}
|
|
|
|
func (j *decidingJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) {
|
|
j.executed++
|
|
return "done", nil
|
|
}
|
|
|
|
func (j *decidingJudge) ExecuteApproved(context.Context, judgement.Decision, judgement.Approval, jsontext.Value, string) (string, error) {
|
|
j.executed++
|
|
return "done", nil
|
|
}
|
|
|
|
func (j *decidingJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type anyBox struct{}
|
|
|
|
func (anyBox) Declarations() []connectors.Declaration { return nil }
|
|
func (anyBox) Tool(string) (connectors.Tool, bool) { return nil, true }
|
|
|
|
// TestStartDryRefusesWhatCannotStart guards the dry-run entry point against
|
|
// testing a document a live run could not start: a declared identity or
|
|
// profile, or a missing required input. The document is refused before a run
|
|
// is queued.
|
|
func TestStartDryRefusesWhatCannotStart(t *testing.T) {
|
|
engine := newEngine(nil, &fakeWork{}, slog.New(slog.DiscardHandler), Options{})
|
|
engine.Use(Deps{Access: &recordingAccess{decision: governance.Allowed()}})
|
|
tests := []struct {
|
|
name string
|
|
doc string
|
|
want string
|
|
}{
|
|
{
|
|
name: "identity",
|
|
doc: "repository: none\nidentity: archie\nsteps:\n - {type: run.notify}\n",
|
|
want: "acts as identity",
|
|
},
|
|
{
|
|
name: "profile",
|
|
doc: "repository: none\nprofile: build\nsteps:\n - {type: run.notify}\n",
|
|
want: "runs under profile",
|
|
},
|
|
{
|
|
name: "required input",
|
|
doc: "inputs: {event: {type: object, required: true}}\nrepository: none\nsteps:\n - {type: run.notify}\n",
|
|
want: `input "event" is required`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := governance.WithPrincipal(context.Background(), governance.SharedTokenOwner())
|
|
_, err := engine.StartDry(ctx, DryRequest{
|
|
OrgID: "org", Workflow: "draft", Document: tt.doc,
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
|
t.Fatalf("StartDry error = %v, want %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestDryRunNeverExecutesAChangingAction guards the dry run's promise: an
|
|
// action that changes something is judged and never executed, however freely
|
|
// judgement would let it run, while one that only reads runs for real.
|
|
func TestDryRunNeverExecutesAChangingAction(t *testing.T) {
|
|
decided := func(level judgement.Level, readOnly bool) judgement.Decision {
|
|
d := judgement.Decision{Level: level}
|
|
d.Action.Effect.ReadOnly = readOnly
|
|
return d
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
dry bool
|
|
decision judgement.Decision
|
|
wantExecuted int
|
|
}{
|
|
{name: "dry: a changing action at act quietly is not executed", dry: true, decision: decided(judgement.ActQuietly, false)},
|
|
{name: "dry: a changing action at act and tell is not executed", dry: true, decision: decided(judgement.ActAndTell, false)},
|
|
{name: "dry: a read-only action runs for real", dry: true, decision: decided(judgement.ActQuietly, true), wantExecuted: 1},
|
|
{name: "live: a changing action at act quietly runs", decision: decided(judgement.ActQuietly, false), wantExecuted: 1},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
judge := &decidingJudge{decision: tt.decision}
|
|
rc := newContext(t.Context(), &fakeWork{}, store.Run{ID: uuid.New(), Dry: tt.dry}, uuid.New(), nil)
|
|
decl := connectors.Declaration{Connector: "c", Kind: connectors.KindAction, Name: "restart"}
|
|
if _, err := call(t.Context(), anyBox{}, judge, rc, decl, jsontext.Value(`{}`)); err != nil {
|
|
t.Fatalf("call: %v", err)
|
|
}
|
|
if judge.executed != tt.wantExecuted {
|
|
t.Fatalf("executed %d times, want %d", judge.executed, tt.wantExecuted)
|
|
}
|
|
})
|
|
}
|
|
}
|