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. type fakeLedger struct { rows []store.Incident } // fakeProposer counts the root-cause fixes requested. type fakeProposer struct{ requests []FixRequest } func (f *fakeProposer) ProposeRootCause(_ context.Context, req FixRequest) error { f.requests = append(f.requests, req) return nil } 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 } // 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} fixes := &fakeProposer{} m := &Module{orgID: "org", ledger: ledger, fixes: fixes} 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(fixes.requests) == 1) != tt.want { t.Fatalf("proposal = %q, requested %d; want proposal %v", review.Proposal, len(fixes.requests), tt.want) } }) } }