Files
archie/modules/judgement/judgement_test.go
T
sam f24c2b4455 feat(connectors): separate packages from scoped installations (#2027)
Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted.

The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
2026-11-24 03:05:27 +11:00

318 lines
11 KiB
Go

package judgement
import (
"context"
"encoding/json/jsontext"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/connectors"
)
type fixedGuard struct {
name string
ceiling Level
err error
advisory bool
panics bool
calls *atomic.Int32
}
func (g fixedGuard) Name() string { return g.name }
func (g fixedGuard) Advisory() bool { return g.advisory }
func (g fixedGuard) Judge(context.Context, Action) (Verdict, error) {
if g.calls != nil {
g.calls.Add(1)
}
if g.panics {
panic("guard bug")
}
return Verdict{Ceiling: g.ceiling}, g.err
}
type refuseAll struct{}
func (refuseAll) Refuses(context.Context, Action) (string, string, bool) {
return "no deletes in production", "production is off limits", true
}
// TestDecisionRunsAtTheLowestCeiling guards the weakest-link decision (spec
// section 5, decision tree 8.3): hard rules refuse before any guard runs; the
// action runs at the lowest ceiling; a guard that fails, panics or gives no
// ceiling holds the action at propose and wait; an advisory verdict is
// recorded but sets no ceiling.
func TestDecisionRunsAtTheLowestCeiling(t *testing.T) {
tests := []struct {
name string
rules []HardRule
guards []Guard
want Level
wantCalls int32
}{
{
name: "the lower of two ceilings wins",
guards: []Guard{fixedGuard{name: "a", ceiling: ActAndTell}, fixedGuard{name: "b", ceiling: ProposeAndWait}},
want: ProposeAndWait,
},
{
name: "guards with no concern act quietly",
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", ceiling: ActQuietly}},
want: ActQuietly,
},
{
name: "a failing guard holds the action",
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", err: errors.New("no evidence")}},
want: ProposeAndWait,
},
{
name: "a panicking guard holds the action",
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b", panics: true}},
want: ProposeAndWait,
},
{
name: "a guard without a ceiling holds the action",
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "b"}},
want: ProposeAndWait,
},
{
name: "an advisory verdict sets no ceiling",
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}, fixedGuard{name: "judge", ceiling: Never, advisory: true}},
want: ActQuietly,
},
{
name: "a failing advisory guard sets no ceiling",
guards: []Guard{fixedGuard{name: "a", ceiling: ActAndTell}, fixedGuard{name: "judge", err: errors.New("timeout"), advisory: true}},
want: ActAndTell,
},
{
name: "no guards at all holds the action",
want: ProposeAndWait,
},
{
name: "a hard rule refuses before any guard runs",
rules: []HardRule{refuseAll{}},
guards: []Guard{fixedGuard{name: "a", ceiling: ActQuietly}},
want: Never,
wantCalls: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var calls atomic.Int32
for i, g := range tt.guards {
if f, ok := g.(fixedGuard); ok {
f.calls = &calls
tt.guards[i] = f
}
}
m := &Module{rules: tt.rules, guards: tt.guards}
d := m.decide(t.Context(), Action{Connector: "c", Name: "delete"})
if d.Level != tt.want {
t.Fatalf("level = %s, want %s (verdicts %+v)", d.Level, tt.want, d.Verdicts)
}
if len(d.Verdicts) != len(tt.guards) && tt.want != Never {
t.Fatalf("verdicts = %d, want one per guard (%d)", len(d.Verdicts), len(tt.guards))
}
if tt.rules != nil && calls.Load() != tt.wantCalls {
t.Fatalf("guards ran %d times, want %d", calls.Load(), tt.wantCalls)
}
})
}
}
type countingTool struct{ invoked *atomic.Int32 }
func (t countingTool) Lookup(context.Context, string, jsontext.Value) (string, bool, error) {
return "", false, nil
}
func (t countingTool) Invoke(context.Context, connectors.Invocation) (string, error) {
t.invoked.Add(1)
return "done", nil
}
func (t countingTool) Installation() connectors.Installation { return connectors.Installation{} }
func (t countingTool) Resolve(context.Context, string, jsontext.Value) (connectors.Resolution, error) {
return connectors.Resolution{}, nil
}
type oneTool struct{ tool connectors.Tool }
func (o oneTool) Tool(context.Context, string) (connectors.Tool, bool) { return o.tool, true }
func (o oneTool) Installed(string) (connectors.Tool, bool) { return o.tool, true }
// TestExecuteRunsOnlyTheJudgedAction guards spec section 5 rule 6 and
// decision tree 8.5: an action runs only at a level that runs without
// approval, and only with exactly the input that was judged. A refused, held
// or altered action never reaches the connector, and neither does a changing
// action judged for a dry run, whatever level it reached.
func TestExecuteRunsOnlyTheJudgedAction(t *testing.T) {
judged := jsontext.Value(`{"path":"/tmp/runner"}`)
decided := func(l Level) Decision {
return Decision{Level: l, Action: Action{Connector: "c", Name: "delete", Input: judged, Digest: connectors.Digest("delete", judged)}}
}
dry := func(d Decision, readOnly bool) Decision {
d.Dry, d.Action.Effect.ReadOnly = true, readOnly
return d
}
tests := []struct {
name string
d Decision
input jsontext.Value
wantErr error
}{
{name: "act quietly runs", d: decided(ActQuietly), input: judged},
{name: "act and tell runs", d: decided(ActAndTell), input: judged},
{name: "propose and wait is held", d: decided(ProposeAndWait), input: judged, wantErr: ErrHeld},
{name: "escalate is held", d: decided(Escalate), input: judged, wantErr: ErrHeld},
{name: "never is refused", d: decided(Never), input: judged, wantErr: ErrRefused},
{name: "altered input is refused", d: decided(ActQuietly), input: jsontext.Value(`{"path":"/"}`), wantErr: ErrAltered},
{name: "a dry run's changing action is never executed at act quietly", d: dry(decided(ActQuietly), false), input: judged, wantErr: ErrDryRun},
{name: "a dry run's changing action is never executed at act and tell", d: dry(decided(ActAndTell), false), input: judged, wantErr: ErrDryRun},
{name: "a dry run's read-only action runs for real", d: dry(decided(ActQuietly), true), input: judged},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var invoked atomic.Int32
m := &Module{tools: oneTool{countingTool{&invoked}}}
_, err := m.Execute(t.Context(), tt.d, tt.input, "run:1")
if !errors.Is(err, tt.wantErr) {
t.Fatalf("err = %v, want %v", err, tt.wantErr)
}
want := int32(1)
if tt.wantErr != nil {
want = 0
}
if invoked.Load() != want {
t.Fatalf("connector invoked %d times, want %d", invoked.Load(), want)
}
})
}
}
// TestExecuteApprovedRunsOnlyWhatThePersonSaw guards the approval boundary: an
// approval covers the decision and the exact action a person was shown, so a
// replayed or replayed-onto-another-action approval runs nothing, and a
// refusal still refuses with one.
func TestExecuteApprovedRunsOnlyWhatThePersonSaw(t *testing.T) {
judged := jsontext.Value(`{"path":"/tmp/runner"}`)
const key = "run:1:3"
decided := func() Decision {
return Decision{
ID: uuid.MustParse("11111111-1111-1111-1111-111111111111"),
Level: ProposeAndWait,
Action: Action{
Connector: "c", Name: "delete", Input: judged,
Digest: connectors.Digest("delete", judged),
},
}
}
approval := func(d Decision) Approval {
return Approval{DecisionID: d.ID, Digest: d.Action.Digest, By: "admin", At: time.Now().UTC()}
}
tests := []struct {
name string
d func() Decision
a func(Decision) Approval
input jsontext.Value
wantErr error
}{
{name: "an approval for this action runs it", d: decided, a: approval, input: judged},
{
name: "an approval naming another decision runs nothing",
d: decided, input: judged,
a: func(d Decision) Approval {
other := approval(d)
other.DecisionID = uuid.MustParse("22222222-2222-2222-2222-222222222222")
return other
},
wantErr: ErrUnapproved,
},
{
name: "an approval for another action runs nothing",
d: decided, input: judged,
a: func(d Decision) Approval {
other := approval(d)
other.Digest = connectors.Digest("delete", jsontext.Value(`{"path":"/"}`))
return other
},
wantErr: ErrUnapproved,
},
{
name: "the same arguments spelled differently still run",
d: decided, a: approval, input: jsontext.Value(`{ "path" : "/tmp/runner" }`),
},
{
name: "input that differs from the judged action is refused",
d: decided, a: approval, input: jsontext.Value(`{"path":"/"}`),
wantErr: ErrAltered,
},
{
name: "a refusal still refuses with one",
d: func() Decision {
d := decided()
d.Level, d.RefusedBy, d.Reason = Never, "blackout", "a release is in flight"
return d
},
a: approval, input: judged, wantErr: ErrRefused,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var invoked atomic.Int32
m := &Module{tools: oneTool{countingTool{&invoked}}}
d := tt.d()
_, err := m.ExecuteApproved(t.Context(), d, tt.a(d), tt.input, key)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("err = %v, want %v", err, tt.wantErr)
}
want := int32(1)
if tt.wantErr != nil {
want = 0
}
if got := invoked.Load(); got != want {
t.Fatalf("connector invoked %d times, want %d", got, want)
}
})
}
}
type actionTypeLevel struct{ level Level }
func (c actionTypeLevel) Level(context.Context, ActionType, bool) (Level, error) { return c.level, nil }
// TestPrecedentCeilingIsTheClassLevel guards the earned-autonomy seam: the
// precedent guard's ceiling is the level its action type stands at, not a
// default, so an action type that was reset, raised or lowered judges accordingly.
func TestPrecedentCeilingIsTheClassLevel(t *testing.T) {
tests := []struct {
name string
level Level
read bool
}{
{name: "a read-only action type at its default", level: ActQuietly, read: true},
{name: "a changing action type raised to act and tell", level: ActAndTell},
{name: "a changing action type at its default", level: ProposeAndWait},
{name: "a read-only action type lowered to escalate", level: Escalate, read: true},
{name: "an action type that never runs", level: Never},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := Precedent{ActionTypes: actionTypeLevel{tt.level}}.Judge(t.Context(), Action{Effect: connectors.Effect{ReadOnly: tt.read}})
if err != nil {
t.Fatal(err)
}
if v.Ceiling != tt.level {
t.Fatalf("ceiling = %s, want the action type level %s", v.Ceiling, tt.level)
}
})
}
}