Files
archie/modules/memory/memory_test.go
T
samandCopilot 64dfad6d53 feat(runs): add typed results and model aliases (#2043)
Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-18 23:47:35 +11:00

79 lines
2.8 KiB
Go

package memory
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/memory/internal/store"
)
// fakeLedger holds one target's incidents, newest first, and the proposals made.
type fakeLedger struct {
rows []store.Incident
proposals []store.Proposal
}
func (f *fakeLedger) Upsert(context.Context, string, store.Incident) error { return nil }
func (f *fakeLedger) SetActions(context.Context, string, uuid.UUID, []byte) error { return nil }
func (f *fakeLedger) Incident(_ context.Context, _ string, runID uuid.UUID) (store.Incident, bool, error) {
for _, r := range f.rows {
if r.RunID == runID {
return r, true, nil
}
}
return store.Incident{}, false, nil
}
func (f *fakeLedger) ByTarget(context.Context, string, string, string, int) ([]store.Incident, error) {
return f.rows, nil
}
func (f *fakeLedger) Propose(_ context.Context, _ string, p store.Proposal) (store.Proposal, error) {
f.proposals = append(f.proposals, p)
return p, nil
}
// TestFinishProposesOnRecurrence checks that only incidents with the same
// cause count towards the configured threshold, even if an action was denied.
func TestFinishProposesOnRecurrence(t *testing.T) {
now := time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC)
run := uuid.New()
incidents := func(causes ...string) []store.Incident {
rows := []store.Incident{{RunID: run, Cause: causes[0], At: now}}
for i, c := range causes[1:] {
rows = append(rows, store.Incident{RunID: uuid.New(), Cause: c, At: now.AddDate(0, 0, -i-1)})
}
return rows
}
tests := []struct {
name string
rows []store.Incident
threshold int
want bool
}{
{"one short of the threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, false},
{"the threshold is met by this incident", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
{"other causes are not counted", incidents("disk-pressure", "oom", "oom", "oom", "oom", "disk-pressure"), 5, false},
{"uses the organisation's threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure"), 3, true},
{"proposes above the threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ledger := &fakeLedger{rows: tt.rows}
m := &Module{orgID: "org", ledger: ledger}
review, err := m.Finish(t.Context(), run, []Action{{Action: "restart", Target: "host:web-1", State: "denied"}}, tt.threshold)
if err != nil {
t.Fatal(err)
}
if got := review.Proposal != ""; got != tt.want || (len(ledger.proposals) == 1) != tt.want {
t.Fatalf("proposal = %q, recorded %d; want proposal %v", review.Proposal, len(ledger.proposals), tt.want)
}
})
}
}